Fixed hybernating zap leaves not properly treated as air

This commit is contained in:
Sollace 2023-05-23 19:30:21 +01:00
parent 5698264da7
commit 4713be600f
4 changed files with 56 additions and 9 deletions

View file

@ -45,6 +45,7 @@ public interface UBlocks {
Block STRIPPED_ZAP_WOOD = register("stripped_zap_wood", new ZapAppleLogBlock(Blocks.STRIPPED_OAK_WOOD, MapColor.GRAY, MapColor.GRAY), ItemGroups.BUILDING_BLOCKS); Block STRIPPED_ZAP_WOOD = register("stripped_zap_wood", new ZapAppleLogBlock(Blocks.STRIPPED_OAK_WOOD, MapColor.GRAY, MapColor.GRAY), ItemGroups.BUILDING_BLOCKS);
Block ZAP_LEAVES = register("zap_leaves", new ZapAppleLeavesBlock(), ItemGroups.NATURAL); Block ZAP_LEAVES = register("zap_leaves", new ZapAppleLeavesBlock(), ItemGroups.NATURAL);
Block ZAP_LEAVES_PLACEHOLDER = register("zap_leaves_placeholder", new ZapAppleLeavesPlaceholderBlock());
Block ZAP_BULB = register("zap_bulb", new FruitBlock(FabricBlockSettings.of(Material.GOURD, MapColor.GRAY).strength(500, 1200).sounds(BlockSoundGroup.AZALEA_LEAVES), Direction.DOWN, ZAP_LEAVES, FruitBlock.DEFAULT_SHAPE, false)); Block ZAP_BULB = register("zap_bulb", new FruitBlock(FabricBlockSettings.of(Material.GOURD, MapColor.GRAY).strength(500, 1200).sounds(BlockSoundGroup.AZALEA_LEAVES), Direction.DOWN, ZAP_LEAVES, FruitBlock.DEFAULT_SHAPE, false));
Block ZAP_APPLE = register("zap_apple", new FruitBlock(FabricBlockSettings.of(Material.GOURD, MapColor.GRAY).sounds(BlockSoundGroup.AZALEA_LEAVES), Direction.DOWN, ZAP_LEAVES, FruitBlock.DEFAULT_SHAPE, false)); Block ZAP_APPLE = register("zap_apple", new FruitBlock(FabricBlockSettings.of(Material.GOURD, MapColor.GRAY).sounds(BlockSoundGroup.AZALEA_LEAVES), Direction.DOWN, ZAP_LEAVES, FruitBlock.DEFAULT_SHAPE, false));

View file

@ -23,6 +23,7 @@ public class ZapAppleLeavesBlock extends LeavesBlock implements TintedBlock {
public static final EnumProperty<ZapAppleStageStore.Stage> STAGE = EnumProperty.of("stage", ZapAppleStageStore.Stage.class); public static final EnumProperty<ZapAppleStageStore.Stage> STAGE = EnumProperty.of("stage", ZapAppleStageStore.Stage.class);
ZapAppleLeavesBlock() { ZapAppleLeavesBlock() {
super(Settings.of(Material.LEAVES) super(Settings.of(Material.LEAVES)
.strength(500, 1200) .strength(500, 1200)
.ticksRandomly() .ticksRandomly()
@ -43,17 +44,20 @@ public class ZapAppleLeavesBlock extends LeavesBlock implements TintedBlock {
@Override @Override
public boolean hasRandomTicks(BlockState state) { public boolean hasRandomTicks(BlockState state) {
return true; return !state.get(PERSISTENT);
} }
@Override @Override
public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
super.randomTick(state, world, pos, random); super.randomTick(state, world, pos, random);
if (state.get(PERSISTENT)) {
return;
}
ZapAppleStageStore store = ZapAppleStageStore.get(world); ZapAppleStageStore store = ZapAppleStageStore.get(world);
ZapAppleStageStore.Stage newStage = store.getStage(); ZapAppleStageStore.Stage newStage = store.getStage();
if (!world.isDay() && state.get(STAGE).mustChangeInto(newStage)) { if (!world.isDay() && state.get(STAGE).mustChangeInto(newStage)) {
world.setBlockState(pos, state.with(STAGE, newStage)); world.setBlockState(pos, newStage == ZapAppleStageStore.Stage.HIBERNATING ? UBlocks.ZAP_LEAVES_PLACEHOLDER.getDefaultState() : state.with(STAGE, newStage));
onStageChanged(store, newStage, world, state, pos, random); onStageChanged(store, newStage, world, state, pos, random);
} }
} }
@ -61,11 +65,14 @@ public class ZapAppleLeavesBlock extends LeavesBlock implements TintedBlock {
@Override @Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) { public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
super.scheduledTick(state, world, pos, random); super.scheduledTick(state, world, pos, random);
if (state.get(PERSISTENT)) {
return;
}
ZapAppleStageStore store = ZapAppleStageStore.get(world); ZapAppleStageStore store = ZapAppleStageStore.get(world);
ZapAppleStageStore.Stage newStage = store.getStage(); ZapAppleStageStore.Stage newStage = store.getStage();
if (!world.isDay() && state.get(STAGE).mustChangeIntoInstantly(newStage)) { if (!world.isDay() && state.get(STAGE).mustChangeIntoInstantly(newStage)) {
world.setBlockState(pos, state.with(STAGE, newStage)); world.setBlockState(pos, newStage == ZapAppleStageStore.Stage.HIBERNATING ? UBlocks.ZAP_LEAVES_PLACEHOLDER.getDefaultState() : state.with(STAGE, newStage));
onStageChanged(store, newStage, world, state, pos, random); onStageChanged(store, newStage, world, state, pos, random);
} }
@ -79,14 +86,10 @@ public class ZapAppleLeavesBlock extends LeavesBlock implements TintedBlock {
@Override @Override
public BlockState getPlacementState(ItemPlacementContext ctx) { public BlockState getPlacementState(ItemPlacementContext ctx) {
if (!ctx.getWorld().isClient) { return super.getPlacementState(ctx).with(STAGE, ZapAppleStageStore.Stage.GREENING);
ctx.getWorld().scheduleBlockTick(ctx.getBlockPos(), this, 1);
return super.getPlacementState(ctx).with(STAGE, ZapAppleStageStore.get(ctx.getWorld()).getStage());
}
return super.getPlacementState(ctx);
} }
private void onStageChanged(ZapAppleStageStore store, ZapAppleStageStore.Stage stage, ServerWorld world, BlockState state, BlockPos pos, Random random) { 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

@ -0,0 +1,36 @@
package com.minelittlepony.unicopia.block;
import com.minelittlepony.unicopia.server.world.ZapAppleStageStore;
import net.minecraft.block.*;
import net.minecraft.server.world.ServerWorld;
import net.minecraft.util.math.*;
import net.minecraft.util.math.random.Random;
public class ZapAppleLeavesPlaceholderBlock extends AirBlock {
ZapAppleLeavesPlaceholderBlock() {
super(AbstractBlock.Settings.of(Material.AIR).noCollision().dropsNothing().air());
}
@Override
public boolean hasRandomTicks(BlockState state) {
return true;
}
@Deprecated
@Override
public void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
super.scheduledTick(state, world, pos, random);
ZapAppleStageStore store = ZapAppleStageStore.get(world);
ZapAppleStageStore.Stage newStage = store.getStage();
if (!world.isDay() && ZapAppleStageStore.Stage.HIBERNATING.mustChangeIntoInstantly(newStage)) {
world.setBlockState(pos, UBlocks.ZAP_LEAVES.getDefaultState().with(ZapAppleLeavesBlock.STAGE, newStage));
ZapAppleLeavesBlock.onStageChanged(store, newStage, world, state, pos, random);
}
world.scheduleBlockTick(pos, this, 1);
}
}

View file

@ -0,0 +1,7 @@
{
"variants": {
"": {
"model": "minecraft:block/air"
}
}
}