From 32a84f24efdc9163c7cf182a90fd5e3c83aab9dd Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Tue, 14 Sep 2021 11:13:39 +0200 Subject: [PATCH] Nicer getters in Pair and Triple --- .../com/viaversion/viaversion/api/ViaAPI.java | 2 +- .../com/viaversion/viaversion/util/Pair.java | 14 +++++++++++++ .../viaversion/viaversion/util/Triple.java | 15 ++++++++++++++ .../bukkit/platform/BukkitViaInjector.java | 10 +++++----- .../protocol/ProtocolManagerImpl.java | 4 ++-- .../protocol/packet/PacketWrapperImpl.java | 20 +++++++++---------- .../Protocol1_11To1_10.java | 4 ++-- .../storage/BlockConnectionStorage.java | 16 +++++++-------- .../packets/EntityPackets.java | 12 +++++------ .../sponge/platform/SpongeViaInjector.java | 10 +++++----- 10 files changed, 68 insertions(+), 39 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java b/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java index abefb011b..22b059228 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java +++ b/api/src/main/java/com/viaversion/viaversion/api/ViaAPI.java @@ -64,7 +64,7 @@ public interface ViaAPI { * @return API version incremented with meaningful API changes */ default int apiVersion() { - return 6; + return 7; } /** diff --git a/api/src/main/java/com/viaversion/viaversion/util/Pair.java b/api/src/main/java/com/viaversion/viaversion/util/Pair.java index 57f0134bb..f90008bbe 100644 --- a/api/src/main/java/com/viaversion/viaversion/util/Pair.java +++ b/api/src/main/java/com/viaversion/viaversion/util/Pair.java @@ -35,14 +35,28 @@ public class Pair { this.value = value; } + public @Nullable X key() { + return key; + } + + public @Nullable Y value() { + return value; + } + + @Deprecated/*(forRemoval = true)*/ public @Nullable X getKey() { return key; } + @Deprecated/*(forRemoval = true)*/ public @Nullable Y getValue() { return value; } + /** + * @deprecated don't count on this continuing to be mutable + */ + @Deprecated/*(forRemoval = true)*/ public void setValue(@Nullable Y value) { this.value = value; } diff --git a/api/src/main/java/com/viaversion/viaversion/util/Triple.java b/api/src/main/java/com/viaversion/viaversion/util/Triple.java index eac115a21..8ca48f2d9 100644 --- a/api/src/main/java/com/viaversion/viaversion/util/Triple.java +++ b/api/src/main/java/com/viaversion/viaversion/util/Triple.java @@ -37,14 +37,29 @@ public class Triple { this.third = third; } + public @Nullable A first() { + return first; + } + + public @Nullable B second() { + return second; + } + + public @Nullable C third() { + return third; + } + + @Deprecated/*(forRemoval = true)*/ public @Nullable A getFirst() { return first; } + @Deprecated/*(forRemoval = true)*/ public @Nullable B getSecond() { return second; } + @Deprecated/*(forRemoval = true)*/ public @Nullable C getThird() { return third; } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java index e43439fc6..4f5fc6bd9 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/BukkitViaInjector.java @@ -179,9 +179,9 @@ public class BukkitViaInjector implements ViaInjector { for (Pair pair : injectedLists) { try { - Object o = pair.getKey().get(pair.getValue()); + Object o = pair.key().get(pair.value()); if (o instanceof ListWrapper) { - pair.getKey().set(pair.getValue(), ((ListWrapper) o).getOriginalList()); + pair.key().set(pair.value(), ((ListWrapper) o).getOriginalList()); } } catch (IllegalAccessException e) { Via.getPlatform().getLogger().severe("Failed to remove injection, reload won't work with connections, please reboot!"); @@ -354,12 +354,12 @@ public class BukkitViaInjector implements ViaInjector { JsonObject currentLists = new JsonObject(); try { for (Pair pair : injectedLists) { - Object list = pair.getKey().get(pair.getValue()); + Object list = pair.key().get(pair.value()); // Note down the current value (could be overridden by another plugin) - currentLists.addProperty(pair.getKey().getName(), list.getClass().getName()); + currentLists.addProperty(pair.key().getName(), list.getClass().getName()); // Also if it's not overridden we can display what's inside our list (possibly another plugin) if (list instanceof ListWrapper) { - wrappedLists.addProperty(pair.getKey().getName(), ((ListWrapper) list).getOriginalList().getClass().getName()); + wrappedLists.addProperty(pair.key().getName(), ((ListWrapper) list).getOriginalList().getClass().getName()); } } data.add("wrappedLists", wrappedLists); diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java index d50ed230a..0ef8a8c54 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/ProtocolManagerImpl.java @@ -340,8 +340,8 @@ public class ProtocolManagerImpl implements ProtocolManager { @Override public Protocol getBaseProtocol(int serverVersion) { for (Pair, Protocol> rangeProtocol : Lists.reverse(baseProtocols)) { - if (rangeProtocol.getKey().contains(serverVersion)) { - return rangeProtocol.getValue(); + if (rangeProtocol.key().contains(serverVersion)) { + return rangeProtocol.value(); } } throw new IllegalStateException("No Base Protocol for " + serverVersion); diff --git a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java index c8ce03dd0..f0b89face 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java +++ b/common/src/main/java/com/viaversion/viaversion/protocol/packet/PacketWrapperImpl.java @@ -72,9 +72,9 @@ public class PacketWrapperImpl implements PacketWrapper { public T get(Type type, int index) throws Exception { int currentIndex = 0; for (Pair packetValue : packetValues) { - if (packetValue.getKey() != type) continue; + if (packetValue.key() != type) continue; if (currentIndex == index) { - return (T) packetValue.getValue(); + return (T) packetValue.value(); } currentIndex++; } @@ -87,7 +87,7 @@ public class PacketWrapperImpl implements PacketWrapper { public boolean is(Type type, int index) { int currentIndex = 0; for (Pair packetValue : packetValues) { - if (packetValue.getKey() != type) continue; + if (packetValue.key() != type) continue; if (currentIndex == index) { return true; } @@ -100,7 +100,7 @@ public class PacketWrapperImpl implements PacketWrapper { public boolean isReadable(Type type, int index) { int currentIndex = 0; for (Pair packetValue : readableObjects) { - if (packetValue.getKey().getBaseClass() != type.getBaseClass()) continue; + if (packetValue.key().getBaseClass() != type.getBaseClass()) continue; if (currentIndex == index) { return true; } @@ -114,7 +114,7 @@ public class PacketWrapperImpl implements PacketWrapper { public void set(Type type, int index, T value) throws Exception { int currentIndex = 0; for (Pair packetValue : packetValues) { - if (packetValue.getKey() != type) continue; + if (packetValue.key() != type) continue; if (currentIndex == index) { packetValue.setValue(attemptTransform(type, value)); return; @@ -139,15 +139,15 @@ public class PacketWrapperImpl implements PacketWrapper { } Pair read = readableObjects.poll(); - Type rtype = read.getKey(); + Type rtype = read.key(); if (rtype == type || (type.getBaseClass() == rtype.getBaseClass() && type.getOutputClass() == rtype.getOutputClass())) { - return (T) read.getValue(); + return (T) read.value(); } else if (rtype == Type.NOTHING) { return read(type); // retry } else { - Exception e = new IOException("Unable to read type " + type.getTypeName() + ", found " + read.getKey().getTypeName()); + Exception e = new IOException("Unable to read type " + type.getTypeName() + ", found " + read.key().getTypeName()); throw new InformativeException(e).set("Type", type.getTypeName()).set("Packet ID", getId()).set("Packet Type", packetType).set("Data", packetValues); } } @@ -207,9 +207,9 @@ public class PacketWrapperImpl implements PacketWrapper { int index = 0; for (Pair packetValue : packetValues) { try { - packetValue.getKey().write(buffer, packetValue.getValue()); + packetValue.key().write(buffer, packetValue.value()); } catch (Exception e) { - throw new InformativeException(e).set("Index", index).set("Type", packetValue.getKey().getTypeName()).set("Packet ID", getId()).set("Packet Type", packetType).set("Data", packetValues); + throw new InformativeException(e).set("Index", index).set("Type", packetValue.key().getTypeName()).set("Packet ID", getId()).set("Packet Type", packetType).set("Data", packetValues); } index++; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java index e7eeb6807..11cdfa402 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_11to1_10/Protocol1_11To1_10.java @@ -299,8 +299,8 @@ public class Protocol1_11To1_10 extends AbstractProtocol 1.10.2 potion data (" + data + ")"); data = 0; } else { - data = newData.getKey(); - isInstant = newData.getValue(); + data = newData.key(); + isInstant = newData.value(); } if (isInstant) { packetWrapper.set(Type.INT, 0, 2007); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java index 5b38a5216..df7b24dd6 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_13to1_12_2/storage/BlockConnectionStorage.java @@ -63,8 +63,8 @@ public class BlockConnectionStorage implements StorableObject { long pair = getChunkSectionIndex(x, y, z); Pair map = getChunkSection(pair, (blockState & 0xF) != 0); int blockIndex = encodeBlockPos(x, y, z); - map.getKey()[blockIndex] = (byte) (blockState >> 4); - NibbleArray nibbleArray = map.getValue(); + map.key()[blockIndex] = (byte) (blockState >> 4); + NibbleArray nibbleArray = map.value(); if (nibbleArray != null) { nibbleArray.set(blockIndex, blockState); } @@ -75,9 +75,9 @@ public class BlockConnectionStorage implements StorableObject { Pair map = blockStorage.get(pair); if (map == null) return 0; short blockPosition = encodeBlockPos(x, y, z); - NibbleArray nibbleArray = map.getValue(); + NibbleArray nibbleArray = map.value(); return WorldPackets.toNewId( - ((map.getKey()[blockPosition] & 0xFF) << 4) + ((map.key()[blockPosition] & 0xFF) << 4) | (nibbleArray == null ? 0 : nibbleArray.get(blockPosition)) ); } @@ -87,7 +87,7 @@ public class BlockConnectionStorage implements StorableObject { Pair map = blockStorage.get(pair); if (map == null) return; int blockIndex = encodeBlockPos(x, y, z); - NibbleArray nibbleArray = map.getValue(); + NibbleArray nibbleArray = map.value(); if (nibbleArray != null) { nibbleArray.set(blockIndex, 0); boolean allZero = true; @@ -99,8 +99,8 @@ public class BlockConnectionStorage implements StorableObject { } if (allZero) map.setValue(null); } - map.getKey()[blockIndex] = 0; - for (short entry : map.getKey()) { + map.key()[blockIndex] = 0; + for (short entry : map.key()) { if (entry != 0) return; } blockStorage.remove(pair); @@ -122,7 +122,7 @@ public class BlockConnectionStorage implements StorableObject { map = new Pair<>(new byte[4096], null); blockStorage.put(index, map); } - if (map.getValue() == null && requireNibbleArray) { + if (map.value() == null && requireNibbleArray) { map.setValue(new NibbleArray(4096)); } return map; diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java index 7189b47dd..05afdd0e1 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_9to1_8/packets/EntityPackets.java @@ -325,12 +325,12 @@ public class EntityPackets { wrapper.write(Type.INT, properties.size()); for (Map.Entry>>> entry : properties.entrySet()) { wrapper.write(Type.STRING, entry.getKey()); // Key - wrapper.write(Type.DOUBLE, entry.getValue().getKey()); // Value - wrapper.write(Type.VAR_INT, entry.getValue().getValue().size()); - for (Triple modifier : entry.getValue().getValue()) { - wrapper.write(Type.UUID, modifier.getFirst()); - wrapper.write(Type.DOUBLE, modifier.getSecond()); // Amount - wrapper.write(Type.BYTE, modifier.getThird()); // Operation + wrapper.write(Type.DOUBLE, entry.getValue().key()); // Value + wrapper.write(Type.VAR_INT, entry.getValue().value().size()); + for (Triple modifier : entry.getValue().value()) { + wrapper.write(Type.UUID, modifier.first()); + wrapper.write(Type.DOUBLE, modifier.second()); // Amount + wrapper.write(Type.BYTE, modifier.third()); // Operation } } } diff --git a/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaInjector.java b/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaInjector.java index e42dd0320..e28002f7d 100644 --- a/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaInjector.java +++ b/sponge/src/main/java/com/viaversion/viaversion/sponge/platform/SpongeViaInjector.java @@ -162,9 +162,9 @@ public class SpongeViaInjector implements ViaInjector { for (Pair pair : injectedLists) { try { - Object o = pair.getKey().get(pair.getValue()); + Object o = pair.key().get(pair.value()); if (o instanceof ListWrapper) { - pair.getKey().set(pair.getValue(), ((ListWrapper) o).getOriginalList()); + pair.key().set(pair.value(), ((ListWrapper) o).getOriginalList()); } } catch (IllegalAccessException e) { Via.getPlatform().getLogger().severe("Failed to remove injection, reload won't work with connections, please reboot!"); @@ -257,12 +257,12 @@ public class SpongeViaInjector implements ViaInjector { JsonObject currentLists = new JsonObject(); try { for (Pair pair : injectedLists) { - Object list = pair.getKey().get(pair.getValue()); + Object list = pair.key().get(pair.value()); // Note down the current value (could be overridden by another plugin) - currentLists.addProperty(pair.getKey().getName(), list.getClass().getName()); + currentLists.addProperty(pair.key().getName(), list.getClass().getName()); // Also if it's not overridden we can display what's inside our list (possibly another plugin) if (list instanceof ListWrapper) { - wrappedLists.addProperty(pair.getKey().getName(), ((ListWrapper) list).getOriginalList().getClass().getName()); + wrappedLists.addProperty(pair.key().getName(), ((ListWrapper) list).getOriginalList().getClass().getName()); } } data.add("wrappedLists", wrappedLists);