3
0
Mirror von https://github.com/PaperMC/Velocity.git synchronisiert 2024-12-24 15:20:35 +01:00

Use Guava immutable integer arrays.

Dieser Commit ist enthalten in:
Andrew Steinborn 2018-08-07 07:18:21 -04:00
Ursprung abbdf70d5e
Commit a2b4291b36
2 geänderte Dateien mit 16 neuen und 17 gelöschten Zeilen

Datei anzeigen

@ -1,6 +1,6 @@
package com.velocitypowered.proxy.protocol; package com.velocitypowered.proxy.protocol;
import java.util.Arrays; import com.google.common.primitives.ImmutableIntArray;
public enum ProtocolConstants { ; public enum ProtocolConstants { ;
public static final int LEGACY = -1; public static final int LEGACY = -1;
@ -23,7 +23,7 @@ public enum ProtocolConstants { ;
public static final String SUPPORTED_GENERIC_VERSION_STRING = "1.8-1.13"; public static final String SUPPORTED_GENERIC_VERSION_STRING = "1.8-1.13";
public static final int[] SUPPORTED_VERSIONS = new int[] { public static final ImmutableIntArray SUPPORTED_VERSIONS = ImmutableIntArray.of(
MINECRAFT_1_8, MINECRAFT_1_8,
MINECRAFT_1_9, MINECRAFT_1_9,
MINECRAFT_1_9_1, MINECRAFT_1_9_1,
@ -36,10 +36,10 @@ public enum ProtocolConstants { ;
MINECRAFT_1_12_1, MINECRAFT_1_12_1,
MINECRAFT_1_12_2, MINECRAFT_1_12_2,
MINECRAFT_1_13 MINECRAFT_1_13
}; );
public static boolean isSupported(int version) { public static boolean isSupported(int version) {
return Arrays.binarySearch(SUPPORTED_VERSIONS, version) >= 0; return SUPPORTED_VERSIONS.contains(version);
} }
public enum Direction { public enum Direction {

Datei anzeigen

@ -1,5 +1,6 @@
package com.velocitypowered.proxy.protocol; package com.velocitypowered.proxy.protocol;
import com.google.common.primitives.ImmutableIntArray;
import com.velocitypowered.proxy.protocol.packet.*; import com.velocitypowered.proxy.protocol.packet.*;
import io.netty.util.collection.IntObjectHashMap; import io.netty.util.collection.IntObjectHashMap;
import io.netty.util.collection.IntObjectMap; import io.netty.util.collection.IntObjectMap;
@ -144,13 +145,13 @@ public enum StateRegistry {
public final PacketRegistry SERVERBOUND = new PacketRegistry(ProtocolConstants.Direction.SERVERBOUND, this); public final PacketRegistry SERVERBOUND = new PacketRegistry(ProtocolConstants.Direction.SERVERBOUND, this);
public static class PacketRegistry { public static class PacketRegistry {
private static final IntObjectMap<int[]> LINKED_PROTOCOL_VERSIONS = new IntObjectHashMap<>(); private static final IntObjectMap<ImmutableIntArray> LINKED_PROTOCOL_VERSIONS = new IntObjectHashMap<>();
static { static {
LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9, new int[] { MINECRAFT_1_9_1, MINECRAFT_1_9_2, MINECRAFT_1_9_4 }); LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9, ImmutableIntArray.of(MINECRAFT_1_9_1, MINECRAFT_1_9_2, MINECRAFT_1_9_4));
LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9_4, new int[] { MINECRAFT_1_10, MINECRAFT_1_11, MINECRAFT_1_11_1 }); LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_9_4, ImmutableIntArray.of(MINECRAFT_1_10, MINECRAFT_1_11, MINECRAFT_1_11_1));
LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_12, new int[] { MINECRAFT_1_12_1 }); LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_12, ImmutableIntArray.of(MINECRAFT_1_12_1));
LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_12_1, new int[] { MINECRAFT_1_12_2 }); LINKED_PROTOCOL_VERSIONS.put(MINECRAFT_1_12_1, ImmutableIntArray.of(MINECRAFT_1_12_2));
} }
private final ProtocolConstants.Direction direction; private final ProtocolConstants.Direction direction;
@ -160,10 +161,7 @@ public enum StateRegistry {
public PacketRegistry(Direction direction, StateRegistry state) { public PacketRegistry(Direction direction, StateRegistry state) {
this.direction = direction; this.direction = direction;
this.state = state; this.state = state;
for (int version : ProtocolConstants.SUPPORTED_VERSIONS) { ProtocolConstants.SUPPORTED_VERSIONS.forEach(version -> versions.put(version, new ProtocolVersion(version)));
versions.put(version, new ProtocolVersion(version));
}
versions.put(MINIMUM_GENERIC_VERSION, new ProtocolVersion(MINIMUM_GENERIC_VERSION));
} }
public ProtocolVersion getVersion(final int version) { public ProtocolVersion getVersion(final int version) {
@ -191,14 +189,15 @@ public enum StateRegistry {
version.packetIdToSupplier.put(mapping.id, packetSupplier); version.packetIdToSupplier.put(mapping.id, packetSupplier);
version.packetClassToId.put(clazz, mapping.id); version.packetClassToId.put(clazz, mapping.id);
int[] linked = LINKED_PROTOCOL_VERSIONS.get(mapping.protocolVersion); ImmutableIntArray linked = LINKED_PROTOCOL_VERSIONS.get(mapping.protocolVersion);
if (linked != null) { if (linked != null) {
links: for (int i : linked) { links: for (int i = 0; i < linked.length(); i++) {
int linkedVersion = linked.get(i);
// Make sure that later mappings override this one. // Make sure that later mappings override this one.
for (PacketMapping m : mappings) { for (PacketMapping m : mappings) {
if (i == m.protocolVersion) continue links; if (linkedVersion == m.protocolVersion) continue links;
} }
register(clazz, packetSupplier, map(mapping.id, i)); register(clazz, packetSupplier, map(mapping.id, linkedVersion));
} }
} }
} }