Clean up and adjust transitions when zap apple trees change stages

This commit is contained in:
Sollace 2024-02-01 13:26:26 +00:00
parent fbe859dcc7
commit 2e938633e9
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
5 changed files with 35 additions and 39 deletions

View file

@ -30,16 +30,10 @@ public class BaseZapAppleLeavesBlock extends LeavesBlock implements TintedBlock,
@Override @Override
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) { public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) {
if (state.get(PERSISTENT) if (state.get(PERSISTENT) || oldState.isOf(state.getBlock())) {
|| 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)) {
return; return;
} }
updateStage(state, world, pos);
updateStage(state, sw, pos);
} }
@Override @Override

View file

@ -22,9 +22,7 @@ public class ZapAppleLeavesPlaceholderBlock extends AirBlock implements ZapStage
@Override @Override
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) { public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) {
if (world instanceof ServerWorld sw) { updateStage(state, world, pos);
updateStage(state, sw, pos);
}
} }
@Deprecated @Deprecated

View file

@ -8,14 +8,18 @@ import net.minecraft.block.Blocks;
import net.minecraft.server.world.ServerWorld; import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.BlockPos; import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.random.Random; import net.minecraft.util.math.random.Random;
import net.minecraft.world.World;
public interface ZapStagedBlock { public interface ZapStagedBlock {
ZapAppleStageStore.Stage getStage(BlockState state); ZapAppleStageStore.Stage getStage(BlockState state);
default void updateStage(BlockState state, ServerWorld world, BlockPos pos) { default void updateStage(BlockState state, World world, BlockPos pos) {
ZapAppleStageStore.Stage currentStage = ZapAppleStageStore.get(world).getStage(); if (!(world instanceof ServerWorld sw)) {
return;
}
ZapAppleStageStore.Stage currentStage = ZapAppleStageStore.get(sw).getStage();
if (currentStage != getStage(state)) { if (currentStage != getStage(state)) {
state = currentStage.getNewState(state); state = getState(currentStage);
world.setBlockState(pos, state); world.setBlockState(pos, state);
} }
world.scheduleBlockTick(pos, state.getBlock(), 1); world.scheduleBlockTick(pos, state.getBlock(), 1);
@ -23,15 +27,35 @@ public interface ZapStagedBlock {
default void tryAdvanceStage(BlockState state, ServerWorld world, BlockPos pos, Random random) { default void tryAdvanceStage(BlockState state, ServerWorld world, BlockPos pos, Random random) {
ZapAppleStageStore store = ZapAppleStageStore.get(world); ZapAppleStageStore store = ZapAppleStageStore.get(world);
ZapAppleStageStore.Stage newStage = store.getStage(); ZapAppleStageStore.Stage currentStage = store.getStage();
if (!world.isDay() && getStage(state).mustChangeIntoInstantly(newStage)) { if (!world.isDay() && currentStage != getStage(state)) {
state = newStage.getNewState(state); int transitionRate = getTransitionRate(currentStage);
world.setBlockState(pos, state); if (transitionRate == 0 || random.nextInt(transitionRate) == 0) {
onStageChanged(store, newStage, world, state, pos, random); state = getState(currentStage);
world.setBlockState(pos, state);
onStageChanged(store, currentStage, world, state, pos, random);
}
} }
world.scheduleBlockTick(pos, state.getBlock(), 1); 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) { 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; boolean mustFruit = Random.create(state.getRenderingSeed(pos)).nextInt(5) < 2;
BlockState below = world.getBlockState(pos.down()); BlockState below = world.getBlockState(pos.down());

View file

@ -30,8 +30,6 @@ public class DynamicContent implements Content {
private final Panel leftPanel = new Panel(this); private final Panel leftPanel = new Panel(this);
private final Panel rightPanel = new Panel(this); private final Panel rightPanel = new Panel(this);
private int headerColor;
public DynamicContent(PacketByteBuf buffer) { public DynamicContent(PacketByteBuf buffer) {
pages = buffer.readList(Page::new); pages = buffer.readList(Page::new);
} }

View file

@ -5,13 +5,10 @@ import java.util.stream.StreamSupport;
import com.minelittlepony.unicopia.USounds; import com.minelittlepony.unicopia.USounds;
import com.minelittlepony.unicopia.Unicopia; 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.LightningBoltParticleEffect;
import com.minelittlepony.unicopia.particle.ParticleUtils; import com.minelittlepony.unicopia.particle.ParticleUtils;
import com.minelittlepony.unicopia.util.Tickable; import com.minelittlepony.unicopia.util.Tickable;
import net.minecraft.block.BlockState;
import net.minecraft.entity.EntityType; import net.minecraft.entity.EntityType;
import net.minecraft.entity.LightningEntity; import net.minecraft.entity.LightningEntity;
import net.minecraft.nbt.*; import net.minecraft.nbt.*;
@ -156,24 +153,9 @@ public class ZapAppleStageStore extends PersistentState implements Tickable {
return VALUES[MathHelper.clamp(id, 0, VALUES.length)]; 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 @Override
public String asString() { public String asString() {
return name().toLowerCase(Locale.ROOT); return name().toLowerCase(Locale.ROOT);
} }
} }
} }