QOL #203
@ -7,74 +7,76 @@ 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.command.PluginCommand;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.ItemMeta;
|
||||
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
|
||||
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("item");
|
||||
super("bind");
|
||||
}
|
||||
|
||||
@Register("bind")
|
||||
public void bind(Player player, @Mapper("command") String... command) {
|
||||
@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() {
|
||||
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>() {
|
||||
@Override
|
||||
public String map(CommandSender commandSender, String[] previousArguments, String s) {
|
||||
public String map(CommandSender sender, PreviousArguments previousArguments, String s) {
|
||||
return s;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Collection<String> tabCompletes(CommandSender sender, PreviousArguments previousArguments, String s) {
|
||||
List<String> args = previousArguments.getAll(String.class);
|
||||
|
||||
if (args.isEmpty()) return commands;
|
||||
|
||||
String inputCommand = args.remove(0);
|
||||
String command = reverseCommandMap.get(inputCommand);
|
||||
|
||||
if (command == null) return null;
|
||||
|
||||
PluginCommand pluginCommand = Bukkit.getPluginCommand(command);
|
||||
|
||||
if (pluginCommand == null) return null;
|
||||
|
||||
return pluginCommand.tabComplete(sender, inputCommand, args.toArray(new String[0]));
|
||||
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(":")).collect(Collectors.toSet());
|
||||
}
|
||||
return tabCompletes;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
In neuem Issue referenzieren
Einen Benutzer sperren