From 761dd4bf30866a8d26c0c67a772a07b89df792c0 Mon Sep 17 00:00:00 2001 From: stijnb1234 Date: Wed, 3 Jun 2020 21:10:49 +0200 Subject: [PATCH] Nearly done with waitingrows! --- pom.xml | 15 +- .../themeparkplus/ThemeParkPlus.java | 31 +-- .../themeparkplus/api/PlusAPI.java | 17 +- .../api/objects/SignLocation.java | 29 +++ .../themeparkplus/api/objects/WaitingRow.java | 20 +- .../listeners/SignListeners.java | 23 ++- .../listeners/WaitingTimeListener.java | 91 +++++--- .../themeparkplus/managers/DBManager.java | 45 +++- .../themeparkplus/util/LGUtil.java | 23 ++- .../themeparkplus/util/Reflection.java | 38 ++++ .../themeparkplus/util/WEUtil.java | 47 ----- .../util/WorldGuardLegacyManager.java | 195 ++++++++++++++++++ src/main/resources/plugin.yml | 2 +- 13 files changed, 437 insertions(+), 139 deletions(-) create mode 100644 src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/SignLocation.java create mode 100644 src/main/lombok/nl/sbdeveloper/themeparkplus/util/Reflection.java delete mode 100644 src/main/lombok/nl/sbdeveloper/themeparkplus/util/WEUtil.java create mode 100644 src/main/lombok/nl/sbdeveloper/themeparkplus/util/WorldGuardLegacyManager.java diff --git a/pom.xml b/pom.xml index 401313a..38eb041 100644 --- a/pom.xml +++ b/pom.xml @@ -107,7 +107,7 @@ sk89q-repo - >http://maven.sk89q.com/repo/ + https://maven.enginehub.org/repo/ jcenter @@ -133,7 +133,13 @@ com.sk89q.worldedit worldedit-bukkit - 7.1.0 + 6.1.4-SNAPSHOT + provided + + + com.sk89q.worldguard + worldguard-legacy + 6.2 provided @@ -163,5 +169,10 @@ HikariCP 3.4.2 + + org.jetbrains + annotations + 19.0.0 + diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java index 76bd7e6..f324981 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/ThemeParkPlus.java @@ -7,10 +7,9 @@ import nl.sbdeveloper.themeparkplus.commands.TPPCMD; import nl.sbdeveloper.themeparkplus.listeners.*; import nl.sbdeveloper.themeparkplus.managers.DBManager; import nl.sbdeveloper.themeparkplus.sbutils.UpdateManager; +import nl.sbdeveloper.themeparkplus.sbutils.YamlFile; import nl.sbdeveloper.themeparkplus.util.LGUtil; import nl.sbdeveloper.themeparkplus.util.License; -import nl.sbdeveloper.themeparkplus.sbutils.YamlFile; -import nl.sbdeveloper.themeparkplus.util.WEUtil; import org.bukkit.Bukkit; import org.bukkit.configuration.file.YamlConfiguration; import org.bukkit.plugin.RegisteredServiceProvider; @@ -19,7 +18,6 @@ import org.jetbrains.annotations.NotNull; import java.io.InputStreamReader; import java.io.Reader; -import java.lang.reflect.InvocationTargetException; import java.nio.charset.StandardCharsets; import java.sql.SQLException; import java.util.HashMap; @@ -35,8 +33,6 @@ public final class ThemeParkPlus extends JavaPlugin { private static Economy econ = null; private static WebhookClient webhookClient; - private static WEUtil weUtil; - private int configVersion = 2; @Override @@ -113,18 +109,24 @@ public final class ThemeParkPlus extends JavaPlugin { }).check(); } - if (Bukkit.getPluginManager().isPluginEnabled("ThemePark")) { + if (Bukkit.getPluginManager().getPlugin("ThemePark") == null) { Bukkit.getLogger().severe("[ThemeParkPlus] Missing ThemePark! Please install it first."); getServer().getPluginManager().disablePlugin(this); return; } - if (Bukkit.getPluginManager().isPluginEnabled("WorldEdit")) { + if (Bukkit.getPluginManager().getPlugin("WorldEdit") == null) { Bukkit.getLogger().severe("[ThemeParkPlus] Missing WorldEdit! Please install it first."); getServer().getPluginManager().disablePlugin(this); return; } + if (Bukkit.getPluginManager().getPlugin("WorldGuard") == null) { + Bukkit.getLogger().severe("[ThemeParkPlus] Missing WorldGuard! Please install it first."); + getServer().getPluginManager().disablePlugin(this); + return; + } + if (!setupEconomy()) { Bukkit.getLogger().severe("[ThemeParkPlus] Missing Vault! Please install it first."); getServer().getPluginManager().disablePlugin(this); @@ -137,6 +139,7 @@ public final class ThemeParkPlus extends JavaPlugin { Bukkit.getLogger().info("[ThemeParkPlus] Loading listeners..."); Bukkit.getPluginManager().registerEvents(new DirectionalGateListener(), this); Bukkit.getPluginManager().registerEvents(new FastpassListeners(), this); + Bukkit.getPluginManager().registerEvents(new SignListeners(), this); Bukkit.getPluginManager().registerEvents(new WaitingTimeListener(), this); if (getSConfig().getFile().getBoolean("AntiFreerun.Enabled")) { Bukkit.getPluginManager().registerEvents(new AntiFreerunListener(), this); @@ -163,15 +166,7 @@ public final class ThemeParkPlus extends JavaPlugin { } Bukkit.getLogger().info("[ThemeParkPlus] Loading Lamp & Gate util..."); - try { - new LGUtil(); - } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { - e.printStackTrace(); - Bukkit.getLogger().severe("[ThemeParkPlus] Lamp & Gate util not compatible with your Spigot version. The plugin won't work as intended."); - } - - Bukkit.getLogger().info("[ThemeParkPlus] Loading WorldEdit util..."); - weUtil = new WEUtil(); + new LGUtil(); Bukkit.getLogger().info("[ThemeParkPlus] Loading data..."); try { @@ -218,10 +213,6 @@ public final class ThemeParkPlus extends JavaPlugin { return econ; } - public static WEUtil getWEUtil() { - return weUtil; - } - public static WebhookClient getWebhookClient() { return webhookClient; } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java index ea152ae..f43bb3e 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/PlusAPI.java @@ -1,6 +1,7 @@ package nl.sbdeveloper.themeparkplus.api; -import com.sk89q.worldedit.bukkit.BukkitAdapter; +import com.sk89q.worldguard.protection.ApplicableRegionSet; +import com.sk89q.worldguard.protection.regions.ProtectedRegion; import de.tr7zw.changeme.nbtapi.NBTItem; import me.paradoxpixel.themepark.api.attraction.Attraction; import nl.sbdeveloper.themeparkplus.ThemeParkPlus; @@ -8,6 +9,7 @@ import nl.sbdeveloper.themeparkplus.api.objects.Gate; import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow; import nl.sbdeveloper.themeparkplus.util.ConfigUtil; import nl.sbdeveloper.themeparkplus.util.XMaterial; +import nl.sbdeveloper.themeparkplus.util.WorldGuardLegacyManager; import org.bukkit.Location; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; @@ -114,9 +116,16 @@ public class PlusAPI { * @param loc The location * @return The row */ - @NotNull - public static Optional getRow(Location loc) { - return rows.values().stream().filter(row -> row.getRegion().contains(BukkitAdapter.asBlockVector(loc))).findFirst(); + @Nullable + public static WaitingRow getRow(Location loc) { + if (loc == null) return null; + ApplicableRegionSet set = WorldGuardLegacyManager.getInstance().getApplicableRegionSet(loc); + for (WaitingRow row : rows.values()) { + if (set.getRegions().stream().anyMatch(region -> region.getId().equalsIgnoreCase(row.getRegionID()))) { + return row; + } + } + return null; } /** diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/SignLocation.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/SignLocation.java new file mode 100644 index 0000000..d54f1bd --- /dev/null +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/SignLocation.java @@ -0,0 +1,29 @@ +package nl.sbdeveloper.themeparkplus.api.objects; + +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; +import lombok.Setter; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.block.Block; +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; + +@Getter @Setter @NoArgsConstructor @AllArgsConstructor +public class SignLocation { + private String worldName; + private int x; + private int y; + private int z; + + @NotNull + public static SignLocation getFromLocation(@NotNull Location in) { + return new SignLocation(Objects.requireNonNull(in.getWorld()).getName(), in.getBlockX(), in.getBlockY(), in.getBlockZ()); + } + + public Block getBlock() { + return Bukkit.getWorld(worldName).getBlockAt(x, y, z); + } +} diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/WaitingRow.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/WaitingRow.java index 3507232..565caf4 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/WaitingRow.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/api/objects/WaitingRow.java @@ -1,36 +1,34 @@ package nl.sbdeveloper.themeparkplus.api.objects; -import com.sk89q.worldedit.regions.Region; import lombok.AllArgsConstructor; import lombok.Getter; import lombok.NoArgsConstructor; import lombok.Setter; -import org.bukkit.Location; import java.util.ArrayList; @NoArgsConstructor @AllArgsConstructor public class WaitingRow { @Getter @Setter private String rideID; - @Getter @Setter private Region region; - @Getter @Setter private int waitingPlayers = 0; - @Getter @Setter private double waitingTimeMinutes = 0; - private ArrayList signLocations = new ArrayList<>(); + @Getter @Setter private String regionID; + @Getter @Setter private transient int waitingPlayers = 0; + @Getter @Setter private transient double waitingTimeMinutes = 0; + private ArrayList signLocations = new ArrayList<>(); - public WaitingRow(String rideID, Region region) { + public WaitingRow(String rideID, String regionID) { this.rideID = rideID; - this.region = region; + this.regionID = regionID; } - public ArrayList getSignLocations() { + public ArrayList getSignLocations() { return signLocations; } - public void addSignLocation(Location loc) { + public void addSignLocation(SignLocation loc) { this.signLocations.add(loc); } - public void removeSignLocation(Location loc) { + public void removeSignLocation(SignLocation loc) { this.signLocations.remove(loc); } } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java index 330c0a5..de571fe 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/SignListeners.java @@ -1,9 +1,9 @@ package nl.sbdeveloper.themeparkplus.listeners; -import com.sk89q.worldedit.regions.Region; import me.paradoxpixel.themepark.api.API; import nl.sbdeveloper.themeparkplus.ThemeParkPlus; import nl.sbdeveloper.themeparkplus.api.PlusAPI; +import nl.sbdeveloper.themeparkplus.api.objects.SignLocation; import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow; import nl.sbdeveloper.themeparkplus.util.ConfigUtil; import org.bukkit.block.BlockState; @@ -42,7 +42,7 @@ public class SignListeners implements Listener { return; } - if (!API.isAttraction(e.getLine(2))) { + if (!API.isAttraction(lines[2])) { p.sendMessage(ConfigUtil.getMessage("Fastpass.UnknownRide", Collections.singletonMap("ridename", e.getLine(2)))); return; } @@ -61,15 +61,18 @@ public class SignListeners implements Listener { e.setLine(0, sLineOne); e.setLine(1, sLineTwo); } else if (lines[1].equalsIgnoreCase("WaitingRow") && !lines[2].isEmpty() && !lines[3].isEmpty()) { + WaitingRow foundRow = PlusAPI.getRow(lines[2]); + if (foundRow == null) { + foundRow = new WaitingRow(lines[2], lines[3]); + PlusAPI.addRow(foundRow); + } + foundRow.addSignLocation(SignLocation.getFromLocation(e.getBlock().getLocation())); + ThemeParkPlus.getData().save(); + e.setLine(0, wrLineOne); e.setLine(1, wrLineTwo); - - Region selected = ThemeParkPlus.getWEUtil().getSelection(p); - - //AND SETUP - WaitingRow row = new WaitingRow(lines[2], selected); - PlusAPI.addRow(row); - ThemeParkPlus.getData().save(); + e.setLine(2, ConfigUtil.makecolored(API.getAttraction(lines[2]).getName())); + e.setLine(3, "0 min."); p.sendMessage(ConfigUtil.getMessage("WaitingRow.SignCreated")); } else { @@ -88,7 +91,7 @@ public class SignListeners implements Listener { if (row.getSignLocations().size() == 1) { PlusAPI.removeRow(row); } else { - row.getSignLocations().remove(e.getBlock().getLocation()); + row.removeSignLocation(SignLocation.getFromLocation(e.getBlock().getLocation())); } ThemeParkPlus.getData().save(); } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/WaitingTimeListener.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/WaitingTimeListener.java index 6e3b51d..8fd6528 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/WaitingTimeListener.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/listeners/WaitingTimeListener.java @@ -2,66 +2,95 @@ package nl.sbdeveloper.themeparkplus.listeners; import nl.sbdeveloper.themeparkplus.ThemeParkPlus; import nl.sbdeveloper.themeparkplus.api.PlusAPI; +import nl.sbdeveloper.themeparkplus.api.objects.SignLocation; import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow; import nl.sbdeveloper.themeparkplus.util.ConfigUtil; import org.bukkit.Bukkit; import org.bukkit.Location; -import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.Sign; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; +import org.bukkit.event.player.PlayerJoinEvent; import org.bukkit.event.player.PlayerMoveEvent; +import org.bukkit.event.player.PlayerQuitEvent; +import org.bukkit.event.player.PlayerTeleportEvent; import org.jetbrains.annotations.NotNull; import java.util.Collections; -import java.util.Optional; public class WaitingTimeListener implements Listener { private static final double PER_PLAYER = ThemeParkPlus.getSConfig().getFile().getDouble("WaitingRow.MinutesPerPlayer"); + @EventHandler + public void onPlayerJoin(@NotNull PlayerJoinEvent e) { + doWaitingRowThings(null, e.getPlayer().getLocation().getBlock().getLocation()); + } + + @EventHandler + public void onPlayerQuit(@NotNull PlayerQuitEvent e) { + doWaitingRowThings(e.getPlayer().getLocation().getBlock().getLocation(), null); + } + + @EventHandler + public void onTeleport(@NotNull PlayerTeleportEvent e) { + if (e.getTo() == null) return; + if ((e.getFrom().getBlockX() != e.getTo().getBlockX()) + || (e.getFrom().getBlockY() != e.getTo().getBlockY()) + || (e.getFrom().getBlockZ() != e.getTo().getBlockZ())) { + doWaitingRowThings(e.getFrom().getBlock().getLocation(), e.getTo().getBlock().getLocation()); + } + } + @EventHandler public void onPlayerMove(@NotNull PlayerMoveEvent e) { if (e.getTo() == null) return; if ((e.getFrom().getBlockX() != e.getTo().getBlockX()) || (e.getFrom().getBlockY() != e.getTo().getBlockY()) || (e.getFrom().getBlockZ() != e.getTo().getBlockZ())) { - Optional rowOptional = PlusAPI.getRow(e.getFrom()); - Optional rowOptional2 = PlusAPI.getRow(e.getTo()); + doWaitingRowThings(e.getFrom().getBlock().getLocation(), e.getTo().getBlock().getLocation()); + } + } - WaitingRow row = null; - int waitingNew = 0; - double waitingTimeNew = 0; - if (!rowOptional.isPresent() && rowOptional2.isPresent()) { - //ENTER - row = rowOptional2.get(); - waitingNew = row.getWaitingPlayers() + 1; - waitingTimeNew = row.getWaitingTimeMinutes() + PER_PLAYER; - } else if (rowOptional.isPresent() && !rowOptional2.isPresent()) { - //LEAVE - row = rowOptional.get(); - waitingNew = row.getWaitingPlayers() - 1; - waitingTimeNew = row.getWaitingTimeMinutes() - PER_PLAYER; - } + private void doWaitingRowThings(Location from, Location to) { + WaitingRow row = PlusAPI.getRow(from); + WaitingRow row2 = PlusAPI.getRow(to); - if (row == null) return; + WaitingRow usedRow = null; + int waitingNew = 0; + double waitingTimeNew = 0; + if (row == null && row2 != null) { + //ENTER + usedRow = row2; + waitingNew = row2.getWaitingPlayers() + 1; + waitingTimeNew = row2.getWaitingTimeMinutes() + PER_PLAYER; + } else if (row != null && row2 == null){ + //LEAVE + usedRow = row; + waitingNew = row.getWaitingPlayers() - 1; + waitingTimeNew = row.getWaitingTimeMinutes() - PER_PLAYER; + } - row.setWaitingPlayers(waitingNew); - row.setWaitingTimeMinutes(waitingTimeNew); + if (usedRow == null) return; - for (Location location : row.getSignLocations()) { - Block signblock = location.getBlock(); - BlockState signblocks = signblock.getState(); - if ((signblocks instanceof Sign)) { - Sign signs = (Sign) signblocks; - signs.setLine(3, waitingNew + " min."); - signs.update(); + usedRow.setWaitingPlayers(waitingNew); + usedRow.setWaitingTimeMinutes(waitingTimeNew); + + for (SignLocation signLocation : usedRow.getSignLocations()) { + BlockState signblocks = signLocation.getBlock().getState(); + if (signblocks instanceof Sign) { + Sign signs = (Sign) signblocks; + signs.setLine(3, waitingNew + " min."); + signs.update(); + } else { + Bukkit.getLogger().warning(ConfigUtil.getMessage("WaitingRow.WrongLocation", Collections.singletonMap("%ridename%", usedRow.getRideID()))); + if (usedRow.getSignLocations().size() == 1) { + PlusAPI.removeRow(usedRow); } else { - Bukkit.getLogger().warning(ConfigUtil.getMessage("WaitingRow.WrongLocation", Collections.singletonMap("%ridename%", row.getRideID()))); - PlusAPI.removeRow(row); - ThemeParkPlus.getData().save(); + usedRow.removeSignLocation(signLocation); } + ThemeParkPlus.getData().save(); } } } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/managers/DBManager.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/managers/DBManager.java index cd0c910..1079909 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/managers/DBManager.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/managers/DBManager.java @@ -3,6 +3,7 @@ package nl.sbdeveloper.themeparkplus.managers; import com.google.gson.Gson; import nl.sbdeveloper.themeparkplus.api.PlusAPI; import nl.sbdeveloper.themeparkplus.api.objects.Gate; +import nl.sbdeveloper.themeparkplus.api.objects.WaitingRow; import nl.sbdeveloper.themeparkplus.sbutils.LocationSerializer; import nl.sbdeveloper.themeparkplus.sbutils.SQLiteDB; import org.bukkit.Location; @@ -27,6 +28,10 @@ public class DBManager { String query = "CREATE TABLE IF NOT EXISTS gates (gateLocation varchar(255) NOT NULL, gateData blob NOT NULL, UNIQUE (gateLocation))"; PreparedStatement statement = con.prepareStatement(query); statement.execute(); + + query = "CREATE TABLE IF NOT EXISTS rows (rideID varchar(255) NOT NULL, rowData blob NOT NULL, UNIQUE (rideID))"; + statement = con.prepareStatement(query); + statement.execute(); } catch (SQLException e) { e.printStackTrace(); } @@ -46,11 +51,24 @@ public class DBManager { Gate gate = gson.fromJson(json, Gate.class); PlusAPI.addGate(gate); } + + /* Load rows */ + query = "SELECT * FROM rows"; + statement = con.prepareStatement(query); + ResultSet rowSet = statement.executeQuery(); + while (rowSet.next()) { + //Loading a gates... + byte[] blob = rowSet.getBytes("rowData"); + String json = new String(blob); + + Gson gson = new Gson(); + WaitingRow row = gson.fromJson(json, WaitingRow.class); + PlusAPI.addRow(row); + } } public void save() { for (Map.Entry entry : PlusAPI.getGates().entrySet()) { - Gson gson = new Gson(); byte[] blob = gson.toJson(entry.getValue()).getBytes(); @@ -72,10 +90,33 @@ public class DBManager { e.printStackTrace(); } } + + for (Map.Entry entry : PlusAPI.getRows().entrySet()) { + + Gson gson = new Gson(); + byte[] blob = gson.toJson(entry.getValue()).getBytes(); + + try { + String query = "INSERT INTO rows (rideID, rowData) VALUES (?, ?)"; + PreparedStatement statement = con.prepareStatement(query); + statement.setString(1, entry.getKey()); + statement.setBytes(2, blob); + statement.executeUpdate(); + } catch (SQLException ignored) {} + + try { + String query2 = "UPDATE rows SET rowData = ? WHERE rideID = ?"; + PreparedStatement statement2 = con.prepareStatement(query2); + statement2.setBytes(1, blob); + statement2.setString(2, entry.getKey()); + statement2.executeUpdate(); + } catch (SQLException e) { + e.printStackTrace(); + } + } } public void closeConnection() { data.closeSource(); } - } diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/util/LGUtil.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/util/LGUtil.java index a835059..9e91ec6 100644 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/util/LGUtil.java +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/util/LGUtil.java @@ -28,7 +28,7 @@ public class LGUtil { private static Object lampAanData; private static Object lampUitData; - public LGUtil() throws ClassNotFoundException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { + public LGUtil() { offsets.add(new Vector(1, 0, 0)); offsets.add(new Vector(-1, 0, 0)); offsets.add(new Vector(0, 1, 0)); @@ -36,16 +36,17 @@ public class LGUtil { offsets.add(new Vector(0, 0, 1)); offsets.add(new Vector(0, 0, -1)); - Class blockDataClass = Class.forName("org.bukkit.block.data.BlockData"); - Method methodCreateBlockData = Bukkit.class.getMethod("createBlockData", String.class); - getBlockDataMethod = Block.class.getMethod("getBlockData"); - setBlockDataMethod = Block.class.getMethod("setBlockData", blockDataClass, - Boolean.TYPE); - getAsStringMethod = blockDataClass.getMethod("getAsString"); - lampAanData = methodCreateBlockData.invoke(null, "minecraft:redstone_lamp[lit=true]"); - lampUitData = methodCreateBlockData.invoke(null, "minecraft:redstone_lamp[lit=false]"); - - nieuweVersie = true; + try { + Class blockDataClass = Class.forName("org.bukkit.block.data.BlockData"); + Method methodCreateBlockData = Bukkit.class.getMethod("createBlockData", String.class); + getBlockDataMethod = Block.class.getMethod("getBlockData"); + setBlockDataMethod = Block.class.getMethod("setBlockData", blockDataClass, + Boolean.TYPE); + getAsStringMethod = blockDataClass.getMethod("getAsString"); + lampAanData = methodCreateBlockData.invoke(null, "minecraft:redstone_lamp[lit=true]"); + lampUitData = methodCreateBlockData.invoke(null, "minecraft:redstone_lamp[lit=false]"); + nieuweVersie = true; + } catch (ClassNotFoundException | NoSuchMethodException | IllegalAccessException | InvocationTargetException ignored) {} } public static boolean zetLampAan(Block lampBlock) { diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/util/Reflection.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/util/Reflection.java new file mode 100644 index 0000000..fa9112d --- /dev/null +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/util/Reflection.java @@ -0,0 +1,38 @@ +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; + } + } +} \ No newline at end of file diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/util/WEUtil.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/util/WEUtil.java deleted file mode 100644 index b5a44cd..0000000 --- a/src/main/lombok/nl/sbdeveloper/themeparkplus/util/WEUtil.java +++ /dev/null @@ -1,47 +0,0 @@ -package nl.sbdeveloper.themeparkplus.util; - -import com.sk89q.worldedit.IncompleteRegionException; -import com.sk89q.worldedit.LocalSession; -import com.sk89q.worldedit.bukkit.WorldEditPlugin; -import com.sk89q.worldedit.regions.Region; -import com.sk89q.worldedit.regions.RegionSelector; -import org.bukkit.Bukkit; -import org.bukkit.entity.Player; -import org.jetbrains.annotations.Nullable; - -public class WEUtil { - private static WorldEditPlugin wep; - - public WEUtil() { - wep = (WorldEditPlugin) Bukkit.getPluginManager().getPlugin("WorldEdit"); - } - - @Nullable - public Region getSelection(Player p) { - LocalSession session; - try { - session = wep.getSession(p); - } catch (Exception e) { - // sometimes after a reload getSession create errors with WorldEdit, this prevent error spam - return null; - } - - return getSelectedRegion(session); - } - - @Nullable - private Region getSelectedRegion(@Nullable LocalSession session) { - if (session != null && session.getSelectionWorld() != null) { - RegionSelector selector = session.getRegionSelector(session.getSelectionWorld()); - - if (selector.isDefined()) { - try { - return selector.getRegion(); - } catch (IncompleteRegionException e) { - Bukkit.getLogger().warning("[ThemeParkPlus] Region still incomplete"); - } - } - } - return null; - } -} diff --git a/src/main/lombok/nl/sbdeveloper/themeparkplus/util/WorldGuardLegacyManager.java b/src/main/lombok/nl/sbdeveloper/themeparkplus/util/WorldGuardLegacyManager.java new file mode 100644 index 0000000..e023806 --- /dev/null +++ b/src/main/lombok/nl/sbdeveloper/themeparkplus/util/WorldGuardLegacyManager.java @@ -0,0 +1,195 @@ +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 getRegions(Location loc) { + ArrayList 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) 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; + } +} \ No newline at end of file diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index 4bbf9f1..02d68c7 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -3,7 +3,7 @@ version: ${project.version} main: nl.sbdeveloper.themeparkplus.ThemeParkPlus api-version: "1.13" authors: [SBDeveloper] -softdepend: [Vault, ThemePark, WorldEdit] +softdepend: [Vault, ThemePark, WorldEdit, WorldGuard] description: Plus version of ThemePark! website: https://sbdplugins.nl commands: