Start command refactor

This commit is contained in:
William278 committed 2023-03-05 19:10:19 +00:00
1 parent 39e0b25d46
commit 3e27f7c5b1
57 files changed
+728 -384

No files matched your search

@@ -5,8 +5,7 @@ import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import net.william278.annotaml.Annotaml;
import net.william278.desertwell.Version;
import net.william278.huskhomes.command.BukkitCommand;
import net.william278.huskhomes.command.BukkitCommandType;
import net.william278.huskhomes.command.CommandBase;
import net.william278.huskhomes.command.Command;
import net.william278.huskhomes.command.DisabledCommand;
import net.william278.huskhomes.config.Spawn;
import net.william278.huskhomes.config.Locales;
@@ -24,6 +23,7 @@ import net.william278.huskhomes.network.Broker;
import net.william278.huskhomes.network.PluginMessageBroker;
import net.william278.huskhomes.network.RedisBroker;
import net.william278.huskhomes.user.BukkitUser;
import net.william278.huskhomes.user.ConsoleUser;
import net.william278.huskhomes.user.OnlineUser;
import net.william278.huskhomes.position.Location;
import net.william278.huskhomes.position.Server;
@@ -76,7 +76,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, PluginMess
private UnsafeBlocks unsafeBlocks;
private EventDispatcher eventDispatcher;
private Set<PluginHook> pluginHooks;
private List<CommandBase> registeredCommands;
private List<Command> registeredCommands;
private Server server;
@Nullable
@@ -207,8 +207,8 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, PluginMess
// Register commands
this.registeredCommands = new ArrayList<>();
Arrays.stream(BukkitCommandType.values()).forEach(commandType -> {
final PluginCommand pluginCommand = getCommand(commandType.commandBase.command);
Arrays.stream(BukkitCommand.Type.values()).forEach(commandType -> {
final PluginCommand pluginCommand = getCommand(commandType.getCommand().getName());
if (pluginCommand == null) {
return;
}
@@ -216,16 +216,16 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, PluginMess
// If the command is disabled, use the disabled CommandBase
if (settings.getDisabledCommands().stream().anyMatch(disabledCommand -> {
final String command = (disabledCommand.startsWith("/") ? disabledCommand.substring(1) : disabledCommand);
return command.equalsIgnoreCase(commandType.commandBase.command) || Arrays.stream(commandType.commandBase.aliases).anyMatch(alias -> alias.equalsIgnoreCase(command));
return command.equalsIgnoreCase(commandType.getCommand().command) || Arrays.stream(commandType.getCommand().aliases).anyMatch(alias -> alias.equalsIgnoreCase(command));
})) {
new BukkitCommand(new DisabledCommand(this), this).register(pluginCommand);
return;
}
// Otherwise, register the command
final CommandBase commandBase = commandType.commandBase;
this.registeredCommands.add(commandBase);
new BukkitCommand(commandBase, this).register(pluginCommand);
final Command command = commandType.getCommand();
this.registeredCommands.add(command);
new BukkitCommand(command, this).register(pluginCommand);
});
getLoggingAdapter().log(Level.INFO, "Successfully registered permissions & commands.");
@@ -286,6 +286,12 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, PluginMess
return logger;
}
@Override
@NotNull
public ConsoleUser getConsole() {
return new ConsoleUser(audiences.console());
}
@NotNull
@Override
public List<OnlineUser> getOnlineUsers() {
@@ -422,7 +428,7 @@ public class BukkitHuskHomes extends JavaPlugin implements HuskHomes, PluginMess
@Override
@NotNull
public List<CommandBase> getCommands() {
public List<Command> getCommands() {
return registeredCommands;
}
@@ -17,10 +17,10 @@ public class BrigadierUtil {
*
* @param plugin instance of the registering Bukkit plugin
* @param pluginCommand the Bukkit PluginCommand to register completions for
* @param command the {@link CommandBase} to register completions for
* @param command the {@link Command} to register completions for
*/
protected static void registerCommodore(@NotNull BukkitHuskHomes plugin, @NotNull PluginCommand pluginCommand,
@NotNull CommandBase command) {
@NotNull Command command) {
final InputStream commandCommodore = plugin.getResource("commodore/" + pluginCommand.getName() + ".commodore");
if (commandCommodore == null) {
return;
@@ -5,73 +5,106 @@ import net.william278.huskhomes.BukkitHuskHomes;
import net.william278.huskhomes.user.BukkitUser;
import org.bukkit.command.*;
import org.bukkit.entity.Player;
import org.bukkit.permissions.Permission;
import org.bukkit.permissions.PermissionDefault;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.List;
import java.util.logging.Level;
import java.util.*;
/**
* Bukkit executor that implements and executes {@link CommandBase}s
*/
public class BukkitCommand implements CommandExecutor, TabExecutor {
public class BukkitCommand implements CommandExecutor, TabCompleter {
/**
* The {@link CommandBase} that will be executed
*/
private final CommandBase command;
/**
* The implementing plugin
*/
private final BukkitHuskHomes plugin;
private final Command command;
public BukkitCommand(@NotNull CommandBase command, @NotNull BukkitHuskHomes implementor) {
public BukkitCommand(@NotNull Command command, @NotNull BukkitHuskHomes plugin) {
this.command = command;
this.plugin = implementor;
}
/**
* Registers a {@link PluginCommand} to this implementation
*
* @param pluginCommand {@link PluginCommand} to register
*/
public void register(@NotNull PluginCommand pluginCommand) {
pluginCommand.setPermission(command.permission);
pluginCommand.setDescription(command.getDescription());
pluginCommand.setExecutor(this);
pluginCommand.setTabCompleter(this);
if (CommodoreProvider.isSupported()) {
BrigadierUtil.registerCommodore(plugin, pluginCommand, command);
}
this.plugin = plugin;
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command,
public boolean onCommand(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command,
@NotNull String label, @NotNull String[] args) {
if (sender instanceof Player player) {
this.command.onExecute(BukkitUser.adapt(player), args);
} else {
if (this.command instanceof ConsoleExecutable consoleExecutable) {
consoleExecutable.onConsoleExecute(args);
} else {
plugin.getLoggingAdapter().log(Level.WARNING, plugin.getLocales()
.getRawLocale("error_in_game_only")
.orElse("Error: That command can only be run in-game."));
}
}
this.command.execute(sender instanceof Player player ? BukkitUser.adapt(player) : plugin.getConsole(), args);
return true;
}
@Nullable
@Override
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull Command command,
public List<String> onTabComplete(@NotNull CommandSender sender, @NotNull org.bukkit.command.Command command,
@NotNull String alias, @NotNull String[] args) {
if (this.command instanceof TabCompletable tabCompletable) {
return tabCompletable.onTabComplete(args, (sender instanceof Player ? BukkitUser.adapt((Player) sender) : null));
}
return Collections.emptyList();
return this.command.suggest(sender instanceof Player player ? BukkitUser.adapt(player) : plugin.getConsole(), args);
}
}
public void register() {
// Register with bukkit
final PluginCommand pluginCommand = Objects.requireNonNull(plugin.getCommand(command.getName()));
pluginCommand.setExecutor(this);
pluginCommand.setTabCompleter(this);
// Register commodore TAB completion
if (CommodoreProvider.isSupported() && plugin.getSettings().doBrigadierTabCompletion()) {
BrigadierUtil.registerCommodore(plugin, pluginCommand, command);
}
// Register permissions
final PluginManager manager = plugin.getServer().getPluginManager();
command.getChildren()
.stream().map(child -> new Permission(child.getPermission(), child.getUsage(),
child.isOperatorCommand() ? PermissionDefault.OP : PermissionDefault.TRUE))
.forEach(manager::addPermission);
manager.addPermission(new Permission(command.getPermission(), "/" + command.getName(),
command.isOperatorCommand() ? PermissionDefault.OP : PermissionDefault.TRUE));
// Register master permission
final Map<String, Boolean> childNodes = new HashMap<>();
command.getChildren().forEach(child -> childNodes.put(child.getPermission(), true));
manager.addPermission(new Permission(command.getPermission() + ".*", command.getUsage(),
PermissionDefault.FALSE, childNodes));
}
/**
* Commands available on the Bukkit HuskHomes implementation
*/
public enum Type {
HOME_COMMAND(new HomeCommand(BukkitHuskHomes.getInstance())),
SET_HOME_COMMAND(new SetHomeCommand(BukkitHuskHomes.getInstance())),
HOME_LIST_COMMAND(new HomeListCommand(BukkitHuskHomes.getInstance())),
DEL_HOME_COMMAND(new DelHomeCommand(BukkitHuskHomes.getInstance())),
EDIT_HOME_COMMAND(new EditHomeCommand(BukkitHuskHomes.getInstance())),
PUBLIC_HOME_COMMAND(new PublicHomeCommand(BukkitHuskHomes.getInstance())),
PUBLIC_HOME_LIST_COMMAND(new PublicHomeListCommand(BukkitHuskHomes.getInstance())),
WARP_COMMAND(new WarpCommand(BukkitHuskHomes.getInstance())),
SET_WARP_COMMAND(new SetWarpCommand(BukkitHuskHomes.getInstance())),
WARP_LIST_COMMAND(new WarpListCommand(BukkitHuskHomes.getInstance())),
DEL_WARP_COMMAND(new DelWarpCommand(BukkitHuskHomes.getInstance())),
EDIT_WARP_COMMAND(new EditWarpCommand(BukkitHuskHomes.getInstance())),
TP_COMMAND(new TpCommand(BukkitHuskHomes.getInstance())),
TP_HERE_COMMAND(new TpHereCommand(BukkitHuskHomes.getInstance())),
TPA_COMMAND(new TpaCommand(BukkitHuskHomes.getInstance())),
TPA_HERE_COMMAND(new TpaHereCommand(BukkitHuskHomes.getInstance())),
TPACCEPT_COMMAND(new TpRespondCommand(BukkitHuskHomes.getInstance(), true)),
TPDECLINE_COMMAND(new TpRespondCommand(BukkitHuskHomes.getInstance(), false)),
RTP_COMMAND(new RtpCommand(BukkitHuskHomes.getInstance())),
TP_IGNORE_COMMAND(new TpIgnoreCommand(BukkitHuskHomes.getInstance())),
TP_OFFLINE_COMMAND(new TpOfflineCommand(BukkitHuskHomes.getInstance())),
TP_ALL_COMMAND(new TpAllCommand(BukkitHuskHomes.getInstance())),
TPA_ALL_COMMAND(new TpaAllCommand(BukkitHuskHomes.getInstance())),
SPAWN_COMMAND(new SpawnCommand(BukkitHuskHomes.getInstance())),
SET_SPAWN_COMMAND(new SetSpawnCommand(BukkitHuskHomes.getInstance())),
BACK_COMMAND(new BackCommand(BukkitHuskHomes.getInstance())),
HUSKHOMES_COMMAND(new HuskHomesCommand(BukkitHuskHomes.getInstance()));
private final Command command;
Type(@NotNull Command command) {
this.command = command;
}
@NotNull
public Command getCommand() {
return command;
}
}
}
@@ -1,48 +0,0 @@
package net.william278.huskhomes.command;
import net.william278.huskhomes.BukkitHuskHomes;
import org.jetbrains.annotations.NotNull;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* Commands available on the Bukkit HuskHomes implementation
*/
public enum BukkitCommandType {
HOME_COMMAND(new HomeCommand(BukkitHuskHomes.getInstance())),
SET_HOME_COMMAND(new SetHomeCommand(BukkitHuskHomes.getInstance())),
HOME_LIST_COMMAND(new HomeListCommand(BukkitHuskHomes.getInstance())),
DEL_HOME_COMMAND(new DelHomeCommand(BukkitHuskHomes.getInstance())),
EDIT_HOME_COMMAND(new EditHomeCommand(BukkitHuskHomes.getInstance())),
PUBLIC_HOME_COMMAND(new PublicHomeCommand(BukkitHuskHomes.getInstance())),
PUBLIC_HOME_LIST_COMMAND(new PublicHomeListCommand(BukkitHuskHomes.getInstance())),
WARP_COMMAND(new WarpCommand(BukkitHuskHomes.getInstance())),
SET_WARP_COMMAND(new SetWarpCommand(BukkitHuskHomes.getInstance())),
WARP_LIST_COMMAND(new WarpListCommand(BukkitHuskHomes.getInstance())),
DEL_WARP_COMMAND(new DelWarpCommand(BukkitHuskHomes.getInstance())),
EDIT_WARP_COMMAND(new EditWarpCommand(BukkitHuskHomes.getInstance())),
TP_COMMAND(new TpCommand(BukkitHuskHomes.getInstance())),
TP_HERE_COMMAND(new TpHereCommand(BukkitHuskHomes.getInstance())),
TPA_COMMAND(new TpaCommand(BukkitHuskHomes.getInstance())),
TPA_HERE_COMMAND(new TpaHereCommand(BukkitHuskHomes.getInstance())),
TPACCEPT_COMMAND(new TpRespondCommand(BukkitHuskHomes.getInstance(), true)),
TPDECLINE_COMMAND(new TpRespondCommand(BukkitHuskHomes.getInstance(), false)),
RTP_COMMAND(new RtpCommand(BukkitHuskHomes.getInstance())),
TP_IGNORE_COMMAND(new TpIgnoreCommand(BukkitHuskHomes.getInstance())),
TP_OFFLINE_COMMAND(new TpOfflineCommand(BukkitHuskHomes.getInstance())),
TP_ALL_COMMAND(new TpAllCommand(BukkitHuskHomes.getInstance())),
TPA_ALL_COMMAND(new TpaAllCommand(BukkitHuskHomes.getInstance())),
SPAWN_COMMAND(new SpawnCommand(BukkitHuskHomes.getInstance())),
SET_SPAWN_COMMAND(new SetSpawnCommand(BukkitHuskHomes.getInstance())),
BACK_COMMAND(new BackCommand(BukkitHuskHomes.getInstance())),
HUSKHOMES_COMMAND(new HuskHomesCommand(BukkitHuskHomes.getInstance()));
public final CommandBase commandBase;
BukkitCommandType(@NotNull CommandBase commandBase) {
this.commandBase = commandBase;
}
}
@@ -2,7 +2,7 @@ package net.william278.huskhomes.hook;
import net.william278.huskhomes.BukkitHuskHomes;
import net.william278.huskhomes.HuskHomes;
import net.william278.huskhomes.command.BukkitCommandType;
import net.william278.huskhomes.command.BukkitCommand;
import net.william278.huskhomes.user.User;
import net.william278.huskhomes.position.Home;
import net.william278.huskhomes.position.Warp;
@@ -74,8 +74,8 @@ public class DynMapHook extends MapHook {
.setDescription(MarkerInformationPopup.create(home.getMeta().getName())
.setThumbnailMarker(PUBLIC_HOME_MARKER_IMAGE_NAME)
.addField("Owner", home.getOwner().getUsername())
.addField("Description", plugin.getLocales().formatDescription(home.getMeta().getDescription()))
.addField("Command", "/" + BukkitCommandType.PUBLIC_HOME_COMMAND.commandBase.command + " " + home.getMeta().getName())
.addField("Description", plugin.getLocales().wrapText(home.getMeta().getDescription()))
.addField("Command", "/" + BukkitCommand.Type.PUBLIC_HOME_COMMAND.getCommand().command + " " + home.getMeta().getName())
.toHtml());
updatedFuture.complete(null);
}));
@@ -120,8 +120,8 @@ public class DynMapHook extends MapHook {
warp.getX(), warp.getY(), warp.getZ(), dynmapAPI.getMarkerAPI().getMarkerIcon(WARP_MARKER_IMAGE_NAME), false)
.setDescription(MarkerInformationPopup.create(warp.getMeta().getName())
.setThumbnailMarker(WARP_MARKER_IMAGE_NAME)
.addField("Description", plugin.getLocales().formatDescription(warp.getMeta().getDescription()))
.addField("Command", "/" + BukkitCommandType.WARP_COMMAND.commandBase.command + " " + warp.getMeta().getName())
.addField("Description", plugin.getLocales().wrapText(warp.getMeta().getDescription()))
.addField("Command", "/" + BukkitCommand.Type.WARP_COMMAND.getCommand().command + " " + warp.getMeta().getName())
.toHtml());
updatedFuture.complete(null);
}));
@@ -102,7 +102,8 @@ public class BukkitUser extends OnlineUser {
}
@Override
protected @NotNull Audience getAudience() {
@NotNull
public Audience getAudience() {
return plugin.getAudiences().player(player);
}
@@ -4,7 +4,7 @@ import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import de.themoep.minedown.adventure.MineDown;
import net.william278.huskhomes.command.BukkitCommandType;
import net.william278.huskhomes.command.BukkitCommand;
import net.william278.huskhomes.user.BukkitUser;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
@@ -41,8 +41,8 @@ public class BukkitPluginTests {
@Test
public void testCommandRegistration() {
// Assert that all commands in BukkitCommandType are registered by the plugin
for (BukkitCommandType command : BukkitCommandType.values()) {
Assertions.assertNotNull(plugin.getCommand(command.commandBase.command));
for (BukkitCommand.Type command : BukkitCommand.Type.values()) {
Assertions.assertNotNull(plugin.getCommand(command.getCommand().command));
}
}