diff --git a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java index 0ea59e0..a9d1cbb 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java +++ b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java @@ -47,8 +47,8 @@ public class BungeeReceiver implements PluginMessageListener { UUID uuid = SteamwarUser.get(byteArrayDataInput.readInt()).getUUID(); if(Bukkit.getPlayer(uuid).isOnline()) { Player player = Bukkit.getPlayer(uuid); - Core.versionDependantCall(new VersionedRunnable(() -> BungeeReceiver_8.playPling(player), 8), - new VersionedRunnable(() -> BungeeReceiver_9.playpling(player))); + VersionedRunnable.versionDependantCall(new VersionedRunnable(() -> BungeeReceiver_8.playPling(player), 8), + new VersionedRunnable(() -> BungeeReceiver_9.playpling(player), 9)); } }); diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index 0694ede..61a7a61 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -83,83 +83,4 @@ public class Core extends JavaPlugin{ Core.instance = instance; } - public static void versionDependantCall(Runnable v12, Runnable v15) { - switch (getVersion()) { - case 12: - v12.run(); - break; - case 15: - default: - v15.run(); - break; - } - } - - public static T versionDependantCall(ExceptionlessCallable v12, ExceptionlessCallable v15) { - switch (getVersion()) { - case 12: - return v12.call(); - case 15: - default: - return v15.call(); - } - } - - public static void versionDependantCall(Runnable v8, Runnable v9, Runnable v10, Runnable v12, Runnable v14, Runnable v15) { - switch (Core.getVersion()) { - case 8: - v8.run(); - break; - case 9: - v9.run(); - break; - case 10: - v10.run(); - break; - case 12: - v12.run(); - break; - case 14: - v14.run(); - break; - case 15: - default: - v15.run(); - break; - } - } - - public static T versionDependantCall(ExceptionlessCallable v8, ExceptionlessCallable v9, ExceptionlessCallable v10, ExceptionlessCallable v12, ExceptionlessCallable v14, ExceptionlessCallable v15) { - switch (Core.getVersion()) { - case 8: - return v8.call(); - case 9: - return v9.call(); - case 10: - return v10.call(); - case 12: - return v12.call(); - case 14: - return v14.call(); - case 15: - default: - return v15.call(); - } - } - - public static void versionDependantCall(VersionedRunnable versionedRunnable1, VersionedRunnable versionedRunnable2) { - if (versionedRunnable1.isVersion()) { - versionedRunnable1.run(); - return; - } - versionedRunnable2.run(); - } - - public static T versionDependantCall(VersionedCallable versionedCallable1, VersionedCallable versionedCallable2) { - if (versionedCallable1.isVersion()) { - return versionedCallable1.call(); - } - return versionedCallable2.call(); - } - } diff --git a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java index 05229ce..ab51b45 100644 --- a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java @@ -71,7 +71,12 @@ public class TPSWatcher { } private static double[] getSpigotTPS() { - return Core.versionDependantCall(SpigotTPS_8::getTps, SpigotTPS_9::getTps, SpigotTPS_10::getTps, SpigotTPS_12::getTps, SpigotTPS_14::getTps, SpigotTPS_15::getTps); + return VersionedCallable.versionDependantCall(new VersionedCallable<>(SpigotTPS_8::getTps, 8), + new VersionedCallable<>(SpigotTPS_9::getTps, 9), + new VersionedCallable<>(SpigotTPS_10::getTps, 10), + new VersionedCallable<>(SpigotTPS_12::getTps, 12), + new VersionedCallable<>(SpigotTPS_14::getTps, 14), + new VersionedCallable<>(SpigotTPS_15::getTps, 15)); } private static double round(double d) { diff --git a/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java index 6a6da1e..57d32b3 100644 --- a/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java @@ -21,25 +21,29 @@ package de.steamwar.core; -import java.util.HashSet; -import java.util.Set; - public class VersionedCallable { private ExceptionlessCallable exceptionlessCallable; - private Set versions = new HashSet<>(); + private int minVersion; - public VersionedCallable(ExceptionlessCallable exceptionlessCallable, int... versions) { + public VersionedCallable(ExceptionlessCallable exceptionlessCallable, int minVersion) { this.exceptionlessCallable = exceptionlessCallable; - for (int version : versions) this.versions.add(version); - } - - boolean isVersion() { - return versions.contains(Core.getVersion()); + this.minVersion = minVersion; } public T call() { return exceptionlessCallable.call(); } + public static T versionDependantCall(VersionedCallable... versionedCallables) { + int version = Core.getVersion(); + for (int i = versionedCallables.length - 1; i >= 0; i--) { + VersionedCallable versionedCallable = versionedCallables[i]; + if (version >= versionedCallable.minVersion || i == 0) { + return versionedCallable.call(); + } + } + return null; + } + } diff --git a/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java b/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java index 5eefc4e..88f516a 100644 --- a/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java @@ -21,25 +21,29 @@ package de.steamwar.core; -import java.util.HashSet; -import java.util.Set; - public class VersionedRunnable { private Runnable runnable; - private Set versions = new HashSet<>(); + private int minVersion; - public VersionedRunnable(Runnable runnable, int... versions) { + public VersionedRunnable(Runnable runnable, int minVersion) { this.runnable = runnable; - for (int version : versions) this.versions.add(version); - } - - boolean isVersion() { - return versions.contains(Core.getVersion()); + this.minVersion = minVersion; } public void run() { runnable.run(); } + public static void versionDependantCall(VersionedRunnable... versionedRunnables) { + int version = Core.getVersion(); + for (int i = versionedRunnables.length - 1; i >= 0; i--) { + VersionedRunnable versionedRunnable = versionedRunnables[i]; + if (version >= versionedRunnable.minVersion) { + versionedRunnable.run(); + return; + } + } + } + } diff --git a/SpigotCore_Main/src/de/steamwar/core/events/ChunkListener.java b/SpigotCore_Main/src/de/steamwar/core/events/ChunkListener.java index 73d290a..9f57920 100644 --- a/SpigotCore_Main/src/de/steamwar/core/events/ChunkListener.java +++ b/SpigotCore_Main/src/de/steamwar/core/events/ChunkListener.java @@ -28,6 +28,7 @@ import com.comphenix.protocol.injector.server.TemporaryPlayer; import com.comphenix.protocol.reflect.StructureModifier; import de.steamwar.chunk.*; import de.steamwar.core.Core; +import de.steamwar.core.VersionedRunnable; import de.steamwar.sql.SteamwarUser; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -71,11 +72,11 @@ public class ChunkListener { } public static void sendChunk(Player p, int chunkX, int chunkZ){ - Core.versionDependantCall(() -> Chunk_8.sendChunk(p, chunkX, chunkZ), - () -> Chunk_9.sendChunk(p, chunkX, chunkZ), - () -> Chunk_10.sendChunk(p, chunkX, chunkZ), - () -> Chunk_12.sendChunk(p, chunkX, chunkZ), - () -> Chunk_14.sendChunk(p, chunkX, chunkZ), - () -> Chunk_15.sendChunk(p, chunkX, chunkZ)); + VersionedRunnable.versionDependantCall(new VersionedRunnable(() -> Chunk_8.sendChunk(p, chunkX, chunkZ), 8), + new VersionedRunnable(() -> Chunk_9.sendChunk(p, chunkX, chunkZ), 9), + new VersionedRunnable(() -> Chunk_10.sendChunk(p, chunkX, chunkZ), 10), + new VersionedRunnable(() -> Chunk_12.sendChunk(p, chunkX, chunkZ), 12), + new VersionedRunnable(() -> Chunk_14.sendChunk(p, chunkX, chunkZ), 14), + new VersionedRunnable(() -> Chunk_15.sendChunk(p, chunkX, chunkZ), 15)); } } diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index 64fcbae..bb66c44 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -45,24 +45,24 @@ public class SWItem { public static SWItem getPlayerSkull(String playerName){ SWItem p = new SWItem(); - ItemStack head = Core.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.setSkullOwner(playerName), 8, 9, 10, 12), - new VersionedCallable<>(() -> SWItem_14.setSkullOwner(playerName))); + ItemStack head = VersionedCallable.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.setSkullOwner(playerName), 8), + new VersionedCallable<>(() -> SWItem_14.setSkullOwner(playerName), 14)); p.setItemStack(head); return p; } public static Material getMaterial(String material){ try{ - return Core.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.getMaterial(material), 8, 9, 10, 12), - new VersionedCallable<>(() -> SWItem_14.getMaterial(material))); + return VersionedCallable.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.getMaterial(material), 8), + new VersionedCallable<>(() -> SWItem_14.getMaterial(material), 14)); }catch(IllegalArgumentException e){ return Material.STONE; } } public static Material getDye(int colorCode){ - return Core.versionDependantCall(new VersionedCallable<>(SWItem_8::getDye, 8, 9, 10, 12), - new VersionedCallable<>(() -> SWItem_14.getDye(colorCode))); + return VersionedCallable.versionDependantCall(new VersionedCallable<>(SWItem_8::getDye, 8), + new VersionedCallable<>(() -> SWItem_14.getDye(colorCode), 14)); } public SWItem() { diff --git a/SpigotCore_Main/src/de/steamwar/message/Message.java b/SpigotCore_Main/src/de/steamwar/message/Message.java index c4811b2..117f7d9 100644 --- a/SpigotCore_Main/src/de/steamwar/message/Message.java +++ b/SpigotCore_Main/src/de/steamwar/message/Message.java @@ -74,8 +74,8 @@ public class Message { } private Locale getLocale(Player player){ - return Core.versionDependantCall(new VersionedCallable<>(() -> Message_8.getLocale(player), 8, 9, 10), - new VersionedCallable<>(() -> Message_12.getLocale(player))); + return VersionedCallable.versionDependantCall(new VersionedCallable<>(() -> Message_8.getLocale(player), 8), + new VersionedCallable<>(() -> Message_12.getLocale(player), 12)); } /* Send a message to one player */ diff --git a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java index f9d68c7..45d81ad 100644 --- a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java +++ b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java @@ -95,8 +95,8 @@ public class SWScoreboard { PacketContainer packet = manager.createPacket(PacketType.Play.Server.SCOREBOARD_OBJECTIVE); packet.getStrings().write(0, SIDEBAR + toggle); packet.getIntegers().write(0, 0); //0 to create - Core.versionDependantCall(new VersionedRunnable(() -> packet.getStrings().write(1, name), 8, 9, 10, 12), - new VersionedRunnable(() -> packet.getChatComponents().write(0, WrappedChatComponent.fromText(name)))); + VersionedRunnable.versionDependantCall(new VersionedRunnable(() -> packet.getStrings().write(1, name), 8), + new VersionedRunnable(() -> packet.getChatComponents().write(0, WrappedChatComponent.fromText(name)), 14)); packet.getEnumModifier(RenderType.class, 2).write(0, RenderType.INTEGER); return packet; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index c98807a..c0318d0 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -21,7 +21,6 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.core.Core; -import de.steamwar.core.VersionedCallable; import org.bukkit.entity.Player; import java.io.IOException;