Skip to content

Commit

Permalink
[2.0.0-dev16]重构部分命令模块
Browse files Browse the repository at this point in the history
  • Loading branch information
YufiriaMazenta committed Feb 3, 2024
1 parent 1cb4775 commit 889edcf
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 175 deletions.
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import java.text.SimpleDateFormat
version = "2.0.0-dev15"
version = "2.0.0-dev16"

plugins {
`java-library`
Expand Down Expand Up @@ -40,7 +40,7 @@ dependencies {
compileOnly("io.lumine:Mythic-Dist:5.3.5")
compileOnly("io.lumine:MythicLib-dist:1.6.2-SNAPSHOT")
compileOnly("net.Indyuce:MMOItems-API:6.9.5-SNAPSHOT")
implementation("com.crypticlib:CrypticLib:0.11.0")
implementation("com.crypticlib:CrypticLib:0.14.2")
}

group = "com.github.yufiriamazenta"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,70 +1,119 @@
package com.github.yufiriamazenta.craftorithm.cmd;

import com.github.yufiriamazenta.craftorithm.cmd.sub.ReloadCommand;
import com.github.yufiriamazenta.craftorithm.cmd.sub.RunArcencielCmd;
import com.github.yufiriamazenta.craftorithm.cmd.sub.VersionCommand;
import com.github.yufiriamazenta.craftorithm.Craftorithm;
import com.github.yufiriamazenta.craftorithm.arcenciel.ArcencielDispatcher;
import com.github.yufiriamazenta.craftorithm.cmd.sub.item.ItemCommand;
import com.github.yufiriamazenta.craftorithm.cmd.sub.recipe.CreateRecipeCommand;
import com.github.yufiriamazenta.craftorithm.cmd.sub.recipe.DisableRecipeCommand;
import com.github.yufiriamazenta.craftorithm.cmd.sub.recipe.RecipeListCommand;
import com.github.yufiriamazenta.craftorithm.cmd.sub.recipe.RemoveRecipeCommand;
import com.github.yufiriamazenta.craftorithm.config.Languages;
import com.github.yufiriamazenta.craftorithm.item.ItemManager;
import com.github.yufiriamazenta.craftorithm.item.impl.CraftorithmItemProvider;
import com.github.yufiriamazenta.craftorithm.recipe.RecipeManager;
import com.github.yufiriamazenta.craftorithm.util.CollectionsUtil;
import com.github.yufiriamazenta.craftorithm.util.ItemUtils;
import com.github.yufiriamazenta.craftorithm.util.LangUtil;
import crypticlib.command.BukkitCommand;
import crypticlib.command.RootCmdExecutor;
import crypticlib.command.SubcmdExecutor;
import org.bukkit.command.Command;
import crypticlib.command.CommandTreeInfo;
import crypticlib.command.CommandTreeNode;
import crypticlib.command.CommandTreeRoot;
import crypticlib.command.annotation.CommandNode;
import crypticlib.command.annotation.CommandTree;
import crypticlib.perm.PermInfo;
import org.bukkit.command.CommandSender;
import org.jetbrains.annotations.NotNull;
import org.bukkit.entity.Item;
import org.bukkit.entity.Player;

import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;

@BukkitCommand(
name = "craftorithm",
aliases = {"craft", "cra"},
permission = "craftorithm.command"
)
public class PluginCommand extends RootCmdExecutor {
@CommandTree
public class PluginCommand extends CommandTreeRoot {

PluginCommand() {
regDefaultSubCommands();
super(new CommandTreeInfo("craftorithm", new PermInfo("craftorithm.command"), new String[]{"craft", "cra"}));
setExecutor((sender, args) -> {
LangUtil.sendLang(sender, Languages.COMMAND_UNDEFINED_SUBCMD);
return true;
});
}

@Override
public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) {
List<String> argList = Arrays.asList(args);
if (argList.isEmpty()) {
LangUtil.sendLang(sender, Languages.COMMAND_NOT_ENOUGH_PARAM, CollectionsUtil.newStringHashMap("<number>", String.valueOf(1)));
@CommandNode
private CommandTreeNode reload = new CommandTreeNode(
new CommandTreeNode.NodeInfo("reload", new PermInfo("craftorithm.command.reload")),
(sender, args) -> {
try {
Craftorithm.instance().reloadConfig();
ItemUtils.reloadCannotCraftLore();
ArcencielDispatcher.INSTANCE.functionFile().reloadConfig();
CraftorithmItemProvider.INSTANCE.reloadItemProvider();
ItemManager.INSTANCE.reloadCustomCookingFuel();
RecipeManager.INSTANCE.reloadRecipeManager();
LangUtil.sendLang(sender, Languages.COMMAND_RELOAD_SUCCESS);
} catch (Exception e) {
e.printStackTrace();
LangUtil.sendLang(sender, Languages.COMMAND_RELOAD_EXCEPTION);
}
return true;
}
SubcmdExecutor subCommand = subcommands().get(argList.get(0));
if (subCommand != null) {
String perm = subCommand.permission();
if (perm != null) {
if (!sender.hasPermission(perm)) {
LangUtil.sendLang(sender, Languages.COMMAND_NO_PERM);
return true;
}
);

@CommandNode
private CommandTreeNode version = new CommandTreeNode(
new CommandTreeNode.NodeInfo("version",new PermInfo("craftorithm.command.version")),
(sender, args) -> {
LangUtil.sendLang(sender, Languages.COMMAND_VERSION);
return true;
}
);

@CommandNode
private CommandTreeNode run = new CommandTreeNode(
new CommandTreeNode.NodeInfo("run", new PermInfo("craftorithm.command.run")),
(sender, args) -> {
if (!checkSenderIsPlayer(sender))
return true;
if (args.isEmpty()) {
sendNotEnoughCmdParamMsg(sender, 1);
return true;
}
return subCommand.onCommand(sender, argList.subList(1, argList.size()));
long startTime = System.currentTimeMillis();
StringJoiner arcencielBlock = new StringJoiner(" ");
for (String arg : args) {
arcencielBlock.add(arg);
}
ArcencielDispatcher.INSTANCE.dispatchArcencielBlock((Player) sender, arcencielBlock.toString());
long execTime = System.currentTimeMillis() - startTime;
LangUtil.sendLang(sender, Languages.COMMAND_RUN_ARCENCIEL_SUCCESS, CollectionsUtil.newStringHashMap("<time>", String.valueOf(execTime)));
return true;
}
else {
LangUtil.sendLang(sender, Languages.COMMAND_UNDEFINED_SUBCMD);
);

@CommandNode
private CommandTreeNode item = ItemCommand.INSTANCE;
@CommandNode
private CommandTreeNode disable = DisableRecipeCommand.INSTANCE;
@CommandNode
private CommandTreeNode removeRecipe = RemoveRecipeCommand.INSTANCE;
@CommandNode
private CommandTreeNode createRecipe = CreateRecipeCommand.INSTANCE;
@CommandNode
private CommandTreeNode recipeList = RecipeListCommand.INSTANCE;

public boolean checkSenderIsPlayer(CommandSender sender) {
if (sender instanceof Player) {
return true;
} else {
LangUtil.sendLang(sender, Languages.COMMAND_PLAYER_ONLY);
return false;
}
}

private void regDefaultSubCommands() {
regSub(ReloadCommand.INSTANCE)
.regSub(VersionCommand.INSTANCE)
.regSub(RemoveRecipeCommand.INSTANCE)
.regSub(DisableRecipeCommand.INSTANCE)
.regSub(ItemCommand.INSTANCE)
.regSub(RunArcencielCmd.INSTANCE)
.regSub(CreateRecipeCommand.INSTANCE)
.regSub(RecipeListCommand.INSTANCE);
public void sendNotEnoughCmdParamMsg(CommandSender sender, int paramNum) {
sendNotEnoughCmdParamMsg(sender, String.valueOf(paramNum));
}

public void sendNotEnoughCmdParamMsg(CommandSender sender, String paramStr) {
LangUtil.sendLang(sender, Languages.COMMAND_NOT_ENOUGH_PARAM, CollectionsUtil.newStringHashMap("<number>", paramStr));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,17 @@
import com.github.yufiriamazenta.craftorithm.config.Languages;
import com.github.yufiriamazenta.craftorithm.util.CollectionsUtil;
import com.github.yufiriamazenta.craftorithm.util.LangUtil;
import crypticlib.command.SubcmdExecutor;
import crypticlib.command.CommandTreeNode;
import crypticlib.perm.PermInfo;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import java.util.List;

public abstract class AbstractSubCommand extends SubcmdExecutor {
public abstract class AbstractSubCommand extends CommandTreeNode {

protected AbstractSubCommand(String command, String perm) {
super(command, perm);
super(new NodeInfo(command, new PermInfo(perm)));
}

protected AbstractSubCommand(String command) {
Expand All @@ -25,18 +26,18 @@ public boolean onCommand(CommandSender sender, List<String> args) {
sendNotEnoughCmdParamMsg(sender, 1);
return true;
}
SubcmdExecutor subCommand = subcommands().get(args.get(0));
if (subCommand == null) {
CommandTreeNode node = nodes().get(args.get(0));
if (node == null) {
LangUtil.sendLang(sender, Languages.COMMAND_UNDEFINED_SUBCMD);
} else {
String perm = subCommand.permission();
String perm = node.permission().permission();
if (perm != null) {
if (!sender.hasPermission(perm)) {
LangUtil.sendLang(sender, Languages.COMMAND_NO_PERM);
return true;
}
}
subCommand.onCommand(sender, args.subList(1, args.size()));
node.onCommand(sender, args.subList(1, args.size()));
}
return true;
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.github.yufiriamazenta.craftorithm.cmd.sub.AbstractSubCommand;
import com.github.yufiriamazenta.craftorithm.cmd.sub.item.fuel.FuelCommand;
import crypticlib.command.CommandTreeNode;
import crypticlib.command.annotation.CommandNode;
import org.bukkit.command.CommandSender;

import java.util.List;
Expand All @@ -10,9 +12,15 @@ public final class ItemCommand extends AbstractSubCommand {

public static final ItemCommand INSTANCE = new ItemCommand();

@CommandNode
private CommandTreeNode save = SaveItemCommand.INSTANCE;
@CommandNode
private CommandTreeNode give = GiveItemCommand.INSTANCE;
@CommandNode
private CommandTreeNode fuel = FuelCommand.INSTANCE;

private ItemCommand() {
super("item", "craftorithm.command.item");
regSub(SaveItemCommand.INSTANCE).regSub(GiveItemCommand.INSTANCE).regSub(FuelCommand.INSTANCE);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package com.github.yufiriamazenta.craftorithm.cmd.sub.item.fuel;

import com.github.yufiriamazenta.craftorithm.cmd.sub.AbstractSubCommand;
import crypticlib.command.CommandTreeNode;
import crypticlib.command.annotation.CommandNode;

public class FuelCommand extends AbstractSubCommand {

public static final FuelCommand INSTANCE = new FuelCommand();
@CommandNode
private CommandTreeNode add = AddFuelCommand.INSTANCE;
@CommandNode
private CommandTreeNode remove = RemoveFuelCommand.INSTANCE;

protected FuelCommand() {
super("fuel", "craftorithm.command.item.fuel");
regSub(AddFuelCommand.INSTANCE).regSub(RemoveFuelCommand.INSTANCE);
}

}
Loading

0 comments on commit 889edcf

Please sign in to comment.