From ee94b516125af4e3c56b84b268406a7168a598cd Mon Sep 17 00:00:00 2001 From: jojo Date: Tue, 15 Dec 2020 20:33:40 +0100 Subject: [PATCH 01/16] Add basic sleep command to ScriptSystem --- .../bausystem/world/ScriptListener.java | 99 ++++++++++++++++--- 1 file changed, 86 insertions(+), 13 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 2be2361..0768813 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -19,6 +19,7 @@ package de.steamwar.bausystem.world; +import de.steamwar.bausystem.BauSystem; import de.steamwar.core.Core; import org.bukkit.Bukkit; import org.bukkit.entity.Player; @@ -30,10 +31,94 @@ import org.bukkit.event.player.PlayerInteractEvent; 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 Map scriptMap = new HashMap<>(); + + private class Script { + + private BookMeta bookMeta; + private List commands = new ArrayList<>(); + private int uses = 0; + + public Script(BookMeta bookMeta) { + this.bookMeta = bookMeta; + + for(String page : bookMeta.getPages()) { + for (String command : page.split("\n")) { + if (command.startsWith("#")) continue; + 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 (command.startsWith("sleep")) { + String sleepTimeString = command.substring(5).trim(); + int sleepTime = 1; + try { + Integer.parseInt(sleepTimeString); + } catch (NumberFormatException e) { + // Ignored + } + Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), this::resume, sleepTime); + break; + } + + PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); + Bukkit.getServer().getPluginManager().callEvent(preprocessEvent); + if(preprocessEvent.isCancelled()) + continue; + + Bukkit.getLogger().log(Level.INFO, player.getName() + " dispatched command: " + command); + Bukkit.getServer().dispatchCommand(player, command); + } + } + + } + @EventHandler public void onLeftClick(PlayerInteractEvent event) { if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) @@ -46,19 +131,7 @@ public class ScriptListener implements Listener { event.setCancelled(true); Player player = event.getPlayer(); BookMeta meta = (BookMeta) item.getItemMeta(); - for(String page : meta.getPages()){ - for(String command : page.split("\n")){ - if (command.startsWith("#")) continue; - - PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); - Bukkit.getServer().getPluginManager().callEvent(preprocessEvent); - if(preprocessEvent.isCancelled()) - continue; - - Bukkit.getLogger().log(Level.INFO, player.getName() + " dispatched command: " + command); - Bukkit.getServer().dispatchCommand(player, command); - } - } + new ScriptExecutor(meta, player).resume(); } private boolean isNoBook(ItemStack item){ From 00a77b378b5e578c7c3c4acc5ebacc641dbb48e5 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 09:04:18 +0100 Subject: [PATCH 02/16] 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){ From 5b73ec53055f4fb70a4666350891e66bc4c8d69d Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 09:04:31 +0100 Subject: [PATCH 03/16] Fix ignored Sleep Time --- .../src/de/steamwar/bausystem/world/ScriptListener.java | 1 + 1 file changed, 1 insertion(+) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 205e52b..f490d35 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -71,6 +71,7 @@ public class ScriptListener implements Listener { String sleepTimeString = command.substring(5).trim(); int sleepTime = 1; try { + sleepTime = Integer.parseInt(sleepTimeString); } catch (NumberFormatException e) { // Ignored } From 15570cab7c6e98987a55edc3d3829e1a0c8529bf Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 09:08:27 +0100 Subject: [PATCH 04/16] Make Sleep 'CaSeInDePeNdEnCe' Make Sleep Command like Bukkit Command (String.split(" ")) --- .../bausystem/world/ScriptListener.java | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index f490d35..552e068 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -67,16 +67,20 @@ public class ScriptListener implements Listener { while (index < commands.size()) { String command = commands.get(index++); - if (command.startsWith("sleep")) { - String sleepTimeString = command.substring(5).trim(); - int sleepTime = 1; - try { - sleepTime = Integer.parseInt(sleepTimeString); - } catch (NumberFormatException e) { - // Ignored + 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 + } + } + Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), this::resume, sleepTime); + break; } - Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), this::resume, sleepTime); - break; } PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); From 93003de08db8b7c9dc0d9a701ca513a1514a9371 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 09:26:17 +0100 Subject: [PATCH 05/16] 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); - } - } - } From 203519b15f62a3f76ebf38cb449255ce6f18584e Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 09:36:28 +0100 Subject: [PATCH 06/16] Add messages for player --- .../de/steamwar/bausystem/world/ScriptListener.java | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 55cc052..38756a3 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -32,6 +32,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.logging.Level; @@ -96,7 +97,10 @@ public class ScriptListener implements Listener { if (strings.length > 1) { try { sleepTime = Integer.parseInt(strings[1]); - if (sleepTime <= 0) sleepTime = 1; + if (sleepTime <= 0) { + player.sendMessage(scriptPrefix + "Sleep kleiner gleich 0, default 1 GameTick"); + sleepTime = 1; + } } catch (NumberFormatException e) { player.sendMessage(scriptPrefix + "Sleep ohne Zahl, default 1 GameTick"); } @@ -109,8 +113,11 @@ public class ScriptListener implements Listener { PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); Bukkit.getServer().getPluginManager().callEvent(preprocessEvent); - if(preprocessEvent.isCancelled()) + if (preprocessEvent.isCancelled()) { + player.sendMessage(scriptPrefix + "Befehl konnte nicht ausgeführt werden:"); + player.sendMessage(scriptPrefix + command); continue; + } Bukkit.getLogger().log(Level.INFO, player.getName() + " dispatched command: " + command); Bukkit.getServer().dispatchCommand(player, command); From 4a9a317428531f589517ea32d7d34f5d5d2a372d Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 10:00:29 +0100 Subject: [PATCH 07/16] Simplify sleep Command Simplify further command additions --- .../bausystem/world/ScriptListener.java | 69 +++++++++++++------ 1 file changed, 47 insertions(+), 22 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 38756a3..2c20792 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -32,7 +32,6 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import java.util.logging.Level; @@ -40,7 +39,35 @@ public class ScriptListener implements Listener { private static final String scriptPrefix = "§eScriptSystem§8» §7"; - @EventHandler + private static final List scriptCommands = new ArrayList<>(); + + static { + scriptCommands.add(new ScriptCommand() { + @Override + public String command() { + return "sleep"; + } + + @Override + public boolean execute(ScriptExecutor scriptExecutor, String[] args) { + int sleepTime = 1; + if (args.length > 0) { + try { + sleepTime = Integer.parseInt(args[0]); + if (sleepTime <= 0) { + scriptExecutor.player.sendMessage(scriptPrefix + "Sleep kleiner gleich 0, default 1 GameTick"); + sleepTime = 1; + } + } catch (NumberFormatException e) { + scriptExecutor.player.sendMessage(scriptPrefix + "Sleep ohne Zahl, default 1 GameTick"); + } + } + Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime); + return false; + } + }); + } + public void onLeftClick(PlayerInteractEvent event) { if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) return; @@ -75,9 +102,11 @@ public class ScriptListener implements Listener { for(String page : bookMeta.getPages()) { for (String command : page.split("\n")) { if (command.startsWith("#")) continue; + if (command.trim().isEmpty()) continue; commands.add(command); } } + if (commands.isEmpty()) return; resume(); } @@ -89,24 +118,11 @@ public class ScriptListener implements Listener { while (index < commands.size()) { String command = commands.get(index++); - 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) { - player.sendMessage(scriptPrefix + "Sleep kleiner gleich 0, default 1 GameTick"); - sleepTime = 1; - } - } catch (NumberFormatException e) { - player.sendMessage(scriptPrefix + "Sleep ohne Zahl, default 1 GameTick"); - } - } - Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), this::resume, sleepTime); - break; + for (ScriptCommand scriptCommand : scriptCommands) { + if (command.toLowerCase().startsWith(scriptCommand.command())) { + String[] args = command.substring(scriptCommand.command().length() + 1).split(" "); + if (!scriptCommand.execute(this, args)) { + return; } } } @@ -114,8 +130,6 @@ public class ScriptListener implements Listener { PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); Bukkit.getServer().getPluginManager().callEvent(preprocessEvent); if (preprocessEvent.isCancelled()) { - player.sendMessage(scriptPrefix + "Befehl konnte nicht ausgeführt werden:"); - player.sendMessage(scriptPrefix + command); continue; } @@ -126,4 +140,15 @@ public class ScriptListener implements Listener { } + private interface ScriptCommand { + + String command(); + + /** + * Should return {@code true} if execution should not stop, {@code false} otherwise. + */ + boolean execute(ScriptExecutor scriptExecutor, String[] args); + + } + } From 552649f88446f9e439edf023e1978108d414e8f1 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 10:31:02 +0100 Subject: [PATCH 08/16] Fix RightClickAir and LeftClickAir --- .../steamwar/bausystem/world/ScriptListener.java | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 2c20792..9b77e66 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -24,6 +24,7 @@ import de.steamwar.core.Core; import org.bukkit.Bukkit; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerCommandPreprocessEvent; @@ -32,7 +33,9 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; import java.util.ArrayList; +import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.logging.Level; public class ScriptListener implements Listener { @@ -68,9 +71,19 @@ public class ScriptListener implements Listener { }); } + private Set playerSet = new HashSet<>(); + + @EventHandler(priority = EventPriority.HIGH) public void onLeftClick(PlayerInteractEvent event) { - if(event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) + if (event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) { + if (event.getAction() == Action.RIGHT_CLICK_AIR) { + playerSet.add(event.getPlayer()); + } return; + } + if (playerSet.remove(event.getPlayer())) { + return; + } ItemStack item = event.getItem(); if(item == null || isNoBook(item) || item.getItemMeta() == null) From 45eea9b26ef1accf6991fd91e6998ad6540a2ec4 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 11:09:10 +0100 Subject: [PATCH 09/16] Remove overhead from sleep Command --- .../bausystem/world/ScriptListener.java | 64 ++++++------------- 1 file changed, 20 insertions(+), 44 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 9b77e66..2746934 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -42,35 +42,6 @@ public class ScriptListener implements Listener { private static final String scriptPrefix = "§eScriptSystem§8» §7"; - private static final List scriptCommands = new ArrayList<>(); - - static { - scriptCommands.add(new ScriptCommand() { - @Override - public String command() { - return "sleep"; - } - - @Override - public boolean execute(ScriptExecutor scriptExecutor, String[] args) { - int sleepTime = 1; - if (args.length > 0) { - try { - sleepTime = Integer.parseInt(args[0]); - if (sleepTime <= 0) { - scriptExecutor.player.sendMessage(scriptPrefix + "Sleep kleiner gleich 0, default 1 GameTick"); - sleepTime = 1; - } - } catch (NumberFormatException e) { - scriptExecutor.player.sendMessage(scriptPrefix + "Sleep ohne Zahl, default 1 GameTick"); - } - } - Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime); - return false; - } - }); - } - private Set playerSet = new HashSet<>(); @EventHandler(priority = EventPriority.HIGH) @@ -131,13 +102,9 @@ public class ScriptListener implements Listener { while (index < commands.size()) { String command = commands.get(index++); - for (ScriptCommand scriptCommand : scriptCommands) { - if (command.toLowerCase().startsWith(scriptCommand.command())) { - String[] args = command.substring(scriptCommand.command().length() + 1).split(" "); - if (!scriptCommand.execute(this, args)) { - return; - } - } + if (command.toLowerCase().startsWith("sleep")) { + ScriptListener.sleepCommand(this, generateArgumentArray("sleep", command)); + return; } PlayerCommandPreprocessEvent preprocessEvent = new PlayerCommandPreprocessEvent(player, "/" + command); @@ -153,15 +120,24 @@ public class ScriptListener implements Listener { } - private interface ScriptCommand { - - String command(); - - /** - * Should return {@code true} if execution should not stop, {@code false} otherwise. - */ - boolean execute(ScriptExecutor scriptExecutor, String[] args); + private static String[] generateArgumentArray(String command, String fullCommand) { + return fullCommand.substring(command.length() + 1).split(" "); + } + private static void sleepCommand(ScriptExecutor scriptExecutor, String[] args) { + int sleepTime = 1; + if (args.length > 0) { + try { + sleepTime = Integer.parseInt(args[0]); + if (sleepTime <= 0) { + scriptExecutor.player.sendMessage(scriptPrefix + "Sleep kleiner gleich 0, default 1 GameTick"); + sleepTime = 1; + } + } catch (NumberFormatException e) { + scriptExecutor.player.sendMessage(scriptPrefix + "Sleep ohne Zahl, default 1 GameTick"); + } + } + Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime); } } From 3cdfae7c86ba9b269a0d1108655de712f0713a26 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 11:31:27 +0100 Subject: [PATCH 10/16] Simplify Error messages for user --- .../bausystem/world/ScriptListener.java | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 2746934..6ffb161 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -44,8 +44,18 @@ public class ScriptListener implements Listener { private Set playerSet = new HashSet<>(); + public ScriptListener() { + Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> { + playerSet.clear(); + }, 1, 1); + } + @EventHandler(priority = EventPriority.HIGH) public void onLeftClick(PlayerInteractEvent event) { + ItemStack item = event.getItem(); + if(item == null || isNoBook(item) || item.getItemMeta() == null) + return; + if (event.getAction() != Action.LEFT_CLICK_AIR && event.getAction() != Action.LEFT_CLICK_BLOCK) { if (event.getAction() == Action.RIGHT_CLICK_AIR) { playerSet.add(event.getPlayer()); @@ -56,10 +66,6 @@ public class ScriptListener implements Listener { return; } - ItemStack item = event.getItem(); - if(item == null || isNoBook(item) || item.getItemMeta() == null) - return; - event.setCancelled(true); new ScriptExecutor((BookMeta) item.getItemMeta(), event.getPlayer()); } @@ -85,8 +91,7 @@ public class ScriptListener implements Listener { for(String page : bookMeta.getPages()) { for (String command : page.split("\n")) { - if (command.startsWith("#")) continue; - if (command.trim().isEmpty()) continue; + if (command.startsWith("#") || command.trim().isEmpty()) continue; commands.add(command); } } @@ -121,7 +126,7 @@ public class ScriptListener implements Listener { } private static String[] generateArgumentArray(String command, String fullCommand) { - return fullCommand.substring(command.length() + 1).split(" "); + return fullCommand.substring(command.length()).trim().split(" "); } private static void sleepCommand(ScriptExecutor scriptExecutor, String[] args) { @@ -130,11 +135,11 @@ public class ScriptListener implements Listener { try { sleepTime = Integer.parseInt(args[0]); if (sleepTime <= 0) { - scriptExecutor.player.sendMessage(scriptPrefix + "Sleep kleiner gleich 0, default 1 GameTick"); + scriptExecutor.player.sendMessage(scriptPrefix + "Eine Sleep zeit von kleiner gleich 0 ist nicht erlaubt. Der default 1 Tick wird verwendet."); sleepTime = 1; } } catch (NumberFormatException e) { - scriptExecutor.player.sendMessage(scriptPrefix + "Sleep ohne Zahl, default 1 GameTick"); + scriptExecutor.player.sendMessage(scriptPrefix + "Eine Sleep zeit sollte keine Buchstaben oder sonstige Zeiten verwenden. Der default 1 Tick wird verwendet."); } } Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime); From ee701cce0d254713f2d99299123c17e531adaade Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 11:32:10 +0100 Subject: [PATCH 11/16] Change ScripSystem prefix to BauSystem prefix --- .../src/de/steamwar/bausystem/world/ScriptListener.java | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 6ffb161..6e7c0de 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -40,8 +40,6 @@ import java.util.logging.Level; public class ScriptListener implements Listener { - private static final String scriptPrefix = "§eScriptSystem§8» §7"; - private Set playerSet = new HashSet<>(); public ScriptListener() { @@ -135,11 +133,11 @@ public class ScriptListener implements Listener { try { sleepTime = Integer.parseInt(args[0]); if (sleepTime <= 0) { - scriptExecutor.player.sendMessage(scriptPrefix + "Eine Sleep zeit von kleiner gleich 0 ist nicht erlaubt. Der default 1 Tick wird verwendet."); + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Eine Sleep zeit von kleiner gleich 0 ist nicht erlaubt. Der default 1 Tick wird verwendet."); sleepTime = 1; } } catch (NumberFormatException e) { - scriptExecutor.player.sendMessage(scriptPrefix + "Eine Sleep zeit sollte keine Buchstaben oder sonstige Zeiten verwenden. Der default 1 Tick wird verwendet."); + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Eine Sleep zeit sollte keine Buchstaben oder sonstige Zeiten verwenden. Der default 1 Tick wird verwendet."); } } Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime); From e21fd6acfba8aa903a7898be16bff9b962f404f9 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 11:40:12 +0100 Subject: [PATCH 12/16] Update chat messages --- .../src/de/steamwar/bausystem/world/ScriptListener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 6e7c0de..bc23c83 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -133,11 +133,11 @@ public class ScriptListener implements Listener { try { sleepTime = Integer.parseInt(args[0]); if (sleepTime <= 0) { - scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Eine Sleep zeit von kleiner gleich 0 ist nicht erlaubt. Der default 1 Tick wird verwendet."); + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Die sleep Zeit sollte eine Zahl großer 0 sein. Als default wird 1 Tick verwendet."); sleepTime = 1; } } catch (NumberFormatException e) { - scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Eine Sleep zeit sollte keine Buchstaben oder sonstige Zeiten verwenden. Der default 1 Tick wird verwendet."); + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Die sleep Zeit sollte nur aus Zahlen bestehen. Als default wird 1 Tick verwendet."); } } Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime); From d24d4ee49f223c38fa1e882b6062b3aa7aa394e2 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 11:49:49 +0100 Subject: [PATCH 13/16] Update chat messages --- .../src/de/steamwar/bausystem/world/ScriptListener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index bc23c83..c326eab 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -133,11 +133,11 @@ public class ScriptListener implements Listener { try { sleepTime = Integer.parseInt(args[0]); if (sleepTime <= 0) { - scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Die sleep Zeit sollte eine Zahl großer 0 sein. Als default wird 1 Tick verwendet."); + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Die Zeit sollte eine Zahl großer 0 sein."); sleepTime = 1; } } catch (NumberFormatException e) { - scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Die sleep Zeit sollte nur aus Zahlen bestehen. Als default wird 1 Tick verwendet."); + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Die Zeit sollte nur aus Zahlen bestehen."); } } Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime); From b1ada402e333c8772473e37ef9543fe38a8d76f7 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 11:58:44 +0100 Subject: [PATCH 14/16] Add message color codes --- .../src/de/steamwar/bausystem/world/ScriptListener.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index c326eab..54f5e95 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -133,11 +133,11 @@ public class ScriptListener implements Listener { try { sleepTime = Integer.parseInt(args[0]); if (sleepTime <= 0) { - scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Die Zeit sollte eine Zahl großer 0 sein."); + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cDie Zeit muss eine Zahl großer 0 sein."); sleepTime = 1; } } catch (NumberFormatException e) { - scriptExecutor.player.sendMessage(BauSystem.PREFIX + "Die Zeit sollte nur aus Zahlen bestehen."); + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cDie Zeit muss nur aus Zahlen bestehen."); } } Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime); From bde263de5072c053d0d313096e6aa1b380bb9567 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 12:00:17 +0100 Subject: [PATCH 15/16] Fix TPS Bugs with ScriptListener --- .../src/de/steamwar/bausystem/world/ScriptListener.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 54f5e95..7ca9c4e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -43,9 +43,7 @@ public class ScriptListener implements Listener { private Set playerSet = new HashSet<>(); public ScriptListener() { - Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), () -> { - playerSet.clear(); - }, 1, 1); + Bukkit.getScheduler().runTaskTimer(BauSystem.getPlugin(), playerSet::clear, 5, 5); } @EventHandler(priority = EventPriority.HIGH) From f4aaefeb2b0edf449b9073cc8d9f1a01301e1be6 Mon Sep 17 00:00:00 2001 From: jojo Date: Wed, 16 Dec 2020 12:05:04 +0100 Subject: [PATCH 16/16] Fix message --- .../src/de/steamwar/bausystem/world/ScriptListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java index 7ca9c4e..a4dacb6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/world/ScriptListener.java @@ -135,7 +135,7 @@ public class ScriptListener implements Listener { sleepTime = 1; } } catch (NumberFormatException e) { - scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cDie Zeit muss nur aus Zahlen bestehen."); + scriptExecutor.player.sendMessage(BauSystem.PREFIX + "§cDie Zeit darf nur aus Zahlen bestehen."); } } Bukkit.getScheduler().runTaskLater(BauSystem.getPlugin(), scriptExecutor::resume, sleepTime);