From 00a77b378b5e578c7c3c4acc5ebacc641dbb48e5 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 09:04:18 +0100 Subject: [PATCH] Merge ScriptListener.Script and ScriptListener.ScriptExecutor --- .../bausystem/world/ScriptListener.java | 57 +++++-------------- 1 file changed, 14 insertions(+), 43 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 0768813..205e52b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -39,16 +39,14 @@ import java.util.logging.Level; public class ScriptListener implements Listener { - private Map scriptMap = new HashMap<>(); + private static class ScriptExecutor { - private class Script { - - private BookMeta bookMeta; + private Player player; private List commands = new ArrayList<>(); - private int uses = 0; + private int index = 0; - public Script(BookMeta bookMeta) { - this.bookMeta = bookMeta; + public ScriptExecutor(BookMeta bookMeta, Player player) { + this.player = player; for(String page : bookMeta.getPages()) { for (String command : page.split("\n")) { @@ -56,50 +54,23 @@ public class ScriptListener implements Listener { commands.add(command); } } - } - - public void keep() { - uses++; - } - - public void free() { - uses--; - if (uses < 0) { - scriptMap.remove(bookMeta); - } - } - - } - - private class ScriptExecutor { - - private Player player; - private Script script; - private int index = 0; - - public ScriptExecutor(BookMeta bookMeta, Player player) { - this.player = player; - - if (!scriptMap.containsKey(bookMeta)) { - scriptMap.put(bookMeta, new Script(bookMeta)); - } - script = scriptMap.get(bookMeta); - script.keep(); - } - - public void start() { resume(); } private void resume() { - while (index < script.commands.size()) { - String command = script.commands.get(index++); + if (!player.isOnline()) { + player = null; + commands = null; + return; + } + + while (index < commands.size()) { + String command = commands.get(index++); if (command.startsWith("sleep")) { String sleepTimeString = command.substring(5).trim(); int sleepTime = 1; try { - Integer.parseInt(sleepTimeString); } catch (NumberFormatException e) { // Ignored } @@ -131,7 +102,7 @@ public class ScriptListener implements Listener { event.setCancelled(true); Player player = event.getPlayer(); BookMeta meta = (BookMeta) item.getItemMeta(); - new ScriptExecutor(meta, player).resume(); + new ScriptExecutor(meta, player); } private boolean isNoBook(ItemStack item){