SteamWar/BauSystem2.0
Archiviert
12
0

Add hotkeys
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2022-12-23 21:08:16 +01:00
Ursprung e1fc5525ec
Commit 766a3aef51
5 geänderte Dateien mit 117 neuen und 2 gelöschten Zeilen

Datei anzeigen

@ -349,6 +349,7 @@ SCRIPT_COMMAND_SLEEP_ERROR = §cThe given number needs to be greater than zero.
SCRIPT_GUI_ITEM_NAME = §eScript Help
## CustomScript
SCRIPT_HOTKEY_ITEM_NAME = §7Hotkey§8: §e{0} §8-§7 {1}
SCRIPT_EVENT_ITEM_NAME = §7Event§8: §e{0} §8-§7 {1}
SCRIPT_COMMAND_ITEM_NAME = §7Command§8: §e{0}

Datei anzeigen

@ -343,6 +343,7 @@ SCRIPT_COMMAND_SLEEP_ERROR = §cDie Zeit muss eine Zahl großer 0 sein.
SCRIPT_GUI_ITEM_NAME = §eScript Hilfe
## CustomScript
SCRIPT_HOTKEY_ITEM_NAME = §7Hotkey§8: §e{0} §8-§7 {1}
SCRIPT_EVENT_ITEM_NAME = §7Event§8: §e{0} §8-§7 {1}
SCRIPT_COMMAND_ITEM_NAME = §7Command§8: §e{0}

Datei anzeigen

@ -47,9 +47,77 @@ public class CustomScript {
SWItem toItem(Player p);
}
public interface Hotkey extends Script {
String hotkey();
boolean execute(Player p, boolean pressed);
}
@RequiredArgsConstructor
public static class InventoryHotkey implements Hotkey {
public final BookMeta bookMeta;
public final String[] args;
@Override
public String hotkey() {
return args[0];
}
@Override
public boolean execute(Player p, boolean pressed) {
Map<String, Value> variables = new HashMap<>();
variables.put("pressed", new Value.BooleanValue(pressed));
variables.put("released", new Value.BooleanValue(!pressed));
new ScriptExecutor(bookMeta, p, variables);
return false;
}
}
@RequiredArgsConstructor
public static class MenuHotkey implements Hotkey, MenuScript {
public final List<String> pages;
public final String[] args;
@Override
public String hotkey() {
return args[0];
}
@Override
public boolean execute(Player p, boolean pressed) {
Map<String, Value> variables = new HashMap<>();
variables.put("pressed", new Value.BooleanValue(pressed));
variables.put("released", new Value.BooleanValue(!pressed));
new ScriptExecutor(pages, p, variables);
return false;
}
@Override
public void toYAPION(YAPIONMap yapionMap) {
YAPIONArray yapionArray = new YAPIONArray();
pages.forEach(yapionArray::add);
yapionMap.put(String.join(" ", args), yapionArray);
}
@Override
public SWItem toItem(Player p) {
StringBuilder st = new StringBuilder();
for (int i = 1; i < args.length; i++) {
if (i != 1) st.append(" ");
st.append(args[i]);
}
SWItem swItem = new SWItem(Material.WRITABLE_BOOK, BauSystem.MESSAGE.parse("SCRIPT_HOTKEY_ITEM_NAME", p, args[0], st.toString()));
BookMeta bookMeta = (BookMeta) swItem.getItemMeta();
bookMeta.setPages(pages.toArray(new String[0]));
swItem.setItemMeta(bookMeta);
return swItem;
}
}
public interface CustomEvent extends Script {
String eventName();
boolean execute(Event e, Player p, Map<String, Value> variables);
}
@RequiredArgsConstructor

Datei anzeigen

@ -77,6 +77,8 @@ public class CustomScriptManager implements Listener {
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryCommand(bookMeta, s.substring(6).split(" ")));
} else if (s.startsWith("#!EVENT ")) {
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryEvent(bookMeta, s.substring(8).split(" ")));
} else if (s.startsWith("#!HOTKEY ")) {
playerMap.computeIfAbsent(p, player -> new ArrayList<>()).add(new CustomScript.InventoryHotkey(bookMeta, s.substring(9).split(" ")));
}
}
}
@ -118,6 +120,12 @@ public class CustomScriptManager implements Listener {
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, event));
});
yapionObject.getYAPIONMapOrSetDefault("hotkeys", new YAPIONMap()).forEach((key, value) -> {
String[] hotkey = ((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.MenuHotkey(pages, hotkey));
});
}
@EventHandler
@ -129,16 +137,24 @@ public class CustomScriptManager implements Listener {
private YAPIONObject output(Player p) {
if (!playerMap.containsKey(p)) return new YAPIONObject();
YAPIONObject yapionObject = new YAPIONObject();
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(eventsMap);
});
YAPIONMap hotkeysMap = new YAPIONMap();
yapionObject.add("hotkeys", hotkeysMap);
playerMap.get(p).stream().filter(CustomScript.MenuHotkey.class::isInstance).map(CustomScript.MenuHotkey.class::cast).forEach(menuCommand -> {
menuCommand.toYAPION(hotkeysMap);
});
return yapionObject;
}
@ -167,7 +183,9 @@ public class CustomScriptManager 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.MenuHotkey) {
itemStack.setType(Material.CHAIN_COMMAND_BLOCK);
} else if (menuItem instanceof CustomScript.MenuEvent) {
itemStack.setType(Material.REPEATING_COMMAND_BLOCK);
} else {
itemStack.setType(Material.COMMAND_BLOCK);
@ -228,6 +246,8 @@ public class CustomScriptManager implements Listener {
return;
}
scriptBookLimit(p, menuEvent);
} else if (s.startsWith("#!HOTKEY ")) {
scriptBookLimit(p, new CustomScript.MenuHotkey(bookMeta.getPages(), s.substring(9).split(" ")));
}
}));
menuCommandSWListInv.open();
@ -270,6 +290,15 @@ public class CustomScriptManager implements Listener {
}
}
public void callHotkey(String pressedHotkey, Player p, boolean pressed) {
List<CustomScript.Hotkey> hotkeys = playerMap.getOrDefault(p, new ArrayList<>()).stream().filter(CustomScript.Hotkey.class::isInstance).map(CustomScript.Hotkey.class::cast).collect(Collectors.toList());
for (CustomScript.Hotkey hotkey : hotkeys) {
if (hotkey.hotkey().equals(pressedHotkey)) {
hotkey.execute(p, pressed);
}
}
}
@EventHandler
public void onPlayerCommandPreprocess(PlayerCommandPreprocessEvent e) {
if (e.getMessage().startsWith("/script:")) {

Datei anzeigen

@ -37,6 +37,7 @@ import org.bukkit.event.entity.EntityDeathEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.*;
import org.bukkit.plugin.messaging.PluginMessageListener;
import java.util.HashMap;
import java.util.HashSet;
@ -44,7 +45,11 @@ import java.util.Map;
import java.util.Set;
@Linked
public class ScriptEventListener implements Listener {
public class ScriptEventListener implements Listener, PluginMessageListener {
{
Bukkit.getServer().getMessenger().registerIncomingPluginChannel(BauSystem.getInstance(), "sw:hotkeys", this);
}
@LinkedInstance
public CustomScriptManager customScriptManager;
@ -141,4 +146,15 @@ public class ScriptEventListener implements Listener {
customScriptManager.callEvent(EventType.EntityDeath, player, event);
}
}
@Override
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (!channel.equals("sw:hotkeys")) return;
if (message.length != 5) return;
int key = (message[0] & 0xFF) << 24 | (message[1] & 0xFF) << 16 | (message[2] & 0xFF) << 8 | (message[3] & 0xFF);
int action = message[4] & 0xFF;
if (action == 2) return;
if (!(key >= 'A' && key <= 'Z' || key >= '0' && key <= '9')) return;
customScriptManager.callHotkey(((char) key) + "", player, action == 1);
}
}