updated command registration for usage without static plugin.yml file

This commit is contained in:
Elias Müller 2025-04-04 22:51:01 +02:00
parent 4592d53d22
commit 8742f5f631
2 changed files with 14 additions and 57 deletions

View File

@ -9,9 +9,11 @@ import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Constructor;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
@ -104,16 +106,22 @@ public abstract class Appliance {
} }
private void setCommandExecutor(JavaPlugin plugin, String name, ApplianceCommand<?> executor) { private void setCommandExecutor(JavaPlugin plugin, String name, ApplianceCommand<?> executor) {
PluginCommand command = plugin.getCommand(name); try {
if(command != null && executor != null) { PluginCommand command = this.createPluginCommand(name, plugin);
command.setExecutor(executor); command.setExecutor(executor);
command.setTabCompleter(executor); command.setTabCompleter(executor);
} else { plugin.getServer().getCommandMap().register(plugin.getName(), command);
Main.logger().warning("Command " + name + " is not specified in plugin.yml!"); } catch(Exception e) {
throw new RuntimeException("All commands must be registered in plugin.yml. Missing command: " + name); throw new RuntimeException(String.format("Failed to register command '%s'", name), e);
} }
} }
private PluginCommand createPluginCommand(String name, JavaPlugin plugin) throws Exception {
Constructor<PluginCommand> constructor = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
constructor.setAccessible(true);
return constructor.newInstance(name, plugin);
}
public List<Listener> getListeners() { public List<Listener> getListeners() {
return this.listeners; return this.listeners;
} }

View File

@ -3,54 +3,3 @@ author: olischma, muelleel
version: '1.0' version: '1.0'
main: eu.mhsl.craftattack.core.Main main: eu.mhsl.craftattack.core.Main
api-version: '1.21' api-version: '1.21'
commands:
moveWorldMuseumVillager:
description: Moves world museum villager to current player location and persists location to config
usage: /moveWmVillager
permission: admin
report:
description: Reporte einen Spieler
reports:
description: Von dir erstellte Reports
userinfo:
description: Get UUID from any user
projectStart:
description: Starts project countdown
projectStartCancel:
description: Cancels project countdown
projectStartReset:
description: Resets project countdown
moveEventVillager:
appliances:
event:
eventOpenSession:
eventEndSession:
eventAdvertise:
help:
spawn:
teamspeak:
aliases: [ "ts" ]
discord:
aliases: [ "dc" ]
setPlayerLimit:
scheduleRestart:
cancelRestart:
kick:
panicBan:
vogelfrei:
settings:
texturepack:
maintanance:
yearRank:
msg:
r:
playtime:
adminchat:
aliases: [ "sc" ]
acInform:
infobar:
endPrevent:
feedback:
requestFeedback:
setSpawnpoint:
mute: