From 93003de08db8b7c9dc0d9a701ca513a1514a9371 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 09:26:17 +0100 Subject: [PATCH] Make ScriptListener.ScriptExecutor.player final Make ScriptListener.ScriptExecutor.commands final Fixing commands without spaces Fixing negative and zero sleepTime Add Error message on sleep without number Remove throw-away-references --- .../bausystem/world/ScriptListener.java | 83 +++++++++---------- 1 file changed, 41 insertions(+), 42 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 552e068..55cc052 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -32,17 +32,40 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.logging.Level; public class ScriptListener implements Listener { + private static final String scriptPrefix = "§eScriptSystem§8» §7"; + + @EventHandler + public void onLeftClick(PlayerInteractEvent event) { + if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) + return; + + ItemStack item = event.getItem(); + if(item == null || isNoBook(item) || item.getItemMeta() == null) + return; + + event.setCancelled(true); + new ScriptExecutor((BookMeta) item.getItemMeta(), event.getPlayer()); + } + + private boolean isNoBook(ItemStack item){ + switch(Core.getVersion()){ + case 12: + return ScriptListener_12.isNoBook(item); + case 15: + default: + return ScriptListener_15.isNoBook(item); + } + } + private static class ScriptExecutor { - private Player player; - private List commands = new ArrayList<>(); + private final Player player; + private final List commands = new ArrayList<>(); private int index = 0; public ScriptExecutor(BookMeta bookMeta, Player player) { @@ -59,27 +82,28 @@ public class ScriptListener implements Listener { private void resume() { if (!player.isOnline()) { - player = null; - commands = null; return; } while (index < commands.size()) { String command = commands.get(index++); - String[] strings = command.split(" "); - if (strings.length > 0) { - if (strings[0].equalsIgnoreCase("sleep")) { - int sleepTime = 1; - if (strings.length > 1) { - try { - sleepTime = Integer.parseInt(strings[1]); - } catch (NumberFormatException e) { - // Ignored + if (command.contains(" ")) { + String[] strings = command.split(" "); + if (strings.length > 0) { + if (strings[0].equalsIgnoreCase("sleep")) { + int sleepTime = 1; + if (strings.length > 1) { + try { + sleepTime = Integer.parseInt(strings[1]); + if (sleepTime <= 0) sleepTime = 1; + } catch (NumberFormatException e) { + player.sendMessage(scriptPrefix + "Sleep ohne Zahl, default 1 GameTick"); + } } + Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), this::resume, sleepTime); + break; } - Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), this::resume, sleepTime); - break; } } @@ -95,29 +119,4 @@ public class ScriptListener implements Listener { } - @EventHandler - public void onLeftClick(PlayerInteractEvent event) { - if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) - return; - - ItemStack item = event.getItem(); - if(item == null || isNoBook(item) || item.getItemMeta() == null) - return; - - event.setCancelled(true); - Player player = event.getPlayer(); - BookMeta meta = (BookMeta) item.getItemMeta(); - new ScriptExecutor(meta, player); - } - - private boolean isNoBook(ItemStack item){ - switch(Core.getVersion()){ - case 12: - return ScriptListener_12.isNoBook(item); - case 15: - default: - return ScriptListener_15.isNoBook(item); - } - } - }