Skip to content

Commit

Permalink
规范文档.
Browse files Browse the repository at this point in the history
  • Loading branch information
SmileYik committed May 17, 2022
1 parent 58aabd1 commit df6626d
Show file tree
Hide file tree
Showing 6 changed files with 231 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private void setupMetrics() {
try {
Metrics metrics = new Metrics(this, 14952);
} catch (Exception e) {

// 注册bstats失败, 但不影响插件正常功能, 忽略报错结果
}
}

Expand Down Expand Up @@ -153,9 +153,8 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
}
} else if (args.length == 3) {
if (args[0].equalsIgnoreCase("testLoop")) {
getServer().getScheduler().runTaskAsynchronously(this, () -> {
LoopTest.doLoop(sender, args[2], args[1]);
});
getServer().getScheduler().runTaskAsynchronously(this, () ->
LoopTest.doLoop(sender, args[2], args[1]));
return true;
}
}
Expand All @@ -174,6 +173,12 @@ public boolean onCommand(CommandSender sender, Command command, String label, St
return super.onCommand(sender, command, label, args);
}

/**
* 此方法不会在正常运行中启用.
* @param str 要写入的内容
* @param fileName 要写入的文件
*/
@Deprecated
public static void writeToFile(String str, String fileName) {
File file = new File(instance.getDataFolder(), fileName);
file.getParentFile().mkdirs();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
import java.util.*;

public class EventRegister {
private static final Map<String, List<LuaEvent<? extends Event>>> events = new HashMap<>();
private static final Map<String,
List<LuaEvent<? extends Event>>> events = new HashMap<>();
private static final Properties EVENT_MAPPER = new Properties();
public static final EventPriority[] PRIORITIES = {
EventPriority.LOWEST,
Expand All @@ -22,8 +23,11 @@ public class EventRegister {
};

static {
// 加载事件名与全类名映射
try {
EVENT_MAPPER.load(EventRegister.class.getResourceAsStream("/events.properties"));
EVENT_MAPPER.load(
EventRegister.class
.getResourceAsStream("/events.properties"));
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down Expand Up @@ -54,11 +58,19 @@ private void registerEvent(LuaEvent<? extends Event> listener, int priority) {
}

if (handler != null) {
InvocationHandler invocationHandler = Proxy.getInvocationHandler(handler);
Field memberValues = invocationHandler.getClass().getDeclaredField("memberValues");
InvocationHandler invocationHandler =
Proxy.getInvocationHandler(handler);
Field memberValues =
invocationHandler.getClass()
.getDeclaredField("memberValues");
memberValues.setAccessible(true);
((Map) memberValues.get(invocationHandler)).put("priority", PRIORITIES[priority]);
LuaInMinecraftBukkit.debug("注册事件优先级: %s; 闭包: %s", handler.priority(), listener.getId());
((Map) memberValues.get(invocationHandler))
.put("priority", PRIORITIES[priority]);
LuaInMinecraftBukkit.debug(
"注册事件优先级: %s; 闭包: %s",
handler.priority(),
listener.getId()
);
}
} catch (Exception e) {
// 恢复默认优先级
Expand All @@ -68,7 +80,8 @@ private void registerEvent(LuaEvent<? extends Event> listener, int priority) {
events.put(listener.getPluginId(), new ArrayList<>());
}
events.get(listener.getPluginId()).add(listener);
Bukkit.getServer().getPluginManager().registerEvents(listener, LuaInMinecraftBukkit.getInstance());
Bukkit.getServer().getPluginManager().registerEvents(
listener, LuaInMinecraftBukkit.getInstance());
}

/**
Expand All @@ -78,26 +91,27 @@ private void registerEvent(LuaEvent<? extends Event> listener, int priority) {
* @param priority 优先级, 范围为[0, 5], 不再此区间内则为正常默认等级.
*/
@Deprecated
public void registerListener(String event, String id, int priority) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, InstantiationException, IllegalAccessException {
public void registerListener(String event, String id, int priority) {
register(event, id, priority);
LuaInMinecraftBukkit.log("eventRegister:registerListener 即将被弃用, 请使用event:register");
LuaInMinecraftBukkit.log(
"eventRegister:registerListener 即将被弃用, " +
"请使用event:register");
}

private Class<?> getClass(String event) throws ClassNotFoundException, NoSuchMethodException {
private Class<?> getClass(String event) throws ClassNotFoundException {
String classPath;
if (EVENT_MAPPER.containsKey(event)) {
classPath = EVENT_MAPPER.getProperty(event);
} else {
classPath = event.startsWith(".") ?
("tk.smileyik.luainminecraftbukkit.bridge.event" + event) :
event;
("tk.smileyik.luainminecraftbukkit.bridge.event" + event) : event;
}
return Class.forName(classPath);
}

/**
* 注册监听事件.
* @param event 要注册的事件.
* @param event 要注册的事件名.
* @param id 闭包id
* @param priority 优先级, 范围为[0, 5], 不再此区间内则为正常默认等级.
*/
Expand All @@ -123,21 +137,21 @@ public void register(String event, String id, int priority) {
* @param priority 优先级, 范围为[0, 5], 不再此区间内则为正常默认等级.
*/
public String register(LuaPlugin plugin,
Object closure,
String event,
int priority) {
Object closure, String event, int priority) {
LuaEvent<? extends Event> listener = null;
try {
listener = (LuaEvent<? extends Event>) getClass(event)
.getDeclaredConstructor(String.class)
.newInstance(plugin.getId());
listener.setClosure(closure);
String id = String.format("%s.%s.%s", plugin.getId(), event, listener.getId());
String id = String.format(
"%s.%s.%s", plugin.getId(), event, listener.getId());
listener.setId(id);
registerEvent(listener, priority);
return id;
} catch (Exception e) {
LuaInMinecraftBukkit.log("注册%s事件失败! id: %s", event, plugin.getId());
LuaInMinecraftBukkit.log(
"注册%s事件失败! id: %s", event, plugin.getId());
e.printStackTrace();
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ protected LuaPluginHybrid(AbstractLuaPlugin luaPlugin, RunType runType) {
*
* @param module 模块名称
* @return 返回脚本插件require所认可的模块路径.
* @throws IOException
*/
@Override
public String getRequirePath(String module) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ public boolean loadPlugin(LuaPlugin plugin) {
}
pluginGlobals.set("self", LuaValueHelper.valueOf(plugin));
// 环境生成完毕
LuaValue func = null;
LuaValue func;
try {
func = pluginGlobals.loadfile(
new File(plugin.getPluginPath(), PLUGIN_MAIN).getCanonicalPath()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public LuaObject getClosure(String[] vars) {
if (g == null) {
throw new LuaPluginNotFountException(vars[0]);
}
LuaObject obj = null;
LuaObject obj;
try {
obj = g.getLuaObject(vars[1]);
for (int i = 2; i < vars.length; ++i) {
Expand Down
Loading

0 comments on commit df6626d

Please sign in to comment.