Skip to content

Commit 3c95834

Browse files
committed
feat: add new command register method: registerRawCommand
1 parent ebc6d30 commit 3c95834

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

src/main/java/org/eu/smileyik/luaInMinecraftBukkitII/api/lua/luaState/ILuaEnv.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.eu.smileyik.luajava.type.ILuaCallable;
99

1010
import java.io.File;
11+
import java.lang.reflect.InvocationTargetException;
1112

1213
public interface ILuaEnv {
1314
/**
@@ -76,6 +77,16 @@ public interface ILuaEnv {
7677
*/
7778
Result<Boolean, Exception> registerCommand(String rootCommand, Class<?>... classes);
7879

80+
/**
81+
* 注册一个原始指令, 该指令与 Bukkit 原始指令一致.
82+
* @param command 指令名称
83+
* @param callable 回调闭包, 指令触发时, 将会传输给闭包形参顺序乳如下: sender, command, label, args, 并且该闭包需要返回 true/false
84+
* @throws InvocationTargetException
85+
* @throws IllegalAccessException
86+
* @throws InstantiationException
87+
*/
88+
void registerRawCommand(String command, ILuaCallable callable) throws InvocationTargetException, IllegalAccessException, InstantiationException;
89+
7990
/**
8091
* 注册指令
8192
* @param rootCommand 指令名称.

src/main/java/org/eu/smileyik/luaInMinecraftBukkitII/luaState/SimpleLuaEnv.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import org.eu.smileyik.simpledebug.DebugLogger;
1919

2020
import java.io.File;
21+
import java.lang.reflect.InvocationTargetException;
2122

2223
public class SimpleLuaEnv implements ILuaEnv {
2324
private final LuaStateEnv env;
@@ -64,6 +65,11 @@ public Result<Boolean, Exception> registerCommand(String rootCommand, Class<?>..
6465
return registerCommand(rootCommand, null, classes);
6566
}
6667

68+
@Override
69+
public void registerRawCommand(String command, ILuaCallable callable) throws InvocationTargetException, IllegalAccessException, InstantiationException {
70+
LuaCommandRegister.register(command, callable);
71+
}
72+
6773
@Override
6874
public Result<Boolean, Exception> registerCommand(String rootCommand, String[] aliases, Class<?>... classes) {
6975
try {
@@ -126,7 +132,7 @@ public File file(String path) {
126132

127133
@Override
128134
public File file(String... paths) {
129-
return new File(env.getRootDir(), String.join(File.pathSeparator, paths));
135+
return new File(env.getRootDir(), String.join(File.separator, paths));
130136
}
131137

132138
@Override

src/main/java/org/eu/smileyik/luaInMinecraftBukkitII/luaState/command/LuaCommandRegister.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import org.bukkit.plugin.Plugin;
99
import org.eu.smileyik.luaInMinecraftBukkitII.LuaInMinecraftBukkit;
1010
import org.eu.smileyik.luaInMinecraftBukkitII.reflect.ReflectUtil;
11+
import org.eu.smileyik.luajava.type.ILuaCallable;
1112
import org.eu.smileyik.simplecommand.CommandMessageFormat;
1213
import org.eu.smileyik.simplecommand.CommandService;
1314
import org.eu.smileyik.simplecommand.CommandTranslator;
@@ -113,4 +114,28 @@ public static CommandService register(String rootCommand, String[] aliases, Clas
113114
}
114115
return commandService;
115116
}
117+
118+
public static void register(String rootCommand, ILuaCallable callable) throws InvocationTargetException, IllegalAccessException, InstantiationException {
119+
register(rootCommand, null, callable);
120+
}
121+
122+
public static void register(String rootCommand, String[] aliases, ILuaCallable callable) throws IllegalAccessException, InvocationTargetException, InstantiationException {
123+
LuaInMinecraftBukkit plugin = LuaInMinecraftBukkit.instance();
124+
PluginCommand pluginCommand = plugin.getCommand(rootCommand);
125+
CommandMap commandMap = (CommandMap) COMMAND_MAP_FIELD.get(plugin.getServer());
126+
if (pluginCommand == null) {
127+
pluginCommand = PLUGIN_COMMAND_CONSTRUCTOR.newInstance(rootCommand, plugin);
128+
}
129+
if (aliases != null) {
130+
pluginCommand.setAliases(Arrays.asList(aliases));
131+
}
132+
commandMap.register(rootCommand, rootCommand, pluginCommand);
133+
pluginCommand.setExecutor((sender, command, label, args) -> {
134+
try {
135+
return (boolean) callable.call(new Object[]{sender, command, label, args}).getOrThrow();
136+
} catch (Exception e) {
137+
throw new RuntimeException(e);
138+
}
139+
});
140+
}
116141
}

0 commit comments

Comments
 (0)