refactor(bukkit): 重构部分代码

This commit is contained in:
2023-01-18 22:36:53 +08:00
parent 078fd54ea5
commit 770689f004
17 changed files with 56 additions and 44 deletions

View File

@@ -68,8 +68,8 @@ public class HamsterCorePlugin extends JavaPlugin {
logger.info("已注册 CallbackListener.");
Bukkit.getPluginManager().registerEvents(DebugListener.INSTANCE, this);
logger.info("已注册 DebugListener.");
ParentCoreCommand.INSTANCE.hook(getCommand("HamsterCore"));
ParentLoreCommand.INSTANCE.hook(getCommand("lore"));
ParentCoreCommand.INSTANCE.hook();
ParentLoreCommand.INSTANCE.hook();
long time = System.currentTimeMillis() - start;
logger.info("仓鼠核心启动完成,总计耗时 " + time + " ms.");
}

View File

@@ -4,7 +4,7 @@ import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -12,27 +12,14 @@ import java.util.*;
import java.util.stream.Collectors;
@SuppressWarnings("unused")
public class ParentCommand extends ChildCommand {
@NotNull
private final Plugin plugin;
public abstract class ParentCommand extends ChildCommand {
@NotNull
private final String name;
@Nullable
private final ParentCommand parent;
@NotNull
private final List<ChildCommand> childCommands;
public ParentCommand(@NotNull Plugin plugin, @NotNull String name) {
this.plugin = plugin;
public ParentCommand(@NotNull String name) {
this.name = name;
parent = null;
childCommands = new ArrayList<>();
}
public ParentCommand(@NotNull Plugin plugin, @NotNull String name, @Nullable ParentCommand parent) {
this.plugin = plugin;
this.name = name;
this.parent = parent;
childCommands = new ArrayList<>();
}
@@ -41,12 +28,25 @@ public class ParentCommand extends ChildCommand {
return name;
}
@Nullable
public ParentCommand getParent() {
return null;
}
@NotNull
public abstract JavaPlugin getPlugin();
@NotNull
public List<ChildCommand> getChildCommands() {
return childCommands;
}
@NotNull
public String getUsage() {
if (parent == null) {
if (getParent() == null) {
return "/" + name;
}
return parent.getUsage() + " " + name;
return getParent().getUsage() + " " + name;
}
@Override
@@ -59,11 +59,6 @@ public class ParentCommand extends ChildCommand {
return "";
}
@NotNull
public List<ChildCommand> getChildCommands() {
return childCommands;
}
@NotNull
public List<ChildCommand> getEndChildCommands() {
ArrayList<ChildCommand> list = new ArrayList<>();
@@ -79,7 +74,7 @@ public class ParentCommand extends ChildCommand {
public void addChildCommand(@NotNull ChildCommand command) {
childCommands.add(command);
plugin.getLogger().info("已为 " + getUsage() + " 添加子命令: " + command.getName() + " .");
getPlugin().getLogger().info("已为 " + getUsage() + " 添加子命令: " + command.getName() + " .");
}
@NotNull
@@ -113,9 +108,11 @@ public class ParentCommand extends ChildCommand {
}
}
public void hook(PluginCommand command) {
public void hook() {
JavaPlugin plugin = getPlugin();
PluginCommand command = plugin.getCommand(getName());
if (command == null) {
return;
throw new IllegalArgumentException("在插件 " + plugin.getName() + " 中未找到指令 " + getName() + ".");
}
command.setExecutor(this);
command.setTabCompleter(this);

View File

@@ -2,16 +2,24 @@ package cn.hamster3.mc.plugin.core.bukkit.command.core;
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
import cn.hamster3.mc.plugin.core.bukkit.command.ParentCommand;
import cn.hamster3.mc.plugin.core.bukkit.command.core.sub.*;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
public class ParentCoreCommand extends ParentCommand {
public static final ParentCoreCommand INSTANCE = new ParentCoreCommand();
private ParentCoreCommand() {
super(HamsterCorePlugin.getInstance(), "core");
super("hamster-core");
addChildCommand(EnvCommand.INSTANCE);
addChildCommand(GCCommand.INSTANCE);
addChildCommand(YamlCommand.INSTANCE);
addChildCommand(InfoModeCommand.INSTANCE);
addChildCommand(ReloadCommand.INSTANCE);
}
@Override
public @NotNull JavaPlugin getPlugin() {
return HamsterCorePlugin.getInstance();
}
}

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.core;
package cn.hamster3.mc.plugin.core.bukkit.command.core.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import org.bukkit.command.Command;

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.core;
package cn.hamster3.mc.plugin.core.bukkit.command.core.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import org.bukkit.command.Command;

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.core;
package cn.hamster3.mc.plugin.core.bukkit.command.core.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.core;
package cn.hamster3.mc.plugin.core.bukkit.command.core.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.page.PageManager;

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.core;
package cn.hamster3.mc.plugin.core.bukkit.command.core.sub;
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;

View File

@@ -2,16 +2,18 @@ package cn.hamster3.mc.plugin.core.bukkit.command.lore;
import cn.hamster3.mc.plugin.core.bukkit.HamsterCorePlugin;
import cn.hamster3.mc.plugin.core.bukkit.command.ParentCommand;
import cn.hamster3.mc.plugin.core.bukkit.command.lore.sub.*;
import cn.hamster3.mc.plugin.core.common.util.CommonUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.jetbrains.annotations.NotNull;
public final class ParentLoreCommand extends ParentCommand {
public static final ParentLoreCommand INSTANCE = new ParentLoreCommand();
public ParentLoreCommand() {
super(HamsterCorePlugin.getInstance(), "lore");
super("lore");
addChildCommand(LoreAddCommand.INSTANCE);
addChildCommand(LoreRemoveCommand.INSTANCE);
addChildCommand(LoreSetCommand.INSTANCE);
@@ -21,6 +23,11 @@ public final class ParentLoreCommand extends ParentCommand {
addChildCommand(LoreFlagCommand.INSTANCE);
}
@Override
public @NotNull JavaPlugin getPlugin() {
return HamsterCorePlugin.getInstance();
}
@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
CommonUtils.replaceColorCode(args);

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.lore;
package cn.hamster3.mc.plugin.core.bukkit.command.lore.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.lore;
package cn.hamster3.mc.plugin.core.bukkit.command.lore.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.lore;
package cn.hamster3.mc.plugin.core.bukkit.command.lore.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.lore;
package cn.hamster3.mc.plugin.core.bukkit.command.lore.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.lore;
package cn.hamster3.mc.plugin.core.bukkit.command.lore.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.lore;
package cn.hamster3.mc.plugin.core.bukkit.command.lore.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.lore;
package cn.hamster3.mc.plugin.core.bukkit.command.lore.sub;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import cn.hamster3.mc.plugin.core.bukkit.constant.CoreMessage;

View File

@@ -23,8 +23,8 @@ libraries:
- 'com.zaxxer:HikariCP:5.0.1'
commands:
HamsterCore:
aliases: [ hcore, hc ]
hamster-core:
aliases: [ hcore, hc, core ]
description: 仓鼠核心调试指令
permission: hamster.core.admin
permission-message: §c你没有这个权限