feat: 添加 GC 命令

This commit is contained in:
2022-10-26 14:21:39 +08:00
parent 1c424f6eed
commit 17702cb8bf
8 changed files with 92 additions and 32 deletions

View File

@@ -1,9 +1,7 @@
package cn.hamster3.mc.plugin.core.bukkit;
import cn.hamster3.mc.plugin.core.bukkit.api.CoreBukkitAPI;
import cn.hamster3.mc.plugin.core.bukkit.command.ParentCommand;
import cn.hamster3.mc.plugin.core.bukkit.command.debug.BlockInfoCommand;
import cn.hamster3.mc.plugin.core.bukkit.command.debug.YamlCommand;
import cn.hamster3.mc.plugin.core.bukkit.command.core.ParentCoreCommand;
import cn.hamster3.mc.plugin.core.bukkit.command.lore.ParentLoreCommand;
import cn.hamster3.mc.plugin.core.bukkit.hook.PointAPI;
import cn.hamster3.mc.plugin.core.bukkit.hook.VaultAPI;
@@ -13,15 +11,11 @@ import cn.hamster3.mc.plugin.core.bukkit.page.listener.PageListener;
import cn.hamster3.mc.plugin.core.common.constant.CoreConstantObjects;
import net.kyori.adventure.platform.bukkit.BukkitAudiences;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.List;
import java.util.logging.Logger;
public class HamsterCorePlugin extends JavaPlugin {
public static final ParentCommand COMMAND_EXECUTOR = new ParentCommand("core");
private static HamsterCorePlugin instance;
private BukkitAudiences audienceProvider;
@@ -42,12 +36,6 @@ public class HamsterCorePlugin extends JavaPlugin {
logger.info("仓鼠核心正在初始化...");
CoreBukkitAPI.init();
logger.info("CoreBukkitAPI 已初始化.");
COMMAND_EXECUTOR.addChildCommand(BlockInfoCommand.INSTANCE);
logger.info("已添加指令: " + BlockInfoCommand.INSTANCE.getName() + " .");
COMMAND_EXECUTOR.addChildCommand(YamlCommand.INSTANCE);
logger.info("已添加指令: " + YamlCommand.INSTANCE.getName() + " .");
COMMAND_EXECUTOR.addChildCommand(ParentLoreCommand.INSTANCE);
logger.info("已添加指令: " + ParentLoreCommand.INSTANCE.getName() + " .");
long time = System.currentTimeMillis() - start;
logger.info("仓鼠核心初始化完成,总计耗时 " + time + " ms.");
}
@@ -69,6 +57,9 @@ public class HamsterCorePlugin extends JavaPlugin {
logger.info("已注册 CallbackListener.");
Bukkit.getPluginManager().registerEvents(DebugListener.INSTANCE, this);
logger.info("已注册 DebugListener.");
//noinspection SpellCheckingInspection
ParentCoreCommand.INSTANCE.hook(getCommand("hamstercore"));
ParentLoreCommand.INSTANCE.hook(getCommand("lore"));
long time = System.currentTimeMillis() - start;
logger.info("仓鼠核心启动完成,总计耗时 " + time + " ms.");
}
@@ -86,16 +77,6 @@ public class HamsterCorePlugin extends JavaPlugin {
logger.info("仓鼠核心已关闭,总计耗时 " + time + " ms.");
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
return COMMAND_EXECUTOR.onCommand(sender, command, label, args);
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
return COMMAND_EXECUTOR.onTabComplete(sender, command, alias, args);
}
public BukkitAudiences getAudienceProvider() {
return audienceProvider;
}

View File

@@ -3,6 +3,8 @@ package cn.hamster3.mc.plugin.core.bukkit.command;
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.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -11,6 +13,8 @@ import java.util.stream.Collectors;
@SuppressWarnings("unused")
public class ParentCommand extends ChildCommand {
@NotNull
private final Plugin plugin;
@NotNull
private final String name;
@Nullable
@@ -18,13 +22,15 @@ public class ParentCommand extends ChildCommand {
@NotNull
private final List<ChildCommand> childCommands;
public ParentCommand(@NotNull String name) {
public ParentCommand(@NotNull Plugin plugin, @NotNull String name) {
this.plugin = plugin;
this.name = name;
parent = null;
childCommands = new ArrayList<>();
}
public ParentCommand(@NotNull String name, @Nullable ParentCommand parent) {
public ParentCommand(@NotNull Plugin plugin, @NotNull String name, @Nullable ParentCommand parent) {
this.plugin = plugin;
this.name = name;
this.parent = parent;
childCommands = new ArrayList<>();
@@ -73,8 +79,10 @@ public class ParentCommand extends ChildCommand {
public void addChildCommand(@NotNull ChildCommand command) {
childCommands.add(command);
plugin.getLogger().info("已为 " + getUsage() + " 添加子命令: " + command.getName() + " .");
}
@NotNull
public Map<String, String> getCommandHelp(CommandSender sender) {
HashMap<String, String> map = new HashMap<>();
for (ChildCommand child : childCommands) {
@@ -91,6 +99,12 @@ public class ParentCommand extends ChildCommand {
return map;
}
public void hook(@NotNull PluginCommand command) {
command.setExecutor(this);
command.setTabCompleter(this);
plugin.getLogger().info("已注册指令 " + getUsage() + ".");
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (args.length == 0) {

View File

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

View File

@@ -0,0 +1,48 @@
package cn.hamster3.mc.plugin.core.bukkit.command.core;
import cn.hamster3.mc.plugin.core.bukkit.command.ChildCommand;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.List;
public class GCCommand extends ChildCommand {
public static final GCCommand INSTANCE = new GCCommand();
private GCCommand() {
}
@Override
public @NotNull String getName() {
return "gc";
}
@Override
public @NotNull String getUsage() {
return "gc";
}
@Override
public boolean hasPermission(@NotNull CommandSender sender) {
return true;
}
@Override
public @NotNull String getDescription() {
return "通知JVM进行一次垃圾回收";
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
System.gc();
sender.sendMessage("§a已发送 GC 信号.");
return true;
}
@Override
public List<String> onTabComplete(CommandSender sender, Command command, String alias, String[] args) {
return Collections.emptyList();
}
}

View File

@@ -0,0 +1,15 @@
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;
public class ParentCoreCommand extends ParentCommand {
public static final ParentCoreCommand INSTANCE = new ParentCoreCommand();
private ParentCoreCommand() {
super(HamsterCorePlugin.getInstance(), "core");
addChildCommand(YamlCommand.INSTANCE);
addChildCommand(GCCommand.INSTANCE);
addChildCommand(BlockInfoCommand.INSTANCE);
}
}

View File

@@ -1,4 +1,4 @@
package cn.hamster3.mc.plugin.core.bukkit.command.debug;
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.ChildCommand;

View File

@@ -5,14 +5,12 @@ import cn.hamster3.mc.plugin.core.bukkit.command.ParentCommand;
import cn.hamster3.mc.plugin.core.common.util.CommonUtils;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class ParentLoreCommand extends ParentCommand {
public static final ParentLoreCommand INSTANCE = new ParentLoreCommand("lore", HamsterCorePlugin.COMMAND_EXECUTOR);
public static final ParentLoreCommand INSTANCE = new ParentLoreCommand();
private ParentLoreCommand(@NotNull String name, @Nullable ParentCommand parent) {
super(name, parent);
public ParentLoreCommand() {
super(HamsterCorePlugin.getInstance(), "lore");
addChildCommand(LoreAddCommand.INSTANCE);
addChildCommand(LoreRemoveCommand.INSTANCE);
addChildCommand(LoreSetCommand.INSTANCE);

View File

@@ -16,6 +16,10 @@ commands:
description: 仓鼠核心调试指令
permission: hamster.core.admin
permission-message: §c你没有这个权限
lore:
description: 仓鼠核心的 lore 修改指令
permission: hamster.lore.admin
permission-message: §c你没有这个权限
permissions:
hamster.core.admin: