Updated to ThemePark v2
This commit is contained in:
parent
8a332b4fbd
commit
73ba5bfaa9
18 changed files with 536 additions and 614 deletions
|
@ -0,0 +1,65 @@
|
|||
package nl.sbdeveloper.themeparkplus.util;
|
||||
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.TypeAdapter;
|
||||
import com.google.gson.reflect.TypeToken;
|
||||
import com.google.gson.stream.JsonReader;
|
||||
import com.google.gson.stream.JsonToken;
|
||||
import com.google.gson.stream.JsonWriter;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.lang.reflect.Type;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class LocationGsonAdapter extends TypeAdapter<Location> {
|
||||
private static Gson g = new Gson();
|
||||
|
||||
private static Type seriType = new TypeToken<Map<String, Object>>(){}.getType();
|
||||
|
||||
private static String UUID = "uuid";
|
||||
private static String X = "x";
|
||||
private static String Y = "y";
|
||||
private static String Z = "z";
|
||||
private static String YAW = "yaw";
|
||||
private static String PITCH = "pitch";
|
||||
|
||||
@Override
|
||||
public void write(JsonWriter jsonWriter, Location location) throws IOException {
|
||||
if(location == null) {
|
||||
jsonWriter.nullValue();
|
||||
return;
|
||||
}
|
||||
jsonWriter.value(getRaw(location));
|
||||
}
|
||||
|
||||
@Override
|
||||
public Location read(JsonReader jsonReader) throws IOException {
|
||||
if(jsonReader.peek() == JsonToken.NULL) {
|
||||
jsonReader.nextNull();
|
||||
return null;
|
||||
}
|
||||
return fromRaw(jsonReader.nextString());
|
||||
}
|
||||
|
||||
private String getRaw (Location location) {
|
||||
Map<String, Object> serial = new HashMap<>();
|
||||
serial.put(UUID, location.getWorld().getUID().toString());
|
||||
serial.put(X, Double.toString(location.getX()));
|
||||
serial.put(Y, Double.toString(location.getY()));
|
||||
serial.put(Z, Double.toString(location.getZ()));
|
||||
serial.put(YAW, Float.toString(location.getYaw()));
|
||||
serial.put(PITCH, Float.toString(location.getPitch()));
|
||||
return g.toJson(serial);
|
||||
}
|
||||
|
||||
private Location fromRaw (String raw) {
|
||||
Map<String, Object> keys = g.fromJson(raw, seriType);
|
||||
World w = Bukkit.getWorld(java.util.UUID.fromString((String) keys.get(UUID)));
|
||||
return new Location(w, Double.parseDouble((String) keys.get(X)), Double.parseDouble((String) keys.get(Y)), Double.parseDouble((String) keys.get(Z)),
|
||||
Float.parseFloat((String) keys.get(YAW)), Float.parseFloat((String) keys.get(PITCH)));
|
||||
}
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
package nl.sbdeveloper.themeparkplus.util;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
public class Reflection {
|
||||
private static String version = Bukkit.getServer().getClass().getPackage().getName().split("\\.")[3];
|
||||
|
||||
@Nullable
|
||||
public static Class<?> getCraftBukkitClass(String name) {
|
||||
try {
|
||||
return Class.forName("org.bukkit.craftbukkit." + version + "." + name);
|
||||
} catch (ClassNotFoundException var2) {
|
||||
var2.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Class<?> getNMSClass(String name) {
|
||||
try {
|
||||
return Class.forName("net.minecraft.server." + version + "." + name);
|
||||
} catch (ClassNotFoundException var2) {
|
||||
var2.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Class<?> getClass(String name) {
|
||||
try {
|
||||
return Class.forName(name);
|
||||
} catch (ClassNotFoundException var2) {
|
||||
var2.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,195 +0,0 @@
|
|||
package nl.sbdeveloper.themeparkplus.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.*;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import com.sk89q.worldedit.Vector;
|
||||
import com.sk89q.worldedit.bukkit.BukkitWorld;
|
||||
import com.sk89q.worldedit.bukkit.WorldEditPlugin;
|
||||
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
|
||||
import com.sk89q.worldguard.protection.ApplicableRegionSet;
|
||||
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
|
||||
import com.sk89q.worldguard.protection.managers.RegionManager;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion;
|
||||
import com.sk89q.worldguard.protection.regions.ProtectedRegion;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
public class WorldGuardLegacyManager {
|
||||
|
||||
private static String wgVerStr = null;
|
||||
private static WorldGuardLegacyManager instance = null;
|
||||
|
||||
public static WorldGuardLegacyManager getInstance() {
|
||||
if (instance == null) {
|
||||
instance = new WorldGuardLegacyManager();
|
||||
}
|
||||
return instance;
|
||||
}
|
||||
|
||||
public WorldGuardPlugin getWorldGuard() {
|
||||
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WorldGuard");
|
||||
if (!(plugin instanceof WorldGuardPlugin)) {
|
||||
return null;
|
||||
}
|
||||
return (WorldGuardPlugin) plugin;
|
||||
}
|
||||
|
||||
public WorldEditPlugin getWorldEdit() {
|
||||
Plugin plugin = Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
|
||||
if (!(plugin instanceof WorldEditPlugin)) {
|
||||
return null;
|
||||
}
|
||||
return (WorldEditPlugin) plugin;
|
||||
}
|
||||
|
||||
public ProtectedRegion getLowerCasePlot(World w, String regionname) {
|
||||
for (ProtectedRegion pr : getRegionManager(w).getRegions().values()) {
|
||||
if (pr.getId().toLowerCase().equalsIgnoreCase(regionname)) {
|
||||
return pr;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public RegionManager getRegionManager(World w) {
|
||||
if (getWgVer().contains("7.")) {
|
||||
try {
|
||||
Class<?> wgClass = Reflection.getClass("com.sk89q.worldguard.WorldGuard");
|
||||
|
||||
Object instance = wgClass.getDeclaredMethod("getInstance").invoke(null);
|
||||
Class<?> wgInstanceClass = instance.getClass();
|
||||
|
||||
Object platform = wgInstanceClass.getDeclaredMethod("getPlatform").invoke(instance);
|
||||
Class<?> wgPlatformClass = platform.getClass();
|
||||
|
||||
Object regionContainer = wgPlatformClass.getDeclaredMethod("getRegionContainer").invoke(platform);
|
||||
Class<?> wgRegionContainer = regionContainer.getClass();
|
||||
|
||||
return (RegionManager) wgRegionContainer.getSuperclass()
|
||||
.getMethod("get", com.sk89q.worldedit.world.World.class)
|
||||
.invoke(regionContainer, new BukkitWorld(w));
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
return getWorldGuard().getRegionManager(w);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<ProtectedRegion> getRegions(Location loc) {
|
||||
ArrayList<ProtectedRegion> regions = new ArrayList<>();
|
||||
if (getWgVer().contains("7.")) {
|
||||
try {
|
||||
RegionManager mngr = getRegionManager(loc.getWorld());
|
||||
|
||||
Class<?> blockVector3 = Reflection.getClass("com.sk89q.worldedit.math.BlockVector3");
|
||||
|
||||
Method applicableRegions = mngr.getClass().getDeclaredMethod("getApplicableRegions", blockVector3);
|
||||
|
||||
Method blockVectorAt = blockVector3.getDeclaredMethod("at", double.class, double.class, double.class);
|
||||
Object blockVector = blockVectorAt.invoke(null, loc.getX(), loc.getY(), loc.getZ());
|
||||
|
||||
Object regionSet = applicableRegions.invoke(mngr, blockVector);
|
||||
|
||||
Method getregions = regionSet.getClass().getMethod("getRegions");
|
||||
|
||||
regions = new ArrayList<>(((HashSet<ProtectedRegion>) getregions.invoke(regionSet)));
|
||||
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
regions = new ArrayList<>(getRegionManager(loc.getWorld())
|
||||
.getApplicableRegions(new Vector(loc.getX(), loc.getY(), loc.getZ())).getRegions());
|
||||
}
|
||||
|
||||
regions.sort(Comparator.comparing(ProtectedRegion::getPriority));
|
||||
return regions;
|
||||
}
|
||||
|
||||
public ProtectedCuboidRegion getProtectedCubiodRegion(String regionname, Location loc1, Location loc2) {
|
||||
if (getWgVer().contains("7.")) {
|
||||
try {
|
||||
|
||||
Object bvloc1 = getBlockVectorV3(loc1);
|
||||
Object bvloc2 = getBlockVectorV3(loc2);
|
||||
|
||||
Class<?> prCbRg = Reflection.getClass("com.sk89q.worldguard.protection.regions.ProtectedCuboidRegion");
|
||||
|
||||
return (ProtectedCuboidRegion) prCbRg.getConstructor(String.class, bvloc1.getClass(), bvloc2.getClass())
|
||||
.newInstance(regionname, bvloc1, bvloc2);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
} else {
|
||||
return new ProtectedCuboidRegion(regionname,
|
||||
new com.sk89q.worldedit.BlockVector(loc1.getX(), loc1.getY(), loc1.getZ()),
|
||||
new com.sk89q.worldedit.BlockVector(loc2.getX(), loc2.getY(), loc2.getZ()));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Object getBlockVectorV3(@NotNull Location loc) throws Exception {
|
||||
Class<?> blockVector3 = Reflection.getClass("com.sk89q.worldedit.math.BlockVector3");
|
||||
|
||||
Method blockVectorAt = blockVector3.getDeclaredMethod("at", double.class, double.class, double.class);
|
||||
return blockVectorAt.invoke(null, loc.getX(), loc.getY(), loc.getZ());
|
||||
}
|
||||
|
||||
public FlagRegistry getFlagRegistry() {
|
||||
if (getWgVer().contains("7.")) {
|
||||
try {
|
||||
Class<?> wgClass = Reflection.getClass("com.sk89q.worldguard.WorldGuard");
|
||||
|
||||
Object instance = wgClass.getDeclaredMethod("getInstance").invoke(null);
|
||||
Class<?> wgInstanceClass = instance.getClass();
|
||||
Method declaredMethod = wgInstanceClass.getDeclaredMethod("getFlagRegistry");
|
||||
return (FlagRegistry) declaredMethod.invoke(instance);
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return getWorldGuard().getFlagRegistry();
|
||||
}
|
||||
}
|
||||
|
||||
public ApplicableRegionSet getApplicableRegionSet(Location loc) {
|
||||
if (getWgVer().contains("7.")) {
|
||||
try {
|
||||
RegionManager mngr = getRegionManager(loc.getWorld());
|
||||
|
||||
Class<?> blockVector3 = Reflection.getClass("com.sk89q.worldedit.math.BlockVector3");
|
||||
|
||||
Method applicableRegions = mngr.getClass().getDeclaredMethod("getApplicableRegions", blockVector3);
|
||||
|
||||
Method blockVectorAt = blockVector3.getDeclaredMethod("at", double.class, double.class, double.class);
|
||||
Object blockVector = blockVectorAt.invoke(null, loc.getX(), loc.getY(), loc.getZ());
|
||||
|
||||
Object regionSet = applicableRegions.invoke(mngr, blockVector);
|
||||
|
||||
return (ApplicableRegionSet) regionSet;
|
||||
} catch (Exception ex) {
|
||||
ex.printStackTrace();
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
return getRegionManager(loc.getWorld()).getApplicableRegions(new Vector(loc.getX(), loc.getY(), loc.getZ()));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public String getWgVer() {
|
||||
if (wgVerStr == null) {
|
||||
wgVerStr = Bukkit.getPluginManager().getPlugin("WorldGuard").getDescription().getVersion();
|
||||
}
|
||||
return wgVerStr;
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load diff
Reference in a new issue