Minor blockconnection cleanup, reduce map lookups

This commit is contained in:
KennyTV 2020-01-28 13:31:36 +01:00
parent 6eeecb271b
commit 7f6c429a55
18 changed files with 275 additions and 242 deletions

View File

@ -24,6 +24,7 @@ import us.myles.ViaVersion.bukkit.providers.BukkitViaBulkChunkTranslator;
import us.myles.ViaVersion.bukkit.providers.BukkitViaMovementTransmitter;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import us.myles.ViaVersion.protocols.protocol1_12to1_11_1.providers.InventoryQuickMoveProvider;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers.BlockConnectionProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.BulkChunkTranslatorProvider;
import us.myles.ViaVersion.protocols.protocol1_9to1_8.providers.HandItemProvider;
@ -143,7 +144,9 @@ public class BukkitViaLoader implements ViaPlatformLoader {
}
if (ProtocolRegistry.SERVER_PROTOCOL < ProtocolVersion.v1_13.getId()) {
if (Via.getConfig().getBlockConnectionMethod().equalsIgnoreCase("world")) {
Via.getManager().getProviders().use(BlockConnectionProvider.class, new BukkitBlockConnectionProvider());
BukkitBlockConnectionProvider blockConnectionProvider = new BukkitBlockConnectionProvider();
Via.getManager().getProviders().use(BlockConnectionProvider.class, blockConnectionProvider);
ConnectionData.blockConnectionProvider = blockConnectionProvider;
}
}
}

View File

@ -1,6 +1,5 @@
package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections;
import lombok.Getter;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.minecraft.BlockFace;
import us.myles.ViaVersion.api.minecraft.Position;
@ -13,27 +12,23 @@ import java.util.Map;
import java.util.Set;
public abstract class AbstractFenceConnectionHandler extends ConnectionHandler {
private final String blockConnections;
@Getter
private Set<Integer> blockStates = new HashSet<>();
private Map<Byte, Integer> connectedBlockStates = new HashMap<>();
private static final StairConnectionHandler STAIR_CONNECTION_HANDLER = new StairConnectionHandler();
private final String blockConnections;
private final Set<Integer> blockStates = new HashSet<>();
private final Map<Byte, Integer> connectedBlockStates = new HashMap<>();
public AbstractFenceConnectionHandler(String blockConnections) {
protected AbstractFenceConnectionHandler(String blockConnections) {
this.blockConnections = blockConnections;
}
public ConnectionData.ConnectorInitAction getInitAction(final String key) {
final AbstractFenceConnectionHandler handler = this;
return new ConnectionData.ConnectorInitAction() {
@Override
public void check(WrappedBlockData blockData) {
if (key.equals(blockData.getMinecraftKey())) {
if (blockData.hasData("waterlogged") && blockData.getValue("waterlogged").equals("true")) return;
blockStates.add(blockData.getSavedBlockStateId());
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
connectedBlockStates.put(getStates(blockData), blockData.getSavedBlockStateId());
}
return blockData -> {
if (key.equals(blockData.getMinecraftKey())) {
if (blockData.hasData("waterlogged") && blockData.getValue("waterlogged").equals("true")) return;
blockStates.add(blockData.getSavedBlockStateId());
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
connectedBlockStates.put(getStates(blockData), blockData.getSavedBlockStateId());
}
};
}
@ -69,8 +64,14 @@ public abstract class AbstractFenceConnectionHandler extends ConnectionHandler {
}
protected boolean connects(BlockFace side, int blockState, boolean pre1_12) {
return blockStates.contains(blockState) || blockConnections != null
&& ConnectionData.blockConnectionData.containsKey(blockState)
&& ConnectionData.blockConnectionData.get(blockState).connectsTo(blockConnections, side.opposite(), pre1_12);
if (blockStates.contains(blockState)) return true;
if (blockConnections == null) return false;
BlockData blockData = ConnectionData.blockConnectionData.get(blockState);
return blockData != null && blockData.connectsTo(blockConnections, side.opposite(), pre1_12);
}
public Set<Integer> getBlockStates() {
return blockStates;
}
}

View File

@ -4,39 +4,32 @@ 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.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.*;
public class AbstractStempConnectionHandler extends ConnectionHandler {
public abstract class AbstractStempConnectionHandler extends ConnectionHandler {
private static final BlockFace[] BLOCK_FACES = {BlockFace.EAST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.WEST};
private int baseStateId;
private Set<Integer> blockId = new HashSet<>();
private final int baseStateId;
private final Set<Integer> blockId = new HashSet<>();
private Map<BlockFace, Integer> stemps = new HashMap<>();
private final Map<BlockFace, Integer> stemps = new HashMap<>();
public AbstractStempConnectionHandler(String baseStateId) {
protected AbstractStempConnectionHandler(String baseStateId) {
this.baseStateId = ConnectionData.getId(baseStateId);
}
public ConnectionData.ConnectorInitAction getInitAction(final String blockId, final String toKey) {
final AbstractStempConnectionHandler handler = this;
return new ConnectionData.ConnectorInitAction() {
@Override
public void check(WrappedBlockData blockData) {
if (blockData.getSavedBlockStateId() == baseStateId || blockId.equals(blockData.getMinecraftKey())) {
if (blockData.getSavedBlockStateId() != baseStateId) {
handler.blockId.add(blockData.getSavedBlockStateId());
}
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
}
if (blockData.getMinecraftKey().equals(toKey)) {
String facing = blockData.getValue("facing").toUpperCase(Locale.ROOT);
stemps.put(BlockFace.valueOf(facing), blockData.getSavedBlockStateId());
return blockData -> {
if (blockData.getSavedBlockStateId() == baseStateId || blockId.equals(blockData.getMinecraftKey())) {
if (blockData.getSavedBlockStateId() != baseStateId) {
handler.blockId.add(blockData.getSavedBlockStateId());
}
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
}
if (blockData.getMinecraftKey().equals(toKey)) {
String facing = blockData.getValue("facing").toUpperCase(Locale.ROOT);
stemps.put(BlockFace.valueOf(facing), blockData.getSavedBlockStateId());
}
};
}

View File

@ -6,7 +6,7 @@ import java.util.HashMap;
import java.util.Map;
public class BlockData {
private Map<String, boolean[]> connectData = new HashMap<>();
private final Map<String, boolean[]> connectData = new HashMap<>();
public void put(String key, boolean[] booleans) {
connectData.put(key, booleans);

View File

@ -4,31 +4,25 @@ 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.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.*;
class ChestConnectionHandler extends ConnectionHandler {
private static Map<Integer, BlockFace> chestFacings = new HashMap<>();
private static Map<Byte, Integer> connectedStates = new HashMap<>();
private static Set<Integer> trappedChests = new HashSet<>();
private static final Map<Integer, BlockFace> chestFacings = new HashMap<>();
private static final Map<Byte, Integer> connectedStates = new HashMap<>();
private static final Set<Integer> trappedChests = new HashSet<>();
static ConnectionData.ConnectorInitAction init() {
final ChestConnectionHandler connectionHandler = new ChestConnectionHandler();
return new ConnectionData.ConnectorInitAction() {
@Override
public void check(WrappedBlockData blockData) {
if (!blockData.getMinecraftKey().equals("minecraft:chest") && !blockData.getMinecraftKey().equals("minecraft:trapped_chest"))
return;
if (blockData.getValue("waterlogged").equals("true")) return;
chestFacings.put(blockData.getSavedBlockStateId(), BlockFace.valueOf(blockData.getValue("facing").toUpperCase(Locale.ROOT)));
if (blockData.getMinecraftKey().equalsIgnoreCase("minecraft:trapped_chest"))
trappedChests.add(blockData.getSavedBlockStateId());
connectedStates.put(getStates(blockData), blockData.getSavedBlockStateId());
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), connectionHandler);
return blockData -> {
if (!blockData.getMinecraftKey().equals("minecraft:chest") && !blockData.getMinecraftKey().equals("minecraft:trapped_chest"))
return;
if (blockData.getValue("waterlogged").equals("true")) return;
chestFacings.put(blockData.getSavedBlockStateId(), BlockFace.valueOf(blockData.getValue("facing").toUpperCase(Locale.ROOT)));
if (blockData.getMinecraftKey().equalsIgnoreCase("minecraft:trapped_chest")) {
trappedChests.add(blockData.getSavedBlockStateId());
}
connectedStates.put(getStates(blockData), blockData.getSavedBlockStateId());
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), connectionHandler);
};
}

View File

@ -3,14 +3,12 @@ package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.minecraft.BlockFace;
import us.myles.ViaVersion.api.minecraft.Position;
import us.myles.ViaVersion.api.protocol.ProtocolVersion;
import us.myles.ViaVersion.protocols.base.ProtocolInfo;
import java.util.ArrayList;
import java.util.List;
public class ChorusPlantConnectionHandler extends AbstractFenceConnectionHandler {
private int endstone;
private final int endstone;
static List<ConnectionData.ConnectorInitAction> init() {
List<ConnectionData.ConnectorInitAction> actions = new ArrayList<>(2);
@ -26,12 +24,9 @@ public class ChorusPlantConnectionHandler extends AbstractFenceConnectionHandler
}
public ConnectionData.ConnectorInitAction getExtraAction() {
return new ConnectionData.ConnectorInitAction() {
@Override
public void check(WrappedBlockData blockData) {
if (blockData.getMinecraftKey().equals("minecraft:chorus_flower")) {
getBlockStates().add(blockData.getSavedBlockStateId());
}
return blockData -> {
if (blockData.getMinecraftKey().equals("minecraft:chorus_flower")) {
getBlockStates().add(blockData.getSavedBlockStateId());
}
};
}

View File

@ -21,6 +21,8 @@ import java.util.*;
import java.util.Map.Entry;
public class ConnectionData {
private static final BlockChangeRecord[] A = new BlockChangeRecord[0];
public static BlockConnectionProvider blockConnectionProvider;
static Map<Integer, String> idToKey = new HashMap<>();
static Map<String, Integer> keyToId = new HashMap<>();
static Map<Integer, ConnectionHandler> connectionHandlerMap = new HashMap<>();
@ -28,10 +30,9 @@ public class ConnectionData {
static Set<Integer> occludingStates = new HashSet<>();
public static void update(UserConnection user, Position position) {
BlockConnectionProvider connectionProvider = Via.getManager().getProviders().get(BlockConnectionProvider.class);
for (BlockFace face : BlockFace.values()) {
Position pos = position.getRelative(face);
int blockState = connectionProvider.getBlockData(user, pos.getX(), pos.getY(), pos.getZ());
int blockState = blockConnectionProvider.getBlockData(user, pos.getX(), pos.getY(), pos.getZ());
ConnectionHandler handler = connectionHandlerMap.get(blockState);
if (handler == null) continue;
@ -112,7 +113,7 @@ public class ConnectionData {
PacketWrapper wrapper = new PacketWrapper(0x0F, null, user);
wrapper.write(Type.INT, chunkX + chunkDeltaX);
wrapper.write(Type.INT, chunkZ + chunkDeltaZ);
wrapper.write(Type.BLOCK_CHANGE_RECORD_ARRAY, updates.toArray(new BlockChangeRecord[0]));
wrapper.write(Type.BLOCK_CHANGE_RECORD_ARRAY, updates.toArray(A));
try {
wrapper.send(Protocol1_13To1_12_2.class, true, true);
} catch (Exception e) {
@ -124,7 +125,7 @@ public class ConnectionData {
}
public static void updateBlock(UserConnection user, Position pos, List<BlockChangeRecord> records) {
int blockState = Via.getManager().getProviders().get(BlockConnectionProvider.class).getBlockData(user, pos.getX(), pos.getY(), pos.getZ());
int blockState = blockConnectionProvider.getBlockData(user, pos.getX(), pos.getY(), pos.getZ());
ConnectionHandler handler = getConnectionHandler(blockState);
if (handler == null) return;
@ -132,26 +133,22 @@ public class ConnectionData {
records.add(new BlockChangeRecord((short) (((pos.getX() & 0xF) << 4) | (pos.getZ() & 0xF)), pos.getY(), newBlockState));
}
public static BlockConnectionProvider getProvider() {
return Via.getManager().getProviders().get(BlockConnectionProvider.class);
}
public static void updateBlockStorage(UserConnection userConnection, int x, int y, int z, int blockState) {
if (!needStoreBlocks()) return;
if (ConnectionData.isWelcome(blockState)) {
ConnectionData.getProvider().storeBlock(userConnection, x, y, z, blockState);
blockConnectionProvider.storeBlock(userConnection, x, y, z, blockState);
} else {
ConnectionData.getProvider().removeBlock(userConnection, x, y, z);
blockConnectionProvider.removeBlock(userConnection, x, y, z);
}
}
public static void clearBlockStorage(UserConnection connection) {
if (!needStoreBlocks()) return;
getProvider().clearStorage(connection);
blockConnectionProvider.clearStorage(connection);
}
public static boolean needStoreBlocks() {
return getProvider().storesBlocks();
return blockConnectionProvider.storesBlocks();
}
public static void connectBlocks(UserConnection user, Chunk chunk) {
@ -260,7 +257,8 @@ public class ConnectionData {
}
if (Via.getConfig().getBlockConnectionMethod().equalsIgnoreCase("packet")) {
Via.getManager().getProviders().register(BlockConnectionProvider.class, new PacketBlockConnectionProvider());
blockConnectionProvider = new PacketBlockConnectionProvider();
Via.getManager().getProviders().register(BlockConnectionProvider.class, blockConnectionProvider);
}
}
@ -289,6 +287,7 @@ public class ConnectionData {
return idToKey.get(id);
}
@FunctionalInterface
interface ConnectorInitAction {
void check(WrappedBlockData blockData);

View File

@ -6,14 +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 user, Position position, int blockState);
public int getBlockData(UserConnection user, Position position) {
return Via.getManager().getProviders().get(BlockConnectionProvider.class).getBlockData(user, position.getX(), position.getY(), position.getZ());
}
public boolean canConnect(int id) {
ConnectionHandler handler = ConnectionData.connectionHandlerMap.get(id);
return handler != null && handler == this;
}
}

View File

@ -1,21 +1,14 @@
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.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;
public class DoorConnectionHandler extends ConnectionHandler {
private static Map<Integer, DoorData> doorDataMap = new HashMap<>();
private static Map<Short, Integer> connectedStates = new HashMap<>();
private static final Map<Integer, DoorData> doorDataMap = new HashMap<>();
private static final Map<Short, Integer> connectedStates = new HashMap<>();
static ConnectionData.ConnectorInitAction init() {
final List<String> baseDoors = new LinkedList<>();
@ -28,29 +21,26 @@ public class DoorConnectionHandler extends ConnectionHandler {
baseDoors.add("minecraft:iron_door");
final DoorConnectionHandler connectionHandler = new DoorConnectionHandler();
return new ConnectionData.ConnectorInitAction() {
@Override
public void check(WrappedBlockData blockData) {
int type = baseDoors.indexOf(blockData.getMinecraftKey());
if (type == -1) return;
return blockData -> {
int type = baseDoors.indexOf(blockData.getMinecraftKey());
if (type == -1) return;
int id = blockData.getSavedBlockStateId();
int id = blockData.getSavedBlockStateId();
DoorData doorData = new DoorData(
blockData.getValue("half").equals("lower"),
blockData.getValue("hinge").equals("right"),
blockData.getValue("powered").equals("true"),
blockData.getValue("open").equals("true"),
BlockFace.valueOf(blockData.getValue("facing").toUpperCase(Locale.ROOT)),
type
);
DoorData doorData = new DoorData(
blockData.getValue("half").equals("lower"),
blockData.getValue("hinge").equals("right"),
blockData.getValue("powered").equals("true"),
blockData.getValue("open").equals("true"),
BlockFace.valueOf(blockData.getValue("facing").toUpperCase(Locale.ROOT)),
type
);
doorDataMap.put(id, doorData);
doorDataMap.put(id, doorData);
connectedStates.put(getStates(doorData), id);
connectedStates.put(getStates(doorData), id);
ConnectionData.connectionHandlerMap.put(id, connectionHandler);
}
ConnectionData.connectionHandlerMap.put(id, connectionHandler);
};
}
@ -87,16 +77,47 @@ public class DoorConnectionHandler extends ConnectionHandler {
if (doorData.isRightHinge()) s |= 8;
s |= lowerHalf.getFacing().ordinal() << 4;
}
Integer newBlockState = connectedStates.get(s);
return newBlockState == null ? blockState : newBlockState;
}
@AllArgsConstructor
@Getter
@ToString
private static class DoorData {
private static final class DoorData {
private final boolean lower, rightHinge, powered, open;
private final BlockFace facing;
private int type;
private final int type;
private DoorData(boolean lower, boolean rightHinge, boolean powered, boolean open, BlockFace facing, int type) {
this.lower = lower;
this.rightHinge = rightHinge;
this.powered = powered;
this.open = open;
this.facing = facing;
this.type = type;
}
public boolean isLower() {
return lower;
}
public boolean isRightHinge() {
return rightHinge;
}
public boolean isPowered() {
return powered;
}
public boolean isOpen() {
return open;
}
public BlockFace getFacing() {
return facing;
}
public int getType() {
return type;
}
}
}

View File

@ -12,7 +12,7 @@ import java.util.Set;
public class FlowerConnectionHandler extends ConnectionHandler {
private static Map<Integer, Integer> flowers = new HashMap<>();
private static final Map<Integer, Integer> flowers = new HashMap<>();
static ConnectionData.ConnectorInitAction init() {
final Set<String> baseFlower = new HashSet<>();
@ -24,15 +24,12 @@ public class FlowerConnectionHandler extends ConnectionHandler {
baseFlower.add("minecraft:lilac");
final FlowerConnectionHandler handler = new FlowerConnectionHandler();
return new ConnectionData.ConnectorInitAction() {
@Override
public void check(WrappedBlockData blockData) {
if (baseFlower.contains(blockData.getMinecraftKey())) {
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
if (blockData.getValue("half").equals("lower")) {
blockData.set("half", "upper");
flowers.put(blockData.getSavedBlockStateId(), blockData.getBlockStateId());
}
return blockData -> {
if (baseFlower.contains(blockData.getMinecraftKey())) {
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
if (blockData.getValue("half").equals("lower")) {
blockData.set("half", "upper");
flowers.put(blockData.getSavedBlockStateId(), blockData.getBlockStateId());
}
}
};
@ -41,14 +38,15 @@ public class FlowerConnectionHandler extends ConnectionHandler {
@Override
public int connect(UserConnection user, Position position, int blockState) {
int blockBelowId = getBlockData(user, position.getRelative(BlockFace.BOTTOM));
if (flowers.containsKey(blockBelowId)) {
Integer connectBelow = flowers.get(blockBelowId);
if (connectBelow != null) {
int blockAboveId = getBlockData(user, position.getRelative(BlockFace.TOP));
if (Via.getConfig().isStemWhenBlockAbove()) {
if (blockAboveId == 0) {
return flowers.get(blockBelowId);
return connectBelow;
}
} else if (!flowers.containsKey(blockAboveId)) {
return flowers.get(blockBelowId);
return connectBelow;
}
}
return blockState;

View File

@ -39,8 +39,10 @@ public class GlassConnectionHandler extends AbstractFenceConnectionHandler {
@Override
protected byte getStates(UserConnection user, Position position, int blockState) {
byte states = super.getStates(user, position, blockState);
return states == 0
&& user.get(ProtocolInfo.class).getServerProtocolVersion() <= 47
&& user.get(ProtocolInfo.class).getServerProtocolVersion() != -1 ? 0xF : states;
if (states != 0) return states;
ProtocolInfo protocolInfo = user.get(ProtocolInfo.class);
return protocolInfo.getServerProtocolVersion() <= 47
&& protocolInfo.getServerProtocolVersion() != -1 ? 0xF : states;
}
}

View File

@ -10,22 +10,19 @@ import java.util.Map;
import java.util.Set;
public class RedstoneConnectionHandler extends ConnectionHandler {
private static Set<Integer> redstone = new HashSet<>();
private static Map<Short, Integer> connectedBlockStates = new HashMap<>();
private static Map<Integer, Integer> powerMappings = new HashMap<>();
private static final Set<Integer> redstone = new HashSet<>();
private static final Map<Short, Integer> connectedBlockStates = new HashMap<>();
private static final Map<Integer, Integer> powerMappings = new HashMap<>();
static ConnectionData.ConnectorInitAction init() {
final RedstoneConnectionHandler connectionHandler = new RedstoneConnectionHandler();
final String redstoneKey = "minecraft:redstone_wire";
return new ConnectionData.ConnectorInitAction() {
@Override
public void check(WrappedBlockData blockData) {
if (!redstoneKey.equals(blockData.getMinecraftKey())) return;
redstone.add(blockData.getSavedBlockStateId());
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), connectionHandler);
connectedBlockStates.put(getStates(blockData), blockData.getSavedBlockStateId());
powerMappings.put(blockData.getSavedBlockStateId(), Integer.valueOf(blockData.getValue("power")));
}
return blockData -> {
if (!redstoneKey.equals(blockData.getMinecraftKey())) return;
redstone.add(blockData.getSavedBlockStateId());
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), connectionHandler);
connectedBlockStates.put(getStates(blockData), blockData.getSavedBlockStateId());
powerMappings.put(blockData.getSavedBlockStateId(), Integer.valueOf(blockData.getValue("power")));
};
}

View File

@ -11,8 +11,8 @@ import java.util.Map;
import java.util.Set;
public class SnowyGrassConnectionHandler extends ConnectionHandler {
private static Map<Pair<Integer, Boolean>, Integer> grassBlocks = new HashMap<>();
private static Set<Integer> snows = new HashSet<>();
private static final Map<Pair<Integer, Boolean>, Integer> grassBlocks = new HashMap<>();
private static final Set<Integer> snows = new HashSet<>();
static ConnectionData.ConnectorInitAction init() {
final Set<String> snowyGrassBlocks = new HashSet<>();
@ -21,20 +21,17 @@ public class SnowyGrassConnectionHandler extends ConnectionHandler {
snowyGrassBlocks.add("minecraft:mycelium");
final SnowyGrassConnectionHandler handler = new SnowyGrassConnectionHandler();
return new ConnectionData.ConnectorInitAction() {
@Override
public void check(WrappedBlockData blockData) {
if (snowyGrassBlocks.contains(blockData.getMinecraftKey())) {
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
blockData.set("snowy", "true");
grassBlocks.put(new Pair<>(blockData.getSavedBlockStateId(), true), blockData.getBlockStateId());
blockData.set("snowy", "false");
grassBlocks.put(new Pair<>(blockData.getSavedBlockStateId(), false), blockData.getBlockStateId());
}
if (blockData.getMinecraftKey().equals("minecraft:snow") || blockData.getMinecraftKey().equals("minecraft:snow_block")) {
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
snows.add(blockData.getSavedBlockStateId());
}
return blockData -> {
if (snowyGrassBlocks.contains(blockData.getMinecraftKey())) {
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
blockData.set("snowy", "true");
grassBlocks.put(new Pair<>(blockData.getSavedBlockStateId(), true), blockData.getBlockStateId());
blockData.set("snowy", "false");
grassBlocks.put(new Pair<>(blockData.getSavedBlockStateId(), false), blockData.getBlockStateId());
}
if (blockData.getMinecraftKey().equals("minecraft:snow") || blockData.getMinecraftKey().equals("minecraft:snow_block")) {
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), handler);
snows.add(blockData.getSavedBlockStateId());
}
};
}

View File

@ -1,21 +1,14 @@
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.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;
public class StairConnectionHandler extends ConnectionHandler {
private static Map<Integer, StairData> stairDataMap = new HashMap<>();
private static Map<Short, Integer> connectedBlocks = new HashMap<>();
private static final Map<Integer, StairData> stairDataMap = new HashMap<>();
private static final Map<Short, Integer> connectedBlocks = new HashMap<>();
static ConnectionData.ConnectorInitAction init() {
final List<String> baseStairs = new LinkedList<>();
@ -38,35 +31,43 @@ public class StairConnectionHandler extends ConnectionHandler {
baseStairs.add("minecraft:dark_prismarine_stairs");
final StairConnectionHandler connectionHandler = new StairConnectionHandler();
return new ConnectionData.ConnectorInitAction() {
@Override
public void check(WrappedBlockData blockData) {
int type = baseStairs.indexOf(blockData.getMinecraftKey());
if (type == -1) return;
return blockData -> {
int type = baseStairs.indexOf(blockData.getMinecraftKey());
if (type == -1) return;
if (blockData.getValue("waterlogged").equals("true")) return;
if (blockData.getValue("waterlogged").equals("true")) return;
byte shape;
switch (blockData.getValue("shape")) {
case "straight": shape = 0; break;
case "inner_left": shape = 1; break;
case "inner_right": shape = 2; break;
case "outer_left": shape = 3; break;
case "outer_right": shape = 4; break;
default: return;
}
StairData stairData = new StairData(
blockData.getValue("half").equals("bottom"),
shape, (byte) type,
BlockFace.valueOf(blockData.getValue("facing").toUpperCase(Locale.ROOT))
);
stairDataMap.put(blockData.getSavedBlockStateId(), stairData);
connectedBlocks.put(getStates(stairData), blockData.getSavedBlockStateId());
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), connectionHandler);
byte shape;
switch (blockData.getValue("shape")) {
case "straight":
shape = 0;
break;
case "inner_left":
shape = 1;
break;
case "inner_right":
shape = 2;
break;
case "outer_left":
shape = 3;
break;
case "outer_right":
shape = 4;
break;
default:
return;
}
StairData stairData = new StairData(
blockData.getValue("half").equals("bottom"),
shape, (byte) type,
BlockFace.valueOf(blockData.getValue("facing").toUpperCase(Locale.ROOT))
);
stairDataMap.put(blockData.getSavedBlockStateId(), stairData);
connectedBlocks.put(getStates(stairData), blockData.getSavedBlockStateId());
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), connectionHandler);
};
}
@ -136,12 +137,32 @@ public class StairConnectionHandler extends ConnectionHandler {
}
}
@AllArgsConstructor
@Getter
@ToString
private static class StairData {
private static final class StairData {
private final boolean bottom;
private final byte shape, type;
private final BlockFace facing;
private StairData(boolean bottom, byte shape, byte type, BlockFace facing) {
this.bottom = bottom;
this.shape = shape;
this.type = type;
this.facing = facing;
}
public boolean isBottom() {
return bottom;
}
public byte getShape() {
return shape;
}
public byte getType() {
return type;
}
public BlockFace getFacing() {
return facing;
}
}
}

View File

@ -1,8 +1,5 @@
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;
@ -12,29 +9,26 @@ import java.util.Locale;
import java.util.Map;
public class TripwireConnectionHandler extends ConnectionHandler {
private static Map<Integer, TripwireData> tripwireDataMap = new HashMap<>();
private static Map<Byte, Integer> connectedBlocks = new HashMap<>();
private static Map<Integer, BlockFace> tripwireHooks = new HashMap<>();
private static final Map<Integer, TripwireData> tripwireDataMap = new HashMap<>();
private static final Map<Byte, Integer> connectedBlocks = new HashMap<>();
private static final Map<Integer, BlockFace> tripwireHooks = new HashMap<>();
static ConnectionData.ConnectorInitAction init() {
final TripwireConnectionHandler connectionHandler = new TripwireConnectionHandler();
return new ConnectionData.ConnectorInitAction() {
@Override
public void check(WrappedBlockData blockData) {
if (blockData.getMinecraftKey().equals("minecraft:tripwire_hook")) {
tripwireHooks.put(blockData.getSavedBlockStateId(), BlockFace.valueOf(blockData.getValue("facing").toUpperCase(Locale.ROOT)));
} else if (blockData.getMinecraftKey().equals("minecraft:tripwire")) {
TripwireData tripwireData = new TripwireData(
blockData.getValue("attached").equals("true"),
blockData.getValue("disarmed").equals("true"),
blockData.getValue("powered").equals("true")
);
return blockData -> {
if (blockData.getMinecraftKey().equals("minecraft:tripwire_hook")) {
tripwireHooks.put(blockData.getSavedBlockStateId(), BlockFace.valueOf(blockData.getValue("facing").toUpperCase(Locale.ROOT)));
} else if (blockData.getMinecraftKey().equals("minecraft:tripwire")) {
TripwireData tripwireData = new TripwireData(
blockData.getValue("attached").equals("true"),
blockData.getValue("disarmed").equals("true"),
blockData.getValue("powered").equals("true")
);
tripwireDataMap.put(blockData.getSavedBlockStateId(), tripwireData);
connectedBlocks.put(getStates(blockData), blockData.getSavedBlockStateId());
tripwireDataMap.put(blockData.getSavedBlockStateId(), tripwireData);
connectedBlocks.put(getStates(blockData), blockData.getSavedBlockStateId());
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), connectionHandler);
}
ConnectionData.connectionHandlerMap.put(blockData.getSavedBlockStateId(), connectionHandler);
}
};
}
@ -82,10 +76,25 @@ public class TripwireConnectionHandler extends ConnectionHandler {
return newBlockState == null ? blockState : newBlockState;
}
@AllArgsConstructor
@Getter
@ToString
private static class TripwireData {
private static final class TripwireData {
private final boolean attached, disarmed, powered;
private TripwireData(final boolean attached, final boolean disarmed, final boolean powered) {
this.attached = attached;
this.disarmed = disarmed;
this.powered = powered;
}
public boolean isAttached() {
return attached;
}
public boolean isDisarmed() {
return disarmed;
}
public boolean isPowered() {
return powered;
}
}
}

View File

@ -1,17 +1,14 @@
package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections;
import lombok.Getter;
import us.myles.ViaVersion.api.Via;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
public class WrappedBlockData {
@Getter
private String minecraftKey;
@Getter
private int savedBlockStateId;
private LinkedHashMap<String, String> blockData = new LinkedHashMap<>();
private final String minecraftKey;
private final int savedBlockStateId;
private final LinkedHashMap<String, String> blockData = new LinkedHashMap<>();
public static WrappedBlockData fromString(String s) {
String[] array = s.split("\\[");
@ -43,12 +40,21 @@ public class WrappedBlockData {
this.savedBlockStateId = savedBlockStateId;
}
@Override
public String toString() {
StringBuilder sb = new StringBuilder(minecraftKey + "[");
for (Entry<String, String> entry : blockData.entrySet()) {
sb.append(entry.getKey()).append('=').append(entry.getValue()).append(',');
}
return sb.substring(0, sb.length()-1) + "]";
return sb.substring(0, sb.length() - 1) + "]";
}
public String getMinecraftKey() {
return minecraftKey;
}
public int getSavedBlockStateId() {
return savedBlockStateId;
}
public int getBlockStateId() {
@ -56,7 +62,8 @@ public class WrappedBlockData {
}
public WrappedBlockData set(String data, Object value) {
if (!hasData(data)) throw new UnsupportedOperationException("No blockdata found for " + data + " at " + minecraftKey);
if (!hasData(data))
throw new UnsupportedOperationException("No blockdata found for " + data + " at " + minecraftKey);
blockData.put(data, value.toString());
return this;
}

View File

@ -1,11 +1,11 @@
package us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.providers;
import us.myles.ViaVersion.api.data.UserConnection;
import us.myles.ViaVersion.api.minecraft.Position;
import us.myles.ViaVersion.api.platform.providers.Provider;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData;
public class BlockConnectionProvider implements Provider {
public int getBlockData(UserConnection connection, int x, int y, int z) {
int oldId = getWorldBlockData(connection, x, y, z);
return MappingData.blockMappings.getNewId(oldId);

View File

@ -12,13 +12,13 @@ import us.myles.ViaVersion.api.protocol.Protocol;
import us.myles.ViaVersion.api.remapper.PacketHandler;
import us.myles.ViaVersion.api.remapper.PacketRemapper;
import us.myles.ViaVersion.api.type.Type;
import us.myles.ViaVersion.api.type.types.Particle;
import us.myles.ViaVersion.packets.State;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.Protocol1_13To1_12_2;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionData;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.blockconnections.ConnectionHandler;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.MappingData;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.NamedSoundRewriter;
import us.myles.ViaVersion.api.type.types.Particle;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.data.ParticleRewriter;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.BlockEntityProvider;
import us.myles.ViaVersion.protocols.protocol1_13to1_12_2.providers.PaintingProvider;
@ -264,7 +264,7 @@ public class WorldPackets {
public void handle(PacketWrapper wrapper) throws Exception {
int x = wrapper.passthrough(Type.INT);
int z = wrapper.passthrough(Type.INT);
ConnectionData.getProvider().unloadChunk(wrapper.user(), x, z);
ConnectionData.blockConnectionProvider.unloadChunk(wrapper.user(), x, z);
}
});
}
@ -355,7 +355,7 @@ public class WorldPackets {
for (int x = 0; x < 16; x++) {
int block = section.getFlatBlock(x, y, z);
if (ConnectionData.isWelcome(block)) {
ConnectionData.getProvider().storeBlock(wrapper.user(), x + (chunk.getX() << 4),
ConnectionData.blockConnectionProvider.storeBlock(wrapper.user(), x + (chunk.getX() << 4),
y + (i << 4),
z + (chunk.getZ() << 4),
block);