SteamWar/BauSystem2.0
Archiviert
12
0

Fix ItemBindCommand
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Signed-off-by: yoyosource <yoyosource@nidido.de>
Dieser Commit ist enthalten in:
yoyosource 2023-09-22 15:08:39 +02:00
Ursprung d810d50d22
Commit c0880fc62a

Datei anzeigen

@ -7,74 +7,76 @@ import de.steamwar.command.TypeMapper;
import de.steamwar.linkage.Linked; import de.steamwar.linkage.Linked;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey; import org.bukkit.NamespacedKey;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta; import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.persistence.PersistentDataType; import org.bukkit.persistence.PersistentDataType;
import org.luaj.vm2.ast.Str;
import java.util.*; 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 @Linked
public class ItemBindCommand extends SWCommand { 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"); private static final NamespacedKey KEY = SWUtils.getNamespaceKey("command");
public ItemBindCommand() { public ItemBindCommand() {
super("item"); super("bind");
} }
@Register("bind") @Register // TODO: Description
public void bind(Player player, @Mapper("command") String... command) { public void bind(Player player, @Mapper("command") /* TODO @ErrorMessage("...") */ String... command) {
player.sendMessage(command); player.sendMessage(command);
ItemStack item = player.getInventory().getItemInMainHand(); ItemStack item = player.getInventory().getItemInMainHand();
ItemMeta meta = item.getItemMeta(); ItemMeta meta = item.getItemMeta();
meta.getPersistentDataContainer().set(KEY, PersistentDataType.STRING, ""); meta.getPersistentDataContainer().set(KEY, PersistentDataType.STRING, "");
} }
@Mapper(value = "command", local = true) @Mapper(value = "command", local = true)
public TypeMapper<String> getCommandMapper() { public TypeMapper<String> getCommandMapper() {
Map<String, String[]> commandMap = Bukkit.getCommandAliases();
Map<String, String> reverseCommandMap = new HashMap<>();
List<String> commands = new ArrayList<>();
commandMap.forEach((command, aliases) -> {
commands.add(command);
commands.addAll(Arrays.asList(aliases));
reverseCommandMap.put(command, command);
for (String alias : aliases) {
reverseCommandMap.put(alias, command);
}
});
return new TypeMapper<String>() { return new TypeMapper<String>() {
@Override @Override
public String map(CommandSender commandSender, String[] previousArguments, String s) { public String map(CommandSender sender, PreviousArguments previousArguments, String s) {
return s; return s;
} }
@Override @Override
public Collection<String> tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) { public Collection<String> tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) {
List<String> args = previousArguments.getAll(String.class); if (previousArguments.mappedArgs.length == 0) return null;
Object[] args = (Object[]) previousArguments.mappedArgs[previousArguments.mappedArgs.length - 1];
if (args.isEmpty()) return commands; List<String> tabCompletes;
if (args.length == 0) {
String inputCommand = args.remove(0); tabCompletes = commandMap.tabComplete(sender, "");
String command = reverseCommandMap.get(inputCommand); } else {
tabCompletes = commandMap.tabComplete(sender, Arrays.stream(args).map(Objects::toString).collect(Collectors.joining(" ")) + " ");
if (command == null) return null; }
if (tabCompletes == null) return null;
PluginCommand pluginCommand = Bukkit.getPluginCommand(command); if (args.length == 0) {
return tabCompletes.stream().map(t -> t.substring(1)).filter(t -> !t.contains(":")).collect(Collectors.toSet());
if (pluginCommand == null) return null; }
return tabCompletes;
return pluginCommand.tabComplete(sender, inputCommand, args.toArray(new String[0]));
} }
}; };
} }