fixes fence breaking not updating neighbors and simplifies related code
This commit is contained in:
@@ -13,7 +13,6 @@ import net.cmr.jurassicrevived.recipe.ModRecipes;
|
||||
import net.cmr.jurassicrevived.screen.ModMenuTypes;
|
||||
import net.cmr.jurassicrevived.sound.ModSounds;
|
||||
import net.cmr.jurassicrevived.util.FenceClimbHandler;
|
||||
import net.cmr.jurassicrevived.util.FenceDiagonalHandler;
|
||||
import net.cmr.jurassicrevived.util.ModEvents;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.world.item.Items;
|
||||
@@ -64,7 +63,6 @@ public class CommonClass
|
||||
ModBlockEntities.register();
|
||||
|
||||
FenceClimbHandler.register();
|
||||
FenceDiagonalHandler.init();
|
||||
|
||||
ModEvents.init();
|
||||
|
||||
|
||||
@@ -117,6 +117,34 @@ public class FencePoleBlock extends Block implements SimpleWaterloggedBlock {
|
||||
super.neighborChanged(state, level, pos, neighborBlock, neighborPos, movedByPiston);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean isMoving) {
|
||||
if (!state.is(oldState.getBlock())) {
|
||||
if (beginGuard()) {
|
||||
try {
|
||||
updateDiagonalsAround(level, pos);
|
||||
} finally {
|
||||
endGuard();
|
||||
}
|
||||
}
|
||||
}
|
||||
super.onPlace(state, level, pos, oldState, isMoving);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean isMoving) {
|
||||
if (!state.is(newState.getBlock())) {
|
||||
if (beginGuard()) {
|
||||
try {
|
||||
updateDiagonalsAround(level, pos);
|
||||
} finally {
|
||||
endGuard();
|
||||
}
|
||||
}
|
||||
}
|
||||
super.onRemove(state, level, pos, newState, isMoving);
|
||||
}
|
||||
|
||||
private static boolean beginGuard() {
|
||||
return FenceUpdateGuard.begin();
|
||||
}
|
||||
|
||||
@@ -154,6 +154,34 @@ public class FenceWireBlock extends Block implements SimpleWaterloggedBlock {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onPlace(BlockState state, Level level, BlockPos pos, BlockState oldState, boolean isMoving) {
|
||||
if (!state.is(oldState.getBlock())) {
|
||||
if (beginGuard()) {
|
||||
try {
|
||||
updateDiagonalsAround(level, pos);
|
||||
} finally {
|
||||
endGuard();
|
||||
}
|
||||
}
|
||||
}
|
||||
super.onPlace(state, level, pos, oldState, isMoving);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onRemove(BlockState state, Level level, BlockPos pos, BlockState newState, boolean isMoving) {
|
||||
if (!state.is(newState.getBlock())) {
|
||||
if (beginGuard()) {
|
||||
try {
|
||||
updateDiagonalsAround(level, pos);
|
||||
} finally {
|
||||
endGuard();
|
||||
}
|
||||
}
|
||||
}
|
||||
super.onRemove(state, level, pos, newState, isMoving);
|
||||
}
|
||||
|
||||
private void recomputeSelfDiagonals(Level level, BlockPos pos, BlockState state) {
|
||||
BlockState updated = state
|
||||
.setValue(NE, canConnectDiagonally(level, pos, Direction.NORTH, Direction.EAST))
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
package net.cmr.jurassicrevived.util;
|
||||
|
||||
import dev.architectury.event.events.common.BlockEvent;
|
||||
import net.cmr.jurassicrevived.block.custom.FencePoleBlock;
|
||||
import net.cmr.jurassicrevived.block.custom.FenceWireBlock;
|
||||
import net.minecraft.core.BlockPos;
|
||||
import net.minecraft.core.Direction;
|
||||
import net.minecraft.world.level.Level;
|
||||
import net.minecraft.world.level.block.Block;
|
||||
import net.minecraft.world.level.block.state.BlockState;
|
||||
|
||||
public final class FenceDiagonalHandler {
|
||||
|
||||
public static void init() {
|
||||
// Register for block placement
|
||||
BlockEvent.PLACE.register((level, pos, state, entity) -> {
|
||||
if (level instanceof Level) {
|
||||
notifyDiagonalFences((Level) level, pos);
|
||||
}
|
||||
return dev.architectury.event.EventResult.pass();
|
||||
});
|
||||
|
||||
// Register for block breaking
|
||||
BlockEvent.BREAK.register((level, pos, state, player, xp) -> {
|
||||
if (level instanceof Level) {
|
||||
notifyDiagonalFences((Level) level, pos);
|
||||
}
|
||||
return dev.architectury.event.EventResult.pass();
|
||||
});
|
||||
}
|
||||
|
||||
private static void notifyDiagonalFences(Level level, BlockPos changedPos) {
|
||||
BlockPos[] diagonals = new BlockPos[] {
|
||||
changedPos.north().east(),
|
||||
changedPos.south().east(),
|
||||
changedPos.south().west(),
|
||||
changedPos.north().west()
|
||||
};
|
||||
|
||||
for (BlockPos p : diagonals) {
|
||||
BlockState bs = level.getBlockState(p);
|
||||
|
||||
if (bs.getBlock() instanceof FenceWireBlock) {
|
||||
BlockState updated = bs
|
||||
.setValue(FenceWireBlock.NE, FenceWireBlock.canConnectDiagonally(level, p, Direction.NORTH, Direction.EAST))
|
||||
.setValue(FenceWireBlock.SE, FenceWireBlock.canConnectDiagonally(level, p, Direction.SOUTH, Direction.EAST))
|
||||
.setValue(FenceWireBlock.SW, FenceWireBlock.canConnectDiagonally(level, p, Direction.SOUTH, Direction.WEST))
|
||||
.setValue(FenceWireBlock.NW, FenceWireBlock.canConnectDiagonally(level, p, Direction.NORTH, Direction.WEST));
|
||||
if (updated != bs) {
|
||||
level.setBlock(p, updated, Block.UPDATE_CLIENTS);
|
||||
}
|
||||
} else if (bs.getBlock() instanceof FencePoleBlock) {
|
||||
BlockState updated = bs
|
||||
.setValue(FencePoleBlock.NE, FenceWireBlock.canConnectDiagonally(level, p, Direction.NORTH, Direction.EAST))
|
||||
.setValue(FencePoleBlock.SE, FenceWireBlock.canConnectDiagonally(level, p, Direction.SOUTH, Direction.EAST))
|
||||
.setValue(FencePoleBlock.SW, FenceWireBlock.canConnectDiagonally(level, p, Direction.SOUTH, Direction.WEST))
|
||||
.setValue(FencePoleBlock.NW, FenceWireBlock.canConnectDiagonally(level, p, Direction.NORTH, Direction.WEST));
|
||||
if (updated != bs) {
|
||||
level.setBlock(p, updated, Block.UPDATE_CLIENTS);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user