-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed zap apple trees not progressing through their stages properly
- Loading branch information
Showing
5 changed files
with
87 additions
and
120 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 8 additions & 23 deletions
31
src/main/java/com/minelittlepony/unicopia/block/ZapAppleLeavesPlaceholderBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,36 @@ | ||
package com.minelittlepony.unicopia.block; | ||
|
||
import com.minelittlepony.unicopia.server.world.ZapAppleStageStore; | ||
import com.minelittlepony.unicopia.server.world.ZapAppleStageStore.Stage; | ||
|
||
import net.minecraft.block.*; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.math.*; | ||
import net.minecraft.util.math.random.Random; | ||
import net.minecraft.world.WorldAccess; | ||
import net.minecraft.world.World; | ||
|
||
public class ZapAppleLeavesPlaceholderBlock extends AirBlock { | ||
public class ZapAppleLeavesPlaceholderBlock extends AirBlock implements ZapStagedBlock { | ||
|
||
ZapAppleLeavesPlaceholderBlock() { | ||
super(Settings.create().replaceable().noCollision().dropsNothing().air()); | ||
} | ||
|
||
@Override | ||
public boolean hasRandomTicks(BlockState state) { | ||
return true; | ||
public Stage getStage(BlockState state) { | ||
return ZapAppleStageStore.Stage.HIBERNATING; | ||
} | ||
|
||
@Override | ||
public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState neighborState, WorldAccess world, BlockPos pos, BlockPos neighborPos) { | ||
|
||
public void onBlockAdded(BlockState state, World world, BlockPos pos, BlockState oldState, boolean notify) { | ||
if (world instanceof ServerWorld sw) { | ||
ZapAppleStageStore store = ZapAppleStageStore.get(sw); | ||
ZapAppleStageStore.Stage currentStage = store.getStage(); | ||
if (currentStage != ZapAppleStageStore.Stage.HIBERNATING) { | ||
return currentStage.getNewState(state); | ||
} | ||
updateStage(state, sw, pos); | ||
} | ||
|
||
return state; | ||
} | ||
|
||
@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)) { | ||
state = newStage.getNewState(state); | ||
world.setBlockState(pos, state); | ||
BaseZapAppleLeavesBlock.onStageChanged(store, newStage, world, state, pos, random); | ||
} | ||
|
||
world.scheduleBlockTick(pos, state.getBlock(), 1); | ||
tryAdvanceStage(state, world, pos, random); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/main/java/com/minelittlepony/unicopia/block/ZapStagedBlock.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.minelittlepony.unicopia.block; | ||
|
||
import com.minelittlepony.unicopia.server.world.ZapAppleStageStore; | ||
|
||
import net.minecraft.block.Block; | ||
import net.minecraft.block.BlockState; | ||
import net.minecraft.block.Blocks; | ||
import net.minecraft.server.world.ServerWorld; | ||
import net.minecraft.util.math.BlockPos; | ||
import net.minecraft.util.math.random.Random; | ||
|
||
public interface ZapStagedBlock { | ||
ZapAppleStageStore.Stage getStage(BlockState state); | ||
|
||
default void updateStage(BlockState state, ServerWorld world, BlockPos pos) { | ||
ZapAppleStageStore.Stage currentStage = ZapAppleStageStore.get(world).getStage(); | ||
if (currentStage != getStage(state)) { | ||
state = currentStage.getNewState(state); | ||
world.setBlockState(pos, state); | ||
} | ||
world.scheduleBlockTick(pos, state.getBlock(), 1); | ||
} | ||
|
||
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); | ||
} | ||
world.scheduleBlockTick(pos, state.getBlock(), 1); | ||
} | ||
|
||
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()); | ||
|
||
if (world.isAir(pos.down())) { | ||
if (stage == ZapAppleStageStore.Stage.FRUITING && mustFruit) { | ||
world.setBlockState(pos.down(), UBlocks.ZAP_BULB.getDefaultState(), Block.NOTIFY_ALL); | ||
store.triggerLightningStrike(pos); | ||
} | ||
} | ||
|
||
if (stage != ZapAppleStageStore.Stage.HIBERNATING && world.getRandom().nextInt(10) == 0) { | ||
store.triggerLightningStrike(pos); | ||
} | ||
|
||
if (stage == ZapAppleStageStore.Stage.RIPE) { | ||
if (below.isOf(UBlocks.ZAP_BULB)) { | ||
world.setBlockState(pos.down(), UBlocks.ZAP_APPLE.getDefaultState(), Block.NOTIFY_ALL); | ||
store.playMoonEffect(pos); | ||
} | ||
} | ||
|
||
if (mustFruit && stage == ZapAppleStageStore.Stage.HIBERNATING) { | ||
if (below.isOf(UBlocks.ZAP_APPLE) || below.isOf(UBlocks.ZAP_BULB)) { | ||
world.setBlockState(pos.down(), Blocks.AIR.getDefaultState()); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters