From b60c56f464d0fc27efff46b7898ee24c69c8b65a Mon Sep 17 00:00:00 2001 From: Gerrygames Date: Mon, 19 Nov 2018 09:28:37 +0100 Subject: [PATCH] Tripwire connections --- .../blockconnections/ConnectionData.java | 1 + .../blockconnections/ConnectionHandler.java | 6 +- .../TripwireConnectionHandler.java | 92 +++++++++++++++++++ 3 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/TripwireConnectionHandler.java diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java index 4e81f9891..dadb1dafc 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionData.java @@ -176,6 +176,7 @@ public class ConnectionData { StairConnectionHandler.init(); FlowerConnectionHandler.init(); ChorusPlantConnectionHandler.init(); + TripwireConnectionHandler.init(); if (Via.getConfig().getBlockConnectionMethod().equalsIgnoreCase("packet")) { Via.getManager().getProviders().register(BlockConnectionProvider.class, new PacketBlockConnectionProvider()); diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionHandler.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionHandler.java index 78d52c79f..621185696 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionHandler.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/ConnectionHandler.java @@ -6,10 +6,10 @@ import us.myles.ViaVersion.api.minecraft.Position; import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.BlockConnectionProvider; public abstract class ConnectionHandler { - public abstract int connect(UserConnection connection, Position position, int blockState); + public abstract int connect(UserConnection user, Position position, int blockState); - public int getBlockData(UserConnection connection, Position position) { - return Via.getManager().getProviders().get(BlockConnectionProvider.class).getBlockdata(connection, position); + public int getBlockData(UserConnection user, Position position) { + return Via.getManager().getProviders().get(BlockConnectionProvider.class).getBlockdata(user, position); } public boolean canConnect(int id) { diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/TripwireConnectionHandler.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/TripwireConnectionHandler.java new file mode 100644 index 000000000..343c0a612 --- /dev/null +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_13to1_12_2/blockconnections/TripwireConnectionHandler.java @@ -0,0 +1,92 @@ +package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.ToString; +import us.myles.ViaVersion.api.data.UserConnection; +import us.myles.ViaVersion.api.minecraft.BlockFace; +import us.myles.ViaVersion.api.minecraft.Position; + +import java.util.HashMap; +import java.util.Map; + +public class TripwireConnectionHandler extends ConnectionHandler { + private static Map tripwireDataMap = new HashMap<>(); + private static Map connectedBlocks = new HashMap<>(); + private static Map tripwireHooks = new HashMap<>(); + + static void init() { + TripwireConnectionHandler connectionHandler = new TripwireConnectionHandler(); + for (Map.Entry blockState : ConnectionData.keyToId.entrySet()) { + String key = blockState.getKey().split("\\[")[0]; + + if (key.equals("minecraft:tripwire_hook")) { + WrappedBlockData blockData = WrappedBlockData.fromString(blockState.getKey()); + tripwireHooks.put(blockState.getValue(), BlockFace.valueOf(blockData.getValue("facing").toUpperCase())); + } else if (key.equals("minecraft:tripwire")) { + WrappedBlockData blockData = WrappedBlockData.fromString(blockState.getKey()); + + TripwireData tripwireData = new TripwireData( + blockData.getValue("attached").equals("true"), + blockData.getValue("disarmed").equals("true"), + blockData.getValue("powered").equals("true") + ); + + tripwireDataMap.put(blockState.getValue(), tripwireData); + connectedBlocks.put(getStates(blockData), blockState.getValue()); + + ConnectionData.connectionHandlerMap.put(blockState.getValue(), connectionHandler); + } + } + } + + private static byte getStates(WrappedBlockData blockData) { + byte b = 0; + if (blockData.getValue("attached").equals("true")) b |= 1; + if (blockData.getValue("disarmed").equals("true")) b |= 2; + if (blockData.getValue("powered").equals("true")) b |= 4; + if (blockData.getValue("east").equals("true")) b |= 8; + if (blockData.getValue("north").equals("true")) b |= 16; + if (blockData.getValue("south").equals("true")) b |= 32; + if (blockData.getValue("west").equals("true")) b |= 64; + return b; + } + + @Override + public int connect(UserConnection user, Position position, int blockState) { + TripwireData tripwireData = tripwireDataMap.get(blockState); + if (tripwireData == null) return blockState; + byte b = 0; + if (tripwireData.isAttached()) b |= 1; + if (tripwireData.isDisarmed()) b |= 2; + if (tripwireData.isPowered()) b |= 4; + + int east = getBlockData(user, position.getRelative(BlockFace.EAST)); + int north = getBlockData(user, position.getRelative(BlockFace.NORTH)); + int south = getBlockData(user, position.getRelative(BlockFace.SOUTH)); + int west = getBlockData(user, position.getRelative(BlockFace.WEST)); + + if (tripwireDataMap.containsKey(east) || tripwireHooks.get(east) == BlockFace.WEST) { + b |= 8; + } + if (tripwireDataMap.containsKey(north) || tripwireHooks.get(north) == BlockFace.SOUTH) { + b |= 16; + } + if (tripwireDataMap.containsKey(south) || tripwireHooks.get(south) == BlockFace.NORTH) { + b |= 32; + } + if (tripwireDataMap.containsKey(west) || tripwireHooks.get(west) == BlockFace.EAST) { + b |= 64; + } + + Integer newBlockState = connectedBlocks.get(b); + return newBlockState == null ? blockState : newBlockState; + } + + @AllArgsConstructor + @Getter + @ToString + private static class TripwireData { + private final boolean attached, disarmed, powered; + } +}