diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java index 57df508f..05205cc3 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java @@ -19,9 +19,12 @@ package de.steamwar.bausystem.features.script; +import de.steamwar.bausystem.features.script.variables.Value; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import de.steamwar.core.VersionedCallable; +import lombok.AccessLevel; +import lombok.RequiredArgsConstructor; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -32,13 +35,44 @@ import org.bukkit.event.player.PlayerQuitEvent; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; -import java.util.HashMap; -import java.util.Map; +import java.util.*; @Linked(LinkageType.LISTENER) public class CustomCommandListener implements Listener { - private Map> playerMap = new HashMap<>(); + @RequiredArgsConstructor(access = AccessLevel.PRIVATE) + private static class CustomCommand { + private final BookMeta bookMeta; + private final String[] args; + + public boolean execute(PlayerCommandPreprocessEvent e) { + String[] command = e.getMessage().split(" "); + if (args.length != command.length) { + return false; + } + + if (!args[0].equals(command[0])) { + return false; + } + + Map arguments = new HashMap<>(); + for (int i = 1; i < args.length; i++) { + String current = args[i]; + if (current.startsWith("<") && current.endsWith(">")) { + arguments.put(current.substring(1, current.length() - 1), new Value.StringValue(command[i])); + } else { + if (!current.equals(command[i])) { + return false; + } + } + } + e.setCancelled(true); + new ScriptExecutor(bookMeta, e.getPlayer(), arguments); + return true; + } + } + + private Map> playerMap = new HashMap<>(); private void updateInventory(Player p) { playerMap.remove(p); @@ -55,10 +89,10 @@ public class CustomCommandListener implements Listener { continue; } String s = bookMeta.getPage(1).split("\n")[0]; - if (!s.startsWith("#!CMD ")) { + if (!s.startsWith("#!CMD /")) { continue; } - playerMap.computeIfAbsent(p, player -> new HashMap<>()).put(s.substring(6), bookMeta); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomCommand(bookMeta, s.substring(6).split(" "))); } } @@ -86,10 +120,16 @@ public class CustomCommandListener implements Listener { return; } - Map bookMetaMap = playerMap.get(e.getPlayer()); - if (bookMetaMap == null) { + List customCommands = playerMap.get(e.getPlayer()); + if (customCommands == null) { return; } + + customCommands.stream().map(customCommand -> customCommand.execute(e)).filter(b -> !b).findFirst(); + + + /*String[] command = e.getMessage().split(" "); + BookMeta bookMeta = bookMetaMap.get(e.getMessage()); if (bookMeta == null) { return; @@ -99,7 +139,7 @@ public class CustomCommandListener implements Listener { new ScriptExecutor(bookMeta, e.getPlayer()); } catch (StackOverflowError exception) { // Ignored - } + }*/ } private boolean isNoBook(ItemStack item) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java index e23d1891..dade614d 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java @@ -39,6 +39,10 @@ public final class ScriptExecutor { private int index = 0; public ScriptExecutor(BookMeta bookMeta, Player player) { + this(bookMeta, player, new HashMap<>()); + } + + public ScriptExecutor(BookMeta bookMeta, Player player, Map localVariables) { this.player = player; globalVariables = ScriptListener.getGlobalContext(player); @@ -62,6 +66,7 @@ public final class ScriptExecutor { } } if (commands.isEmpty()) return; + localVariables.forEach(this.localVariables::putValue); resume(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java index 13b3a23b..58289a9b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/simulator/TNTSimulatorListener.java @@ -35,7 +35,6 @@ import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerItemHeldEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.event.player.PlayerQuitEvent; -import org.bukkit.inventory.Inventory; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.util.BoundingBox;