From 2e938633e952582867415fb5ca5c0ecde6fba025 Mon Sep 17 00:00:00 2001 From: Sollace Date: Thu, 1 Feb 2024 13:26:26 +0000 Subject: [PATCH] Clean up and adjust transitions when zap apple trees change stages --- .../block/BaseZapAppleLeavesBlock.java | 10 +---- .../block/ZapAppleLeavesPlaceholderBlock.java | 4 +- .../unicopia/block/ZapStagedBlock.java | 40 +++++++++++++++---- .../gui/spellbook/element/DynamicContent.java | 2 - .../server/world/ZapAppleStageStore.java | 18 --------- 5 files changed, 35 insertions(+), 39 deletions(-) diff --git a/src/main/java/com/minelittlepony/unicopia/block/BaseZapAppleLeavesBlock.java b/src/main/java/com/minelittlepony/unicopia/block/BaseZapAppleLeavesBlock.java index a971749f..902b5a11 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/BaseZapAppleLeavesBlock.java +++ b/src/main/java/com/minelittlepony/unicopia/block/BaseZapAppleLeavesBlock.java @@ -30,16 +30,10 @@ public class BaseZapAppleLeavesBlock extends LeavesBlock implements TintedBlock, @Override public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) { - if (state.get(PERSISTENT) - || oldState.isOf(state.getBlock()) - || oldState.isOf(UBlocks.ZAP_LEAVES) - || oldState.isOf(UBlocks.FLOWERING_ZAP_LEAVES) - || oldState.isOf(UBlocks.ZAP_LEAVES_PLACEHOLDER) - || !(world instanceof ServerWorld sw)) { + if (state.get(PERSISTENT) || oldState.isOf(state.getBlock())) { return; } - - updateStage(state, sw, pos); + updateStage(state, world, pos); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesPlaceholderBlock.java b/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesPlaceholderBlock.java index 7c187001..40fa9fc2 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesPlaceholderBlock.java +++ b/src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesPlaceholderBlock.java @@ -22,9 +22,7 @@ public class ZapAppleLeavesPlaceholderBlock extends AirBlock implements ZapStage @Override public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) { - if (world instanceof ServerWorld sw) { - updateStage(state, sw, pos); - } + updateStage(state, world, pos); } @Deprecated diff --git a/src/main/java/com/minelittlepony/unicopia/block/ZapStagedBlock.java b/src/main/java/com/minelittlepony/unicopia/block/ZapStagedBlock.java index a47e5cb2..5e3f41d4 100644 --- a/src/main/java/com/minelittlepony/unicopia/block/ZapStagedBlock.java +++ b/src/main/java/com/minelittlepony/unicopia/block/ZapStagedBlock.java @@ -8,14 +8,18 @@ import net.minecraft.block.Blocks; import net.minecraft.server.world.ServerWorld; import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.random.Random; +import net.minecraft.world.World; public interface ZapStagedBlock { ZapAppleStageStore.Stage getStage(BlockState state); - default void updateStage(BlockState state, ServerWorld world, BlockPos pos) { - ZapAppleStageStore.Stage currentStage = ZapAppleStageStore.get(world).getStage(); + default void updateStage(BlockState state, World world, BlockPos pos) { + if (!(world instanceof ServerWorld sw)) { + return; + } + ZapAppleStageStore.Stage currentStage = ZapAppleStageStore.get(sw).getStage(); if (currentStage != getStage(state)) { - state = currentStage.getNewState(state); + state = getState(currentStage); world.setBlockState(pos, state); } world.scheduleBlockTick(pos, state.getBlock(), 1); @@ -23,15 +27,35 @@ public interface ZapStagedBlock { default void tryAdvanceStage(BlockState state, ServerWorld world, BlockPos pos, Random random) { ZapAppleStageStore store = ZapAppleStageStore.get(world); - ZapAppleStageStore.Stage newStage = store.getStage(); - if (!world.isDay() && getStage(state).mustChangeIntoInstantly(newStage)) { - state = newStage.getNewState(state); - world.setBlockState(pos, state); - onStageChanged(store, newStage, world, state, pos, random); + ZapAppleStageStore.Stage currentStage = store.getStage(); + if (!world.isDay() && currentStage != getStage(state)) { + int transitionRate = getTransitionRate(currentStage); + if (transitionRate == 0 || random.nextInt(transitionRate) == 0) { + state = getState(currentStage); + world.setBlockState(pos, state); + onStageChanged(store, currentStage, world, state, pos, random); + } } world.scheduleBlockTick(pos, state.getBlock(), 1); } + default int getTransitionRate(ZapAppleStageStore.Stage stage) { + if (stage == ZapAppleStageStore.Stage.HIBERNATING || stage == ZapAppleStageStore.Stage.GREENING) { + return 10; + } + return 2500; + } + + default BlockState getState(ZapAppleStageStore.Stage stage) { + if (stage == ZapAppleStageStore.Stage.HIBERNATING) { + return UBlocks.ZAP_LEAVES_PLACEHOLDER.getDefaultState(); + } + if (stage == ZapAppleStageStore.Stage.FLOWERING) { + return UBlocks.FLOWERING_ZAP_LEAVES.getDefaultState(); + } + return UBlocks.ZAP_LEAVES.getDefaultState().with(ZapAppleLeavesBlock.STAGE, stage); + } + private static void onStageChanged(ZapAppleStageStore store, ZapAppleStageStore.Stage stage, ServerWorld world, BlockState state, BlockPos pos, Random random) { boolean mustFruit = Random.create(state.getRenderingSeed(pos)).nextInt(5) < 2; BlockState below = world.getBlockState(pos.down()); diff --git a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/element/DynamicContent.java b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/element/DynamicContent.java index 804d107d..cbcd7392 100644 --- a/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/element/DynamicContent.java +++ b/src/main/java/com/minelittlepony/unicopia/client/gui/spellbook/element/DynamicContent.java @@ -30,8 +30,6 @@ public class DynamicContent implements Content { private final Panel leftPanel = new Panel(this); private final Panel rightPanel = new Panel(this); - private int headerColor; - public DynamicContent(PacketByteBuf buffer) { pages = buffer.readList(Page::new); } 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 361b59bd..bb936372 100644 --- a/src/main/java/com/minelittlepony/unicopia/server/world/ZapAppleStageStore.java +++ b/src/main/java/com/minelittlepony/unicopia/server/world/ZapAppleStageStore.java @@ -5,13 +5,10 @@ import java.util.stream.StreamSupport; import com.minelittlepony.unicopia.USounds; import com.minelittlepony.unicopia.Unicopia; -import com.minelittlepony.unicopia.block.UBlocks; -import com.minelittlepony.unicopia.block.ZapAppleLeavesBlock; import com.minelittlepony.unicopia.particle.LightningBoltParticleEffect; import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.util.Tickable; -import net.minecraft.block.BlockState; import net.minecraft.entity.EntityType; import net.minecraft.entity.LightningEntity; import net.minecraft.nbt.*; @@ -156,24 +153,9 @@ public class ZapAppleStageStore extends PersistentState implements Tickable { return VALUES[MathHelper.clamp(id, 0, VALUES.length)]; } - public boolean mustChangeIntoInstantly(Stage to) { - return this != to;// && (this == HIBERNATING || to == HIBERNATING); - } - - public BlockState getNewState(BlockState currentState) { - if (this == ZapAppleStageStore.Stage.HIBERNATING) { - return UBlocks.ZAP_LEAVES_PLACEHOLDER.getDefaultState(); - } - if (this == ZapAppleStageStore.Stage.FLOWERING) { - return UBlocks.FLOWERING_ZAP_LEAVES.getDefaultState(); - } - return UBlocks.ZAP_LEAVES.getDefaultState().with(ZapAppleLeavesBlock.STAGE, this); - } - @Override public String asString() { return name().toLowerCase(Locale.ROOT); } } - }