From c25ef42068923e006d0dc3c23dad1577be7315a4 Mon Sep 17 00:00:00 2001 From: Sollace Date: Wed, 24 May 2023 19:18:19 +0100 Subject: [PATCH] Pick-block on zap leaves now gives you the corresponding state --- .../unicopia/block/ZapAppleLeavesBlock.java | 60 +++++-------------- .../block/ZapAppleLeavesPlaceholderBlock.java | 1 - .../server/world/ZapAppleStageStore.java | 7 ++- 3 files changed, 21 insertions(+), 47 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesBlock.java b/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesBlock.java index f533fd0e..f99bd33e 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesBlock.java +++ b/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesBlock.java @@ -7,23 +7,20 @@ import com.minelittlepony.unicopia.server.world.ZapAppleStageStore; import net.minecraft.block.*; import net.minecraft.entity.player.PlayerEntity; -import net.minecraft.fluid.Fluid; import net.minecraft.item.ItemPlacementContext; +import net.minecraft.item.ItemStack; import net.minecraft.server.world.ServerWorld; import net.minecraft.sound.BlockSoundGroup; import net.minecraft.state.StateManager; import net.minecraft.state.property.*; import net.minecraft.util.math.*; import net.minecraft.util.math.random.Random; -import net.minecraft.util.shape.VoxelShape; -import net.minecraft.util.shape.VoxelShapes; import net.minecraft.world.*; public class ZapAppleLeavesBlock extends LeavesBlock implements TintedBlock { public static final EnumProperty STAGE = EnumProperty.of("stage", ZapAppleStageStore.Stage.class); ZapAppleLeavesBlock() { - super(Settings.of(Material.LEAVES) .strength(500, 1200) .ticksRandomly() @@ -50,21 +47,17 @@ public class ZapAppleLeavesBlock extends LeavesBlock implements TintedBlock { @Override public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { super.randomTick(state, world, pos, random); - if (state.get(PERSISTENT)) { - return; - } - - ZapAppleStageStore store = ZapAppleStageStore.get(world); - ZapAppleStageStore.Stage newStage = store.getStage(); - if (!world.isDay() && state.get(STAGE).mustChangeInto(newStage)) { - world.setBlockState(pos, newStage == ZapAppleStageStore.Stage.HIBERNATING ? UBlocks.ZAP_LEAVES_PLACEHOLDER.getDefaultState() : state.with(STAGE, newStage)); - onStageChanged(store, newStage, world, state, pos, random); - } + tryAdvanceStage(state, world, pos, random); } @Override public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { super.scheduledTick(state, world, pos, random); + tryAdvanceStage(state, world, pos, random); + world.scheduleBlockTick(pos, this, 1); + } + + private void tryAdvanceStage(BlockState state, ServerWorld world, BlockPos pos, Random random) { if (state.get(PERSISTENT)) { return; } @@ -75,8 +68,6 @@ public class ZapAppleLeavesBlock extends LeavesBlock implements TintedBlock { world.setBlockState(pos, newStage == ZapAppleStageStore.Stage.HIBERNATING ? UBlocks.ZAP_LEAVES_PLACEHOLDER.getDefaultState() : state.with(STAGE, newStage)); onStageChanged(store, newStage, world, state, pos, random); } - - world.scheduleBlockTick(pos, this, 1); } @Override @@ -84,9 +75,16 @@ public class ZapAppleLeavesBlock extends LeavesBlock implements TintedBlock { return false; } + @Override + public ItemStack getPickStack(BlockView world, BlockPos pos, BlockState state) { + ItemStack stack = super.getPickStack(world, pos, state); + stack.setDamage(Math.max(0, state.get(STAGE).ordinal() - 1)); + return stack; + } + @Override public BlockState getPlacementState(ItemPlacementContext ctx) { - return super.getPlacementState(ctx).with(STAGE, ZapAppleStageStore.Stage.GREENING); + return super.getPlacementState(ctx).with(STAGE, ZapAppleStageStore.Stage.byId(ctx.getStack().getDamage() + 1)); } static void onStageChanged(ZapAppleStageStore store, ZapAppleStageStore.Stage stage, ServerWorld world, BlockState state, BlockPos pos, Random random) { @@ -123,34 +121,6 @@ public class ZapAppleLeavesBlock extends LeavesBlock implements TintedBlock { ZapBlock.triggerLightning(state, world, pos, player); } - @Deprecated - @Override - public BlockRenderType getRenderType(BlockState state) { - return isAir(state) ? BlockRenderType.INVISIBLE : super.getRenderType(state); - } - - @Deprecated - @Override - public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) { - return isAir(state) ? VoxelShapes.empty() : super.getOutlineShape(state, world, pos, context); - } - - @Deprecated - @Override - public boolean canReplace(BlockState state, ItemPlacementContext context) { - return isAir(state) || super.canReplace(state, context); - } - - @Deprecated - @Override - public boolean canBucketPlace(BlockState state, Fluid fluid) { - return isAir(state) || super.canBucketPlace(state, fluid); - } - - protected boolean isAir(BlockState state) { - return state.get(STAGE) == ZapAppleStageStore.Stage.HIBERNATING; - } - @Override public int getTint(BlockState state, @Nullable BlockRenderView world, @Nullable BlockPos pos, int foliageColor) { diff --git a/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesPlaceholderBlock.java b/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesPlaceholderBlock.java index 776cd63e..c73abc36 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesPlaceholderBlock.java +++ b/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesPlaceholderBlock.java @@ -32,5 +32,4 @@ public class ZapAppleLeavesPlaceholderBlock extends AirBlock { world.scheduleBlockTick(pos, this, 1); } - } diff --git a/src/main/java/com/minelittlepony/unicopia/server/world/ZapAppleStageStore.java b/src/main/java/com/minelittlepony/unicopia/server/world/ZapAppleStageStore.java index 74e6aa70..d61bfc84 100644 --- a/src/main/java/com/minelittlepony/unicopia/server/world/ZapAppleStageStore.java +++ b/src/main/java/com/minelittlepony/unicopia/server/world/ZapAppleStageStore.java @@ -16,6 +16,7 @@ import net.minecraft.sound.SoundEvents; import net.minecraft.util.Identifier; import net.minecraft.util.StringIdentifiable; import net.minecraft.util.math.BlockPos; +import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; import net.minecraft.world.PersistentState; import net.minecraft.world.World; @@ -145,7 +146,11 @@ public class ZapAppleStageStore extends PersistentState implements Tickable { static final Stage[] VALUES = values(); public Stage getNext() { - return VALUES[(ordinal() + 1) % VALUES.length]; + return byId((ordinal() + 1) % VALUES.length); + } + + public static Stage byId(int id) { + return VALUES[MathHelper.clamp(id, 0, VALUES.length)]; } public boolean mustChangeInto(Stage to) {