From 8468d9a13e4268e0aa2fa94fc6edd7dceb5d9652 Mon Sep 17 00:00:00 2001 From: jojo Date: Thu, 24 Dec 2020 12:04:50 +0100 Subject: [PATCH 01/11] Add versionDependantCall, See #144 BauSystem --- .../src/de/steamwar/core/Core.java | 69 +++++++++++++++++++ .../steamwar/core/ExceptionlessCallable.java | 6 ++ 2 files changed, 75 insertions(+) create mode 100644 SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index d9c59df..cf28ec6 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -28,6 +28,10 @@ import de.steamwar.sql.SQL; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; +import java.util.Optional; +import java.util.concurrent.Callable; +import java.util.logging.Logger; + public class Core extends JavaPlugin{ private static Core instance; @@ -82,4 +86,69 @@ public class Core extends JavaPlugin{ private static void setInstance(Core instance) { 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(); + } + } + } diff --git a/SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java b/SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java new file mode 100644 index 0000000..044c83f --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java @@ -0,0 +1,6 @@ +package de.steamwar.core; + +@FunctionalInterface +public interface ExceptionlessCallable { + T call(); +} From 1b188977a017a4b649182982a481b2bff308c955 Mon Sep 17 00:00:00 2001 From: jojo Date: Thu, 24 Dec 2020 12:07:11 +0100 Subject: [PATCH 02/11] Add Copyright Optimize Imports --- .../src/de/steamwar/core/Core.java | 4 ---- .../steamwar/core/ExceptionlessCallable.java | 19 +++++++++++++++++++ 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index cf28ec6..db5a023 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -28,10 +28,6 @@ import de.steamwar.sql.SQL; import org.bukkit.Bukkit; import org.bukkit.plugin.java.JavaPlugin; -import java.util.Optional; -import java.util.concurrent.Callable; -import java.util.logging.Logger; - public class Core extends JavaPlugin{ private static Core instance; diff --git a/SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java b/SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java index 044c83f..1b17a27 100644 --- a/SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java +++ b/SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java @@ -1,3 +1,22 @@ +/* + This file is a part of the SteamWar software. + + Copyright (C) 2020 SteamWar.de-Serverteam + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + package de.steamwar.core; @FunctionalInterface From 19394d03eeda9568467089e5a33a8e3dfe48e493 Mon Sep 17 00:00:00 2001 From: jojo Date: Thu, 24 Dec 2020 12:56:17 +0100 Subject: [PATCH 03/11] Update to Core.versionDependantCall --- .../src/de/steamwar/comms/BungeeReceiver.java | 8 ++-- .../src/de/steamwar/core/Core.java | 15 +++++++ .../src/de/steamwar/core/TPSWatcher.java | 16 +------ .../de/steamwar/core/VersionedCallable.java | 45 +++++++++++++++++++ .../de/steamwar/core/VersionedRunnable.java | 45 +++++++++++++++++++ .../steamwar/core/events/ChunkListener.java | 26 +++-------- .../src/de/steamwar/inventory/SWItem.java | 42 +++-------------- .../src/de/steamwar/message/Message.java | 14 ++---- .../de/steamwar/scoreboard/SWScoreboard.java | 15 ++----- .../src/de/steamwar/sql/Schematic.java | 1 + 10 files changed, 129 insertions(+), 98 deletions(-) create mode 100644 SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java create mode 100644 SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java diff --git a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java index 16ddf21..0ea59e0 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java +++ b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java @@ -24,6 +24,7 @@ import com.google.common.io.ByteStreams; import de.steamwar.comms.handlers.BungeeHandler; import de.steamwar.comms.handlers.InventoryHandler; import de.steamwar.core.Core; +import de.steamwar.core.VersionedRunnable; import de.steamwar.sql.*; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -46,11 +47,8 @@ public class BungeeReceiver implements PluginMessageListener { UUID uuid = SteamwarUser.get(byteArrayDataInput.readInt()).getUUID(); if(Bukkit.getPlayer(uuid).isOnline()) { Player player = Bukkit.getPlayer(uuid); - if (Core.getVersion() == 8) { - BungeeReceiver_8.playPling(player); - } else { - BungeeReceiver_9.playpling(player); - } + Core.versionDependantCall(new VersionedRunnable(() -> BungeeReceiver_8.playPling(player), 8), + new VersionedRunnable(() -> BungeeReceiver_9.playpling(player))); } }); diff --git a/SpigotCore_Main/src/de/steamwar/core/Core.java b/SpigotCore_Main/src/de/steamwar/core/Core.java index db5a023..0694ede 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -146,5 +146,20 @@ public class Core extends JavaPlugin{ 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 0e37709..05229ce 100644 --- a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java @@ -71,21 +71,7 @@ public class TPSWatcher { } private static double[] getSpigotTPS() { - switch (Core.getVersion()) { - case 8: - return SpigotTPS_8.getTps(); - case 9: - return SpigotTPS_9.getTps(); - case 10: - return SpigotTPS_10.getTps(); - case 12: - return SpigotTPS_12.getTps(); - case 14: - return SpigotTPS_14.getTps(); - case 15: - default: - return SpigotTPS_15.getTps(); - } + return Core.versionDependantCall(SpigotTPS_8::getTps, SpigotTPS_9::getTps, SpigotTPS_10::getTps, SpigotTPS_12::getTps, SpigotTPS_14::getTps, SpigotTPS_15::getTps); } 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 new file mode 100644 index 0000000..6a6da1e --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java @@ -0,0 +1,45 @@ +/* + * + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * / + */ + +package de.steamwar.core; + +import java.util.HashSet; +import java.util.Set; + +public class VersionedCallable { + + private ExceptionlessCallable exceptionlessCallable; + private Set versions = new HashSet<>(); + + public VersionedCallable(ExceptionlessCallable exceptionlessCallable, int... versions) { + this.exceptionlessCallable = exceptionlessCallable; + for (int version : versions) this.versions.add(version); + } + + boolean isVersion() { + return versions.contains(Core.getVersion()); + } + + public T call() { + return exceptionlessCallable.call(); + } + +} diff --git a/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java b/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java new file mode 100644 index 0000000..5eefc4e --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java @@ -0,0 +1,45 @@ +/* + * + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + * / + */ + +package de.steamwar.core; + +import java.util.HashSet; +import java.util.Set; + +public class VersionedRunnable { + + private Runnable runnable; + private Set versions = new HashSet<>(); + + public VersionedRunnable(Runnable runnable, int... versions) { + this.runnable = runnable; + for (int version : versions) this.versions.add(version); + } + + boolean isVersion() { + return versions.contains(Core.getVersion()); + } + + public void run() { + runnable.run(); + } + +} diff --git a/SpigotCore_Main/src/de/steamwar/core/events/ChunkListener.java b/SpigotCore_Main/src/de/steamwar/core/events/ChunkListener.java index 86d07ab..73d290a 100644 --- a/SpigotCore_Main/src/de/steamwar/core/events/ChunkListener.java +++ b/SpigotCore_Main/src/de/steamwar/core/events/ChunkListener.java @@ -71,25 +71,11 @@ public class ChunkListener { } public static void sendChunk(Player p, int chunkX, int chunkZ){ - switch(Core.getVersion()){ - case 15: - Chunk_15.sendChunk(p, chunkX, chunkZ); - break; - case 14: - Chunk_14.sendChunk(p, chunkX, chunkZ); - break; - case 12: - Chunk_12.sendChunk(p, chunkX, chunkZ); - break; - case 10: - Chunk_10.sendChunk(p, chunkX, chunkZ); - break; - case 9: - Chunk_9.sendChunk(p, chunkX, chunkZ); - break; - case 8: - Chunk_8.sendChunk(p, chunkX, chunkZ); - break; - } + 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)); } } diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index d6e92ff..64fcbae 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -22,6 +22,7 @@ package de.steamwar.inventory; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import de.steamwar.core.Core; +import de.steamwar.core.VersionedCallable; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.enchantments.Enchantment; @@ -44,53 +45,24 @@ public class SWItem { public static SWItem getPlayerSkull(String playerName){ SWItem p = new SWItem(); - ItemStack head; - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - head = SWItem_8.setSkullOwner(playerName); - break; - case 14: - case 15: - default: - head = SWItem_14.setSkullOwner(playerName); - } + ItemStack head = Core.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.setSkullOwner(playerName), 8, 9, 10, 12), + new VersionedCallable<>(() -> SWItem_14.setSkullOwner(playerName))); p.setItemStack(head); return p; } public static Material getMaterial(String material){ try{ - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - return SWItem_8.getMaterial(material); - case 14: - case 15: - default: - return SWItem_14.getMaterial(material); - } + return Core.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.getMaterial(material), 8, 9, 10, 12), + new VersionedCallable<>(() -> SWItem_14.getMaterial(material))); }catch(IllegalArgumentException e){ return Material.STONE; } } public static Material getDye(int colorCode){ - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - return SWItem_8.getDye(); - case 14: - case 15: - default: - return SWItem_14.getDye(colorCode); - } + return Core.versionDependantCall(new VersionedCallable<>(SWItem_8::getDye, 8, 9, 10, 12), + new VersionedCallable<>(() -> SWItem_14.getDye(colorCode))); } public SWItem() { diff --git a/SpigotCore_Main/src/de/steamwar/message/Message.java b/SpigotCore_Main/src/de/steamwar/message/Message.java index 065a560..c4811b2 100644 --- a/SpigotCore_Main/src/de/steamwar/message/Message.java +++ b/SpigotCore_Main/src/de/steamwar/message/Message.java @@ -20,6 +20,7 @@ package de.steamwar.message; import de.steamwar.core.Core; +import de.steamwar.core.VersionedCallable; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.ClickEvent; import net.md_5.bungee.api.chat.HoverEvent; @@ -73,17 +74,8 @@ public class Message { } private Locale getLocale(Player player){ - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - return Message_8.getLocale(player); - case 12: - case 14: - case 15: - default: - return Message_12.getLocale(player); - } + return Core.versionDependantCall(new VersionedCallable<>(() -> Message_8.getLocale(player), 8, 9, 10), + new VersionedCallable<>(() -> Message_12.getLocale(player))); } /* 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 7ea47f8..f9d68c7 100644 --- a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java +++ b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java @@ -25,6 +25,7 @@ import com.comphenix.protocol.ProtocolManager; import com.comphenix.protocol.events.PacketContainer; import com.comphenix.protocol.wrappers.WrappedChatComponent; import de.steamwar.core.Core; +import de.steamwar.core.VersionedRunnable; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -94,18 +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 - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - packet.getStrings().write(1, name); - break; - case 14: - case 15: - default: - packet.getChatComponents().write(0, WrappedChatComponent.fromText(name)); - } + Core.versionDependantCall(new VersionedRunnable(() -> packet.getStrings().write(1, name), 8, 9, 10, 12), + new VersionedRunnable(() -> packet.getChatComponents().write(0, WrappedChatComponent.fromText(name)))); 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 c0318d0..c98807a 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -21,6 +21,7 @@ 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; From 65f747fdb345bccd64293ef5e9b6fedd81152b5d Mon Sep 17 00:00:00 2001 From: jojo Date: Thu, 24 Dec 2020 14:24:48 +0100 Subject: [PATCH 04/11] Simplify VersionedCallable Simplify VersionedRunnable --- .../src/de/steamwar/comms/BungeeReceiver.java | 4 +- .../src/de/steamwar/core/Core.java | 79 ------------------- .../src/de/steamwar/core/TPSWatcher.java | 7 +- .../de/steamwar/core/VersionedCallable.java | 24 +++--- .../de/steamwar/core/VersionedRunnable.java | 24 +++--- .../steamwar/core/events/ChunkListener.java | 13 +-- .../src/de/steamwar/inventory/SWItem.java | 12 +-- .../src/de/steamwar/message/Message.java | 4 +- .../de/steamwar/scoreboard/SWScoreboard.java | 4 +- .../src/de/steamwar/sql/Schematic.java | 1 - 10 files changed, 53 insertions(+), 119 deletions(-) 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; From 97a766c79758064a3d4c40af7688c3ce743d371a Mon Sep 17 00:00:00 2001 From: jojo Date: Thu, 24 Dec 2020 14:44:03 +0100 Subject: [PATCH 05/11] Fix naming Fix SecurityException --- SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java | 3 +-- SpigotCore_Main/src/de/steamwar/core/Core.java | 1 - SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java | 2 +- .../src/de/steamwar/core/VersionedCallable.java | 7 +++---- .../src/de/steamwar/core/VersionedRunnable.java | 5 ++--- .../src/de/steamwar/core/events/ChunkListener.java | 2 +- SpigotCore_Main/src/de/steamwar/inventory/SWItem.java | 7 +++---- SpigotCore_Main/src/de/steamwar/message/Message.java | 3 +-- .../src/de/steamwar/scoreboard/SWScoreboard.java | 2 +- 9 files changed, 13 insertions(+), 19 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java index a9d1cbb..96e9e3a 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java +++ b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java @@ -23,7 +23,6 @@ import com.google.common.io.ByteArrayDataInput; import com.google.common.io.ByteStreams; import de.steamwar.comms.handlers.BungeeHandler; import de.steamwar.comms.handlers.InventoryHandler; -import de.steamwar.core.Core; import de.steamwar.core.VersionedRunnable; import de.steamwar.sql.*; import org.bukkit.Bukkit; @@ -47,7 +46,7 @@ public class BungeeReceiver implements PluginMessageListener { UUID uuid = SteamwarUser.get(byteArrayDataInput.readInt()).getUUID(); if(Bukkit.getPlayer(uuid).isOnline()) { Player player = Bukkit.getPlayer(uuid); - VersionedRunnable.versionDependantCall(new VersionedRunnable(() -> BungeeReceiver_8.playPling(player), 8), + VersionedRunnable.call(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 61a7a61..d9c59df 100644 --- a/SpigotCore_Main/src/de/steamwar/core/Core.java +++ b/SpigotCore_Main/src/de/steamwar/core/Core.java @@ -82,5 +82,4 @@ public class Core extends JavaPlugin{ private static void setInstance(Core instance) { Core.instance = instance; } - } diff --git a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java index ab51b45..314f246 100644 --- a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java @@ -71,7 +71,7 @@ public class TPSWatcher { } private static double[] getSpigotTPS() { - return VersionedCallable.versionDependantCall(new VersionedCallable<>(SpigotTPS_8::getTps, 8), + return VersionedCallable.call(new VersionedCallable<>(SpigotTPS_8::getTps, 8), new VersionedCallable<>(SpigotTPS_9::getTps, 9), new VersionedCallable<>(SpigotTPS_10::getTps, 10), new VersionedCallable<>(SpigotTPS_12::getTps, 12), diff --git a/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java index 57d32b3..841af0b 100644 --- a/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java @@ -35,15 +35,14 @@ public class VersionedCallable { return exceptionlessCallable.call(); } - public static T versionDependantCall(VersionedCallable... versionedCallables) { - int version = Core.getVersion(); + public static T call(VersionedCallable... versionedCallables) { for (int i = versionedCallables.length - 1; i >= 0; i--) { VersionedCallable versionedCallable = versionedCallables[i]; - if (version >= versionedCallable.minVersion || i == 0) { + if (Core.getVersion() >= versionedCallable.minVersion) { return versionedCallable.call(); } } - return null; + throw new SecurityException(); } } diff --git a/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java b/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java index 88f516a..b3790ad 100644 --- a/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java @@ -35,11 +35,10 @@ public class VersionedRunnable { runnable.run(); } - public static void versionDependantCall(VersionedRunnable... versionedRunnables) { - int version = Core.getVersion(); + public static void call(VersionedRunnable... versionedRunnables) { for (int i = versionedRunnables.length - 1; i >= 0; i--) { VersionedRunnable versionedRunnable = versionedRunnables[i]; - if (version >= versionedRunnable.minVersion) { + if (Core.getVersion() >= 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 9f57920..d6bf688 100644 --- a/SpigotCore_Main/src/de/steamwar/core/events/ChunkListener.java +++ b/SpigotCore_Main/src/de/steamwar/core/events/ChunkListener.java @@ -72,7 +72,7 @@ public class ChunkListener { } public static void sendChunk(Player p, int chunkX, int chunkZ){ - VersionedRunnable.versionDependantCall(new VersionedRunnable(() -> Chunk_8.sendChunk(p, chunkX, chunkZ), 8), + VersionedRunnable.call(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), diff --git a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java index bb66c44..d82a9af 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -21,7 +21,6 @@ package de.steamwar.inventory; import com.google.gson.JsonArray; import com.google.gson.JsonObject; -import de.steamwar.core.Core; import de.steamwar.core.VersionedCallable; import org.bukkit.Material; import org.bukkit.OfflinePlayer; @@ -45,7 +44,7 @@ public class SWItem { public static SWItem getPlayerSkull(String playerName){ SWItem p = new SWItem(); - ItemStack head = VersionedCallable.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.setSkullOwner(playerName), 8), + ItemStack head = VersionedCallable.call(new VersionedCallable<>(() -> SWItem_8.setSkullOwner(playerName), 8), new VersionedCallable<>(() -> SWItem_14.setSkullOwner(playerName), 14)); p.setItemStack(head); return p; @@ -53,7 +52,7 @@ public class SWItem { public static Material getMaterial(String material){ try{ - return VersionedCallable.versionDependantCall(new VersionedCallable<>(() -> SWItem_8.getMaterial(material), 8), + return VersionedCallable.call(new VersionedCallable<>(() -> SWItem_8.getMaterial(material), 8), new VersionedCallable<>(() -> SWItem_14.getMaterial(material), 14)); }catch(IllegalArgumentException e){ return Material.STONE; @@ -61,7 +60,7 @@ public class SWItem { } public static Material getDye(int colorCode){ - return VersionedCallable.versionDependantCall(new VersionedCallable<>(SWItem_8::getDye, 8), + return VersionedCallable.call(new VersionedCallable<>(SWItem_8::getDye, 8), new VersionedCallable<>(() -> SWItem_14.getDye(colorCode), 14)); } diff --git a/SpigotCore_Main/src/de/steamwar/message/Message.java b/SpigotCore_Main/src/de/steamwar/message/Message.java index 117f7d9..7ae38e0 100644 --- a/SpigotCore_Main/src/de/steamwar/message/Message.java +++ b/SpigotCore_Main/src/de/steamwar/message/Message.java @@ -19,7 +19,6 @@ package de.steamwar.message; -import de.steamwar.core.Core; import de.steamwar.core.VersionedCallable; import net.md_5.bungee.api.ChatMessageType; import net.md_5.bungee.api.chat.ClickEvent; @@ -74,7 +73,7 @@ public class Message { } private Locale getLocale(Player player){ - return VersionedCallable.versionDependantCall(new VersionedCallable<>(() -> Message_8.getLocale(player), 8), + return VersionedCallable.call(new VersionedCallable<>(() -> Message_8.getLocale(player), 8), new VersionedCallable<>(() -> Message_12.getLocale(player), 12)); } diff --git a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java index 45d81ad..809fc30 100644 --- a/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java +++ b/SpigotCore_Main/src/de/steamwar/scoreboard/SWScoreboard.java @@ -95,7 +95,7 @@ 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 - VersionedRunnable.versionDependantCall(new VersionedRunnable(() -> packet.getStrings().write(1, name), 8), + VersionedRunnable.call(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; From e01a72a42bddbc1f09c18e1f762c890e2a62d669 Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 25 Dec 2020 18:30:46 +0100 Subject: [PATCH 06/11] Simplify VersionedCallable Simplify VersionedRunnable Remove ExceptionlessCallable --- .../steamwar/core/ExceptionlessCallable.java | 25 ------------------- .../de/steamwar/core/VersionedCallable.java | 18 +++++++------ .../de/steamwar/core/VersionedRunnable.java | 6 +---- 3 files changed, 11 insertions(+), 38 deletions(-) delete mode 100644 SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java diff --git a/SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java b/SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java deleted file mode 100644 index 1b17a27..0000000 --- a/SpigotCore_Main/src/de/steamwar/core/ExceptionlessCallable.java +++ /dev/null @@ -1,25 +0,0 @@ -/* - This file is a part of the SteamWar software. - - Copyright (C) 2020 SteamWar.de-Serverteam - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU Affero General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU Affero General Public License for more details. - - You should have received a copy of the GNU Affero General Public License - along with this program. If not, see . -*/ - -package de.steamwar.core; - -@FunctionalInterface -public interface ExceptionlessCallable { - T call(); -} diff --git a/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java index 841af0b..430d7fa 100644 --- a/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java @@ -21,25 +21,27 @@ package de.steamwar.core; +import java.util.concurrent.Callable; + public class VersionedCallable { - private ExceptionlessCallable exceptionlessCallable; + private Callable callable; private int minVersion; - public VersionedCallable(ExceptionlessCallable exceptionlessCallable, int minVersion) { - this.exceptionlessCallable = exceptionlessCallable; + public VersionedCallable(Callable callable, int minVersion) { + this.callable = callable; this.minVersion = minVersion; } - public T call() { - return exceptionlessCallable.call(); - } - public static T call(VersionedCallable... versionedCallables) { for (int i = versionedCallables.length - 1; i >= 0; i--) { VersionedCallable versionedCallable = versionedCallables[i]; if (Core.getVersion() >= versionedCallable.minVersion) { - return versionedCallable.call(); + try { + return versionedCallable.callable.call(); + } catch (Exception e) { + throw new RuntimeException("Could not run version dependant code", e); + } } } throw new SecurityException(); diff --git a/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java b/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java index b3790ad..fc65517 100644 --- a/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java @@ -31,15 +31,11 @@ public class VersionedRunnable { this.minVersion = minVersion; } - public void run() { - runnable.run(); - } - public static void call(VersionedRunnable... versionedRunnables) { for (int i = versionedRunnables.length - 1; i >= 0; i--) { VersionedRunnable versionedRunnable = versionedRunnables[i]; if (Core.getVersion() >= versionedRunnable.minVersion) { - versionedRunnable.run(); + versionedRunnable.runnable.run(); return; } } From e0f6b2e5bf5900d176145f7d59424b0b3c4707fa Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 25 Dec 2020 18:32:00 +0100 Subject: [PATCH 07/11] Fix Typo --- SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java index 430d7fa..2846fb1 100644 --- a/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java @@ -40,7 +40,7 @@ public class VersionedCallable { try { return versionedCallable.callable.call(); } catch (Exception e) { - throw new RuntimeException("Could not run version dependant code", e); + throw new RuntimeException("Could not run version dependent code", e); } } } From f3ada581af7ad70a0c0d64bd75b3bc42ef2703fa Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 25 Dec 2020 18:35:45 +0100 Subject: [PATCH 08/11] Change Schematic version dependant calls --- SpigotCore_Main/src/de/steamwar/sql/Schematic.java | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index c0318d0..5ab3634 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -21,6 +21,7 @@ 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; @@ -189,17 +190,8 @@ public class Schematic { if(schemData == null) throw new IOException("SchemData is null"); InputStream is = schemData.getBinaryStream(); - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - return Schematic_8.getClipboard(is, schemFormat); - case 14: - case 15: - default: - return Schematic_14.getClipboard(is, schemFormat); - } + return VersionedCallable.call(new VersionedCallable<>(() -> Schematic_8.getClipboard(is, schemFormat), 8), + new VersionedCallable<>(() -> Schematic_14.getClipboard(is, schemFormat), 14)); } catch (SQLException e) { throw new IOException(e); } From c646bf9533ffcd035449abf807a8e5af71c5f74d Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 25 Dec 2020 19:06:12 +0100 Subject: [PATCH 09/11] Change Schematic version dependant calls --- .../src/de/steamwar/sql/Schematic.java | 23 ++++++++++--------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index 5ab3634..b74a253 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -22,6 +22,7 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; import de.steamwar.core.Core; import de.steamwar.core.VersionedCallable; +import de.steamwar.core.VersionedRunnable; import org.bukkit.entity.Player; import java.io.IOException; @@ -198,7 +199,6 @@ public class Schematic { } public void loadToPlayer(Player player) throws IOException, NoClipboardException { - ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID); try { rs.next(); @@ -206,18 +206,19 @@ public class Schematic { if(blob == null) throw new NoClipboardException(); InputStream is = blob.getBinaryStream(); - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: + VersionedRunnable.call(new VersionedRunnable(() -> { + try { Schematic_8.setPlayerClipboard(player, is, schemFormat); - break; - case 14: - case 15: - default: + } catch (IOException exception) { + throw new RuntimeException(exception.getMessage(), exception); + } + }, 8), new VersionedRunnable(() -> { + try { Schematic_14.setPlayerClipboard(player, is, schemFormat); - } + } catch (IOException | NoClipboardException exception) { + throw new RuntimeException(exception.getMessage(), exception); + } + }, 14)); } catch (SQLException e) { throw new IOException(e); } From 2c0c75e6ea28c65635b670eb2121fe03eaa09933 Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 25 Dec 2020 19:12:24 +0100 Subject: [PATCH 10/11] Change Schematic version dependant calls --- .../src/de/steamwar/sql/Schematic.java | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index b74a253..ed589a7 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -20,7 +20,6 @@ package de.steamwar.sql; import com.sk89q.worldedit.extent.clipboard.Clipboard; -import de.steamwar.core.Core; import de.steamwar.core.VersionedCallable; import de.steamwar.core.VersionedRunnable; import org.bukkit.entity.Player; @@ -233,26 +232,27 @@ public class Schematic { } private void saveFromPlayer(Player player, boolean newFormat) throws IOException, NoClipboardException { - try{ - Blob blob = SQL.blob(); - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - newFormat = false; - blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); - break; - case 14: - case 15: - default: - blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); + Blob blob = SQL.blob(); + VersionedRunnable.call(new VersionedRunnable(() -> { + try { + blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); + } catch (IOException | NoClipboardException | SQLException e) { + throw new RuntimeException(e.getMessage(), e); } - SQL.update("UPDATE Schematic SET SchemData = ?, SchemFormat = ? WHERE SchemID = ?", blob, newFormat, schemID); - schemFormat = newFormat; - }catch(SQLException e){ - throw new IOException(e); - } + updateDatabase(blob, player, false); + }, 8), new VersionedRunnable(() -> { + try { + blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); + } catch (IOException | NoClipboardException | SQLException e) { + throw new RuntimeException(e.getMessage(), e); + } + updateDatabase(blob, player, newFormat); + }, 14)); + } + + private void updateDatabase(Blob blob, Player player, boolean newFormat) { + SQL.update("UPDATE Schematic SET SchemData = ?, SchemFormat = ? WHERE SchemID = ?", blob, newFormat, schemID); + schemFormat = newFormat; } public void remove(){ From 68155e228a23678bdbca398905968f6020942e85 Mon Sep 17 00:00:00 2001 From: jojo Date: Fri, 25 Dec 2020 21:01:03 +0100 Subject: [PATCH 11/11] Change Schematic version dependant calls --- .../src/de/steamwar/sql/Schematic_14.java | 21 ++++++++++++------- .../src/de/steamwar/sql/Schematic_8.java | 21 +++++++++++++------ .../src/de/steamwar/sql/Schematic.java | 21 +++++-------------- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java b/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java index b383998..675da6e 100644 --- a/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java +++ b/SpigotCore_14/src/de/steamwar/sql/Schematic_14.java @@ -64,17 +64,17 @@ class Schematic_14 { private static final ClipboardFormat SCHEMATIC = BuiltInClipboardFormat.MCEDIT_SCHEMATIC; private static final ClipboardFormat SCHEM = BuiltInClipboardFormat.SPONGE_SCHEMATIC; - static byte[] getPlayerClipboard(Player player, boolean schemFormat) throws IOException, NoClipboardException { + static byte[] getPlayerClipboard(Player player, boolean schemFormat) { ClipboardHolder clipboardHolder; try { clipboardHolder = getWorldEditPlugin().getSession(player).getClipboard(); } catch (EmptyClipboardException e) { - throw new NoClipboardException(); + throw new RuntimeException(e.getMessage(), new NoClipboardException()); } Clipboard clipboard = clipboardHolder.getClipboard(); if(clipboard == null) - throw new NoClipboardException(); + throw new RuntimeException("Clipboard was null", new NoClipboardException()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); try{ @@ -86,16 +86,23 @@ class Schematic_14 { SCHEMATIC.getWriter(outputStream).write(clipboard); } }catch(NullPointerException e){ - throw new IOException(e); + throw new RuntimeException(e.getMessage(), new IOException(e)); + } catch (IOException e) { + throw new RuntimeException(e.getMessage(), e); } return outputStream.toByteArray(); } - static void setPlayerClipboard(Player player, InputStream is, boolean schemFormat) throws IOException, NoClipboardException { - Clipboard clipboard = getClipboard(is, schemFormat); + static void setPlayerClipboard(Player player, InputStream is, boolean schemFormat) { + Clipboard clipboard = null; + try { + clipboard = getClipboard(is, schemFormat); + } catch (IOException | NoClipboardException e) { + throw new RuntimeException(e.getMessage(), e); + } if (clipboard == null) - throw new NoClipboardException(); + throw new RuntimeException("clipboard was null", new NoClipboardException()); Actor actor = getWorldEditPlugin().wrapCommandSender(player); getWorldEditPlugin().getWorldEdit().getSessionManager().get(actor).setClipboard(new ClipboardHolder(clipboard)); diff --git a/SpigotCore_8/src/de/steamwar/sql/Schematic_8.java b/SpigotCore_8/src/de/steamwar/sql/Schematic_8.java index 1ae9881..8578007 100644 --- a/SpigotCore_8/src/de/steamwar/sql/Schematic_8.java +++ b/SpigotCore_8/src/de/steamwar/sql/Schematic_8.java @@ -52,26 +52,35 @@ import java.util.zip.GZIPInputStream; class Schematic_8 { private Schematic_8(){} - static byte[] getPlayerClipboard(Player player) throws IOException, NoClipboardException { + static byte[] getPlayerClipboard(Player player) { ClipboardHolder clipboardHolder; try { clipboardHolder = getWorldEditPlugin().getSession(player).getClipboard(); } catch (EmptyClipboardException e) { - throw new NoClipboardException(); + throw new RuntimeException(e.getMessage(), e); } Clipboard clipboard = clipboardHolder.getClipboard(); if(clipboard == null) - throw new NoClipboardException(); + throw new RuntimeException("clipboard was null", new NoClipboardException()); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); - ClipboardFormat.SCHEMATIC.getWriter(outputStream).write(clipboard, clipboardHolder.getWorldData()); + try { + ClipboardFormat.SCHEMATIC.getWriter(outputStream).write(clipboard, clipboardHolder.getWorldData()); + } catch (IOException e) { + throw new RuntimeException(e.getMessage(), e); + } return outputStream.toByteArray(); } - static void setPlayerClipboard(Player player, InputStream is, boolean schemFormat) throws IOException { + static void setPlayerClipboard(Player player, InputStream is, boolean schemFormat) { WorldData world = new BukkitWorld(player.getWorld()).getWorldData(); - Clipboard clipboard = getClipboard(is, schemFormat); + Clipboard clipboard; + try { + clipboard = getClipboard(is, schemFormat); + } catch (IOException e) { + throw new RuntimeException(e); + } Actor actor = getWorldEditPlugin().wrapCommandSender(player); getWorldEditPlugin().getWorldEdit().getSessionManager().get(actor).setClipboard(new ClipboardHolder(clipboard, world)); diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index ed589a7..4be00c8 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -205,19 +205,8 @@ public class Schematic { if(blob == null) throw new NoClipboardException(); InputStream is = blob.getBinaryStream(); - VersionedRunnable.call(new VersionedRunnable(() -> { - try { - Schematic_8.setPlayerClipboard(player, is, schemFormat); - } catch (IOException exception) { - throw new RuntimeException(exception.getMessage(), exception); - } - }, 8), new VersionedRunnable(() -> { - try { - Schematic_14.setPlayerClipboard(player, is, schemFormat); - } catch (IOException | NoClipboardException exception) { - throw new RuntimeException(exception.getMessage(), exception); - } - }, 14)); + VersionedRunnable.call(new VersionedRunnable(() -> Schematic_8.setPlayerClipboard(player, is, schemFormat), 8), + new VersionedRunnable(() -> Schematic_14.setPlayerClipboard(player, is, schemFormat), 14)); } catch (SQLException e) { throw new IOException(e); } @@ -236,15 +225,15 @@ public class Schematic { VersionedRunnable.call(new VersionedRunnable(() -> { try { blob.setBytes(1, Schematic_8.getPlayerClipboard(player)); - } catch (IOException | NoClipboardException | SQLException e) { + } catch (SQLException e) { throw new RuntimeException(e.getMessage(), e); } updateDatabase(blob, player, false); }, 8), new VersionedRunnable(() -> { try { blob.setBytes(1, Schematic_14.getPlayerClipboard(player, newFormat)); - } catch (IOException | NoClipboardException | SQLException e) { - throw new RuntimeException(e.getMessage(), e); + } catch (SQLException exception) { + throw new RuntimeException(exception.getMessage(), exception); } updateDatabase(blob, player, newFormat); }, 14));