From 6e20629f038a7d1af75bdf1dbe762685a3fcea82 Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Sun, 29 Sep 2019 09:55:47 -0500 Subject: [PATCH 1/4] formatting --- .../worldguard/WorldGuardFlagHandler.java | 139 ++++++++++-------- 1 file changed, 79 insertions(+), 60 deletions(-) diff --git a/Core/src/main/java/com/songoda/core/hooks/worldguard/WorldGuardFlagHandler.java b/Core/src/main/java/com/songoda/core/hooks/worldguard/WorldGuardFlagHandler.java index df70ec7c..0cff57d8 100644 --- a/Core/src/main/java/com/songoda/core/hooks/worldguard/WorldGuardFlagHandler.java +++ b/Core/src/main/java/com/songoda/core/hooks/worldguard/WorldGuardFlagHandler.java @@ -26,24 +26,24 @@ import org.bukkit.Location; /** * Hooks for adding a custom WorldGuard flag - * + * * Note: Hooks must be added before WG loads! */ public class WorldGuardFlagHandler { - - static Boolean wgPlugin = null; + + static Boolean wgPlugin = null; static Object worldGuardPlugin; static boolean wg_v7 = false; static boolean legacy_v60 = false; static boolean legacy_v62 = false; static boolean legacy_v5 = false; - static boolean hooksInstalled = false; + static boolean hooksInstalled = false; static Map flags = new HashMap(); /** * Attempt to register a worldGuard flag (ALLOW/DENY)
* Note: This must be called before WorldGuard loads, or it will fail. - * + * * @param flag name of the flag to set * @param state default value of the flag */ @@ -64,7 +64,8 @@ public class WorldGuardFlagHandler { // if this class exists, we're on 6.0 Class.forName("com.sk89q.worldguard.protection.flags.BuildFlag"); legacy_v60 = true; - } catch (ClassNotFoundException ex3) {try { + } catch (ClassNotFoundException ex3) { + try { // if this class exists, we're on 5.x Class.forName("com.sk89q.worldguard.protection.flags.DefaultFlag"); legacy_v5 = true; @@ -76,7 +77,9 @@ public class WorldGuardFlagHandler { } } } - if (!wgPlugin) return; + if (!wgPlugin) { + return; + } if (legacy_v62 || legacy_v60 || legacy_v5) { addLegacyHook(flag, state); @@ -84,21 +87,21 @@ public class WorldGuardFlagHandler { } StateFlag addFlag = new StateFlag(flag, state); - try { - WorldGuard.getInstance().getFlagRegistry().register(addFlag); + try { + WorldGuard.getInstance().getFlagRegistry().register(addFlag); flags.put(flag, addFlag); - } catch (Exception ex) { + } catch (Exception ex) { Bukkit.getServer().getLogger().log(Level.WARNING, "Could not add flag {0} to WorldGuard", addFlag.getName()); - Flag wgFlag = (StateFlag) WorldGuard.getInstance().getFlagRegistry().get(addFlag.getName()); - if (wgFlag == null) { + Flag wgFlag = (StateFlag) WorldGuard.getInstance().getFlagRegistry().get(addFlag.getName()); + if (wgFlag == null) { wgPlugin = false; - Bukkit.getServer().getLogger().log(Level.WARNING, "Could not hook WorldGuard"); + Bukkit.getServer().getLogger().log(Level.WARNING, "Could not hook WorldGuard"); } else { flags.put(flag, wgFlag); - Bukkit.getServer().getLogger().log(Level.WARNING, "Loaded existing {1} {0}", new Object[] {wgFlag.getName(), wgFlag.getClass().getSimpleName()}); + Bukkit.getServer().getLogger().log(Level.WARNING, "Loaded existing {1} {0}", new Object[]{wgFlag.getName(), wgFlag.getClass().getSimpleName()}); } - } - } + } + } // reflection to add hooks private static void addLegacyHook(String flag, boolean state) { @@ -106,15 +109,15 @@ public class WorldGuardFlagHandler { // 6.0 has the same classpath for StateFlag as the current version does // does this flag exist already? Class defaultFlagClazz = Class.forName("com.sk89q.worldguard.protection.flags.DefaultFlag"); - Field flagField = defaultFlagClazz.getField("flagsList"); + Field flagField = defaultFlagClazz.getField("flagsList"); Flag[] flagsOld = (Flag[]) flagField.get(null); Flag wgFlag = Stream.of(flagsOld) - .filter(f -> ((Flag)f).getName().equalsIgnoreCase(flag)) + .filter(f -> ((Flag) f).getName().equalsIgnoreCase(flag)) .findFirst().orElse(null); if (wgFlag != null) { // we already have one flags.put(flag, wgFlag); - Bukkit.getServer().getLogger().log(Level.WARNING, "Loaded existing {1} {0}", new Object[] {wgFlag.getName(), wgFlag.getClass().getSimpleName()}); + Bukkit.getServer().getLogger().log(Level.WARNING, "Loaded existing {1} {0}", new Object[]{wgFlag.getName(), wgFlag.getClass().getSimpleName()}); return; } @@ -123,16 +126,16 @@ public class WorldGuardFlagHandler { // we need to sneak our flag into the array // make a copy first - Flag[] flagsNew = new Flag[flagsOld.length + 1]; - System.arraycopy(flagsOld, 0, flagsNew, 0, flagsOld.length); + Flag[] flagsNew = new Flag[flagsOld.length + 1]; + System.arraycopy(flagsOld, 0, flagsNew, 0, flagsOld.length); // add ours - flagsNew[flagsNew.length - 1] = wgFlag; + flagsNew[flagsNew.length - 1] = wgFlag; // and put the new list into place - setStaticField(flagField, flagsNew); + setStaticField(flagField, flagsNew); - if(legacy_v62) { // SimpleFlagRegistry is NOT in 6.0 + if (legacy_v62) { // SimpleFlagRegistry is NOT in 6.0 // register this flag in the registry Object flagRegistry = getPrivateField(worldGuardPlugin.getClass(), worldGuardPlugin, "flagRegistry"); Class simpleFlagRegistryClazz = Class.forName("com.sk89q.worldguard.protection.flags.registry.SimpleFlagRegistry"); @@ -145,24 +148,24 @@ public class WorldGuardFlagHandler { } catch (Exception ex) { //Bukkit.getServer().getLogger().log(Level.WARNING, "Failed to set legacy WorldGuard Flags", ex); Bukkit.getServer().getLogger().log(Level.WARNING, "Could not add flag {0} to WorldGuard " + (legacy_v62 ? "6.2" : (legacy_v60 ? "6.0" : "5")), flag); - Bukkit.getServer().getLogger().log(Level.WARNING, "Could not hook WorldGuard"); + Bukkit.getServer().getLogger().log(Level.WARNING, "Could not hook WorldGuard"); wgPlugin = false; } } private static Object getPrivateField(Class c, Object handle, String fieldName) throws Exception { - Field field = c.getDeclaredField(fieldName); - field.setAccessible(true); - return field.get(handle); - } + Field field = c.getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(handle); + } private static void setStaticField(Field field, Object value) throws Exception { field.setAccessible(true); - Field modifier = Field.class.getDeclaredField("modifiers"); - modifier.setAccessible(true); - modifier.setInt(field, field.getModifiers() & ~Modifier.FINAL); - field.set(null, value); - } + Field modifier = Field.class.getDeclaredField("modifiers"); + modifier.setAccessible(true); + modifier.setInt(field, field.getModifiers() & ~Modifier.FINAL); + field.set(null, value); + } public static boolean isEnabled() { return wgPlugin != null && wgPlugin; @@ -170,20 +173,25 @@ public class WorldGuardFlagHandler { /** * Checks this location to see what this flag is set to + * * @param l location to check * @param flag ALLOW/DENY flag to check * @return flag state, or null if undefined */ public static Boolean getBooleanFlag(Location l, String flag) { - if (wgPlugin == null || !wgPlugin) return null; + if (wgPlugin == null || !wgPlugin) { + return null; + } Object flagObj = flags.get(flag); // There's a different way to get this in the old version - if (legacy_v62 || legacy_v60 || legacy_v5) + if (legacy_v62 || legacy_v60 || legacy_v5) { return flagObj == null ? null : getBooleanFlagLegacy(l, flagObj); + } // for convinience, we can load a flag if we don't know it - if (flagObj == null) + if (flagObj == null) { flags.put(flag, flagObj = WorldGuard.getInstance().getFlagRegistry().get(flag)); + } // so, what's up? if (flagObj instanceof StateFlag) { @@ -197,33 +205,40 @@ public class WorldGuardFlagHandler { /** * Query all regions that are in or intersect this chunk + * * @param c chunk to check for regions in * @param flag ALLOW/DENY flag to check * @return flag state, or null if undefined */ public static Boolean getBooleanFlag(Chunk c, String flag) { - if (wgPlugin == null || !wgPlugin) return null; + if (wgPlugin == null || !wgPlugin) { + return null; + } Object flagObj = flags.get(flag); // There's a different way to get this in the old version - if (legacy_v62 || legacy_v60 || legacy_v5) + if (legacy_v62 || legacy_v60 || legacy_v5) { return flagObj == null ? null : getBooleanFlagLegacy(c, flagObj); + } // for convinience, we can load a flag if we don't know it - if (flagObj == null) + if (flagObj == null) { flags.put(flag, flagObj = WorldGuard.getInstance().getFlagRegistry().get(flag)); + } // so, what's up? if (flagObj instanceof StateFlag) { RegionManager worldManager = WorldGuard.getInstance().getPlatform().getRegionContainer().get(BukkitAdapter.adapt(c.getWorld())); - if (worldManager == null) + if (worldManager == null) { return null; + } ProtectedCuboidRegion chunkRegion = new ProtectedCuboidRegion("__TEST__", - BlockVector3.at(c.getX() << 4, c.getWorld().getMaxHeight(), c.getZ() << 4), - BlockVector3.at((c.getX() << 4) + 15, 0, (c.getZ() << 4) + 15)); + BlockVector3.at(c.getX() << 4, c.getWorld().getMaxHeight(), c.getZ() << 4), + BlockVector3.at((c.getX() << 4) + 15, 0, (c.getZ() << 4) + 15)); ApplicableRegionSet set = worldManager.getApplicableRegions(chunkRegion); State result = set.queryState((RegionAssociable) null, (StateFlag) flagObj); - if (result == null && set.size() == 0) + if (result == null && set.size() == 0) { return null; + } return result == State.ALLOW; } return null; @@ -246,9 +261,9 @@ public class WorldGuardFlagHandler { if (legacy_getRegionManager == null) { legacy_getRegionManager = worldGuardPlugin.getClass() .getDeclaredMethod("getRegionManager", org.bukkit.World.class); - legacy_getApplicableRegions_Region = RegionManager.class.getDeclaredMethod("getApplicableRegions", + legacy_getApplicableRegions_Region = RegionManager.class.getDeclaredMethod("getApplicableRegions", Class.forName("com.sk89q.worldguard.protection.regions.ProtectedRegion")); - legacy_getApplicableRegions_Location = RegionManager.class.getDeclaredMethod("getApplicableRegions", + legacy_getApplicableRegions_Location = RegionManager.class.getDeclaredMethod("getApplicableRegions", Location.class); legacy_blockVectorClazz = Class.forName("com.sk89q.worldedit.BlockVector"); legacy_newblockVector = legacy_blockVectorClazz.getConstructor(int.class, int.class, int.class); @@ -261,8 +276,9 @@ public class WorldGuardFlagHandler { // grab the applicable manager for this world Object worldManager = (RegionManager) legacy_getRegionManager.invoke(worldGuardPlugin, l.getWorld()); - if (worldManager == null) + if (worldManager == null) { return null; + } // create a vector object Object vec = legacy_newVectorClazz.newInstance(l.getBlockX(), l.getBlockY(), l.getBlockZ()); @@ -271,23 +287,24 @@ public class WorldGuardFlagHandler { // so what's the verdict? State result; - if(legacy_v62 || legacy_v60) { + if (legacy_v62 || legacy_v60) { result = (State) ((ApplicableRegionSet) set).queryState((RegionAssociable) null, (StateFlag) flag); } else { // v5 has a different class signature for ApplicableRegionSet // also doesn't have a "queryState" function //getFlag(T flag) - if(legacy5_applicableRegionSet_getFlag == null) { + if (legacy5_applicableRegionSet_getFlag == null) { legacy5_applicableRegionSet_getFlag = Class.forName("com.sk89q.worldguard.protection.ApplicableRegionSet").getMethod("getFlag", Object.class); } result = (State) legacy5_applicableRegionSet_getFlag.invoke(set, flag); } - if (result == null && set != null && ((Iterable) set).iterator().hasNext()) + if (result == null && set != null && ((Iterable) set).iterator().hasNext()) { return null; + } return result == State.ALLOW; } catch (Exception ex) { - Bukkit.getServer().getLogger().log(Level.WARNING, "Could not grab flags from WorldGuard", ex); + Bukkit.getServer().getLogger().log(Level.WARNING, "Could not grab flags from WorldGuard", ex); } return null; } @@ -300,9 +317,9 @@ public class WorldGuardFlagHandler { if (legacy_getRegionManager == null) { legacy_getRegionManager = worldGuardPlugin.getClass() .getDeclaredMethod("getRegionManager", org.bukkit.World.class); - legacy_getApplicableRegions_Region = RegionManager.class.getDeclaredMethod("getApplicableRegions", + legacy_getApplicableRegions_Region = RegionManager.class.getDeclaredMethod("getApplicableRegions", Class.forName("com.sk89q.worldguard.protection.regions.ProtectedRegion")); - legacy_getApplicableRegions_Location = RegionManager.class.getDeclaredMethod("getApplicableRegions", + legacy_getApplicableRegions_Location = RegionManager.class.getDeclaredMethod("getApplicableRegions", Location.class); legacy_blockVectorClazz = Class.forName("com.sk89q.worldedit.BlockVector"); legacy_newblockVector = legacy_blockVectorClazz.getConstructor(int.class, int.class, int.class); @@ -315,36 +332,38 @@ public class WorldGuardFlagHandler { // grab the applicable manager for this world Object worldManager = (RegionManager) legacy_getRegionManager.invoke(worldGuardPlugin, c.getWorld()); - if (worldManager == null) + if (worldManager == null) { return null; + } // Create a legacy ProtectedCuboidRegion Object chunkRegion = legacy_newProtectedCuboidRegion.newInstance("__TEST__", - legacy_newblockVector.newInstance(c.getX() << 4, c.getWorld().getMaxHeight(), c.getZ() << 4), - legacy_newblockVector.newInstance((c.getX() << 4) + 15, 0, (c.getZ() << 4) + 15)); + legacy_newblockVector.newInstance(c.getX() << 4, c.getWorld().getMaxHeight(), c.getZ() << 4), + legacy_newblockVector.newInstance((c.getX() << 4) + 15, 0, (c.getZ() << 4) + 15)); // now look for any intersecting regions Object set = legacy_getApplicableRegions_Region.invoke(worldManager, chunkRegion); // so what's the verdict? State result; - if(legacy_v62 || legacy_v60) { + if (legacy_v62 || legacy_v60) { result = (State) ((ApplicableRegionSet) set).queryState((RegionAssociable) null, (StateFlag) flag); } else { // v5 has a different class signature for ApplicableRegionSet // also doesn't have a "queryState" function //getFlag(T flag) - if(legacy5_applicableRegionSet_getFlag == null) { + if (legacy5_applicableRegionSet_getFlag == null) { legacy5_applicableRegionSet_getFlag = Class.forName("com.sk89q.worldguard.protection.ApplicableRegionSet").getMethod("getFlag", Flag.class); } result = (State) legacy5_applicableRegionSet_getFlag.invoke(set, flag); } - if (result == null && set != null && ((Iterable) set).iterator().hasNext()) + if (result == null && set != null && ((Iterable) set).iterator().hasNext()) { return null; + } return result == State.ALLOW; } catch (Exception ex) { - Bukkit.getServer().getLogger().log(Level.WARNING, "Could not grab flags from WorldGuard", ex); + Bukkit.getServer().getLogger().log(Level.WARNING, "Could not grab flags from WorldGuard", ex); } return null; } From a305dfa014f3ccdd6bb65f737de5d68037af8a9e Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Mon, 30 Sep 2019 18:33:45 -0500 Subject: [PATCH 2/4] add setter for gui page count --- Core/src/main/java/com/songoda/core/gui/Gui.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Core/src/main/java/com/songoda/core/gui/Gui.java b/Core/src/main/java/com/songoda/core/gui/Gui.java index 3debbed7..71afebe6 100644 --- a/Core/src/main/java/com/songoda/core/gui/Gui.java +++ b/Core/src/main/java/com/songoda/core/gui/Gui.java @@ -635,6 +635,13 @@ public class Gui { return this; } + public void setPages(int pages) { + this.pages = Math.max(1, pages); + if (page > pages) { + setPage(pages); + } + } + public void setPage(int page) { int lastPage = this.page; this.page = Math.max(1, Math.min(pages, page)); From d66e2f94c81b4336fc4912c35f6a726750af0e9c Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Mon, 30 Sep 2019 18:38:46 -0500 Subject: [PATCH 3/4] fix prev/next page item when not setting button --- Core/src/main/java/com/songoda/core/gui/Gui.java | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/Core/src/main/java/com/songoda/core/gui/Gui.java b/Core/src/main/java/com/songoda/core/gui/Gui.java index 71afebe6..82da0cae 100644 --- a/Core/src/main/java/com/songoda/core/gui/Gui.java +++ b/Core/src/main/java/com/songoda/core/gui/Gui.java @@ -602,8 +602,9 @@ public class Gui { @NotNull public Gui setNextPage(int cell, @NotNull ItemStack item) { nextPageIndex = cell; + nextPage = item; if (page < pages) { - setButton(nextPageIndex, nextPage = item, ClickType.LEFT, (event) -> this.nextPage()); + setButton(nextPageIndex, nextPage, ClickType.LEFT, (event) -> this.nextPage()); } return this; } @@ -611,8 +612,9 @@ public class Gui { @NotNull public Gui setNextPage(int row, int col, @NotNull ItemStack item) { nextPageIndex = col + row * 9; + nextPage = item; if (page < pages) { - setButton(nextPageIndex, nextPage = item, ClickType.LEFT, (event) -> this.nextPage()); + setButton(nextPageIndex, nextPage, ClickType.LEFT, (event) -> this.nextPage()); } return this; } @@ -620,8 +622,9 @@ public class Gui { @NotNull public Gui setPrevPage(int cell, @NotNull ItemStack item) { prevPageIndex = cell; + prevPage = item; if (page > 1) { - setButton(prevPageIndex, prevPage = item, ClickType.LEFT, (event) -> this.prevPage()); + setButton(prevPageIndex, prevPage, ClickType.LEFT, (event) -> this.prevPage()); } return this; } @@ -629,8 +632,9 @@ public class Gui { @NotNull public Gui setPrevPage(int row, int col, @NotNull ItemStack item) { prevPageIndex = col + row * 9; + prevPage = item; if (page > 1) { - setButton(prevPageIndex, prevPage = item, ClickType.LEFT, (event) -> this.prevPage()); + setButton(prevPageIndex, prevPage, ClickType.LEFT, (event) -> this.prevPage()); } return this; } From d6847ec4f545248d4c3c9bba7c71d70ba20d348a Mon Sep 17 00:00:00 2001 From: jascotty2 Date: Mon, 30 Sep 2019 18:50:49 -0500 Subject: [PATCH 4/4] Version 2.1.10 --- .gitlab-ci.yml | 2 +- pom.xml | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index f77007ad..e6fb4860 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -4,7 +4,7 @@ stages: variables: name: "SongodaCore" path: "/builds/$CI_PROJECT_PATH" - version: "2.1.9" + version: "2.1.10" build: stage: build diff --git a/pom.xml b/pom.xml index 1919327b..b42ccf44 100644 --- a/pom.xml +++ b/pom.xml @@ -3,7 +3,6 @@ com.songoda SongodaCore-Modules maven-version-number - 4.0.0 pom