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/comms/BungeeReceiver.java b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java index 16ddf21..96e9e3a 100644 --- a/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java +++ b/SpigotCore_Main/src/de/steamwar/comms/BungeeReceiver.java @@ -23,7 +23,7 @@ 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; import org.bukkit.entity.Player; @@ -46,11 +46,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); - } + 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/TPSWatcher.java b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java index 0e37709..314f246 100644 --- a/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java +++ b/SpigotCore_Main/src/de/steamwar/core/TPSWatcher.java @@ -71,21 +71,12 @@ 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 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), + 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 new file mode 100644 index 0000000..2846fb1 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedCallable.java @@ -0,0 +1,50 @@ +/* + * + * 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.concurrent.Callable; + +public class VersionedCallable { + + private Callable callable; + private int minVersion; + + public VersionedCallable(Callable callable, int minVersion) { + this.callable = callable; + this.minVersion = minVersion; + } + + public static T call(VersionedCallable... versionedCallables) { + for (int i = versionedCallables.length - 1; i >= 0; i--) { + VersionedCallable versionedCallable = versionedCallables[i]; + if (Core.getVersion() >= versionedCallable.minVersion) { + try { + return versionedCallable.callable.call(); + } catch (Exception e) { + throw new RuntimeException("Could not run version dependent 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 new file mode 100644 index 0000000..fc65517 --- /dev/null +++ b/SpigotCore_Main/src/de/steamwar/core/VersionedRunnable.java @@ -0,0 +1,44 @@ +/* + * + * 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; + +public class VersionedRunnable { + + private Runnable runnable; + private int minVersion; + + public VersionedRunnable(Runnable runnable, int minVersion) { + this.runnable = runnable; + this.minVersion = minVersion; + } + + 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.runnable.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 86d07ab..d6bf688 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,25 +72,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; - } + 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), + 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 d6e92ff..d82a9af 100644 --- a/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java +++ b/SpigotCore_Main/src/de/steamwar/inventory/SWItem.java @@ -21,7 +21,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 +44,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 = VersionedCallable.call(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{ - 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 VersionedCallable.call(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){ - 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 VersionedCallable.call(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 065a560..7ae38e0 100644 --- a/SpigotCore_Main/src/de/steamwar/message/Message.java +++ b/SpigotCore_Main/src/de/steamwar/message/Message.java @@ -19,7 +19,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 +73,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 VersionedCallable.call(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 7ea47f8..809fc30 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)); - } + 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; } diff --git a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java index c0318d0..4be00c8 100644 --- a/SpigotCore_Main/src/de/steamwar/sql/Schematic.java +++ b/SpigotCore_Main/src/de/steamwar/sql/Schematic.java @@ -20,7 +20,8 @@ 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; @@ -189,24 +190,14 @@ 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); } } public void loadToPlayer(Player player) throws IOException, NoClipboardException { - ResultSet rs = SQL.select("SELECT SchemData FROM Schematic WHERE SchemID = ?", schemID); try { rs.next(); @@ -214,18 +205,8 @@ public class Schematic { if(blob == null) throw new NoClipboardException(); InputStream is = blob.getBinaryStream(); - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - Schematic_8.setPlayerClipboard(player, is, schemFormat); - break; - case 14: - case 15: - default: - Schematic_14.setPlayerClipboard(player, is, schemFormat); - } + 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); } @@ -240,26 +221,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 (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 (SQLException exception) { + throw new RuntimeException(exception.getMessage(), exception); + } + 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(){