初始化并且写了些东西

This commit is contained in:
Starrysky
2026-02-16 18:51:24 +08:00
commit 65f516a11f
24 changed files with 849 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
package top.sunsetlab.utils;
import com.google.gson.Gson;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import top.sunsetlab.actions.IActionBase;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
class ActionData {
String id;
String classname;
}
public class JsonAction {
private final ActionData data;
public JsonAction(String location) {
InputStream is = JsonAction.class.getResourceAsStream("/" + location);
if (is == null) {
throw new RuntimeException(location + " not found");
}
BufferedReader br = new BufferedReader(new InputStreamReader(is));
Gson gson = new Gson();
data = gson.fromJson(br, ActionData.class);
}
public boolean run(Location location, Player caller) {
try {
Class<?> clazz = Class.forName(data.classname);
if (!IActionBase.class.isAssignableFrom(clazz)) {
throw new RuntimeException(data.classname + " is not a Action");
}
Constructor<?> constructor = clazz.getConstructor();
IActionBase action = (IActionBase) constructor.newInstance();
return action.call(location, caller);
}catch (ClassNotFoundException
| NoSuchMethodException
| SecurityException
| InstantiationException
| IllegalAccessException
| IllegalArgumentException
| InvocationTargetException e) {
throw new RuntimeException(
"Something went wrong while trying to run action " + data.id + ":",
e);
}
}
public String getId() {
return data.id;
}
}