SteamWar/BauSystem2.0
Archiviert
12
0

Add CustomScript execution for events

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2021-09-21 11:00:06 +02:00
Ursprung 36ef8be6f1
Commit 841921d458
4 geänderte Dateien mit 99 neuen und 54 gelöschten Zeilen

Datei anzeigen

@ -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<Player> 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) {

Datei anzeigen

@ -47,22 +47,22 @@ public class CustomScript {
public interface CustomEvent extends Script {
String eventName();
boolean execute(PlayerEvent e);
boolean execute(PlayerEvent e, Map<String, Value> 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<String, Value> 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<String> 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<String, Value> 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);

Datei anzeigen

@ -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<String>) key).get();
String[] event = ((YAPIONValue<String>) key).get().split(" ");
List<String> 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,9 +222,25 @@ public class CustomScriptListener implements Listener {
return;
}
}
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(menuCommand);
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;
}
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(customCommand -> customCommand == menuCommand);
playerMap.get(p).removeIf(script -> script == menuScript);
p.closeInventory();
SWUtils.giveItemToPlayer(p, p.getItemOnCursor());
save(p);
@ -229,15 +249,38 @@ public class CustomScriptListener implements Listener {
}
p.setItemOnCursor(null);
openCommandsMenu(p);
} else if (s.startsWith("#!EVENT ")) {
// Event stuff
}
}));
menuCommandSWListInv.open();
@AllArgsConstructor
@Getter
public enum EventType {
FF(PlayerSwapHandItemsEvent.class, event -> null);
private Class<? extends PlayerEvent> eventType;
private Function<PlayerEvent, Map<String, Value>> eventValues;
}
private void callEvent(EventType eventType, PlayerEvent e) {
if (!eventType.getEventType().equals(e.getClass())) {
return;
}
List<CustomScript.CustomEvent> 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<String, Value> 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<Player> 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());
}
}
}

Datei anzeigen

@ -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));