Nicer getters in Pair and Triple

This commit is contained in:
Nassim Jahnke 2021-09-14 11:13:39 +02:00
parent 68e4146f3b
commit 32a84f24ef
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
10 changed files with 68 additions and 39 deletions

View File

@ -64,7 +64,7 @@ public interface ViaAPI<T> {
* @return API version incremented with meaningful API changes
*/
default int apiVersion() {
return 6;
return 7;
}
/**

View File

@ -35,14 +35,28 @@ public class Pair<X, Y> {
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;
}

View File

@ -37,14 +37,29 @@ public class Triple<A, B, C> {
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;
}

View File

@ -179,9 +179,9 @@ public class BukkitViaInjector implements ViaInjector {
for (Pair<Field, Object> 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<Field, Object> 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);

View File

@ -340,8 +340,8 @@ public class ProtocolManagerImpl implements ProtocolManager {
@Override
public Protocol getBaseProtocol(int serverVersion) {
for (Pair<Range<Integer>, 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);

View File

@ -72,9 +72,9 @@ public class PacketWrapperImpl implements PacketWrapper {
public <T> T get(Type<T> type, int index) throws Exception {
int currentIndex = 0;
for (Pair<Type, Object> 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<Type, Object> 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<Type, Object> 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 <T> void set(Type<T> type, int index, T value) throws Exception {
int currentIndex = 0;
for (Pair<Type, Object> 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<Type, Object> 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<Type, Object> 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++;
}

View File

@ -299,8 +299,8 @@ public class Protocol1_11To1_10 extends AbstractProtocol<ClientboundPackets1_9_3
Via.getPlatform().getLogger().warning("Received unknown 1.11 -> 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);

View File

@ -63,8 +63,8 @@ public class BlockConnectionStorage implements StorableObject {
long pair = getChunkSectionIndex(x, y, z);
Pair<byte[], NibbleArray> 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<byte[], NibbleArray> 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<byte[], NibbleArray> 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;

View File

@ -325,12 +325,12 @@ public class EntityPackets {
wrapper.write(Type.INT, properties.size());
for (Map.Entry<String, Pair<Double, List<Triple<UUID, Double, Byte>>>> 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<UUID, Double, Byte> 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<UUID, Double, Byte> modifier : entry.getValue().value()) {
wrapper.write(Type.UUID, modifier.first());
wrapper.write(Type.DOUBLE, modifier.second()); // Amount
wrapper.write(Type.BYTE, modifier.third()); // Operation
}
}
}

View File

@ -162,9 +162,9 @@ public class SpongeViaInjector implements ViaInjector {
for (Pair<Field, Object> 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<Field, Object> 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);