From 3984098883f5c1f1bad5028ba56d11469bd24207 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 20 Jan 2022 19:54:13 +0100 Subject: [PATCH] Add CustomScript.CustomGUIItem Signed-off-by: yoyosource --- BauSystem_Main/src/BauSystem.properties | 1 + .../features/script/CustomScript.java | 66 +++++++++++++++++++ .../features/script/CustomScriptListener.java | 37 ++++++++++- 3 files changed, 103 insertions(+), 1 deletion(-) diff --git a/BauSystem_Main/src/BauSystem.properties b/BauSystem_Main/src/BauSystem.properties index be1713d7..e4f094d0 100644 --- a/BauSystem_Main/src/BauSystem.properties +++ b/BauSystem_Main/src/BauSystem.properties @@ -395,6 +395,7 @@ SCRIPT_MENU_GUI_ITEM_ADD_LORE = §7Klicke mit einem Buch zum hinzufügen SCRIPT_MENU_GUI_DUPLICATE_COMMAND = §cCommand '{0}' bereits definiert SCRIPT_MENU_GUI_UNKNOWN_EVENT = §cEvent '{0}' ist nicht definierbar +SCRIPT_MENU_GUI_UNKNOWN_GUI = §cGui Item '{0}' ist nicht definierbar SCRIPT_MENU_GUI_LIMIT = §cScript-Buch Limit erreicht ## ScriptCommand 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 0645b61b..2a97636e 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScript.java @@ -32,6 +32,7 @@ import org.bukkit.inventory.meta.BookMeta; import yapion.hierarchy.types.YAPIONArray; import yapion.hierarchy.types.YAPIONMap; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -251,4 +252,69 @@ public class CustomScript { return swItem; } } + + public interface CustomGUIItem extends Script { + Material material(); + String name(); + List lore(); + boolean execute(Player p, Map variables); + } + + @RequiredArgsConstructor + public static class MenuCustomGUIItem implements CustomGUIItem, MenuScript { + public final Material material; + public final String name; + public final List pages; + + @Override + public Material material() { + return material; + } + + @Override + public String name() { + return name; + } + + @Override + public List lore() { + if (!pages.isEmpty()) { + String[] lines = pages.get(0).split("\n"); + List lore = new ArrayList<>(); + if (lines.length > 1) { + for (int i = 1; i < lines.length; i++) { + if (lines[i].isEmpty()) continue; + if (lines[i].startsWith("#")) { + lore.add(lines[i].substring(1)); + } else { + break; + } + } + } + return lore; + } + return new ArrayList<>(); + } + + @Override + public boolean execute(Player p, Map variables) { + return false; + } + + @Override + public void toYAPION(YAPIONMap yapionMap) { + YAPIONArray yapionArray = new YAPIONArray(); + pages.forEach(yapionArray::add); + yapionMap.put(material.name() + " " + name, yapionArray); + } + + @Override + public SWItem toItem(Player p) { + SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_COMMAND_ITEM_NAME", p, material.name() + " " + name)); + BookMeta bookMeta = (BookMeta) swItem.getItemMeta(); + bookMeta.setPages(pages.toArray(new String[0])); + swItem.setItemMeta(bookMeta); + 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 173b50f2..8a59dacb 100644 --- a/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java +++ b/BauSystem_Main/src/de/steamwar/bausystem/features/script/CustomScriptListener.java @@ -129,6 +129,13 @@ public class CustomScriptListener implements Listener { 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, event)); }); + + yapionObject.getYAPIONMapOrSetDefault("guis", new YAPIONMap()).forEach((key, value) -> { + String[] gui = ((YAPIONValue) key).get().split(" "); + List pages = ((YAPIONArray) value).stream().map(YAPIONValue.class::cast).map(YAPIONValue::get).map(String.class::cast).collect(Collectors.toList()); + Material material = Material.getMaterial(gui[0]); + playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.MenuCustomGUIItem(material, String.join(" ", Arrays.copyOfRange(gui, 1, gui.length)), pages)); + }); } @EventHandler @@ -150,6 +157,11 @@ public class CustomScriptListener implements Listener { playerMap.get(p).stream().filter(CustomScript.MenuEvent.class::isInstance).map(CustomScript.MenuEvent.class::cast).forEach(menuCommand -> { menuCommand.toYAPION(eventsMap); }); + YAPIONMap guisMap = new YAPIONMap(); + yapionObject.add("guis", guisMap); + playerMap.get(p).stream().filter(CustomScript.MenuCustomGUIItem.class::isInstance).map(CustomScript.MenuCustomGUIItem.class::cast).forEach(menuCommand -> { + menuCommand.toYAPION(guisMap); + }); return yapionObject; } @@ -182,7 +194,9 @@ public class CustomScriptListener implements Listener { playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.MenuScript.class::isInstance).map(CustomScript.MenuScript.class::cast).forEach(menuItem -> { SWItem swItem = menuItem.toItem(p); ItemStack itemStack = swItem.getItemStack(); - if (menuItem instanceof CustomScript.MenuEvent) { + if (menuItem instanceof CustomScript.MenuCustomGUIItem) { + itemStack.setType(Material.CHAIN_COMMAND_BLOCK); + } else if (menuItem instanceof CustomScript.MenuEvent) { itemStack.setType(Material.REPEATING_COMMAND_BLOCK); } else { itemStack.setType(Material.COMMAND_BLOCK); @@ -243,6 +257,27 @@ public class CustomScriptListener implements Listener { return; } scriptBookLimit(p, menuEvent); + } else if (s.startsWith("#!GUI ")) { + String[] strings = s.substring(5).split(" "); + if (strings.length != 2) { + BauSystem.MESSAGE.sendPrefixless("SCRIPT_MENU_GUI_UNKNOWN_GUI", p, s.substring(5)); + return; + } + Material material = null; + try { + material = Material.valueOf(strings[0]); + if (!material.isItem()) { + material = null; + } + } catch (Exception e) { + } + if (material == null) { + BauSystem.MESSAGE.sendPrefixless("SCRIPT_MENU_GUI_UNKNOWN_GUI", p, s.substring(5)); + return; + } + String name = String.join(" ", Arrays.copyOfRange(strings, 1, strings.length)); + CustomScript.MenuCustomGUIItem menuCustomGUIItem = new CustomScript.MenuCustomGUIItem(material, name, bookMeta.getPages()); + scriptBookLimit(p, menuCustomGUIItem); } })); menuCommandSWListInv.open();