3
0
Mirror von https://github.com/ViaVersion/ViaVersion.git synchronisiert 2024-11-08 17:20:24 +01:00

Merge pull request #1798 from KennyTV/abstraction

Add method to get protocol instance by class
Dieser Commit ist enthalten in:
Myles 2020-06-07 09:18:57 +01:00 committet von GitHub
Commit d58959fcca
Es konnte kein GPG-Schlüssel zu dieser Signatur gefunden werden
GPG-Schlüssel-ID: 4AEE18F83AFDEB23

Datei anzeigen

@ -53,10 +53,11 @@ public class ProtocolRegistry {
public static int SERVER_PROTOCOL = -1; public static int SERVER_PROTOCOL = -1;
// Input Version -> Output Version & Protocol (Allows fast lookup) // Input Version -> Output Version & Protocol (Allows fast lookup)
private static final Map<Integer, Map<Integer, Protocol>> registryMap = new ConcurrentHashMap<>(); private static final Map<Integer, Map<Integer, Protocol>> registryMap = new ConcurrentHashMap<>();
private static final Map<Class<? extends Protocol>, Protocol> protocols = new HashMap<>();
private static final Map<Pair<Integer, Integer>, List<Pair<Integer, Protocol>>> pathCache = new ConcurrentHashMap<>(); private static final Map<Pair<Integer, Integer>, List<Pair<Integer, Protocol>>> pathCache = new ConcurrentHashMap<>();
private static final List<Protocol> registerList = new ArrayList<>();
private static final Set<Integer> supportedVersions = new HashSet<>(); private static final Set<Integer> supportedVersions = new HashSet<>();
private static final List<Pair<Range<Integer>, Protocol>> baseProtocols = Lists.newCopyOnWriteArrayList(); private static final List<Pair<Range<Integer>, Protocol>> baseProtocols = Lists.newCopyOnWriteArrayList();
private static final List<Protocol> registerList = new ArrayList<>();
private static final Object MAPPING_LOADER_LOCK = new Object(); private static final Object MAPPING_LOADER_LOCK = new Object();
private static Map<Class<? extends Protocol>, CompletableFuture<Void>> mappingLoaderFutures = new HashMap<>(); private static Map<Class<? extends Protocol>, CompletableFuture<Void>> mappingLoaderFutures = new HashMap<>();
@ -131,7 +132,9 @@ public class ProtocolRegistry {
pathCache.clear(); pathCache.clear();
} }
for (Integer version : supported) { protocols.put(protocol.getClass(), protocol);
for (int version : supported) {
Map<Integer, Protocol> protocolMap = registryMap.computeIfAbsent(version, k -> new HashMap<>()); Map<Integer, Protocol> protocolMap = registryMap.computeIfAbsent(version, k -> new HashMap<>());
protocolMap.put(output, protocol); protocolMap.put(output, protocol);
} }
@ -269,7 +272,7 @@ public class ProtocolRegistry {
} }
/** /**
* Calculate a path from a client version to server version * Calculate a path from a client version to server version.
* *
* @param clientVersion The input client version * @param clientVersion The input client version
* @param serverVersion The desired output server version * @param serverVersion The desired output server version
@ -292,6 +295,17 @@ public class ProtocolRegistry {
return outputPath; return outputPath;
} }
/**
* Returns a protocol instance by its class.
*
* @param protocolClass class of the protocol
* @return protocol if present
*/
@Nullable
public static Protocol getProtocol(Class<? extends Protocol> protocolClass) {
return protocols.get(protocolClass);
}
public static Protocol getBaseProtocol(int serverVersion) { public static Protocol getBaseProtocol(int serverVersion) {
for (Pair<Range<Integer>, Protocol> rangeProtocol : Lists.reverse(baseProtocols)) { for (Pair<Range<Integer>, Protocol> rangeProtocol : Lists.reverse(baseProtocols)) {
if (rangeProtocol.getKey().contains(serverVersion)) { if (rangeProtocol.getKey().contains(serverVersion)) {