From ac7b0d929eeb6c0dd07ca4f441487e3e27659be5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Sep 2021 21:44:23 +0200 Subject: [PATCH 01/11] Add CustomScript Signed-off-by: yoyosource --- .../features/script/CustomScript.java | 211 ++++++++++++++++++ ...istener.java => CustomScriptListener.java} | 18 +- .../features/script/ScriptCommand.java | 4 +- .../features/script/ScriptExecutor.java | 4 + 4 files changed, 228 insertions(+), 9 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java rename BauSystem_Main/src/de/steamwar/bausystem/features/script/{CustomCommandListener.java => CustomScriptListener.java} (96%) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java new file mode 100644 index 00000000..6c9eb5f7 --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -0,0 +1,211 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.script; + +import de.steamwar.bausystem.features.script.variables.Value; +import de.steamwar.inventory.SWItem; +import lombok.RequiredArgsConstructor; +import lombok.experimental.UtilityClass; +import org.bukkit.Material; +import org.bukkit.event.player.PlayerCommandPreprocessEvent; +import org.bukkit.event.player.PlayerEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.meta.BookMeta; +import yapion.hierarchy.types.YAPIONArray; +import yapion.hierarchy.types.YAPIONMap; + +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +@UtilityClass +public class CustomScript { + + public interface Script { + } + + public interface MenuScript { + void toYAPION(YAPIONMap yapionMap); + ItemStack toItem(); + } + + public interface CustomEvent extends Script { + String eventName(); + boolean execute(PlayerEvent e); + } + + @RequiredArgsConstructor + public class InventoryEvent implements CustomEvent { + private final BookMeta bookMeta; + private final String eventName; + + @Override + public String eventName() { + return eventName; + } + + @Override + public boolean execute(PlayerEvent e) { + new ScriptExecutor(bookMeta, e.getPlayer()); + return true; + } + } + + @RequiredArgsConstructor + public class MenuEvent implements CustomEvent, MenuScript { + private final List pages; + private final String eventName; + + @Override + public String eventName() { + return eventName; + } + + @Override + public boolean execute(PlayerEvent e) { + new ScriptExecutor(pages, e.getPlayer()); + return false; + } + + @Override + public void toYAPION(YAPIONMap yapionMap) { + YAPIONArray yapionArray = new YAPIONArray(); + pages.forEach(yapionArray::add); + yapionMap.put(eventName, yapionArray); + } + + @Override + public ItemStack toItem() { + SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Event§8: §e" + eventName); + BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); + bookMeta.setPages(pages.toArray(new String[0])); + swItem.setItemMeta(bookMeta); + return swItem.getItemStack(); + } + } + + public interface CustomCommand extends Script { + default Map check(String[] args, String[] command) { + if (args.length < command.length) { + return null; + } + + if (!args[0].equals(command[0])) { + return null; + } + + Map arguments = new HashMap<>(); + if (!check(arguments, args, command, 0, 0)) { + return null; + } + return arguments; + } + + default boolean check(Map arguments, String[] args, String[] command, int argsIndex, int commandIndex) { + if (command.length <= commandIndex) { + for (int i = argsIndex; i < args.length; i++) { + if (!(args[i].startsWith("(") && args[i].endsWith(")"))) { + return false; + } + } + return true; + } + if (args.length <= argsIndex) return true; + + String currentArg = args[argsIndex]; + String currentCommand = command[commandIndex]; + + if (currentArg.startsWith("<") && currentArg.endsWith(">")) { + arguments.put(trim(currentArg, 1), new Value.StringValue(currentCommand)); + return check(arguments, args, command, argsIndex + 1, commandIndex + 1); + } else if (currentArg.startsWith("(<") && currentArg.endsWith(">)")) { + arguments.put(trim(currentArg, 2), new Value.StringValue(currentCommand)); + return check(arguments, args, command, argsIndex + 1, commandIndex + 1); + } else if (currentArg.startsWith("(") && currentArg.endsWith(")")) { + if (!trim(currentArg, 1).equals(currentCommand)) { + arguments.put(trim(currentArg, 1), new Value.BooleanValue(false)); + return check(arguments, args, command, argsIndex + 1, commandIndex); + } else { + arguments.put(trim(currentArg, 1), new Value.BooleanValue(true)); + return check(arguments, args, command, argsIndex + 1, commandIndex + 1); + } + } else { + if (!currentArg.equals(currentCommand)) return false; + return check(arguments, args, command, argsIndex + 1, commandIndex + 1); + } + } + + default String trim(String s, int count) { + return s.substring(count, s.length() - count); + } + + boolean execute(String[] command, PlayerCommandPreprocessEvent e); + } + + @RequiredArgsConstructor + public class InventoryCommand implements CustomCommand { + private final BookMeta bookMeta; + private final String[] args; + + public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { + Map arguments = check(args, command); + if (arguments == null) { + return false; + } + + e.setCancelled(true); + new ScriptExecutor(bookMeta, e.getPlayer(), arguments); + return true; + } + } + + @RequiredArgsConstructor + public class MenuCommand implements CustomCommand, MenuScript { + private final List pages; + private final String[] args; + + public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { + Map arguments = check(args, command); + if (arguments == null) { + return false; + } + + e.setCancelled(true); + new ScriptExecutor(pages, e.getPlayer(), arguments); + return true; + } + + @Override + public void toYAPION(YAPIONMap yapionMap) { + YAPIONArray yapionArray = new YAPIONArray(); + pages.forEach(yapionArray::add); + yapionMap.put(String.join(" ", args), yapionArray); + } + + @Override + public ItemStack toItem() { + SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§8/§e" + String.join(" ", args)); + BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); + bookMeta.setPages(pages.toArray(new String[0])); + swItem.setItemMeta(bookMeta); + return swItem.getItemStack(); + } + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java similarity index 96% rename from BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java rename to BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index 4945a789..5b98bc44 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomCommandListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -51,7 +51,7 @@ import java.util.*; import java.util.stream.Collectors; @Linked(LinkageType.LISTENER) -public class CustomCommandListener implements Listener { +public class CustomScriptListener implements Listener { private interface CustomCommand { default Map check(String[] args, String[] command) { @@ -191,15 +191,19 @@ public class CustomCommandListener implements Listener { Player p = event.getPlayer(); updateInventory(p); - String s = UserConfig.getConfig(p.getUniqueId(), "bausystem-commands"); + String s = UserConfig.getConfig(p.getUniqueId(), "bausystem-scripts"); YAPIONObject yapionObject; if (s == null) { yapionObject = new YAPIONObject(); } else { yapionObject = YAPIONParser.parse(s); + if (yapionObject.containsKey("")) { + yapionObject.add("commands", yapionObject.getMap("")); + yapionObject.remove(""); + } } - yapionObject.getYAPIONMapOrSetDefault("", new YAPIONMap()).forEach((key, value) -> { + yapionObject.getYAPIONMapOrSetDefault("commands", new YAPIONMap()).forEach((key, value) -> { String[] command = ((YAPIONValue) key).get().split(" "); List pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList()); playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new MenuCommand(pages, command)); @@ -216,7 +220,7 @@ public class CustomCommandListener implements Listener { if (!playerMap.containsKey(p)) return new YAPIONObject(); YAPIONObject yapionObject = new YAPIONObject(); YAPIONMap yapionMap = new YAPIONMap(); - yapionObject.add("", yapionMap); + yapionObject.add("commands", yapionMap); playerMap.get(p).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> { menuCommand.toYAPION(yapionMap); }); @@ -225,14 +229,14 @@ public class CustomCommandListener implements Listener { private boolean save(Player p) { if (!playerMap.containsKey(p)) { - UserConfig.removePlayerConfig(p.getUniqueId(), "bausystem-commands"); + UserConfig.removePlayerConfig(p.getUniqueId(), "bausystem-scripts"); return true; } YAPIONObject yapionObject = output(p); if (yapionObject.toYAPION(new LengthOutput()).getLength() > 64 * 1024) { return false; } - UserConfig.updatePlayerConfig(p.getUniqueId(), "bausystem-commands", yapionObject.toYAPION(new StringOutput()).getResult()); + UserConfig.updatePlayerConfig(p.getUniqueId(), "bausystem-scripts", yapionObject.toYAPION(new StringOutput()).getResult()); return true; } @@ -331,7 +335,7 @@ public class CustomCommandListener implements Listener { p.closeInventory(); SWUtils.giveItemToPlayer(p, p.getItemOnCursor()); save(p); - p.sendMessage("§cScript CMD-Buch Limit erreicht"); + p.sendMessage("§cScript-Buch Limit erreicht"); return; } p.setItemOnCursor(null); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index f2828e72..156be531 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -24,7 +24,7 @@ public class ScriptCommand extends SWCommand { } @LinkedInstance - private CustomCommandListener customCommandListener = null; + private CustomScriptListener customScriptListener = null; private static List> swItems = new ArrayList<>(); @@ -134,6 +134,6 @@ public class ScriptCommand extends SWCommand { @Register({"menu"}) public void menuGUICommand(Player p) { - customCommandListener.openCommandsMenu(p); + customScriptListener.openCommandsMenu(p); } } 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 46529054..d2da1bb6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptExecutor.java @@ -42,6 +42,10 @@ public final class ScriptExecutor { this(bookMeta, player, new HashMap<>()); } + public ScriptExecutor(List pages, Player player) { + this(pages, player, new HashMap<>()); + } + public ScriptExecutor(BookMeta bookMeta, Player player, Map localVariables) { this.player = player; globalVariables = ScriptListener.getGlobalContext(player); -- 2.39.5 From 966ed270db7efcd46121cfd518f5a1e32af2af07 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Sep 2021 21:47:00 +0200 Subject: [PATCH 02/11] Implement update logic from old system Signed-off-by: yoyosource --- .../bausystem/features/script/CustomScriptListener.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index 5b98bc44..82333c5a 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -192,6 +192,10 @@ public class CustomScriptListener implements Listener { updateInventory(p); String s = UserConfig.getConfig(p.getUniqueId(), "bausystem-scripts"); + if (s == null) { + s = UserConfig.getConfig(p.getUniqueId(), "bausystem-commands"); + UserConfig.removePlayerConfig(p.getUniqueId(), "bausystem-commands"); + } YAPIONObject yapionObject; if (s == null) { yapionObject = new YAPIONObject(); -- 2.39.5 From 1b22f9b895d3f72385c2569eec4efdb42ed08980 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Sep 2021 22:01:37 +0200 Subject: [PATCH 03/11] Implement new logic for CustomScript Signed-off-by: yoyosource --- .../features/script/CustomScript.java | 27 ++- .../features/script/CustomScriptListener.java | 206 +++++------------- .../features/script/ScriptCommand.java | 2 +- 3 files changed, 68 insertions(+), 167 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java index 6c9eb5f7..f67c89e1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -26,7 +26,6 @@ import lombok.experimental.UtilityClass; import org.bukkit.Material; import org.bukkit.event.player.PlayerCommandPreprocessEvent; import org.bukkit.event.player.PlayerEvent; -import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; import yapion.hierarchy.types.YAPIONArray; import yapion.hierarchy.types.YAPIONMap; @@ -43,7 +42,7 @@ public class CustomScript { public interface MenuScript { void toYAPION(YAPIONMap yapionMap); - ItemStack toItem(); + SWItem toItem(); } public interface CustomEvent extends Script { @@ -53,8 +52,8 @@ public class CustomScript { @RequiredArgsConstructor public class InventoryEvent implements CustomEvent { - private final BookMeta bookMeta; - private final String eventName; + public final BookMeta bookMeta; + public final String eventName; @Override public String eventName() { @@ -70,8 +69,8 @@ public class CustomScript { @RequiredArgsConstructor public class MenuEvent implements CustomEvent, MenuScript { - private final List pages; - private final String eventName; + public final List pages; + public final String eventName; @Override public String eventName() { @@ -92,12 +91,12 @@ public class CustomScript { } @Override - public ItemStack toItem() { + public SWItem toItem() { SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Event§8: §e" + eventName); BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); bookMeta.setPages(pages.toArray(new String[0])); swItem.setItemMeta(bookMeta); - return swItem.getItemStack(); + return swItem; } } @@ -161,8 +160,8 @@ public class CustomScript { @RequiredArgsConstructor public class InventoryCommand implements CustomCommand { - private final BookMeta bookMeta; - private final String[] args; + public final BookMeta bookMeta; + public final String[] args; public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { Map arguments = check(args, command); @@ -178,8 +177,8 @@ public class CustomScript { @RequiredArgsConstructor public class MenuCommand implements CustomCommand, MenuScript { - private final List pages; - private final String[] args; + public final List pages; + public final String[] args; public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { Map arguments = check(args, command); @@ -200,12 +199,12 @@ public class CustomScript { } @Override - public ItemStack toItem() { + public SWItem toItem() { SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§8/§e" + String.join(" ", args)); BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); bookMeta.setPages(pages.toArray(new String[0])); swItem.setItemMeta(bookMeta); - return swItem.getItemStack(); + return swItem; } } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index 82333c5a..eb6e0e52 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -20,15 +20,12 @@ package de.steamwar.bausystem.features.script; import de.steamwar.bausystem.SWUtils; -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 de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; import de.steamwar.sql.UserConfig; -import lombok.AccessLevel; -import lombok.RequiredArgsConstructor; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; @@ -53,117 +50,11 @@ import java.util.stream.Collectors; @Linked(LinkageType.LISTENER) public class CustomScriptListener implements Listener { - private interface CustomCommand { - default Map check(String[] args, String[] command) { - if (args.length < command.length) { - return null; - } - - if (!args[0].equals(command[0])) { - return null; - } - - Map arguments = new HashMap<>(); - if (!check(arguments, args, command, 0, 0)) { - return null; - } - return arguments; - } - - default boolean check(Map arguments, String[] args, String[] command, int argsIndex, int commandIndex) { - if (command.length <= commandIndex) { - for (int i = argsIndex; i < args.length; i++) { - if (!(args[i].startsWith("(") && args[i].endsWith(")"))) { - return false; - } - } - return true; - } - if (args.length <= argsIndex) return true; - - String currentArg = args[argsIndex]; - String currentCommand = command[commandIndex]; - - if (currentArg.startsWith("<") && currentArg.endsWith(">")) { - arguments.put(trim(currentArg, 1), new Value.StringValue(currentCommand)); - return check(arguments, args, command, argsIndex + 1, commandIndex + 1); - } else if (currentArg.startsWith("(<") && currentArg.endsWith(">)")) { - arguments.put(trim(currentArg, 2), new Value.StringValue(currentCommand)); - return check(arguments, args, command, argsIndex + 1, commandIndex + 1); - } else if (currentArg.startsWith("(") && currentArg.endsWith(")")) { - if (!trim(currentArg, 1).equals(currentCommand)) { - arguments.put(trim(currentArg, 1), new Value.BooleanValue(false)); - return check(arguments, args, command, argsIndex + 1, commandIndex); - } else { - arguments.put(trim(currentArg, 1), new Value.BooleanValue(true)); - return check(arguments, args, command, argsIndex + 1, commandIndex + 1); - } - } else { - if (!currentArg.equals(currentCommand)) return false; - return check(arguments, args, command, argsIndex + 1, commandIndex + 1); - } - } - - default String trim(String s, int count) { - return s.substring(count, s.length() - count); - } - - boolean execute(String[] command, PlayerCommandPreprocessEvent e); - } - - @RequiredArgsConstructor(access = AccessLevel.PRIVATE) - private static class InventoryCommand implements CustomCommand { - private final BookMeta bookMeta; - private final String[] args; - - public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { - Map arguments = check(args, command); - if (arguments == null) { - return false; - } - - e.setCancelled(true); - new ScriptExecutor(bookMeta, e.getPlayer(), arguments); - return true; - } - } - - @RequiredArgsConstructor(access = AccessLevel.PRIVATE) - private static class MenuCommand implements CustomCommand { - private final List pages; - private final String[] args; - - public boolean execute(String[] command, PlayerCommandPreprocessEvent e) { - Map arguments = check(args, command); - if (arguments == null) { - return false; - } - - e.setCancelled(true); - new ScriptExecutor(pages, e.getPlayer(), arguments); - return true; - } - - public void toYAPION(YAPIONMap yapionMap) { - YAPIONArray yapionArray = new YAPIONArray(); - pages.forEach(yapionArray::add); - yapionMap.put(String.join(" ", args), yapionArray); - } - - public ItemStack toItem() { - SWItem swItem = new SWItem(Material.WRITABLE_BOOK, String.join(" ", args)); - BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); - bookMeta.setPages(pages.toArray(new String[0])); - swItem.setItemMeta(bookMeta); - return swItem.getItemStack(); - } - } - - private Map> playerMap = new HashMap<>(); + private Map> playerMap = new HashMap<>(); private void updateInventory(Player p) { playerMap.computeIfPresent(p, (player, customCommands) -> { - customCommands.removeIf(InventoryCommand.class::isInstance); + customCommands.removeIf(CustomScript.InventoryCommand.class::isInstance); return customCommands; }); for (ItemStack item : p.getInventory().getContents()) { @@ -179,10 +70,11 @@ public class CustomScriptListener implements Listener { continue; } String s = bookMeta.getPage(1).split("\n")[0]; - if (!s.startsWith("#!CMD /")) { - continue; + if (s.startsWith("#!CMD /")) { + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryCommand(bookMeta, s.substring(6).split(" "))); + } else if (s.startsWith("#!EVENT ")) { + // Event System } - playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new InventoryCommand(bookMeta, s.substring(6).split(" "))); } } @@ -210,7 +102,13 @@ public class CustomScriptListener implements Listener { yapionObject.getYAPIONMapOrSetDefault("commands", new YAPIONMap()).forEach((key, value) -> { String[] command = ((YAPIONValue) key).get().split(" "); List pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList()); - playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new MenuCommand(pages, command)); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuCommand(pages, command)); + }); + + yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).forEach((key, value) -> { + String eventName = ((YAPIONValue) key).get(); + List pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList()); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuEvent(pages, eventName)); }); } @@ -223,10 +121,15 @@ public class CustomScriptListener implements Listener { private YAPIONObject output(Player p) { if (!playerMap.containsKey(p)) return new YAPIONObject(); YAPIONObject yapionObject = new YAPIONObject(); - YAPIONMap yapionMap = new YAPIONMap(); - yapionObject.add("commands", yapionMap); - playerMap.get(p).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> { - menuCommand.toYAPION(yapionMap); + YAPIONMap commandsMap = new YAPIONMap(); + yapionObject.add("commands", commandsMap); + playerMap.get(p).stream().filter(CustomScript.MenuCommand.class::isInstance).map(CustomScript.MenuCommand.class::cast).forEach(menuCommand -> { + menuCommand.toYAPION(commandsMap); + }); + YAPIONMap eventsMap = new YAPIONMap(); + yapionObject.add("events", eventsMap); + playerMap.get(p).stream().filter(CustomScript.MenuEvent.class::isInstance).map(CustomScript.MenuEvent.class::cast).forEach(menuCommand -> { + menuCommand.toYAPION(commandsMap); }); return yapionObject; } @@ -258,13 +161,9 @@ public class CustomScriptListener implements Listener { return; } - List inventoryCommands = playerMap.get(e.getPlayer()); - if (inventoryCommands == null) { - return; - } - + List customCommands = playerMap.getOrDefault(e.getPlayer(), new ArrayList<>()).stream().filter(CustomScript.CustomCommand.class::isInstance).map(CustomScript.CustomCommand.class::cast).collect(Collectors.toList()); String[] command = e.getMessage().split(" "); - for (CustomCommand customCommand : inventoryCommands) { + for (CustomScript.CustomCommand customCommand : customCommands) { if (customCommand.execute(command, e)) { return; } @@ -276,13 +175,14 @@ public class CustomScriptListener implements Listener { } public void openCommandsMenu(Player p) { - List> menuCommands = new ArrayList<>(); - playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(MenuCommand.class::isInstance).map(MenuCommand.class::cast).forEach(menuCommand -> { - String command = "§e" + String.join(" ", menuCommand.args); + List> menuCommands = new ArrayList<>(); + playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.MenuScript.class::isInstance).map(CustomScript.MenuScript.class::cast).forEach(menuItem -> { + SWItem swItem = menuItem.toItem(); + swItem.setLore(Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren")); - menuCommands.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, command, Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren"), false, clickType -> { - }), menuCommand)); + menuCommands.add(new SWListInv.SWListEntry<>(swItem, menuItem)); }); + int length = (int) output(p).toYAPION(new LengthOutput()).getLength(); StringBuilder menuName = new StringBuilder(); menuName.append("§eScript Commands "); @@ -295,11 +195,12 @@ public class CustomScriptListener implements Listener { menuName.append("§a"); } menuName.append(percentage).append("§7%"); - SWListInv menuCommandSWListInv = new SWListInv<>(p, menuName.toString(), false, menuCommands, (clickType, menuCommand) -> { + + SWListInv menuCommandSWListInv = new SWListInv<>(p, menuName.toString(), false, menuCommands, (clickType, menuCommand) -> { if (!clickType.isCreativeAction()) { playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand); } - SWUtils.giveItemToPlayer(p, menuCommand.toItem()); + SWUtils.giveItemToPlayer(p, menuCommand.toItem().getItemStack()); p.closeInventory(); save(p); }); @@ -320,30 +221,31 @@ public class CustomScriptListener implements Listener { return; } String s = bookMeta.getPage(1).split("\n")[0]; - if (!s.startsWith("#!CMD /")) { - return; - } - MenuCommand menuCommand = new MenuCommand(bookMeta.getPages(), s.substring(6).split(" ")); - for (CustomCommand customCommand : playerMap.computeIfAbsent(p, player -> new ArrayList<>())) { - if (!(customCommand instanceof MenuCommand)) { - continue; + if (s.startsWith("#!CMD /")) { + CustomScript.MenuCommand menuCommand = new CustomScript.MenuCommand(bookMeta.getPages(), s.substring(6).split(" ")); + for (CustomScript.Script script : playerMap.computeIfAbsent(p, player -> new ArrayList<>())) { + if (!(script instanceof CustomScript.MenuCommand)) { + continue; + } + if (Arrays.equals(((CustomScript.MenuCommand) script).args, menuCommand.args)) { + p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert"); + return; + } } - if (Arrays.equals(((MenuCommand) customCommand).args, menuCommand.args)) { - p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert"); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuCommand); + if (!save(p)) { + playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand); + p.closeInventory(); + SWUtils.giveItemToPlayer(p, p.getItemOnCursor()); + save(p); + p.sendMessage("§cScript-Buch Limit erreicht"); return; } + p.setItemOnCursor(null); + openCommandsMenu(p); + } else if (s.startsWith("#!EVENT ")) { + // Event stuff } - playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuCommand); - if (!save(p)) { - playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand); - p.closeInventory(); - SWUtils.giveItemToPlayer(p, p.getItemOnCursor()); - save(p); - p.sendMessage("§cScript-Buch Limit erreicht"); - return; - } - p.setItemOnCursor(null); - openCommandsMenu(p); })); menuCommandSWListInv.open(); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index 156be531..5a394436 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -43,7 +43,7 @@ public class ScriptCommand extends SWCommand { } swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht (<>) wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen.", "§7Nutzbare Events sind:", "§8-§7 FF"), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eOther", Arrays.asList("§7Kommentare fangen mit §e#§7 an.", "§7Jump_Points fangen mit §e.§7 an.", "§7Eine Variablen Namen in '<>'", "§7eingeschlossen wird ersetzt, bis zu zwei mal.", "§7Eine Variable in '<>' kann mit 'const.'", "§7oder 'local.' oder 'global.' prefixed werden", "§7für den genauen type wo nach geguckt werden soll"), false, clickType -> { }), null)); -- 2.39.5 From e4b3f32e42b6e8b9c94c2335a2c77e5abdc5e758 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Sep 2021 22:04:13 +0200 Subject: [PATCH 04/11] Add default 'FF' event Signed-off-by: yoyosource --- .../bausystem/features/script/CustomScriptListener.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index eb6e0e52..92221f21 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -91,11 +91,13 @@ public class CustomScriptListener implements Listener { YAPIONObject yapionObject; if (s == null) { yapionObject = new YAPIONObject(); + yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF\ngui")); } else { yapionObject = YAPIONParser.parse(s); if (yapionObject.containsKey("")) { yapionObject.add("commands", yapionObject.getMap("")); yapionObject.remove(""); + yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF\ngui")); } } -- 2.39.5 From ba04619bf41d543067303896bf2c582fde54792e Mon Sep 17 00:00:00 2001 From: yoyosource Date: Mon, 20 Sep 2021 22:11:18 +0200 Subject: [PATCH 05/11] Fix migration problems Signed-off-by: yoyosource --- .../steamwar/bausystem/features/script/CustomScript.java | 8 ++++---- .../bausystem/features/script/CustomScriptListener.java | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java index f67c89e1..94bc2f75 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -51,7 +51,7 @@ public class CustomScript { } @RequiredArgsConstructor - public class InventoryEvent implements CustomEvent { + public static class InventoryEvent implements CustomEvent { public final BookMeta bookMeta; public final String eventName; @@ -68,7 +68,7 @@ public class CustomScript { } @RequiredArgsConstructor - public class MenuEvent implements CustomEvent, MenuScript { + public static class MenuEvent implements CustomEvent, MenuScript { public final List pages; public final String eventName; @@ -159,7 +159,7 @@ public class CustomScript { } @RequiredArgsConstructor - public class InventoryCommand implements CustomCommand { + public static class InventoryCommand implements CustomCommand { public final BookMeta bookMeta; public final String[] args; @@ -176,7 +176,7 @@ public class CustomScript { } @RequiredArgsConstructor - public class MenuCommand implements CustomCommand, MenuScript { + public static class MenuCommand implements CustomCommand, MenuScript { public final List pages; public final String[] args; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index 92221f21..d440ca70 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -131,7 +131,7 @@ public class CustomScriptListener implements Listener { YAPIONMap eventsMap = new YAPIONMap(); yapionObject.add("events", eventsMap); playerMap.get(p).stream().filter(CustomScript.MenuEvent.class::isInstance).map(CustomScript.MenuEvent.class::cast).forEach(menuCommand -> { - menuCommand.toYAPION(commandsMap); + menuCommand.toYAPION(eventsMap); }); return yapionObject; } -- 2.39.5 From a88573aaec12fdb0fc25365a5dc437f2581e584f Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 21 Sep 2021 08:50:55 +0200 Subject: [PATCH 06/11] Fix CustomScript duplication errors Signed-off-by: yoyosource --- .../features/script/CustomScript.java | 23 +++++++++++- .../features/script/CustomScriptListener.java | 36 ++++++++++--------- 2 files changed, 41 insertions(+), 18 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java index 94bc2f75..99ace538 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -191,6 +191,27 @@ public class CustomScript { return true; } + public boolean equals(MenuCommand menuCommand) { + if (menuCommand.args.length != args.length) { + return false; + } + for (int i = 0; i < args.length; i++) { + if (i == 0) continue; + String s1 = args[i]; + String s2 = menuCommand.args[i]; + if (s1.equals(s2)) { + return true; + } + if (s1.startsWith("<") && s1.endsWith(">") && s2.startsWith("<") && s2.endsWith(">")) { + return true; + } + if (s1.startsWith("[<") && s1.endsWith(">]") && s2.startsWith("[<") && s2.endsWith(">]")) { + return true; + } + } + return false; + } + @Override public void toYAPION(YAPIONMap yapionMap) { YAPIONArray yapionArray = new YAPIONArray(); @@ -200,7 +221,7 @@ public class CustomScript { @Override public SWItem toItem() { - SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§8/§e" + String.join(" ", args)); + SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Command§8: §e" + String.join(" ", args)); BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); bookMeta.setPages(pages.toArray(new String[0])); swItem.setItemMeta(bookMeta); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index d440ca70..0234c821 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -156,22 +156,6 @@ public class CustomScriptListener implements Listener { } } - @EventHandler - public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { - if (e.getMessage().startsWith("/script:")) { - e.setMessage("/" + e.getMessage().substring(8)); - return; - } - - List customCommands = playerMap.getOrDefault(e.getPlayer(), new ArrayList<>()).stream().filter(CustomScript.CustomCommand.class::isInstance).map(CustomScript.CustomCommand.class::cast).collect(Collectors.toList()); - String[] command = e.getMessage().split(" "); - for (CustomScript.CustomCommand customCommand : customCommands) { - if (customCommand.execute(command, e)) { - return; - } - } - } - private boolean isNoBook(ItemStack item) { return VersionedCallable.call(new VersionedCallable<>(() -> ScriptListener_15.isNoBook(item), 15)); } @@ -229,7 +213,7 @@ public class CustomScriptListener implements Listener { if (!(script instanceof CustomScript.MenuCommand)) { continue; } - if (Arrays.equals(((CustomScript.MenuCommand) script).args, menuCommand.args)) { + if (((CustomScript.MenuCommand) script).equals(menuCommand)) { p.sendMessage("§cCommand '" + (String.join(" ", menuCommand.args)) + "' bereits definiert"); return; } @@ -251,4 +235,22 @@ public class CustomScriptListener implements Listener { })); menuCommandSWListInv.open(); } + + // EventListener for Commands as well as Events + + @EventHandler + public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { + if (e.getMessage().startsWith("/script:")) { + e.setMessage("/" + e.getMessage().substring(8)); + return; + } + + List customCommands = playerMap.getOrDefault(e.getPlayer(), new ArrayList<>()).stream().filter(CustomScript.CustomCommand.class::isInstance).map(CustomScript.CustomCommand.class::cast).collect(Collectors.toList()); + String[] command = e.getMessage().split(" "); + for (CustomScript.CustomCommand customCommand : customCommands) { + if (customCommand.execute(command, e)) { + return; + } + } + } } -- 2.39.5 From 36ef8be6f1307211e38dc70d3314486dd0efadbb Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 21 Sep 2021 08:53:52 +0200 Subject: [PATCH 07/11] Fix docs Signed-off-by: yoyosource --- .../src/de/steamwar/bausystem/features/script/CustomScript.java | 2 +- .../de/steamwar/bausystem/features/script/ScriptCommand.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java index 99ace538..198a6f14 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -205,7 +205,7 @@ public class CustomScript { if (s1.startsWith("<") && s1.endsWith(">") && s2.startsWith("<") && s2.endsWith(">")) { return true; } - if (s1.startsWith("[<") && s1.endsWith(">]") && s2.startsWith("[<") && s2.endsWith(">]")) { + if (s1.startsWith("(<") && s1.endsWith(">)") && s2.startsWith("(<") && s2.endsWith(">)")) { return true; } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index 5a394436..01a1eab8 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -41,7 +41,7 @@ public class ScriptCommand extends SWCommand { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { }), null)); } - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht (<>) wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht '<>' wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen.", "§7Nutzbare Events sind:", "§8-§7 FF"), false, clickType -> { }), null)); -- 2.39.5 From 841921d458156207e77af538a4d3e73f06f85241 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 21 Sep 2021 11:00:06 +0200 Subject: [PATCH 08/11] Add CustomScript execution for events Signed-off-by: yoyosource --- .../features/gui/BauGuiListener.java | 22 ----- .../features/script/CustomScript.java | 30 +++--- .../features/script/CustomScriptListener.java | 99 +++++++++++++++---- .../features/script/ScriptCommand.java | 2 +- 4 files changed, 99 insertions(+), 54 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGuiListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGuiListener.java index 5602f90d..e8c48493 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGuiListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/gui/BauGuiListener.java @@ -19,38 +19,16 @@ package de.steamwar.bausystem.features.gui; -import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.block.Action; import org.bukkit.event.player.PlayerInteractEvent; -import org.bukkit.event.player.PlayerSwapHandItemsEvent; - -import java.util.HashSet; -import java.util.Set; @Linked(LinkageType.LISTENER) public class BauGuiListener implements Listener { - private static final Set LAST_FS = new HashSet<>(); - - public BauGuiListener() { - Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), LAST_FS::clear, 20, 20); - } - - @EventHandler - public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { - if (LAST_FS.contains(event.getPlayer())) { - BauGUI.openBauGui(event.getPlayer()); - } else { - LAST_FS.add(event.getPlayer()); - } - } - @EventHandler public void onPlayerInteract(PlayerInteractEvent event) { if (event.getItem() == null) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java index 198a6f14..97818046 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -47,22 +47,22 @@ public class CustomScript { public interface CustomEvent extends Script { String eventName(); - boolean execute(PlayerEvent e); + boolean execute(PlayerEvent e, Map variables); } @RequiredArgsConstructor public static class InventoryEvent implements CustomEvent { public final BookMeta bookMeta; - public final String eventName; + public final String[] args; @Override public String eventName() { - return eventName; + return args[0]; } @Override - public boolean execute(PlayerEvent e) { - new ScriptExecutor(bookMeta, e.getPlayer()); + public boolean execute(PlayerEvent e, Map variables) { + new ScriptExecutor(bookMeta, e.getPlayer(), variables); return true; } } @@ -70,16 +70,16 @@ public class CustomScript { @RequiredArgsConstructor public static class MenuEvent implements CustomEvent, MenuScript { public final List pages; - public final String eventName; + public final String[] args; @Override public String eventName() { - return eventName; + return args[0]; } @Override - public boolean execute(PlayerEvent e) { - new ScriptExecutor(pages, e.getPlayer()); + public boolean execute(PlayerEvent e, Map variables) { + new ScriptExecutor(pages, e.getPlayer(), variables); return false; } @@ -87,12 +87,20 @@ public class CustomScript { public void toYAPION(YAPIONMap yapionMap) { YAPIONArray yapionArray = new YAPIONArray(); pages.forEach(yapionArray::add); - yapionMap.put(eventName, yapionArray); + yapionMap.put(String.join(" ", args), yapionArray); } @Override public SWItem toItem() { - SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Event§8: §e" + eventName); + StringBuilder st = new StringBuilder(); + for (int i = 0; i < args.length; i++) { + if (i != 0) st.append(" "); + if (i == 0) st.append("§e"); + st.append(args[i]); + if (i == 0) st.append("§8 -§7"); + } + + SWItem swItem = new SWItem(Material.WRITABLE_BOOK, "§7Event§8: " + st); BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); bookMeta.setPages(pages.toArray(new String[0])); swItem.setItemMeta(bookMeta); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index 0234c821..30030db3 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -19,21 +19,24 @@ package de.steamwar.bausystem.features.script; +import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.SWUtils; +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 de.steamwar.inventory.SWItem; import de.steamwar.inventory.SWListInv; import de.steamwar.sql.UserConfig; +import lombok.AllArgsConstructor; +import lombok.Getter; +import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.inventory.InventoryCloseEvent; -import org.bukkit.event.player.PlayerCommandPreprocessEvent; -import org.bukkit.event.player.PlayerJoinEvent; -import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.*; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.BookMeta; import yapion.hierarchy.output.LengthOutput; @@ -45,6 +48,7 @@ import yapion.hierarchy.types.YAPIONValue; import yapion.parser.YAPIONParser; import java.util.*; +import java.util.function.Function; import java.util.stream.Collectors; @Linked(LinkageType.LISTENER) @@ -54,7 +58,7 @@ public class CustomScriptListener implements Listener { private void updateInventory(Player p) { playerMap.computeIfPresent(p, (player, customCommands) -> { - customCommands.removeIf(CustomScript.InventoryCommand.class::isInstance); + customCommands.removeIf(script -> !(script instanceof CustomScript.MenuScript)); return customCommands; }); for (ItemStack item : p.getInventory().getContents()) { @@ -73,14 +77,14 @@ public class CustomScriptListener implements Listener { if (s.startsWith("#!CMD /")) { playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryCommand(bookMeta, s.substring(6).split(" "))); } else if (s.startsWith("#!EVENT ")) { - // Event System + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryEvent(bookMeta, s.substring(8).split(" "))); } } } @EventHandler - public void onPlayerJoin(PlayerJoinEvent event) { - Player p = event.getPlayer(); + public void onPlayerJoin(PlayerJoinEvent e) { + Player p = e.getPlayer(); updateInventory(p); String s = UserConfig.getConfig(p.getUniqueId(), "bausystem-scripts"); @@ -108,9 +112,9 @@ public class CustomScriptListener implements Listener { }); yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).forEach((key, value) -> { - String eventName = ((YAPIONValue) key).get(); + String[] event = ((YAPIONValue) key).get().split(" "); List pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList()); - playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuEvent(pages, eventName)); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuEvent(pages, event)); }); } @@ -218,26 +222,65 @@ public class CustomScriptListener implements Listener { return; } } - playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuCommand); - if (!save(p)) { - playerMap.get(p).removeIf(customCommand -> customCommand == menuCommand); - p.closeInventory(); - SWUtils.giveItemToPlayer(p, p.getItemOnCursor()); - save(p); - p.sendMessage("§cScript-Buch Limit erreicht"); + scriptBookLimit(p, menuCommand); + } else if (s.startsWith("#!EVENT ")) { + CustomScript.MenuEvent menuEvent = new CustomScript.MenuEvent(bookMeta.getPages(), s.substring(8).split(" ")); + try { + EventType.valueOf(menuEvent.eventName()); + } catch (Exception e) { + p.sendMessage("§cEvent '" + menuEvent.eventName() + "' ist nicht definierbar"); return; } - p.setItemOnCursor(null); - openCommandsMenu(p); - } else if (s.startsWith("#!EVENT ")) { - // Event stuff + scriptBookLimit(p, menuEvent); } })); menuCommandSWListInv.open(); } + private void scriptBookLimit(Player p, CustomScript.Script menuScript) { + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuScript); + if (!save(p)) { + playerMap.get(p).removeIf(script -> script == menuScript); + p.closeInventory(); + SWUtils.giveItemToPlayer(p, p.getItemOnCursor()); + save(p); + p.sendMessage("§cScript-Buch Limit erreicht"); + return; + } + p.setItemOnCursor(null); + openCommandsMenu(p); + } + + @AllArgsConstructor + @Getter + public enum EventType { + FF(PlayerSwapHandItemsEvent.class, event -> null); + + private Class eventType; + private Function> eventValues; + } + + private void callEvent(EventType eventType, PlayerEvent e) { + if (!eventType.getEventType().equals(e.getClass())) { + return; + } + + List customEvents = playerMap.getOrDefault(e.getPlayer(), new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList()); + for (CustomScript.CustomEvent customEvent : customEvents) { + if (customEvent.eventName().equals(eventType.name())) { + Map variables = eventType.getEventValues().apply(e); + if (variables == null) { + variables = new HashMap<>(); + } + customEvent.execute(e, variables); + return; + } + } + } + // EventListener for Commands as well as Events + // Event Command @EventHandler public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) { if (e.getMessage().startsWith("/script:")) { @@ -253,4 +296,20 @@ public class CustomScriptListener implements Listener { } } } + + // Event FF + private static final Set LAST_FS = new HashSet<>(); + + { + Bukkit.getScheduler().runTaskTimer(BauSystem.getInstance(), LAST_FS::clear, 20, 20); + } + + @EventHandler + public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { + if (LAST_FS.contains(event.getPlayer())) { + callEvent(EventType.FF, event); + } else { + LAST_FS.add(event.getPlayer()); + } + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index 01a1eab8..46474e93 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -43,7 +43,7 @@ public class ScriptCommand extends SWCommand { } swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht '<>' wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen.", "§7Nutzbare Events sind:", "§8-§7 FF"), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen.", "§7Nutzbare Events sind:", "§eFF"), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eOther", Arrays.asList("§7Kommentare fangen mit §e#§7 an.", "§7Jump_Points fangen mit §e.§7 an.", "§7Eine Variablen Namen in '<>'", "§7eingeschlossen wird ersetzt, bis zu zwei mal.", "§7Eine Variable in '<>' kann mit 'const.'", "§7oder 'local.' oder 'global.' prefixed werden", "§7für den genauen type wo nach geguckt werden soll"), false, clickType -> { }), null)); -- 2.39.5 From 0373408f2674ac9e2ba4806b3dfaa75f3a48e279 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 21 Sep 2021 12:53:00 +0200 Subject: [PATCH 09/11] Fix script commands Add BlockPlace, BlockBreak, RightClick and LeftClick to script event system Signed-off-by: yoyosource --- .../features/script/CustomScript.java | 13 +- .../features/script/CustomScriptListener.java | 83 +++++++- .../features/script/ScriptCommand.java | 16 +- .../script/command/arithmetic/Add.java | 4 +- .../script/command/arithmetic/Div.java | 4 +- .../script/command/arithmetic/Mul.java | 4 +- .../script/command/arithmetic/Sub.java | 4 +- .../features/script/command/logic/And.java | 4 +- .../features/script/command/logic/Equal.java | 2 +- .../script/command/logic/Greater.java | 4 +- .../features/script/command/logic/Less.java | 4 +- .../features/script/command/logic/Not.java | 2 +- .../features/script/command/logic/Or.java | 4 +- .../script/command/string/Insert.java | 6 +- .../script/command/string/Length.java | 2 +- .../script/command/string/Remove.java | 4 +- .../script/command/string/Replace.java | 6 +- .../script/command/string/Substring.java | 4 +- .../script/command/variable/Const.java | 73 ++++++++ .../script/command/variable/Global.java | 13 +- .../features/script/command/variable/Var.java | 1 + .../features/script/variables/Constants.java | 177 ++++++++++++++++-- .../features/script/variables/Context.java | 6 +- .../features/script/variables/Value.java | 56 +----- 24 files changed, 373 insertions(+), 123 deletions(-) create mode 100644 BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Const.java diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java index 97818046..deb73c3e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -24,8 +24,9 @@ import de.steamwar.inventory.SWItem; import lombok.RequiredArgsConstructor; import lombok.experimental.UtilityClass; import org.bukkit.Material; +import org.bukkit.entity.Player; +import org.bukkit.event.Event; import org.bukkit.event.player.PlayerCommandPreprocessEvent; -import org.bukkit.event.player.PlayerEvent; import org.bukkit.inventory.meta.BookMeta; import yapion.hierarchy.types.YAPIONArray; import yapion.hierarchy.types.YAPIONMap; @@ -47,7 +48,7 @@ public class CustomScript { public interface CustomEvent extends Script { String eventName(); - boolean execute(PlayerEvent e, Map variables); + boolean execute(Event e, Player p, Map variables); } @RequiredArgsConstructor @@ -61,8 +62,8 @@ public class CustomScript { } @Override - public boolean execute(PlayerEvent e, Map variables) { - new ScriptExecutor(bookMeta, e.getPlayer(), variables); + public boolean execute(Event e, Player p, Map variables) { + new ScriptExecutor(bookMeta, p, variables); return true; } } @@ -78,8 +79,8 @@ public class CustomScript { } @Override - public boolean execute(PlayerEvent e, Map variables) { - new ScriptExecutor(pages, e.getPlayer(), variables); + public boolean execute(Event e, Player p, Map variables) { + new ScriptExecutor(pages, p, variables); return false; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index 30030db3..03f56d65 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -33,8 +33,13 @@ import lombok.Getter; import org.bukkit.Bukkit; import org.bukkit.Material; import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.block.Action; +import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.inventory.InventoryCloseEvent; import org.bukkit.event.player.*; import org.bukkit.inventory.ItemStack; @@ -95,13 +100,13 @@ public class CustomScriptListener implements Listener { YAPIONObject yapionObject; if (s == null) { yapionObject = new YAPIONObject(); - yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF\ngui")); + yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF /gui Kürzel\ngui")); } else { yapionObject = YAPIONParser.parse(s); if (yapionObject.containsKey("")) { yapionObject.add("commands", yapionObject.getMap("")); yapionObject.remove(""); - yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF\ngui")); + yapionObject.getYAPIONMapOrSetDefault("events", new YAPIONMap()).add("FF", new YAPIONArray().add("#!EVENT FF /gui Kürzel\ngui")); } } @@ -251,28 +256,66 @@ public class CustomScriptListener implements Listener { openCommandsMenu(p); } - @AllArgsConstructor @Getter public enum EventType { - FF(PlayerSwapHandItemsEvent.class, event -> null); + FF(PlayerSwapHandItemsEvent.class, event -> null), + PlaceBlock(BlockPlaceEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("blockX", new Value.LongValue(event.getBlockPlaced().getX())); + valueMap.put("blockY", new Value.LongValue(event.getBlockPlaced().getY())); + valueMap.put("blockZ", new Value.LongValue(event.getBlockPlaced().getZ())); + valueMap.put("blockType", new Value.StringValue(event.getBlockPlaced().getType().name())); + return valueMap; + }), + BreakBlock(BlockBreakEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("blockX", new Value.LongValue(event.getBlock().getX())); + valueMap.put("blockY", new Value.LongValue(event.getBlock().getY())); + valueMap.put("blockZ", new Value.LongValue(event.getBlock().getZ())); + valueMap.put("blockType", new Value.StringValue(event.getBlock().getType().name())); + return valueMap; + }), + RightClick(PlayerInteractEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("blockInHand", new Value.BooleanValue(event.isBlockInHand())); + return valueMap; + }), + LeftClick(PlayerInteractEvent.class, event -> { + Map valueMap = new HashMap<>(); + valueMap.put("blockInHand", new Value.BooleanValue(event.isBlockInHand())); + return valueMap; + }); - private Class eventType; - private Function> eventValues; + private Class eventType; + private Function> eventValues; + + EventType(Class eventType, Function> eventValues) { + this.eventType = eventType; + this.eventValues = event -> { + return eventValues.apply((T) event); + }; + } } - private void callEvent(EventType eventType, PlayerEvent e) { + private void callEvent(EventType eventType, Player p, Event e) { if (!eventType.getEventType().equals(e.getClass())) { return; } - List customEvents = playerMap.getOrDefault(e.getPlayer(), new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList()); + List customEvents = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.CustomEvent.class::isInstance).map(CustomScript.CustomEvent.class::cast).collect(Collectors.toList()); for (CustomScript.CustomEvent customEvent : customEvents) { if (customEvent.eventName().equals(eventType.name())) { Map variables = eventType.getEventValues().apply(e); if (variables == null) { variables = new HashMap<>(); } - customEvent.execute(e, variables); + customEvent.execute(e, p, variables); + if (e instanceof Cancellable && variables.containsKey("cancel")) { + Value value = variables.get("cancel"); + if (value.asBoolean()) { + ((Cancellable) e).setCancelled(true); + } + } return; } } @@ -307,9 +350,29 @@ public class CustomScriptListener implements Listener { @EventHandler public void onPlayerSwapHandItems(PlayerSwapHandItemsEvent event) { if (LAST_FS.contains(event.getPlayer())) { - callEvent(EventType.FF, event); + callEvent(EventType.FF, event.getPlayer(), event); } else { LAST_FS.add(event.getPlayer()); } } + + @EventHandler + public void onBlockPlace(BlockPlaceEvent event) { + callEvent(EventType.PlaceBlock, event.getPlayer(), event); + } + + @EventHandler + public void onBlockBreak(BlockBreakEvent event) { + callEvent(EventType.BreakBlock, event.getPlayer(), event); + } + + @EventHandler + public void onPlayerInteract(PlayerInteractEvent event) { + if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) { + callEvent(EventType.RightClick, event.getPlayer(), event); + } + if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) { + callEvent(EventType.RightClick, event.getPlayer(), event); + } + } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index 46474e93..fbf8053b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -43,7 +43,7 @@ public class ScriptCommand extends SWCommand { } swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Commands", Arrays.asList("§7Schreibe§8: §7#!CMD 'COMMAND'", "§7an den Anfang eines Script Buches um", "§7ein Custom Command zu nutzen. Der", "§7Befehl startet immer mit / und kann dann so", "§7aufgebaut sein wie du willst. Alles was in Spitzen", "§7Klammern steht '<>' wird als Parameter und somit", "§7als Variable gewertet.", "§7Parameter, welche in runden Klammern", "§7stehen sind Optional. Einfache", "§7Texte als Parameter bekommen", "§7eine gleichnamige Variable mit", "§7true/false als Wert je nachdem", "§7ob dieser angegeben wurde oder nicht"), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen.", "§7Nutzbare Events sind:", "§eFF"), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eCustom Events", Arrays.asList("§7Schreibe§8: §7#!EVENT 'EventName'", "§7an den Anfang eines Script Buches um", "§7ein Custom Event zu nutzen. Jedes Event kann durch", "§7'var cancel true' gecancelt werden.", "§7Hinter dem Event Namen stehen die Variablen,", "§7welche im Script durch das Event nutztbar sind.", "§7Nutzbare Events sind:", "§eFF", "§ePlaceBlock §8-§7 blockX, blockY, blockZ, blockType", "§eBreakBlock §8-§7 blockX, blockY, blockZ, blockType", "§eRightClick §8-§7 blockInHand", "§eLeftClick §8-§7 blockInHand"), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.BOOK, "§eOther", Arrays.asList("§7Kommentare fangen mit §e#§7 an.", "§7Jump_Points fangen mit §e.§7 an.", "§7Eine Variablen Namen in '<>'", "§7eingeschlossen wird ersetzt, bis zu zwei mal.", "§7Eine Variable in '<>' kann mit 'const.'", "§7oder 'local.' oder 'global.' prefixed werden", "§7für den genauen type wo nach geguckt werden soll"), false, clickType -> { }), null)); @@ -80,7 +80,7 @@ public class ScriptCommand extends SWCommand { }); swItems.add(new SWListInv.SWListEntry<>(swItem, specialCommand)); }); - for (int i = 0; i < 9 + 4; i++) { + for (int i = 0; i < 9 + 3; i++) { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { }), null)); } @@ -104,17 +104,21 @@ public class ScriptCommand extends SWCommand { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.OBSIDIAN, "§7Constant §eprotect", Arrays.asList("§etrue§7 wenn Protect angeschaltet ist."), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ex", Arrays.asList("§ex§7 Position des Spielers."), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ex", Arrays.asList("§ex§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ey", Arrays.asList("§ey§7 Position des Spielers."), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ey", Arrays.asList("§ey§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> { }), null)); - swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ez", Arrays.asList("§ez§7 Position des Spielers."), false, clickType -> { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.PLAYER_HEAD, "§7Constant §ez", Arrays.asList("§ez§7 Position des Spielers.", "§eÜberschreibbar"), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.NAME_TAG, "§7Constant §ename", Arrays.asList("§eDisplay§7 Name des Spielers."), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.IRON_BOOTS, "§7Constant §esneak", Arrays.asList("§etrue§7 wenn der Spieler gerade sneakt."), false, clickType -> { }), null)); - for (int i = 0; i < 6 + 2 * 9; i++) { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.ARROW, "§7Constant §eslot", Arrays.asList("§e0-8§7 für den ausgewählten slot.", "§eÜberschreibbar"), false, clickType -> { + }), null)); + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRASS_BLOCK, "§7Constant §eslotmaterial", Arrays.asList("§eMaterial§7 des Items im Slot"), false, clickType -> { + }), null)); + for (int i = 0; i < 4 + 2 * 9; i++) { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { }), null)); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java index 47b4e2d3..4bb50b4e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Add.java @@ -44,11 +44,11 @@ public class Add implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können addiert werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java index 17e26444..ff4499f7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Div.java @@ -44,11 +44,11 @@ public class Div implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können dividiert werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java index 8844a282..a1238ef6 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Mul.java @@ -44,11 +44,11 @@ public class Mul implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können multipliziert werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java index 036de18b..e7ff65b2 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/arithmetic/Sub.java @@ -44,11 +44,11 @@ public class Sub implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können subtrahiert werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/And.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/And.java index e7526748..e19f2c59 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/And.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/And.java @@ -44,11 +44,11 @@ public class And implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.BooleanValue.class) { + if (!(v1 instanceof Value.BooleanValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); return true; } - if (v2.getClass() != Value.BooleanValue.class) { + if (!(v2 instanceof Value.BooleanValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Equal.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Equal.java index a02aaf71..6fe644a7 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Equal.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Equal.java @@ -45,7 +45,7 @@ public class Equal implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); Value result; - if (v1.getClass() != v2.getClass()) { + if (!v1.getClass().isInstance(v2.getClass()) && !v2.getClass().isInstance(v1.getClass())) { result = new Value.BooleanValue(false); } else if (v1.asString().equals(v2.asString())) { result = new Value.BooleanValue(true); diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Greater.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Greater.java index 196b3ad4..8539de02 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Greater.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Greater.java @@ -44,11 +44,11 @@ public class Greater implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Less.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Less.java index 32cfd20d..a9e086d4 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Less.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Less.java @@ -44,11 +44,11 @@ public class Less implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.LongValue.class) { + if (!(v1 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verglichen werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Not.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Not.java index 72c9a59f..9be6f034 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Not.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Not.java @@ -39,7 +39,7 @@ public class Not implements SpecialCommand { } Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.BooleanValue.class) { + if (!(v1 instanceof Value.BooleanValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können genichtet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Or.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Or.java index ce67eede..7d6c9bae 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Or.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/logic/Or.java @@ -44,11 +44,11 @@ public class Or implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.BooleanValue.class) { + if (!(v1 instanceof Value.BooleanValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); return true; } - if (v2.getClass() != Value.BooleanValue.class) { + if (!(v2 instanceof Value.BooleanValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Booleans können verglichen werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java index e3629d0e..0036af54 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Insert.java @@ -70,15 +70,15 @@ public class Insert implements SpecialCommand { Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.StringValue.class) { + if (!(v1 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v2.getClass() != Value.StringValue.class) { + if (!(v2 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v3.getClass() != Value.LongValue.class) { + if (!(v3 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Length.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Length.java index 749c1511..77d463eb 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Length.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Length.java @@ -64,7 +64,7 @@ public class Length implements SpecialCommand { String resultName = command[1]; Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v2.getClass() != Value.StringValue.class) { + if (!(v2 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Remove.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Remove.java index f6bad4d7..ff692ee0 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Remove.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Remove.java @@ -65,11 +65,11 @@ public class Remove implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.StringValue.class) { + if (!(v1 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v2.getClass() != Value.StringValue.class) { + if (!(v2 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Replace.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Replace.java index 3aacb7de..0e8eebd1 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Replace.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Replace.java @@ -70,15 +70,15 @@ public class Replace implements SpecialCommand { Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v3 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.StringValue.class) { + if (!(v1 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v2.getClass() != Value.StringValue.class) { + if (!(v2 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v3.getClass() != Value.StringValue.class) { + if (!(v3 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Substring.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Substring.java index edba61f8..88623656 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Substring.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/string/Substring.java @@ -65,11 +65,11 @@ public class Substring implements SpecialCommand { Value v1 = scriptExecutor.getOrItselfValue(command[command.length - 2]); Value v2 = scriptExecutor.getOrItselfValue(command[command.length - 1]); - if (v1.getClass() != Value.StringValue.class) { + if (!(v1 instanceof Value.StringValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Strings können verwendet werden"); return true; } - if (v2.getClass() != Value.LongValue.class) { + if (!(v2 instanceof Value.LongValue)) { scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cNur Zahlen können verwendet werden"); return true; } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Const.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Const.java new file mode 100644 index 00000000..79915ddc --- /dev/null +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Const.java @@ -0,0 +1,73 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2021 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.bausystem.features.script.command.variable; + +import de.steamwar.bausystem.BauSystem; +import de.steamwar.bausystem.features.script.ScriptExecutor; +import de.steamwar.bausystem.features.script.SpecialCommand; +import de.steamwar.bausystem.features.script.variables.Constants; +import de.steamwar.bausystem.linkage.LinkageType; +import de.steamwar.bausystem.linkage.Linked; +import org.bukkit.Material; + +@Linked(LinkageType.SCRIPT_COMMAND) +public class Const implements SpecialCommand { + + @Override + public String[] description() { + return new String[]{ + "§econst §8<§7Variable§8> §8[§7Value§8(§7s§8)§8]", + "", + "Schreibt in eine Konstante einen Wert rein, welcher eine Zahl sein kann, ein Boolscher Wert oder ein Text." + }; + } + + @Override + public Material material() { + return Material.STRUCTURE_BLOCK; + } + + @Override + public String command() { + return "const"; + } + + @Override + public boolean execute(String[] command, ScriptExecutor scriptExecutor) { + if (command.length <= 1) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas erste Argument fehlt und sollte eine Variable sein"); + return true; + } + if (command.length <= 2) { + scriptExecutor.getPlayer().sendMessage(BauSystem.PREFIX + "§cDas zweite Argument fehlt und sollte ein Wert sein"); + return true; + } + String varName = command[1]; + StringBuilder varValue = new StringBuilder(); + for (int i = 2; i < command.length; i++) { + if (varValue.length() != 0) { + varValue.append(" "); + } + varValue.append(command[i]); + } + Constants.getConstant(varName, scriptExecutor.getPlayer()).fromValue(scriptExecutor.parse(varValue.toString())); + return true; + } +} diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Global.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Global.java index 9c983fd6..97821477 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Global.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Global.java @@ -3,7 +3,6 @@ package de.steamwar.bausystem.features.script.command.variable; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.script.ScriptExecutor; import de.steamwar.bausystem.features.script.SpecialCommand; -import de.steamwar.bausystem.features.script.variables.Value; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; import org.bukkit.Material; @@ -48,17 +47,7 @@ public class Global implements SpecialCommand { } varValue.append(command[i]); } - try { - long value = Long.parseLong(varValue.toString()); - scriptExecutor.getLocalVariables().putValue(varName, new Value.LongValue(value)); - } catch (NumberFormatException e) { - String s = varValue.toString(); - if (s.equalsIgnoreCase("true") || s.equalsIgnoreCase("false")) { - scriptExecutor.getGlobalVariables().putValue(varName, new Value.BooleanValue(s.equalsIgnoreCase("true"))); - } else { - scriptExecutor.getGlobalVariables().putValue(varName, new Value.StringValue(s)); - } - } + scriptExecutor.getGlobalVariables().putValue(varName, scriptExecutor.parse(varValue.toString())); return true; } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java index e8dc2cb5..82033387 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/command/variable/Var.java @@ -3,6 +3,7 @@ package de.steamwar.bausystem.features.script.command.variable; import de.steamwar.bausystem.BauSystem; import de.steamwar.bausystem.features.script.ScriptExecutor; import de.steamwar.bausystem.features.script.SpecialCommand; +import de.steamwar.bausystem.features.script.variables.Constants; import de.steamwar.bausystem.features.script.variables.Value; import de.steamwar.bausystem.linkage.LinkageType; import de.steamwar.bausystem.linkage.Linked; diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java index df1a8720..8a78d10e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java @@ -8,54 +8,206 @@ import de.steamwar.bausystem.region.flags.flagvalues.FreezeMode; import de.steamwar.bausystem.region.flags.flagvalues.ProtectMode; import de.steamwar.bausystem.region.flags.flagvalues.TNTMode; import lombok.experimental.UtilityClass; +import org.bukkit.Location; import org.bukkit.entity.Player; import java.util.HashMap; import java.util.Map; import java.util.Set; +import java.util.function.Consumer; import java.util.function.Function; +import java.util.function.Supplier; @UtilityClass public class Constants { private final Map> CONSTANTS = new HashMap<>(); + private static class ConstantLongValue extends Value.LongValue { + + private Supplier longSupplier; + private Consumer longConsumer = ignored -> {}; + + public ConstantLongValue(Supplier longSupplier) { + super(longSupplier.get()); + this.longSupplier = longSupplier; + } + + public ConstantLongValue(Supplier longSupplier, Consumer longConsumer) { + super(longSupplier.get()); + this.longSupplier = longSupplier; + this.longConsumer = longConsumer; + } + + @Override + public long asLong() { + value = longSupplier.get(); + return super.asLong(); + } + + @Override + public boolean asBoolean() { + value = longSupplier.get(); + return super.asBoolean(); + } + + @Override + public String asString() { + value = longSupplier.get(); + return super.asString(); + } + + @Override + public void fromValue(Value value) { + super.fromValue(value); + longConsumer.accept(this.value); + } + } + + private static class ConstantBooleanValue extends Value.BooleanValue { + + private Supplier booleanSupplier; + private Consumer booleanConsumer = ignored -> {}; + + public ConstantBooleanValue(Supplier booleanSupplier) { + super(booleanSupplier.get()); + this.booleanSupplier = booleanSupplier; + } + + public ConstantBooleanValue(Supplier booleanSupplier, Consumer booleanConsumer) { + super(booleanSupplier.get()); + this.booleanSupplier = booleanSupplier; + this.booleanConsumer = booleanConsumer; + } + + @Override + public long asLong() { + value = booleanSupplier.get(); + return super.asLong(); + } + + @Override + public boolean asBoolean() { + value = booleanSupplier.get(); + return super.asBoolean(); + } + + @Override + public String asString() { + value = booleanSupplier.get(); + return super.asString(); + } + + @Override + public void fromValue(Value value) { + super.fromValue(value); + booleanConsumer.accept(this.value); + } + } + + private static class ConstantStringValue extends Value.StringValue { + + private Supplier stringSupplier; + private Consumer stringConsumer = ignored -> {}; + + public ConstantStringValue(Supplier stringSupplier) { + super(stringSupplier.get()); + this.stringSupplier = stringSupplier; + } + + public ConstantStringValue(Supplier stringSupplier, Consumer stringConsumer) { + super(stringSupplier.get()); + this.stringSupplier = stringSupplier; + this.stringConsumer = stringConsumer; + } + + @Override + public long asLong() { + value = stringSupplier.get(); + return super.asLong(); + } + + @Override + public boolean asBoolean() { + value = stringSupplier.get(); + return super.asBoolean(); + } + + @Override + public String asString() { + value = stringSupplier.get(); + return super.asString(); + } + + @Override + public void fromValue(Value value) { + super.fromValue(value); + stringConsumer.accept(this.value); + } + } + static { CONSTANTS.put("trace", player -> { - return new Value.BooleanValue(RecordStateMachine.getRecordStatus().isTracing()); + return new ConstantBooleanValue(RecordStateMachine.getRecordStatus()::isTracing); }); CONSTANTS.put("autotrace", player -> { - return new Value.BooleanValue(RecordStateMachine.getRecordStatus().isAutoTrace()); + return new ConstantBooleanValue(RecordStateMachine.getRecordStatus()::isAutoTrace); }); CONSTANTS.put("tnt", player -> { - return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.DENY); + return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.DENY); }); CONSTANTS.put("tnt-onlytb", player -> { - return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.ONLY_TB); + return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.TNT, TNTMode.class) != TNTMode.ONLY_TB); }); CONSTANTS.put("freeze", player -> { - return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE); + return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.FREEZE, FreezeMode.class) == FreezeMode.ACTIVE); }); CONSTANTS.put("fire", player -> { - return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.ALLOW); + return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.FIRE, FireMode.class) == FireMode.ALLOW); }); CONSTANTS.put("protect", player -> { - return new Value.BooleanValue(Region.getRegion(player.getLocation()).getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.ACTIVE); + return new ConstantBooleanValue(() -> Region.getRegion(player.getLocation()).getPlain(Flag.PROTECT, ProtectMode.class) == ProtectMode.ACTIVE); }); CONSTANTS.put("x", player -> { - return new Value.LongValue(player.getLocation().getBlockX()); + return new ConstantLongValue(() -> (long) player.getLocation().getBlockX(), aLong -> { + Location location = player.getLocation(); + location.setX((double) aLong); + player.teleport(location); + }); }); CONSTANTS.put("y", player -> { - return new Value.LongValue(player.getLocation().getBlockY()); + return new ConstantLongValue(() -> (long) player.getLocation().getBlockY(), aLong -> { + Location location = player.getLocation(); + location.setY((double) aLong); + player.teleport(location); + }); }); CONSTANTS.put("z", player -> { - return new Value.LongValue(player.getLocation().getBlockZ()); + return new ConstantLongValue(() -> (long) player.getLocation().getBlockZ(), aLong -> { + Location location = player.getLocation(); + location.setZ((double) aLong); + player.teleport(location); + }); }); CONSTANTS.put("name", player -> { - return new Value.StringValue(player.getDisplayName()); + return new ConstantStringValue(player::getDisplayName); }); CONSTANTS.put("sneaking", player -> { - return new Value.BooleanValue(player.isSneaking()); + return new ConstantBooleanValue(player::isSneaking); + }); + CONSTANTS.put("slot", player -> { + return new ConstantLongValue(() -> (long) player.getInventory().getHeldItemSlot(), slot -> { + if (slot > 8) { + slot = 8L; + } + if (slot < 0) { + slot = 0L; + } + player.getInventory().setHeldItemSlot((int) (long) slot); + }); + }); + CONSTANTS.put("slotmaterial", player -> { + return new ConstantStringValue(() -> player.getInventory().getItemInMainHand().getType().name()); }); } @@ -70,5 +222,4 @@ public class Constants { public Value getConstant(String variableName, Player player) { return CONSTANTS.get(variableName).apply(player); } - } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java index a5bb62f8..1ad1e22b 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Context.java @@ -9,7 +9,11 @@ public class Context { private Map variables = new HashMap<>(); public void putValue(String variableName, Value value) { - variables.put(variableName, value); + if (variables.containsKey(variableName)) { + variables.get(variableName).fromValue(value); + } else { + variables.put(variableName, value); + } } public void removeValue(String variableName) { diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java index 98c1cb46..b70c776e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Value.java @@ -8,15 +8,13 @@ public interface Value { boolean asBoolean(); String asString(); - void fromLong(long value); - void fromBoolean(boolean value); - void fromString(String value); + void fromValue(Value value); @AllArgsConstructor @ToString class LongValue implements Value { - private long value; + protected long value; @Override public long asLong() { @@ -34,22 +32,8 @@ public interface Value { } @Override - public void fromLong(long value) { - this.value = value; - } - - @Override - public void fromBoolean(boolean value) { - this.value = value ? 1 : 0; - } - - @Override - public void fromString(String value) { - try { - this.value = Long.parseLong(value); - } catch (NumberFormatException e) { - this.value = 0; - } + public void fromValue(Value value) { + this.value = value.asLong(); } } @@ -57,7 +41,7 @@ public interface Value { @ToString class BooleanValue implements Value { - private boolean value; + protected boolean value; @Override public long asLong() { @@ -75,18 +59,8 @@ public interface Value { } @Override - public void fromLong(long value) { - this.value = value != 0; - } - - @Override - public void fromBoolean(boolean value) { - this.value = value; - } - - @Override - public void fromString(String value) { - this.value = value.equalsIgnoreCase("true") || value.equalsIgnoreCase("yes"); + public void fromValue(Value value) { + this.value = value.asBoolean(); } } @@ -94,7 +68,7 @@ public interface Value { @ToString class StringValue implements Value { - private String value; + protected String value; @Override public long asLong() { @@ -116,18 +90,8 @@ public interface Value { } @Override - public void fromLong(long value) { - this.value = value + ""; - } - - @Override - public void fromBoolean(boolean value) { - this.value = value + ""; - } - - @Override - public void fromString(String value) { - this.value = value; + public void fromValue(Value value) { + this.value = value.asString(); } } -- 2.39.5 From 663f380e18c74ae48f40031de0d11fcadcb0898c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 21 Sep 2021 12:55:11 +0200 Subject: [PATCH 10/11] Fix RightClick and LeftClick Signed-off-by: yoyosource --- .../bausystem/features/script/CustomScriptListener.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index 03f56d65..37f82cde 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -372,7 +372,7 @@ public class CustomScriptListener implements Listener { callEvent(EventType.RightClick, event.getPlayer(), event); } if (event.getAction() == Action.LEFT_CLICK_AIR || event.getAction() == Action.LEFT_CLICK_BLOCK) { - callEvent(EventType.RightClick, event.getPlayer(), event); + callEvent(EventType.LeftClick, event.getPlayer(), event); } } } -- 2.39.5 From ef1bdbbcf6bb44d31f0e7d1944d4b9378265c13d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 21 Sep 2021 15:51:54 +0200 Subject: [PATCH 11/11] Add Constant for sprinting and offhandmaterial Signed-off-by: yoyosource --- .../features/script/CustomScriptListener.java | 17 ++++++++++++----- .../features/script/ScriptCommand.java | 6 +++++- .../features/script/variables/Constants.java | 6 ++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java index 37f82cde..c76bc6f9 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -173,6 +173,13 @@ public class CustomScriptListener implements Listener { List> menuCommands = new ArrayList<>(); playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.MenuScript.class::isInstance).map(CustomScript.MenuScript.class::cast).forEach(menuItem -> { SWItem swItem = menuItem.toItem(); + ItemStack itemStack = swItem.getItemStack(); + if (menuItem instanceof CustomScript.MenuEvent) { + itemStack.setType(Material.REPEATING_COMMAND_BLOCK); + } else { + itemStack.setType(Material.COMMAND_BLOCK); + } + swItem.setItemStack(itemStack); swItem.setLore(Arrays.asList("§7Klicke zum rausnehmen", "§7Middle Klicke zum kopieren")); menuCommands.add(new SWListInv.SWListEntry<>(swItem, menuItem)); @@ -291,9 +298,7 @@ public class CustomScriptListener implements Listener { EventType(Class eventType, Function> eventValues) { this.eventType = eventType; - this.eventValues = event -> { - return eventValues.apply((T) event); - }; + this.eventValues = event -> eventValues.apply((T) event); } } @@ -309,14 +314,16 @@ public class CustomScriptListener implements Listener { if (variables == null) { variables = new HashMap<>(); } + if (e instanceof Cancellable) { + variables.put("cancel", new Value.BooleanValue(false)); + } customEvent.execute(e, p, variables); - if (e instanceof Cancellable && variables.containsKey("cancel")) { + if (variables.containsKey("cancel")) { Value value = variables.get("cancel"); if (value.asBoolean()) { ((Cancellable) e).setCancelled(true); } } - return; } } } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java index fbf8053b..554f5437 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/ScriptCommand.java @@ -114,11 +114,15 @@ public class ScriptCommand extends SWCommand { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.IRON_BOOTS, "§7Constant §esneak", Arrays.asList("§etrue§7 wenn der Spieler gerade sneakt."), false, clickType -> { }), null)); + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.DIAMOND_BOOTS, "§7Constant §esprinting", Arrays.asList("§etrue§7 wenn der Spieler gerade rennt."), false, clickType -> { + }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.ARROW, "§7Constant §eslot", Arrays.asList("§e0-8§7 für den ausgewählten slot.", "§eÜberschreibbar"), false, clickType -> { }), null)); swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRASS_BLOCK, "§7Constant §eslotmaterial", Arrays.asList("§eMaterial§7 des Items im Slot"), false, clickType -> { }), null)); - for (int i = 0; i < 4 + 2 * 9; i++) { + swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.IRON_BLOCK, "§7Constant §eoffhandmaterial", Arrays.asList("§eMaterial§7 des Items in oder Off Hand"), false, clickType -> { + }), null)); + for (int i = 0; i < 2 + 2 * 9; i++) { swItems.add(new SWListInv.SWListEntry<>(new SWItem(Material.GRAY_STAINED_GLASS_PANE, "§7", new ArrayList<>(), false, clickType -> { }), null)); } diff --git a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java index 8a78d10e..eeb53f07 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/variables/Constants.java @@ -195,6 +195,9 @@ public class Constants { CONSTANTS.put("sneaking", player -> { return new ConstantBooleanValue(player::isSneaking); }); + CONSTANTS.put("sprinting", player -> { + return new ConstantBooleanValue(player::isSprinting); + }); CONSTANTS.put("slot", player -> { return new ConstantLongValue(() -> (long) player.getInventory().getHeldItemSlot(), slot -> { if (slot > 8) { @@ -209,6 +212,9 @@ public class Constants { CONSTANTS.put("slotmaterial", player -> { return new ConstantStringValue(() -> player.getInventory().getItemInMainHand().getType().name()); }); + CONSTANTS.put("offhandmaterial", player -> { + return new ConstantStringValue(() -> player.getInventory().getItemInOffHand().getType().name()); + }); } public Set allVariables() { -- 2.39.5