From bcc994b0dd6fd21ddf13a835bd59591895529edf Mon Sep 17 00:00:00 2001 From: Mats Date: Thu, 19 May 2016 19:56:42 +0200 Subject: [PATCH] Add possibility to get the supported versions from the api and status response (#402) * Possibility to send the Supported versions inside the status response packet * Make lowercase --- .../us/myles/ViaVersion/ViaVersionPlugin.java | 16 ++++++--- .../myles/ViaVersion/api/ViaVersionAPI.java | 8 +++++ .../ViaVersion/api/ViaVersionConfig.java | 7 ++++ .../api/protocol/ProtocolRegistry.java | 33 +++++++++++++++---- .../protocols/base/BaseProtocol.java | 2 ++ src/main/resources/config.yml | 4 ++- 6 files changed, 59 insertions(+), 11 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index c03a1928d..3ebf77cfb 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -33,10 +33,7 @@ import us.myles.ViaVersion.util.ReflectionUtil; import java.io.File; import java.lang.reflect.Field; import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; import java.util.concurrent.Callable; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Future; @@ -96,6 +93,7 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe getLogger().warning("ViaVersion will not function on the current protocol."); } } + ProtocolRegistry.refreshVersions(); } }); @@ -363,6 +361,11 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe return compatSpigotBuild; } + @Override + public SortedSet getSupportedVersions() { + return ProtocolRegistry.getSupportedVersions(); + } + public boolean isCheckForUpdates() { return getConfig().getBoolean("checkforupdates", true); } @@ -447,6 +450,11 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe return getConfig().getBoolean("anti-xray-patch", true); } + @Override + public boolean isSendSupportedVersions() { + return getConfig().getBoolean("send-supported-versions", false); + } + public boolean isAutoTeam() { // Collision has to be enabled first return isPreventCollision() && getConfig().getBoolean("auto-team", true); diff --git a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java index 3a5c309a5..6e4d0c0a3 100644 --- a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java +++ b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java @@ -7,6 +7,7 @@ import us.myles.ViaVersion.api.boss.BossColor; import us.myles.ViaVersion.api.boss.BossStyle; import us.myles.ViaVersion.api.command.ViaVersionCommand; +import java.util.SortedSet; import java.util.UUID; public interface ViaVersionAPI { @@ -109,4 +110,11 @@ public interface ViaVersionAPI { * @return True if it is */ boolean isCompatSpigotBuild(); + + /** + * Get the supported protocol versions + * + * @return a list of protocol versions + */ + SortedSet getSupportedVersions(); } diff --git a/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java b/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java index 4ddd89c14..3c60ba098 100644 --- a/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java +++ b/src/main/java/us/myles/ViaVersion/api/ViaVersionConfig.java @@ -149,4 +149,11 @@ public interface ViaVersionConfig { * @return A boolean */ boolean isAntiXRay(); + + /** + * Send supported versions in the status response packet + * + * @return If true, enabled + */ + boolean isSendSupportedVersions(); } diff --git a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java index 0c05ea35f..bed042b21 100644 --- a/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java +++ b/src/main/java/us/myles/ViaVersion/api/protocol/ProtocolRegistry.java @@ -1,5 +1,7 @@ package us.myles.ViaVersion.api.protocol; +import com.google.common.collect.Lists; +import com.google.common.collect.Sets; import org.bukkit.Bukkit; import us.myles.ViaVersion.api.Pair; import us.myles.ViaVersion.protocols.base.BaseProtocol; @@ -10,14 +12,16 @@ import us.myles.ViaVersion.protocols.protocol1_9to1_8.Protocol1_9TO1_8; import us.myles.ViaVersion.protocols.protocol1_9to1_9_1.Protocol1_9TO1_9_1; import java.util.*; +import java.util.concurrent.ConcurrentHashMap; public class ProtocolRegistry { + public static final Protocol BASE_PROTOCOL = new BaseProtocol(); public static int SERVER_PROTOCOL = -1; // Input Version -> Output Version & Protocol (Allows fast lookup) - private static Map> registryMap = new HashMap<>(); - private static Map, List>> pathCache = new HashMap<>(); - private static List registerList = new ArrayList<>(); - public static final Protocol BASE_PROTOCOL = new BaseProtocol(); + private static Map> registryMap = new ConcurrentHashMap<>(); + private static Map, List>> pathCache = new ConcurrentHashMap<>(); + private static List registerList = Lists.newCopyOnWriteArrayList(); + private static Set supportedVersions = Sets.newConcurrentHashSet(); static { // Base Protocol @@ -29,7 +33,6 @@ public class ProtocolRegistry { // Only supported for 1.9.4 server to 1.9 (nothing else) registerProtocol(new Protocol1_9TO1_9_1(), Arrays.asList(ProtocolVersion.v1_9.getId()), ProtocolVersion.v1_9_2.getId()); registerProtocol(new Protocol1_9_1_2TO1_9_3(), Arrays.asList(ProtocolVersion.v1_9_1.getId(), ProtocolVersion.v1_9_2.getId()), ProtocolVersion.v1_9_3.getId()); - } /** @@ -54,11 +57,29 @@ public class ProtocolRegistry { if (Bukkit.getPluginManager().getPlugin("ViaVersion").isEnabled()) { protocol.registerListeners(); + refreshVersions(); } else { registerList.add(protocol); } } + public static void refreshVersions() { + supportedVersions.clear(); + + supportedVersions.add(ProtocolRegistry.SERVER_PROTOCOL); + for (ProtocolVersion versions : ProtocolVersion.getProtocols()) { + List> paths = getProtocolPath(versions.getId(), ProtocolRegistry.SERVER_PROTOCOL); + if (paths == null) continue; + supportedVersions.add(versions.getId()); + for (Pair path : paths) + supportedVersions.add(path.getKey()); + } + } + + public static SortedSet getSupportedVersions() { + return Collections.unmodifiableSortedSet(new TreeSet<>(supportedVersions)); + } + /** * Check if this plugin is useful to the server. * @@ -90,7 +111,7 @@ public class ProtocolRegistry { * @return The path which has been generated, null if failed. */ private static List> getProtocolPath(List> current, int clientVersion, int serverVersion) { - if(clientVersion == serverVersion) return null; // We're already there + if (clientVersion == serverVersion) return null; // We're already there if (current.size() > 50) return null; // Fail safe, protocol too complicated. // First check if there is any protocols for this diff --git a/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java b/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java index d3de8842e..a830ef8ec 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java +++ b/src/main/java/us/myles/ViaVersion/protocols/base/BaseProtocol.java @@ -42,6 +42,8 @@ public class BaseProtocol extends Protocol { try { JSONObject json = (JSONObject) new JSONParser().parse(originalStatus); JSONObject version = (JSONObject) json.get("version"); + if (ViaVersion.getConfig().isSendSupportedVersions()) + version.put("supportedVersions", ViaVersion.getInstance().getSupportedVersions()); if (ProtocolRegistry.SERVER_PROTOCOL == -1) { Long original = (Long) version.get("protocol"); ProtocolRegistry.SERVER_PROTOCOL = original.intValue(); diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index 867fa8e04..afad0532e 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -53,4 +53,6 @@ tracking-warning-pps: 120 tracking-max-warnings: 4 tracking-max-kick-msg: "You are sending too many packets, :(" # Patch the Anti xray to work on 1.9 (If your server is 1.8) This can cost more performance, so disable it if you don't use it. -anti-xray-patch: true \ No newline at end of file +anti-xray-patch: true +# Send the supported versions with the Status response packet +send-supported-versions: false \ No newline at end of file