command register relogic

This commit is contained in:
MeexReay 2024-12-30 19:28:51 +03:00
parent 265000e775
commit 759802b586
2 changed files with 23 additions and 30 deletions

View File

@ -12,26 +12,6 @@ spigot {
main "ru.themixray.puton.Main" main "ru.themixray.puton.Main"
apiVersion "1.17" apiVersion "1.17"
named "PutOn" named "PutOn"
command {
named "helmet"
description "Puts on item from your hand as helmet"
}
command {
named "chestplate"
description "Puts on item from your hand as chestplate"
}
command {
named "leggings"
description "Puts on item from your hand as leggings"
}
command {
named "boots"
description "Puts on item from your hand as boots"
}
} }
} }

View File

@ -2,6 +2,7 @@ package ru.themixray.puton;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.*; import org.bukkit.command.*;
import org.bukkit.command.defaults.BukkitCommand;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import ru.themixray.puton.command.BootsCommand; import ru.themixray.puton.command.BootsCommand;
@ -18,10 +19,10 @@ public final class Main extends JavaPlugin implements Listener {
public void onEnable() { public void onEnable() {
Config.loadConfig(new UnrealConfig(this, getDataFolder(), "config.yml")); Config.loadConfig(new UnrealConfig(this, getDataFolder(), "config.yml"));
Bukkit.getPluginManager().registerEvents(new SwapItemListener(), this); Bukkit.getPluginManager().registerEvents(new SwapItemListener(), this);
registerCommand(getCommand("helmet"), Config.HELMET_COMMANDS, new HelmetCommand()); registerCommand(Config.HELMET_COMMANDS, "Puts on item from your hand as helmet", new HelmetCommand());
registerCommand(getCommand("chestplate"), Config.CHESTPLATE_COMMANDS, new ChestplateCommand()); registerCommand(Config.CHESTPLATE_COMMANDS, "Puts on item from your hand as chestplate", new ChestplateCommand());
registerCommand(getCommand("leggings"), Config.LEGGINGS_COMMANDS, new LeggingsCommand()); registerCommand(Config.LEGGINGS_COMMANDS, "Puts on item from your hand as leggings", new LeggingsCommand());
registerCommand(getCommand("boots"), Config.BOOTS_COMMANDS, new BootsCommand()); registerCommand(Config.BOOTS_COMMANDS, "Puts on item from your hand as boots", new BootsCommand());
} }
public CommandMap getCommandMap() { public CommandMap getCommandMap() {
@ -34,14 +35,26 @@ public final class Main extends JavaPlugin implements Listener {
} }
} }
public void registerCommand(PluginCommand command, List<String> names, CommandExecutor executor) { public void registerCommand(List<String> names, String description, CommandExecutor executor) {
if (names.isEmpty()) { if (names.isEmpty()) {
command.unregister(getCommandMap());
return; return;
} }
command.setExecutor(executor); BukkitCommand command = new BukkitCommand(
command.setName(names.get(0)); names.get(0),
if (names.size() > 1) command.setAliases(names.subList(1, names.size())); description,
command.setTabCompleter((a, b, c, d) -> List.of()); "/"+names.get(0),
names.subList(1, names.size())
) {
@Override
public boolean execute(CommandSender sender, String alias, String[] args) {
return executor.onCommand(sender, this, alias, args);
}
@Override
public List<String> tabComplete(CommandSender a, String b, String[] c) throws IllegalArgumentException {
return List.of();
}
};
getCommandMap().register(names.get(0), command);
} }
} }