SteamWar/BauSystem2.0
Archiviert
12
0

Finalize BindCommand
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2023-09-23 14:35:23 +02:00
Ursprung 438010a7fb
Commit 22334ab15a
5 geänderte Dateien mit 162 neuen und 87 gelöschten Zeilen

Datei anzeigen

@ -708,6 +708,12 @@ OTHER_TPS_HEAD = §7TPS: 1s 10s 1m 5m 10m
OTHER_TPS_MESSAGE = §7 §e{0}§7 §e{1}§7 §e{2}§7 §e{3}§7 §e{4} OTHER_TPS_MESSAGE = §7 §e{0}§7 §e{1}§7 §e{2}§7 §e{3}§7 §e{4}
OTHER_TPS_SINGLE = §8TPS: §e{0} OTHER_TPS_SINGLE = §8TPS: §e{0}
OTHER_WORLDSPAWN_HELP=§8/§eworldspawn §8-§e Teleport to the spawn OTHER_WORLDSPAWN_HELP=§8/§eworldspawn §8-§e Teleport to the spawn
OTHER_BIND_HELP=§8/§ebind §8[§7Command§8] §8-§e Bind a command on item interaction
OTHER_BIND_ERROR=§cInvalid or unknown command
OTHER_BIND_UNBINDABLE=§cCould not bind command
OTHER_BIND_LORE=§eCommand§8:§7 {0}
OTHER_BIND_MESSAGE_BIND=§7Bound command §e{0} §7to item
OTHER_BIND_MESSAGE_UNBIND=§7Unbound command
# DebugStick # DebugStick
DEBUG_STICK_COMMAND_HELP=§8/§edebugstick §8-§7 receive a debugstick DEBUG_STICK_COMMAND_HELP=§8/§edebugstick §8-§7 receive a debugstick
DEBUG_STICK_NAME=§eDebugstick DEBUG_STICK_NAME=§eDebugstick

Datei anzeigen

@ -674,6 +674,11 @@ OTHER_TIME_RESULT=§7§oWhooosh
OTHER_TPS_HEAD = §7TPS: 1s 10s 1m 5m 10m OTHER_TPS_HEAD = §7TPS: 1s 10s 1m 5m 10m
OTHER_TPS_MESSAGE = §7 §e{0}§7 §e{1}§7 §e{2}§7 §e{3}§7 §e{4} OTHER_TPS_MESSAGE = §7 §e{0}§7 §e{1}§7 §e{2}§7 §e{3}§7 §e{4}
OTHER_TPS_SINGLE = §8TPS: §e{0} OTHER_TPS_SINGLE = §8TPS: §e{0}
OTHER_BIND_HELP=§8/§ebind §8[§7Command§8] §8-§e Binde ein Befehl auf Item Interaktion
OTHER_BIND_ERROR=§cFalscher oder unbekannter Befehl
OTHER_BIND_UNBINDABLE=§cBefehl konnte nicht gebunden werden
OTHER_BIND_MESSAGE_BIND=§7Befehl §e{0} §7ans Item gebunden
OTHER_BIND_MESSAGE_UNBIND=§7Befehl entbunden
# DebugStick # DebugStick
DEBUG_STICK_COMMAND_HELP=§8/§edebugstick §8-§7 Erhalte einen DebugStick DEBUG_STICK_COMMAND_HELP=§8/§edebugstick §8-§7 Erhalte einen DebugStick
DEBUG_STICK_NAME=§eDebugstick DEBUG_STICK_NAME=§eDebugstick

Datei anzeigen

@ -48,6 +48,10 @@ public class ScriptRunner {
private static final Map<Player, Map<Hotkey, List<LuaFunction>>> HOTKEY_MAP = new HashMap<>(); private static final Map<Player, Map<Hotkey, List<LuaFunction>>> HOTKEY_MAP = new HashMap<>();
private static final Map<Player, Map<String, CommandRegister>> COMMAND_MAP = new HashMap<>(); private static final Map<Player, Map<String, CommandRegister>> COMMAND_MAP = new HashMap<>();
public Set<String> getCommandsOfPlayer(Player player) {
return COMMAND_MAP.getOrDefault(player, new HashMap<>()).keySet();
}
public static void runScript(String script, Player player) { public static void runScript(String script, Player player) {
Globals globals = SteamWarPlatform.createClickGlobals(player); Globals globals = SteamWarPlatform.createClickGlobals(player);
catchScript("SCRIPT_ERROR_CLICK", player, () -> globals.load(script).call()); catchScript("SCRIPT_ERROR_CLICK", player, () -> globals.load(script).call());

Datei anzeigen

@ -0,0 +1,147 @@
package de.steamwar.bausystem.features.util;
import de.steamwar.bausystem.BauSystem;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.bausystem.features.script.ScriptCommand;
import de.steamwar.bausystem.features.script.ScriptRunner;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerCommandPreprocessEvent;
import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import java.lang.reflect.Field;
import java.util.*;
import java.util.stream.Collectors;
@Linked
public class BindCommand extends SWCommand implements Listener {
@Linked
public static class UnBindCommand extends SWCommand {
public UnBindCommand() {
super("unbind");
}
@Register
public void generic(Player player) {
bindInternal(player);
}
}
private static final CommandMap commandMap;
static {
Field knownCommandsField;
try {
knownCommandsField = Bukkit.getServer().getClass().getDeclaredField("commandMap");
knownCommandsField.setAccessible(true);
commandMap = (CommandMap)knownCommandsField.get(Bukkit.getServer());
} catch (IllegalAccessException | NoSuchFieldException var2) {
Bukkit.shutdown();
throw new SecurityException("Oh shit. Commands cannot be registered.", var2);
}
}
private static final NamespacedKey KEY = SWUtils.getNamespaceKey("command");
public BindCommand() {
super("bind");
}
@Register(description = "OTHER_BIND_HELP")
public void bind(Player player, @Mapper("command") @ErrorMessage("OTHER_BIND_ERROR") String... command) {
bindInternal(player, command);
}
private static void bindInternal(Player player, String... command) {
ItemStack item = player.getInventory().getItemInMainHand();
ItemMeta meta = item.getItemMeta();
if (meta == null) {
BauSystem.MESSAGE.send("OTHER_BIND_UNBINDABLE", player);
return;
}
if (command.length != 0) {
String commandText = String.join(" ", command);
meta.getPersistentDataContainer().set(KEY, PersistentDataType.STRING, commandText);
meta.setLore(Collections.singletonList(BauSystem.MESSAGE.parse("OTHER_BIND_LORE", player, commandText)));
BauSystem.MESSAGE.send("OTHER_BIND_MESSAGE_BIND", player, commandText);
} else {
meta.getPersistentDataContainer().remove(KEY);
meta.setLore(Collections.emptyList());
BauSystem.MESSAGE.send("OTHER_BIND_MESSAGE_UNBIND", player);
}
item.setItemMeta(meta);
player.getInventory().setItemInMainHand(item);
}
@EventHandler
public void onPlayerInteract(PlayerInteractEvent event) {
if (!event.hasItem()) return;
ItemStack itemStack = event.getItem();
ItemMeta meta = itemStack.getItemMeta();
if (meta == null) {
return;
}
String command = meta.getPersistentDataContainer().get(KEY, PersistentDataType.STRING);
if (command == null) {
return;
}
event.setCancelled(true);
Bukkit.getScheduler().runTaskLater(BauSystem.getInstance(), () -> {
PlayerCommandPreprocessEvent playerCommandPreprocessEvent = new PlayerCommandPreprocessEvent(event.getPlayer(), "/" + command);
Bukkit.getPluginManager().callEvent(playerCommandPreprocessEvent);
if (playerCommandPreprocessEvent.isCancelled()) return;
Bukkit.getServer().dispatchCommand(event.getPlayer(), command);
}, 1);
}
@Mapper(value = "command", local = true)
public TypeMapper<String> getCommandMapper() {
return new TypeMapper<String>() {
@Override
public String map(CommandSender sender, PreviousArguments previousArguments, String s) {
return s;
}
@Override
public Collection<String> tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) {
if (previousArguments.mappedArgs.length == 0) return null;
Object[] args = (Object[]) previousArguments.mappedArgs[previousArguments.mappedArgs.length - 1];
List<String> tabCompletes;
if (args.length == 0) {
tabCompletes = commandMap.tabComplete(sender, "");
} else {
tabCompletes = commandMap.tabComplete(sender, Arrays.stream(args).map(Objects::toString).collect(Collectors.joining(" ")) + " ");
}
if (tabCompletes == null) return null;
if (args.length == 0) {
tabCompletes = tabCompletes.stream()
.map(t -> t.substring(1))
.filter(t -> !t.equals("bind"))
.collect(Collectors.toList());
tabCompletes.addAll(ScriptRunner.getCommandsOfPlayer((Player) sender));
return tabCompletes.stream()
.filter(t -> !t.contains(":"))
.collect(Collectors.toList());
}
return tabCompletes;
}
};
}
}

Datei anzeigen

@ -1,87 +0,0 @@
package de.steamwar.bausystem.features.util;
import de.steamwar.bausystem.SWUtils;
import de.steamwar.command.PreviousArguments;
import de.steamwar.command.SWCommand;
import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
@Linked
public class ItemBindCommand extends SWCommand {
private static final CommandMap commandMap;
static {
Field knownCommandsField;
try {
knownCommandsField = Bukkit.getServer().getClass().getDeclaredField("commandMap");
knownCommandsField.setAccessible(true);
commandMap = (CommandMap)knownCommandsField.get(Bukkit.getServer());
} catch (IllegalAccessException | NoSuchFieldException var2) {
Bukkit.shutdown();
throw new SecurityException("Oh shit. Commands cannot be registered.", var2);
}
}
private static final NamespacedKey KEY = SWUtils.getNamespaceKey("command");
public ItemBindCommand() {
super("bind");
}
@Register // TODO: Description
public void bind(Player player, @Mapper("command") /* TODO @ErrorMessage("...") */ String... command) {
player.sendMessage(command);
ItemStack item = player.getInventory().getItemInMainHand();
ItemMeta meta = item.getItemMeta();
meta.getPersistentDataContainer().set(KEY, PersistentDataType.STRING, "");
}
@Mapper(value = "command", local = true)
public TypeMapper<String> getCommandMapper() {
return new TypeMapper<String>() {
@Override
public String map(CommandSender sender, PreviousArguments previousArguments, String s) {
return s;
}
@Override
public Collection<String> tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) {
if (previousArguments.mappedArgs.length == 0) return null;
Object[] args = (Object[]) previousArguments.mappedArgs[previousArguments.mappedArgs.length - 1];
List<String> tabCompletes;
if (args.length == 0) {
tabCompletes = commandMap.tabComplete(sender, "");
} else {
tabCompletes = commandMap.tabComplete(sender, Arrays.stream(args).map(Objects::toString).collect(Collectors.joining(" ")) + " ");
}
if (tabCompletes == null) return null;
if (args.length == 0) {
return tabCompletes.stream()
.map(t -> t.substring(1))
.filter(t -> !t.contains(":"))
.filter(t -> !t.equals("bind"))
.collect(Collectors.toSet());
}
return tabCompletes;
}
};
}
}