65 lines
1.8 KiB
Java
65 lines
1.8 KiB
Java
package top.sunsetlab.utils;
|
||
|
||
import com.google.gson.Gson;
|
||
import org.bukkit.Location;
|
||
import org.bukkit.entity.Player;
|
||
import top.sunsetlab.PluginMain;
|
||
import top.sunsetlab.actions.IActionBase;
|
||
|
||
import java.io.BufferedReader;
|
||
import java.io.InputStream;
|
||
import java.io.InputStreamReader;
|
||
import java.util.ArrayList;
|
||
import java.util.HashMap;
|
||
|
||
class ActionList {
|
||
ArrayList<String> locations;
|
||
}
|
||
|
||
/**
|
||
* 动作管理器
|
||
*/
|
||
public class ActionManager {
|
||
private final HashMap<String, JsonAction> actions;
|
||
public ActionManager() {
|
||
actions = new HashMap<>();
|
||
}
|
||
|
||
/**
|
||
* 初始化
|
||
*/
|
||
public void load() {
|
||
// 从本地文件读取
|
||
InputStream is = ActionManager.class.getResourceAsStream("/actions.json");
|
||
if (is == null) {
|
||
throw new RuntimeException("actions.json file not found!");
|
||
}
|
||
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||
// 解析json
|
||
Gson gson = new Gson();
|
||
ActionList actionList = gson.fromJson(reader, ActionList.class);
|
||
// 加载单个动作
|
||
for (String location : actionList.locations) {
|
||
JsonAction action = new JsonAction(location);
|
||
actions.put(action.getId(), action);
|
||
}
|
||
PluginMain.LOGGER.info("Loaded " + actions.size() + " actions.");
|
||
}
|
||
|
||
/**
|
||
* 调用动作
|
||
* @param actionId 动作id
|
||
* @param location 执行位置
|
||
* @param caller 执行者
|
||
* @return 动作是否执行成功,sync动作始终返回true
|
||
*/
|
||
public boolean call(String actionId, Location location, Player caller) {
|
||
// 获取动作
|
||
JsonAction action = actions.get(actionId);
|
||
if (action == null) {
|
||
throw new RuntimeException("Invalid action id " + actionId);
|
||
}
|
||
return action.run(location, caller);
|
||
}
|
||
}
|