From 3ddc43bf7b64b79c3a27f14e0435e902100422d0 Mon Sep 17 00:00:00 2001 From: Myles Date: Sat, 21 May 2016 22:07:20 +0100 Subject: [PATCH] Implement isSpigot() in the API and make it so xray does not activate if they do not use spigot. --- .../us/myles/ViaVersion/ViaVersionPlugin.java | 19 ++++++++++++++++++ .../myles/ViaVersion/api/ViaVersionAPI.java | 8 ++++++++ .../storage/ClientChunks.java | 20 +++++++++++-------- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java index 3ebf77cfb..4d2bb6559 100644 --- a/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java +++ b/src/main/java/us/myles/ViaVersion/ViaVersionPlugin.java @@ -47,11 +47,14 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe private ViaCommandHandler commandHandler; private boolean debug = false; private boolean compatSpigotBuild = false; + private boolean spigot = true; @Override public void onLoad() { ViaVersion.setInstance(this); + // Config magic generateConfig(); + // Handle reloads if (System.getProperty("ViaVersion") != null) { if (Bukkit.getPluginManager().getPlugin("ProtocolLib") != null) { getLogger().severe("ViaVersion is already loaded, we're going to kick all the players... because otherwise we'll crash because of ProtocolLib."); @@ -64,6 +67,12 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe } } + // Spigot detector + try { + Class.forName("org.spigotmc.SpigotConfig"); + } catch (ClassNotFoundException e) { + spigot = false; + } // Check if it's a spigot build with a protocol mod try { compatSpigotBuild = ReflectionUtil.nms("PacketEncoder").getDeclaredField("version") != null; @@ -105,6 +114,11 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe // Register Protocol Listeners ProtocolRegistry.registerListeners(); + + // Warn them if they have anti-xray on and they aren't using spigot + if(isAntiXRay() && !spigot){ + getLogger().info("You have anti-xray on in your config, since you're not using spigot it won't fix xray!"); + } } @Override @@ -366,6 +380,11 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaVersionAPI, ViaVe return ProtocolRegistry.getSupportedVersions(); } + @Override + public boolean isSpigot() { + return this.spigot; + } + public boolean isCheckForUpdates() { return getConfig().getBoolean("checkforupdates", true); } diff --git a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java index 6e4d0c0a3..3a3fc84ca 100644 --- a/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java +++ b/src/main/java/us/myles/ViaVersion/api/ViaVersionAPI.java @@ -117,4 +117,12 @@ public interface ViaVersionAPI { * @return a list of protocol versions */ SortedSet getSupportedVersions(); + + /** + * Gets if the server uses spigot + * + * Note: Will only work after ViaVersion load + * @return True if spigot + */ + boolean isSpigot(); } diff --git a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/ClientChunks.java b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/ClientChunks.java index 2adcc1ddb..f22051818 100644 --- a/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/ClientChunks.java +++ b/src/main/java/us/myles/ViaVersion/protocols/protocol1_9to1_8/storage/ClientChunks.java @@ -51,17 +51,21 @@ public class ClientChunks extends StoredObject { int[] zcoords = mapChunkBulkRef.getFieldValue("b", packet, int[].class); Object[] chunkMaps = mapChunkBulkRef.getFieldValue("c", packet, Object[].class); - if (ViaVersion.getConfig().isAntiXRay()) { //Spigot anti-xray patch - Object world = mapChunkBulkRef.getFieldValue("world", packet, Object.class); + if (ViaVersion.getConfig().isAntiXRay() && ViaVersion.getInstance().isSpigot()) { //Spigot anti-xray patch + try { + Object world = mapChunkBulkRef.getFieldValue("world", packet, Object.class); - for (int i = 0; i < xcoords.length; ++i) { - Object spigotConfig = ReflectionUtil.getPublic(world, "spigotConfig", Object.class); - Object antiXrayInstance = ReflectionUtil.getPublic(spigotConfig, "antiXrayInstance", Object.class); + for (int i = 0; i < xcoords.length; ++i) { + Object spigotConfig = ReflectionUtil.getPublic(world, "spigotConfig", Object.class); + Object antiXrayInstance = ReflectionUtil.getPublic(spigotConfig, "antiXrayInstance", Object.class); - Object b = ReflectionUtil.get(chunkMaps[i], "b", Object.class); - Object a = ReflectionUtil.get(chunkMaps[i], "a", Object.class); + Object b = ReflectionUtil.get(chunkMaps[i], "b", Object.class); + Object a = ReflectionUtil.get(chunkMaps[i], "a", Object.class); - obfuscateRef.invoke(antiXrayInstance, xcoords[i], zcoords[i], b, a, world); + obfuscateRef.invoke(antiXrayInstance, xcoords[i], zcoords[i], b, a, world); + } + } catch (Exception e) { + // not spigot, or it failed. } } for (int i = 0; i < chunkMaps.length; i++) {