From 623ee6c75a266e2b1fa8c64642827292f2505f10 Mon Sep 17 00:00:00 2001 From: jojo Date: Mon, 4 Jan 2021 22:21:36 +0100 Subject: [PATCH 1/3] Refactor version dependant calls to new System --- .../steamwar/fightsystem/fight/FightTeam.java | 51 ++++++------------- .../fightsystem/listener/BasicListener.java | 43 +++------------- .../listener/PersonalKitCreator.java | 22 +++----- .../fightsystem/record/RecordSystem.java | 14 ++--- .../fightsystem/utils/ColorConverter.java | 17 ++----- .../steamwar/fightsystem/utils/TechHider.java | 19 ++----- .../fightsystem/utils/WaterRemover.java | 28 ++-------- 7 files changed, 45 insertions(+), 149 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java b/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java index b14e0ab..05ea9ec 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java @@ -21,7 +21,7 @@ package de.steamwar.fightsystem.fight; import com.sk89q.worldedit.EditSession; import de.steamwar.comms.packets.TablistNamePacket; -import de.steamwar.core.Core; +import de.steamwar.core.VersionedRunnable; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.IFightSystem; @@ -277,26 +277,17 @@ public class FightTeam implements IFightTeam{ return; } - EditSession e; - try { - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - e = FightTeam_8.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); - FightTeam_8.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); - break; - case 12: - e = FightTeam_8.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); - FightTeam_12.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); - break; - case 14: - case 15: - default: - e = FightTeam_14.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); - FightTeam_14.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); - } + VersionedRunnable.call(new VersionedRunnable(() -> { + EditSession e = FightTeam_8.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); + FightTeam_8.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); + }, 8), new VersionedRunnable(() -> { + EditSession e = FightTeam_8.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); + FightTeam_12.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); + }, 12), new VersionedRunnable(() -> { + EditSession e = FightTeam_14.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); + FightTeam_14.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); + }, 14)); } catch (Schematic.WrongVersionException ex) { broadcast(FightSystem.PREFIX + "§cDie gewählte Schematic kann nicht in dieser Version geladen werden"); HandlerList.unregisterAll(freezer); @@ -444,22 +435,10 @@ public class FightTeam implements IFightTeam{ } private void setTeamColor(Team team, ChatColor color){ - switch(Core.getVersion()){ - case 8: - FightTeam_8.setTeamColor(team, color); - break; - case 9: - case 10: - FightTeam_9.setTeamColor(team, color); - break; - case 12: - FightTeam_12.setTeamColor(team, color); - break; - case 14: - case 15: - default: - FightTeam_14.setTeamColor(team, color); - } + VersionedRunnable.call(new VersionedRunnable(() -> FightTeam_8.setTeamColor(team, color), 8), + new VersionedRunnable(() -> FightTeam_9.setTeamColor(team, color), 9), + new VersionedRunnable(() -> FightTeam_12.setTeamColor(team, color), 12), + new VersionedRunnable(() -> FightTeam_14.setTeamColor(team, color), 14)); } private static class FreezeWorld implements Listener { diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/BasicListener.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/BasicListener.java index 4f1484f..6c16d44 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/BasicListener.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/BasicListener.java @@ -19,7 +19,7 @@ package de.steamwar.fightsystem.listener; -import de.steamwar.core.Core; +import de.steamwar.core.VersionedRunnable; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.fight.Fight; import de.steamwar.fightsystem.states.FightState; @@ -46,47 +46,18 @@ public abstract class BasicListener implements Listener, StateDependent { } public static void toActionbar(Player player, BaseComponent... components){ - switch(Core.getVersion()){ - case 8: - BasicListener_8.toActionbar(player, components); - break; - case 9: - case 10: - case 12: - case 14: - case 15: - default: - BasicListener_9.toActionbar(player, components); - } + VersionedRunnable.call(new VersionedRunnable(() -> BasicListener_8.toActionbar(player, components), 8), + new VersionedRunnable(() -> BasicListener_9.toActionbar(player, components), 9)); } public static void toChat(Player player, BaseComponent... components){ - switch(Core.getVersion()){ - case 8: - BasicListener_8.toChat(player, components); - break; - case 9: - case 10: - case 12: - case 14: - case 15: - default: - BasicListener_9.toChat(player, components); - } + VersionedRunnable.call(new VersionedRunnable(() -> BasicListener_8.toChat(player, components), 8), + new VersionedRunnable(() -> BasicListener_9.toChat(player, components), 9)); } void setAttackSpeed(Player player){ - switch(Core.getVersion()){ - case 8: - break; - case 9: - case 10: - case 12: - case 14: - case 15: - default: - BasicListener_9.setAttackSpeed(player); - } + VersionedRunnable.call(new VersionedRunnable(() -> {}, 8), + new VersionedRunnable(() -> BasicListener_9.setAttackSpeed(player), 9)); } @Override diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/PersonalKitCreator.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/PersonalKitCreator.java index c89e924..0b8e181 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/PersonalKitCreator.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/PersonalKitCreator.java @@ -20,6 +20,7 @@ package de.steamwar.fightsystem.listener; import de.steamwar.core.Core; +import de.steamwar.core.VersionedCallable; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.fight.Fight; @@ -177,21 +178,12 @@ public class PersonalKitCreator extends BasicListener { } private static boolean hasItems(ItemStack stack){ - switch(Core.getVersion()){ - case 8: - return PersonalKitCreator_8.hasItems(stack); - case 9: - return PersonalKitCreator_9.hasItems(stack); - case 10: - return PersonalKitCreator_10.hasItems(stack); - case 12: - return PersonalKitCreator_12.hasItems(stack); - case 14: - return PersonalKitCreator_14.hasItems(stack); - case 15: - default: - return PersonalKitCreator_15.hasItems(stack); - } + return VersionedCallable.call(new VersionedCallable<>(() -> PersonalKitCreator_8.hasItems(stack), 8), + new VersionedCallable<>(() -> PersonalKitCreator_9.hasItems(stack), 9), + new VersionedCallable<>(() -> PersonalKitCreator_10.hasItems(stack), 10), + new VersionedCallable<>(() -> PersonalKitCreator_12.hasItems(stack), 12), + new VersionedCallable<>(() -> PersonalKitCreator_14.hasItems(stack), 14), + new VersionedCallable<>(() -> PersonalKitCreator_15.hasItems(stack), 15)); } private static class InventoryBackup{ diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/record/RecordSystem.java b/FightSystem_Main/src/de/steamwar/fightsystem/record/RecordSystem.java index 84660b4..14771ec 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/record/RecordSystem.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/record/RecordSystem.java @@ -19,7 +19,7 @@ package de.steamwar.fightsystem.record; -import de.steamwar.core.Core; +import de.steamwar.core.VersionedCallable; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; import de.steamwar.fightsystem.states.FightState; @@ -289,15 +289,7 @@ public class RecordSystem { } private static int blockToId(Block block){ - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - return RecordSystem_8.blockToId(block); - case 15: - default: - return RecordSystem_15.blockToId(block); - } + return VersionedCallable.call(new VersionedCallable<>(() -> RecordSystem_8.blockToId(block), 8), + new VersionedCallable<>(() -> RecordSystem_15.blockToId(block), 15)); } } diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/utils/ColorConverter.java b/FightSystem_Main/src/de/steamwar/fightsystem/utils/ColorConverter.java index 80c1d9d..faa10ac 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/utils/ColorConverter.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/utils/ColorConverter.java @@ -19,7 +19,7 @@ package de.steamwar.fightsystem.utils; -import de.steamwar.core.Core; +import de.steamwar.core.VersionedRunnable; import org.bukkit.ChatColor; import org.bukkit.DyeColor; @@ -49,19 +49,8 @@ public class ColorConverter { chat2dye.put(ChatColor.BLACK, DyeColor.BLACK); //Rosa 9pink, Braun 3brown - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - chat2dye.put(ChatColor.GRAY, ColorConverter_8.getSilver()); - break; - case 14: - case 15: - default: - chat2dye.put(ChatColor.GRAY, ColorConverter_14.getSilver()); - break; - } + VersionedRunnable.call(new VersionedRunnable(() -> chat2dye.put(ChatColor.GRAY, ColorConverter_8.getSilver()), 8), + new VersionedRunnable(() -> chat2dye.put(ChatColor.GRAY, ColorConverter_14.getSilver()), 14)); } public static DyeColor chat2dye(ChatColor color){ diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/utils/TechHider.java b/FightSystem_Main/src/de/steamwar/fightsystem/utils/TechHider.java index 354482b..5017333 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/utils/TechHider.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/utils/TechHider.java @@ -30,6 +30,7 @@ import com.comphenix.protocol.wrappers.ChunkCoordIntPair; import com.comphenix.protocol.wrappers.MultiBlockChangeInfo; import com.comphenix.protocol.wrappers.WrappedBlockData; import de.steamwar.core.Core; +import de.steamwar.core.VersionedRunnable; import de.steamwar.core.events.ChunkListener; import de.steamwar.fightsystem.Config; import de.steamwar.fightsystem.FightSystem; @@ -87,20 +88,10 @@ public class TechHider { blockHider(); multiBlockHider(); updateBlockEntity(); - switch(Core.getVersion()){ - case 15: - TechHider_15.start(); - break; - case 14: - TechHider_14.start(); - break; - case 10: - case 9: - case 8: - break; - default: - TechHider_12.start(); - } + VersionedRunnable.call(new VersionedRunnable(() -> {}, 8), + new VersionedRunnable(TechHider_12::start, 12), + new VersionedRunnable(TechHider_14::start, 14), + new VersionedRunnable(TechHider_15::start, 15)); } private static void multiBlockHider(){ diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/utils/WaterRemover.java b/FightSystem_Main/src/de/steamwar/fightsystem/utils/WaterRemover.java index 34f78b6..973ca54 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/utils/WaterRemover.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/utils/WaterRemover.java @@ -19,7 +19,7 @@ package de.steamwar.fightsystem.utils; -import de.steamwar.core.Core; +import de.steamwar.core.VersionedCallable; import de.steamwar.fightsystem.Config; import org.bukkit.block.Block; import org.bukkit.block.BlockFace; @@ -58,31 +58,13 @@ public class WaterRemover { } public static boolean isWater(Block block){ - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - return WaterRemover_8.isWater(block); - case 14: - case 15: - default: - return WaterRemover_14.isWater(block); - } + return VersionedCallable.call(new VersionedCallable<>(() -> WaterRemover_8.isWater(block), 8), + new VersionedCallable<>(() -> WaterRemover_14.isWater(block), 14)); } public static boolean removeWater(Block block){ //checks for water and removes it, if present - switch(Core.getVersion()){ - case 8: - case 9: - case 10: - case 12: - return WaterRemover_8.removeWater(block); - case 14: - case 15: - default: - return WaterRemover_14.removeWater(block); - } + return VersionedCallable.call(new VersionedCallable<>(() -> WaterRemover_8.removeWater(block), 8), + new VersionedCallable<>(() -> WaterRemover_14.removeWater(block), 14)); } } From 042449e59192305855af269ececd29862710f979 Mon Sep 17 00:00:00 2001 From: jojo Date: Mon, 4 Jan 2021 22:26:29 +0100 Subject: [PATCH 2/3] Fix try/catch in FightTeam.pasteSchematic --- .../steamwar/fightsystem/fight/FightTeam.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java b/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java index 05ea9ec..61cdb04 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/fight/FightTeam.java @@ -279,22 +279,30 @@ public class FightTeam implements IFightTeam{ try { VersionedRunnable.call(new VersionedRunnable(() -> { - EditSession e = FightTeam_8.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); - FightTeam_8.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); + try { + EditSession e = FightTeam_8.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); + FightTeam_8.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); + } catch (Schematic.WrongVersionException | IOException | NoClipboardException ex) { + throw new SecurityException("Error pasting arena in schematic", ex); + } }, 8), new VersionedRunnable(() -> { - EditSession e = FightTeam_8.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); - FightTeam_12.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); + try { + EditSession e = FightTeam_8.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); + FightTeam_12.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); + } catch (Schematic.WrongVersionException | IOException | NoClipboardException ex) { + throw new SecurityException("Error pasting arena in schematic", ex); + } }, 12), new VersionedRunnable(() -> { - EditSession e = FightTeam_14.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); - FightTeam_14.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); + try { + EditSession e = FightTeam_14.pasteSchematic(schem, pasteX, pasteY, pasteZ, rotate); + FightTeam_14.replaceTeamColor(e, c, cornerX, cornerY, cornerZ); + } catch (Schematic.WrongVersionException | IOException | NoClipboardException ex) { + throw new SecurityException("Error pasting arena in schematic", ex); + } }, 14)); - } catch (Schematic.WrongVersionException ex) { - broadcast(FightSystem.PREFIX + "§cDie gewählte Schematic kann nicht in dieser Version geladen werden"); - HandlerList.unregisterAll(freezer); - return; - } catch (IOException | NoClipboardException ex) { + } catch (SecurityException securityException) { broadcast(FightSystem.PREFIX + "§cFehler beim Pasten der Schematic"); - throw new SecurityException("Error pasting arena in schematic", ex); + throw securityException; } Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), () -> HandlerList.unregisterAll(freezer), 3); Bukkit.getScheduler().runTaskLater(FightSystem.getPlugin(), this::teleportToSpawn,40); From 00e9e348771b05aa03e317b719755dd8696448cd Mon Sep 17 00:00:00 2001 From: jojo Date: Mon, 4 Jan 2021 22:31:48 +0100 Subject: [PATCH 3/3] Optimize some VersionedRunnables --- .../src/de/steamwar/fightsystem/listener/BasicListener.java | 3 +-- .../src/de/steamwar/fightsystem/utils/TechHider.java | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/listener/BasicListener.java b/FightSystem_Main/src/de/steamwar/fightsystem/listener/BasicListener.java index 6c16d44..467a6e6 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/listener/BasicListener.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/listener/BasicListener.java @@ -56,8 +56,7 @@ public abstract class BasicListener implements Listener, StateDependent { } void setAttackSpeed(Player player){ - VersionedRunnable.call(new VersionedRunnable(() -> {}, 8), - new VersionedRunnable(() -> BasicListener_9.setAttackSpeed(player), 9)); + VersionedRunnable.call(new VersionedRunnable(() -> BasicListener_9.setAttackSpeed(player), 9)); } @Override diff --git a/FightSystem_Main/src/de/steamwar/fightsystem/utils/TechHider.java b/FightSystem_Main/src/de/steamwar/fightsystem/utils/TechHider.java index 5017333..3b9df81 100644 --- a/FightSystem_Main/src/de/steamwar/fightsystem/utils/TechHider.java +++ b/FightSystem_Main/src/de/steamwar/fightsystem/utils/TechHider.java @@ -88,8 +88,7 @@ public class TechHider { blockHider(); multiBlockHider(); updateBlockEntity(); - VersionedRunnable.call(new VersionedRunnable(() -> {}, 8), - new VersionedRunnable(TechHider_12::start, 12), + VersionedRunnable.call(new VersionedRunnable(TechHider_12::start, 12), new VersionedRunnable(TechHider_14::start, 14), new VersionedRunnable(TechHider_15::start, 15)); }