初始化并且写了些东西
This commit is contained in:
47
src/main/java/top/sunsetlab/utils/ActionManager.java
Normal file
47
src/main/java/top/sunsetlab/utils/ActionManager.java
Normal file
@@ -0,0 +1,47 @@
|
||||
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));
|
||||
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.");
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user