diff --git a/src/main/java/me/parsell/glowstonewire/common/GlowstoneWireBlock.java b/src/main/java/me/parsell/glowstonewire/common/GlowstoneWireBlock.java index 65b4726..274b5c0 100644 --- a/src/main/java/me/parsell/glowstonewire/common/GlowstoneWireBlock.java +++ b/src/main/java/me/parsell/glowstonewire/common/GlowstoneWireBlock.java @@ -4,14 +4,19 @@ import java.util.Map; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; + +import org.jetbrains.annotations.Nullable; + import java.util.Iterator; import net.minecraft.block.Block; import net.minecraft.block.BlockState; import net.minecraft.block.ShapeContext; import net.minecraft.entity.EquipmentSlot; +import net.minecraft.entity.LivingEntity; import net.minecraft.entity.player.PlayerEntity; import net.minecraft.item.ItemPlacementContext; +import net.minecraft.item.ItemStack; import net.minecraft.state.StateManager; import net.minecraft.state.property.EnumProperty; import net.minecraft.util.ActionResult; @@ -63,32 +68,32 @@ public class GlowstoneWireBlock extends Block{ ((GlowWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnected(); } + private static boolean hasUpConnection(BlockState state) { + return ((GlowWireConnection)state.get(WIRE_CONNECTION_NORTH)).isConnectedUp() || + ((GlowWireConnection)state.get(WIRE_CONNECTION_SOUTH)).isConnectedUp() || + ((GlowWireConnection)state.get(WIRE_CONNECTION_EAST)).isConnectedUp() || + ((GlowWireConnection)state.get(WIRE_CONNECTION_WEST)).isConnectedUp(); + } + private BlockState determineState(WorldAccess world, BlockState state, BlockPos pos){ Iterator dirItr = Direction.Type.HORIZONTAL.iterator(); while(dirItr.hasNext()){ Direction dir = dirItr.next(); - if(world.getBlockState(pos.offset(dir)).isOf(this) && !state.get(DIRECTION_TO_WIRE_CONNECTION.get(dir)).isBroken()) - state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(dir), determineConnection(world, pos, dir)); + state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(dir), determineConnection(world, pos, dir)); } return state; } private GlowWireConnection determineConnection(WorldAccess world, BlockPos pos, Direction direction){ - System.out.println("Determining connections..."); - System.out.println(pos + " is checking up connection at " + pos.offset(direction).up()); - if(world.getBlockState(pos.offset(direction).up()).isOf(this)) { - if(!world.getBlockState(pos.offset(direction).up()).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken()) { - System.out.println(pos + " has found up connection at " + pos.offset(direction).up()); + if((world.getBlockState(pos.offset(direction).up()).isOf(this) && world.isAir(pos.up()))) // If offset+up is me && if up is air + if(!world.getBlockState(pos.offset(direction).up()).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken()) //if offset+up is not broken return GlowWireConnection.UP; - } - } - if(world.getBlockState(pos.offset(direction).down()).isOf(this)) - if(!world.getBlockState(pos.offset(direction).down()).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken()) + if(world.getBlockState(pos.offset(direction).down()).isOf(this) && world.isAir(pos.offset(direction))) // if offset+down is me && if offset is air + if(!world.getBlockState(pos.offset(direction).down()).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken()) //if offset+down is not broken return GlowWireConnection.SIDE; - if(world.getBlockState(pos.offset(direction)).isOf(this)) - if(!world.getBlockState(pos.offset(direction)).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken()) { + if(world.getBlockState(pos.offset(direction)).isOf(this)) // if offset is me + if(!world.getBlockState(pos.offset(direction)).get(DIRECTION_TO_WIRE_CONNECTION.get(direction.getOpposite())).isBroken()) // if offset is not broken return GlowWireConnection.SIDE; - } return GlowWireConnection.NONE; } @@ -147,7 +152,8 @@ public class GlowstoneWireBlock extends Block{ } } - @Override + // Place block check disconnect (if block block connection) + @Override // For each link, set to break and update the blocks around me (above and below) public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit){ if(player.abilities.allowModifyWorld) if(hasConnection(state) && !player.hasStackEquipped(EquipmentSlot.MAINHAND)) { @@ -155,33 +161,72 @@ public class GlowstoneWireBlock extends Block{ while(dirItr.hasNext()){ Direction dir = dirItr.next(); if(state.get(DIRECTION_TO_WIRE_CONNECTION.get(dir)).isConnected()) - state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(dir), GlowWireConnection.BROKE); + state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(dir), GlowWireConnection.BROKE); } world.setBlockState(pos, state); - world.updateNeighborsAlways(pos, this); + // These call neighborUpdate not getStateForNeighborUpdate!! + //world.updateNeighborsAlways(pos, this); // Update those around me + world.updateNeighborsAlways(pos.down(), this); // Update those below me + world.updateNeighborsAlways(pos.up(), this); // Update those above me return ActionResult.SUCCESS; } return ActionResult.PASS; } - @Override + @Override // Update the link in the direction of the update, and if it's a block above me, break all links that are UP, down carry on as normal (i will break so dont do anything) public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) { - System.out.println("Update"); - if (direction != Direction.DOWN && direction != Direction.UP && !state.get(DIRECTION_TO_WIRE_CONNECTION.get(direction)).isBroken()) - state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(direction), determineConnection(world, pos, direction)); - return state; + System.out.println("State for Neighbor Update"); + if (direction == Direction.DOWN) + return state; // Do nothing + else if(direction == Direction.UP){ + if(!hasUpConnection(state)) + return state; + Iterator dirItr = Direction.Type.HORIZONTAL.iterator(); + while(dirItr.hasNext()){ + Direction dir = dirItr.next(); + if(!state.get(DIRECTION_TO_WIRE_CONNECTION.get(dir)).isBroken()) + state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(dir), determineConnection(world, pos, dir)); + } + return state; + } else if(state.get(DIRECTION_TO_WIRE_CONNECTION.get(direction)).isBroken()) + return state; + return state.with(DIRECTION_TO_WIRE_CONNECTION.get(direction), determineConnection(world, pos, direction)); } - @Override + @Override // Get the state I should place in, and update those around me (above/below) as needed public BlockState getPlacementState(ItemPlacementContext ctx){ + System.out.println("Placement"); return determineState(ctx.getWorld(), this.getDefaultState(), ctx.getBlockPos()); } @Override + public void onPlaced(World world, BlockPos pos, BlockState state, @Nullable LivingEntity placer, ItemStack itemStack) { + Iterator dirItr = Direction.Type.HORIZONTAL.iterator(); + while(dirItr.hasNext()){ + Direction dir = dirItr.next(); + if (state.get(DIRECTION_TO_WIRE_CONNECTION.get(dir)).isConnectedUp()) { + BlockPos posOffset = pos.offset(dir).offset(Direction.UP); + world.getBlockState(posOffset).neighborUpdate(world, posOffset, this, pos, false); + } + } + } + + @Override // Called when I get told to update, so I should see what/why I need to update. I also use this to determine if I need to be drop or not. public void neighborUpdate(BlockState state, World world, BlockPos pos, Block block, BlockPos fromPos, boolean notify) { - if (!world.isClient && !state.canPlaceAt(world, pos)) { - dropStacks(state, world, pos); - world.removeBlock(pos, false); + System.out.println(pos + " Neighbor Update from " + fromPos); + if (!world.isClient) { + if(!state.canPlaceAt(world, pos)) { + dropStacks(state, world, pos); + world.removeBlock(pos, false); + } else { + Iterator dirItr = Direction.Type.HORIZONTAL.iterator(); + while(dirItr.hasNext()){ + Direction dir = dirItr.next(); + if(!state.get(DIRECTION_TO_WIRE_CONNECTION.get(dir)).isBroken()) + state = state.with(DIRECTION_TO_WIRE_CONNECTION.get(dir), determineConnection(world, pos, dir)); + } + world.setBlockState(pos, state); + } } }