2022-09-29 22:01:56 +02:00
|
|
|
package com.minelittlepony.unicopia.block;
|
|
|
|
|
2022-09-30 00:23:16 +02:00
|
|
|
import com.minelittlepony.unicopia.AwaitTickQueue;
|
|
|
|
import com.minelittlepony.unicopia.UGameEvents;
|
2022-09-29 22:01:56 +02:00
|
|
|
import com.minelittlepony.unicopia.util.SoundEmitter;
|
|
|
|
|
|
|
|
import net.minecraft.block.*;
|
2022-09-30 00:23:16 +02:00
|
|
|
import net.minecraft.entity.Entity;
|
2022-09-29 22:01:56 +02:00
|
|
|
import net.minecraft.entity.ai.pathing.NavigationType;
|
|
|
|
import net.minecraft.entity.player.PlayerEntity;
|
2022-09-30 00:23:16 +02:00
|
|
|
import net.minecraft.fluid.FluidState;
|
|
|
|
import net.minecraft.fluid.Fluids;
|
2022-09-29 22:01:56 +02:00
|
|
|
import net.minecraft.item.*;
|
2022-09-30 00:23:16 +02:00
|
|
|
import net.minecraft.particle.ParticleTypes;
|
2022-09-29 22:01:56 +02:00
|
|
|
import net.minecraft.sound.SoundEvents;
|
|
|
|
import net.minecraft.stat.Stats;
|
|
|
|
import net.minecraft.state.StateManager;
|
2022-09-30 00:23:16 +02:00
|
|
|
import net.minecraft.state.property.*;
|
2022-09-29 22:01:56 +02:00
|
|
|
import net.minecraft.util.ActionResult;
|
|
|
|
import net.minecraft.util.Hand;
|
|
|
|
import net.minecraft.util.hit.BlockHitResult;
|
|
|
|
import net.minecraft.util.math.*;
|
2022-09-30 00:23:16 +02:00
|
|
|
import net.minecraft.util.math.random.Random;
|
2022-09-29 22:01:56 +02:00
|
|
|
import net.minecraft.util.shape.VoxelShape;
|
|
|
|
import net.minecraft.util.shape.VoxelShapes;
|
|
|
|
import net.minecraft.world.*;
|
|
|
|
import net.minecraft.world.event.GameEvent;
|
|
|
|
|
2022-09-30 00:23:16 +02:00
|
|
|
public class PieBlock extends Block implements Waterloggable {
|
2022-09-29 22:01:56 +02:00
|
|
|
public static final int MAX_BITES = 3;
|
|
|
|
public static final IntProperty BITES = IntProperty.of("bites", 0, MAX_BITES);
|
2022-09-30 00:23:16 +02:00
|
|
|
public static final BooleanProperty STOMPED = BooleanProperty.of("stomped");
|
|
|
|
public static final BooleanProperty WATERLOGGED = Properties.WATERLOGGED;
|
|
|
|
|
2022-09-29 22:01:56 +02:00
|
|
|
private static final VoxelShape[] SHAPES;
|
|
|
|
static {
|
|
|
|
final int PIE_HEIGHT = 5;
|
|
|
|
final VoxelShape WEDGE = Block.createCuboidShape(1, 0, 1, 8, PIE_HEIGHT, 8);
|
|
|
|
final float OFFSET_AMOUNT = 7F/16F;
|
|
|
|
SHAPES = new VoxelShape[] {
|
|
|
|
Block.createCuboidShape(1, 0, 1, 15, PIE_HEIGHT, 15),
|
|
|
|
VoxelShapes.union(WEDGE, WEDGE.offset(OFFSET_AMOUNT, 0, 0), WEDGE.offset(OFFSET_AMOUNT, 0, OFFSET_AMOUNT)),
|
|
|
|
VoxelShapes.union(WEDGE, WEDGE.offset(OFFSET_AMOUNT, 0, 0)),
|
|
|
|
WEDGE
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public PieBlock(Settings settings) {
|
|
|
|
super(settings);
|
2022-09-30 00:23:16 +02:00
|
|
|
setDefaultState(getDefaultState().with(STOMPED, false).with(WATERLOGGED, false));
|
2022-09-29 22:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
@Override
|
|
|
|
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
|
|
|
|
return SHAPES[state.get(BITES)];
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
@Override
|
|
|
|
public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit) {
|
|
|
|
ItemStack itemStack = player.getStackInHand(hand);
|
|
|
|
|
|
|
|
if (world.isClient) {
|
|
|
|
if (tryEat(world, pos, state, player).isAccepted()) {
|
|
|
|
return ActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (itemStack.isEmpty()) {
|
|
|
|
return ActionResult.CONSUME;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tryEat(world, pos, state, player);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected ActionResult tryEat(WorldAccess world, BlockPos pos, BlockState state, PlayerEntity player) {
|
|
|
|
if (!player.canConsume(false)) {
|
|
|
|
return ActionResult.PASS;
|
|
|
|
}
|
|
|
|
player.incrementStat(Stats.EAT_CAKE_SLICE);
|
2022-09-30 00:23:16 +02:00
|
|
|
player.getHungerManager().add(state.get(STOMPED) ? 1 : 2, 0.1f);
|
2022-09-29 22:01:56 +02:00
|
|
|
int bites = state.get(BITES);
|
|
|
|
world.emitGameEvent(player, GameEvent.EAT, pos);
|
2022-09-30 00:23:16 +02:00
|
|
|
SoundEmitter.playSoundAt(player, SoundEvents.ENTITY_GENERIC_EAT, 0.5F, world.getRandom().nextFloat() * 0.1F + 0.9F);
|
|
|
|
if (world instanceof World ww && (!player.canConsume(false) || world.getRandom().nextInt(10) == 0)) {
|
|
|
|
AwaitTickQueue.scheduleTask(ww, w -> {
|
|
|
|
SoundEmitter.playSoundAt(player, SoundEvents.ENTITY_PLAYER_BURP, 0.5F, world.getRandom().nextFloat() * 0.1F + 0.9F);
|
|
|
|
}, 5);
|
|
|
|
}
|
2022-09-29 22:01:56 +02:00
|
|
|
|
|
|
|
if (bites < MAX_BITES) {
|
|
|
|
world.setBlockState(pos, state.with(BITES, bites + 1), Block.NOTIFY_ALL);
|
|
|
|
} else {
|
|
|
|
world.removeBlock(pos, false);
|
|
|
|
world.emitGameEvent(player, GameEvent.BLOCK_DESTROY, pos);
|
|
|
|
}
|
|
|
|
return ActionResult.SUCCESS;
|
|
|
|
}
|
|
|
|
|
2022-09-30 00:23:16 +02:00
|
|
|
@Override
|
|
|
|
public void randomDisplayTick(BlockState state, World world, BlockPos pos, Random random) {
|
|
|
|
if (state.get(STOMPED)) {
|
|
|
|
Vec3d center = Vec3d.ofCenter(pos);
|
|
|
|
world.addParticle(ParticleTypes.SNEEZE,
|
|
|
|
random.nextTriangular(center.getX(), 0.9),
|
|
|
|
random.nextTriangular(center.getY(), 0.9),
|
|
|
|
random.nextTriangular(center.getZ(), 0.9),
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
if (world.random.nextInt(10) == 0) {
|
|
|
|
Vec3d center = Vec3d.ofCenter(pos);
|
|
|
|
world.addParticle(ParticleTypes.SNEEZE,
|
|
|
|
random.nextTriangular(center.getX(), 0.2),
|
|
|
|
random.nextTriangular(center.getY(), 0.2),
|
|
|
|
random.nextTriangular(center.getZ(), 0.2),
|
|
|
|
0,
|
|
|
|
0.01F,
|
|
|
|
0
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void onSteppedOn(World world, BlockPos pos, BlockState state, Entity entity) {
|
|
|
|
if (!state.get(STOMPED)) {
|
|
|
|
world.setBlockState(pos, state.cycle(STOMPED));
|
|
|
|
world.syncWorldEvent(WorldEvents.BLOCK_BROKEN, pos, Block.getRawIdFromState(state));
|
|
|
|
world.emitGameEvent(UGameEvents.PIE_STOMP, pos, GameEvent.Emitter.of(entity, state));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-29 22:01:56 +02:00
|
|
|
@Deprecated
|
|
|
|
@Override
|
|
|
|
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) {
|
|
|
|
if (direction == Direction.DOWN && !state.canPlaceAt(world, pos)) {
|
|
|
|
return Blocks.AIR.getDefaultState();
|
|
|
|
}
|
2022-09-30 00:23:16 +02:00
|
|
|
if (state.get(WATERLOGGED)) {
|
|
|
|
world.createAndScheduleFluidTick(pos, Fluids.WATER, Fluids.WATER.getTickRate(world));
|
|
|
|
}
|
2022-09-29 22:01:56 +02:00
|
|
|
return super.getStateForNeighborUpdate(state, direction, neighborState, world, pos, neighborPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
@Override
|
|
|
|
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
|
2022-09-30 00:23:16 +02:00
|
|
|
return world.getBlockState(pos.down()).isSideSolidFullSquare(world, pos, Direction.UP);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
@Override
|
|
|
|
public BlockState getPlacementState(ItemPlacementContext ctx) {
|
|
|
|
return super.getDefaultState().with(WATERLOGGED, ctx.getWorld().getFluidState(ctx.getBlockPos()).getFluid() == Fluids.WATER);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Deprecated
|
|
|
|
@Override
|
|
|
|
public FluidState getFluidState(BlockState state) {
|
|
|
|
if (state.get(WATERLOGGED)) {
|
|
|
|
return Fluids.WATER.getStill(false);
|
|
|
|
}
|
|
|
|
return super.getFluidState(state);
|
2022-09-29 22:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
|
2022-09-30 00:23:16 +02:00
|
|
|
builder.add(BITES, STOMPED, WATERLOGGED);
|
2022-09-29 22:01:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getComparatorOutput(BlockState state, World world, BlockPos pos) {
|
|
|
|
return (5 - state.get(BITES)) * 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean hasComparatorOutput(BlockState state) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean canPathfindThrough(BlockState state, BlockView world, BlockPos pos, NavigationType type) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|