From ead8d12c6e2f7d0a9c0a07820a569cffaca8afa9 Mon Sep 17 00:00:00 2001 From: Justin Parsell Date: Fri, 30 Apr 2021 19:17:05 -0400 Subject: [PATCH] Working wireconnection w/ expected functionality --- .../parsell/glowstonewire/GlowstoneWire.java | 15 ++++- .../common/GlowWireConnection.java | 4 +- .../common/GlowstoneWireBlock.java | 65 ++++++++++--------- 3 files changed, 48 insertions(+), 36 deletions(-) diff --git a/src/main/java/me/parsell/glowstonewire/GlowstoneWire.java b/src/main/java/me/parsell/glowstonewire/GlowstoneWire.java index 5533732..71a16d6 100644 --- a/src/main/java/me/parsell/glowstonewire/GlowstoneWire.java +++ b/src/main/java/me/parsell/glowstonewire/GlowstoneWire.java @@ -1,8 +1,11 @@ package me.parsell.glowstonewire; +import java.util.Iterator; + import me.parsell.glowstonewire.core.glowBlocks; import me.parsell.glowstonewire.core.glowItems; import net.fabricmc.api.ModInitializer; +import net.minecraft.util.math.Direction; public class GlowstoneWire implements ModInitializer { @Override @@ -12,7 +15,15 @@ public class GlowstoneWire implements ModInitializer { // Proceed with mild caution. System.out.println("Hello Fabric world!"); - glowBlocks.init(); - glowItems.init(); + glowBlocks.init(); + glowItems.init(); + + String[] states = {"none", "broke", "side", "up"}; + //for(int i = 0; i < 4; i++) + // for (int j = 0; j < 4; j++) + // for (int k = 0; k < 4; k++) + // for (int l = 0; l < 4; l++) + // System.out.println("north: " + states[i] + " south: " + states[j] + " east: " + states[k] + " west: " + states[l]); + } } diff --git a/src/main/java/me/parsell/glowstonewire/common/GlowWireConnection.java b/src/main/java/me/parsell/glowstonewire/common/GlowWireConnection.java index acfe5b0..95aa351 100644 --- a/src/main/java/me/parsell/glowstonewire/common/GlowWireConnection.java +++ b/src/main/java/me/parsell/glowstonewire/common/GlowWireConnection.java @@ -23,10 +23,10 @@ public enum GlowWireConnection implements StringIdentifiable{ } public boolean isBroken() { - return this != BROKE; + return this == BROKE; } public boolean isConnected() { - return this != NONE; + return this != NONE && this != BROKE; } } diff --git a/src/main/java/me/parsell/glowstonewire/common/GlowstoneWireBlock.java b/src/main/java/me/parsell/glowstonewire/common/GlowstoneWireBlock.java index bef90be..64f06f7 100644 --- a/src/main/java/me/parsell/glowstonewire/common/GlowstoneWireBlock.java +++ b/src/main/java/me/parsell/glowstonewire/common/GlowstoneWireBlock.java @@ -4,15 +4,16 @@ import java.util.Map; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Maps; +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.player.PlayerEntity; import net.minecraft.item.ItemPlacementContext; import net.minecraft.state.StateManager; import net.minecraft.state.property.EnumProperty; -import net.minecraft.state.property.Properties; import net.minecraft.util.ActionResult; import net.minecraft.util.BlockMirror; import net.minecraft.util.BlockRotation; @@ -77,22 +78,26 @@ public class GlowstoneWireBlock extends Block{ } private BlockState determineState(WorldAccess world, BlockState state, BlockPos pos){ - if(world.getBlockState(pos.offset(Direction.NORTH)).isOf(this)) - state = state.with(WIRE_CONNECTION_NORTH, determineConnection(world, pos, Direction.NORTH)); - if(world.getBlockState(pos.offset(Direction.SOUTH)).isOf(this)) - state = state.with(WIRE_CONNECTION_SOUTH, determineConnection(world, pos, Direction.SOUTH)); - if(world.getBlockState(pos.offset(Direction.EAST)).isOf(this)) - state = state.with(WIRE_CONNECTION_EAST, determineConnection(world, pos, Direction.EAST)); - if(world.getBlockState(pos.offset(Direction.WEST)).isOf(this)) - state = state.with(WIRE_CONNECTION_WEST, determineConnection(world, pos, Direction.WEST)); + 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_PROPERTY.get(dir)).isBroken()) + state = state.with(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(dir), determineConnection(world, pos, dir)); + } return state; } private GlowWireConnection determineConnection(WorldAccess world, BlockPos pos, Direction direction){ if(world.getBlockState(pos.offset(direction).up()).isOf(this)) - return GlowWireConnection.UP; - if(world.getBlockState(pos.offset(direction)).isOf(this) || world.getBlockState(pos.offset(direction).down()).isOf(this)) - return GlowWireConnection.SIDE; + if(!world.getBlockState(pos.offset(direction).up()).get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction.getOpposite())).isBroken()) + return GlowWireConnection.UP; + if(world.getBlockState(pos.offset(direction).down()).isOf(this)) + if(!world.getBlockState(pos.offset(direction).down()).get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction.getOpposite())).isBroken()) + return GlowWireConnection.SIDE; + if(world.getBlockState(pos.offset(direction)).isOf(this)) + if(!world.getBlockState(pos.offset(direction)).get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction.getOpposite())).isBroken()) { + return GlowWireConnection.SIDE; + } return GlowWireConnection.NONE; } @@ -124,8 +129,8 @@ public class GlowstoneWireBlock extends Block{ } } - @Override - public BlockState mirror(BlockState state, BlockMirror mirror) { + @Override + public BlockState mirror(BlockState state, BlockMirror mirror) { switch(mirror) { case LEFT_RIGHT: return (BlockState)((BlockState)state.with(WIRE_CONNECTION_NORTH, state.get(WIRE_CONNECTION_SOUTH))).with(WIRE_CONNECTION_SOUTH, state.get(WIRE_CONNECTION_NORTH)); @@ -136,31 +141,27 @@ public class GlowstoneWireBlock extends Block{ } } - // This needs to be fixed, it should notify it's neighbors that it shouldn't be connected to me anymore + // This needs to be fixed, it should notify it's neighbors that it shouldn't be connected to me anymore @Override public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit){ - // Set to blank state - if(hasConnection(state)) { - world.setBlockState(pos, this.getDefaultState()); - world.updateNeighborsAlways(pos, this); - return ActionResult.SUCCESS; - } - - // Check if we are updating the state to a new state - BlockState newState = determineState(world, state, pos); - if(state != newState) { - world.setBlockState(pos, newState); - return ActionResult.SUCCESS; - } - + if(player.abilities.allowModifyWorld) + if(hasConnection(state) && !player.hasStackEquipped(EquipmentSlot.MAINHAND)) { + Iterator dirItr = Direction.Type.HORIZONTAL.iterator(); + while(dirItr.hasNext()){ + Direction dir = dirItr.next(); + if(state.get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(dir)).isConnected()) + state = state.with(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(dir), GlowWireConnection.BROKE); + } + world.setBlockState(pos, state); + world.updateNeighborsAlways(pos, this); + return ActionResult.SUCCESS; + } return ActionResult.PASS; } - // This decides what I should look like whens something around me happens (use this for wire connection) @Override public BlockState getStateForNeighborUpdate(BlockState state, Direction direction, BlockState newState, WorldAccess world, BlockPos pos, BlockPos posFrom) { - System.out.println(world.getBlockState(posFrom).toString()); - if (direction != Direction.DOWN && direction != Direction.UP) + if (direction != Direction.DOWN && direction != Direction.UP && !state.get(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction)).isBroken()) state = state.with(DIRECTION_TO_WIRE_CONNECTION_PROPERTY.get(direction), determineConnection(world, pos, direction)); return state; }