From 841921d458156207e77af538a4d3e73f06f85241 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Tue, 21 Sep 2021 11:00:06 +0200 Subject: [PATCH] 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));