From 7c0d1f4e0a6b67be1ad24ecea826432febe99313 Mon Sep 17 00:00:00 2001 From: nossr50 Date: Fri, 20 Sep 2019 15:07:37 -0700 Subject: [PATCH] Remove Static Abuse - WorldGuardUtils --- .../commands/party/teleport/PtpCommand.java | 3 +-- .../nossr50/listeners/BlockListener.java | 9 ++++---- .../nossr50/listeners/EntityListener.java | 21 +++++++++---------- .../nossr50/listeners/InventoryListener.java | 13 ++++++------ .../nossr50/listeners/PlayerListener.java | 20 ++++++++---------- .../gmail/nossr50/listeners/SelfListener.java | 3 +-- src/main/java/com/gmail/nossr50/mcMMO.java | 9 ++++++++ .../gmail/nossr50/util/HardcoreManager.java | 5 ++--- .../nossr50/worldguard/WorldGuardUtils.java | 20 +++++++++++------- 9 files changed, 55 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/gmail/nossr50/commands/party/teleport/PtpCommand.java b/src/main/java/com/gmail/nossr50/commands/party/teleport/PtpCommand.java index 84c47c031..15c570b59 100644 --- a/src/main/java/com/gmail/nossr50/commands/party/teleport/PtpCommand.java +++ b/src/main/java/com/gmail/nossr50/commands/party/teleport/PtpCommand.java @@ -7,7 +7,6 @@ import com.gmail.nossr50.datatypes.party.PartyTeleportRecord; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.util.Misc; -import com.gmail.nossr50.worldguard.WorldGuardUtils; import com.google.common.collect.ImmutableList; import org.bukkit.command.Command; import org.bukkit.command.CommandExecutor; @@ -44,7 +43,7 @@ public class PtpCommand implements TabExecutor { Player player = (Player) sender; /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return true; } diff --git a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java index 69f382db7..d7f00e21e 100644 --- a/src/main/java/com/gmail/nossr50/listeners/BlockListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/BlockListener.java @@ -15,7 +15,6 @@ import com.gmail.nossr50.skills.mining.MiningManager; import com.gmail.nossr50.skills.woodcutting.WoodcuttingManager; import com.gmail.nossr50.util.sounds.SoundManager; import com.gmail.nossr50.util.sounds.SoundType; -import com.gmail.nossr50.worldguard.WorldGuardUtils; import org.bukkit.ChatColor; import org.bukkit.GameMode; import org.bukkit.Material; @@ -249,7 +248,7 @@ public class BlockListener implements Listener { return; /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(event.getPlayer())) return; } @@ -343,7 +342,7 @@ public class BlockListener implements Listener { return; /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(event.getPlayer())) return; } @@ -412,7 +411,7 @@ public class BlockListener implements Listener { return; /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(event.getPlayer())) return; } @@ -482,7 +481,7 @@ public class BlockListener implements Listener { return; /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(event.getPlayer())) return; } diff --git a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java index 374feef67..6aa6895bc 100644 --- a/src/main/java/com/gmail/nossr50/listeners/EntityListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/EntityListener.java @@ -14,7 +14,6 @@ import com.gmail.nossr50.skills.taming.TamingManager; import com.gmail.nossr50.skills.unarmed.UnarmedManager; import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.skills.SkillActivationType; -import com.gmail.nossr50.worldguard.WorldGuardUtils; import org.bukkit.Material; import org.bukkit.OfflinePlayer; import org.bukkit.block.Block; @@ -78,7 +77,7 @@ public class EntityListener implements Listener { Player player = (Player) event.getEntity(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -115,7 +114,7 @@ public class EntityListener implements Listener { Player player = (Player) event.getEntity().getShooter(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -276,7 +275,7 @@ public class EntityListener implements Listener { Entity defender = event.getEntity(); Entity attacker = event.getDamager(); - if(WorldGuardUtils.isWorldGuardLoaded()) + if(pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if(attacker instanceof Player) { @@ -450,7 +449,7 @@ public class EntityListener implements Listener { if (event.getEntity() instanceof Player) { Player player = (Player) event.getEntity(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -528,7 +527,7 @@ public class EntityListener implements Listener { if (owner instanceof Player) { Player player = (Player) owner; /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -745,7 +744,7 @@ public class EntityListener implements Listener { } /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -783,7 +782,7 @@ public class EntityListener implements Listener { } /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -846,7 +845,7 @@ public class EntityListener implements Listener { } /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -947,7 +946,7 @@ public class EntityListener implements Listener { Player player = (Player) event.getOwner(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -993,7 +992,7 @@ public class EntityListener implements Listener { Player player = (Player) target; /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } diff --git a/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java b/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java index 829d1b273..1019cad60 100644 --- a/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/InventoryListener.java @@ -5,7 +5,6 @@ import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.datatypes.skills.SubSkillType; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.runnables.player.PlayerUpdateInventoryTask; -import com.gmail.nossr50.worldguard.WorldGuardUtils; import org.bukkit.block.Block; import org.bukkit.block.BlockState; import org.bukkit.block.BrewingStand; @@ -93,7 +92,7 @@ public class InventoryListener implements Listener { Player player = getPlayerFromFurnace(furnaceBlock); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -126,7 +125,7 @@ public class InventoryListener implements Listener { Player player = getPlayerFromFurnace(furnaceBlock); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -158,7 +157,7 @@ public class InventoryListener implements Listener { Player player = getPlayerFromFurnace(furnaceBlock); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -225,7 +224,7 @@ public class InventoryListener implements Listener { Player player = (Player) whoClicked; /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -343,7 +342,7 @@ public class InventoryListener implements Listener { // Player player = (Player) whoClicked; // // /* WORLD GUARD MAIN FLAG CHECK */ -// if (WorldGuardUtils.isWorldGuardLoaded()) { +// if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { // if (!plugin.getWorldGuardManager().hasMainFlag(player)) // return; // } @@ -466,7 +465,7 @@ public class InventoryListener implements Listener { Player player = (Player) whoClicked; /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } diff --git a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java index 2640323aa..39fe9bfae 100644 --- a/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/PlayerListener.java @@ -7,7 +7,6 @@ import com.gmail.nossr50.datatypes.party.Party; import com.gmail.nossr50.datatypes.player.McMMOPlayer; import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.datatypes.skills.SubSkillType; -import com.gmail.nossr50.datatypes.skills.subskills.taming.CallOfTheWildType; import com.gmail.nossr50.events.fake.FakePlayerAnimationEvent; import com.gmail.nossr50.mcMMO; import com.gmail.nossr50.runnables.player.PlayerProfileLoadingTask; @@ -23,7 +22,6 @@ import com.gmail.nossr50.util.Misc; import com.gmail.nossr50.util.Motd; import com.gmail.nossr50.util.sounds.SoundManager; import com.gmail.nossr50.util.sounds.SoundType; -import com.gmail.nossr50.worldguard.WorldGuardUtils; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.GameMode; @@ -67,7 +65,7 @@ public class PlayerListener implements Listener { Player player = event.getPlayer(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -106,7 +104,7 @@ public class PlayerListener implements Listener { String deathMessage = event.getDeathMessage(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(event.getEntity())) return; } @@ -151,7 +149,7 @@ public class PlayerListener implements Listener { Player killer = killedPlayer.getKiller(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(killedPlayer)) return; } @@ -215,7 +213,7 @@ public class PlayerListener implements Listener { return; /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(event.getPlayer())) return; } @@ -247,7 +245,7 @@ public class PlayerListener implements Listener { Player player = event.getPlayer(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -322,7 +320,7 @@ public class PlayerListener implements Listener { Player player = event.getPlayer(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -415,7 +413,7 @@ public class PlayerListener implements Listener { Player player = (Player) event.getEntity(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -567,7 +565,7 @@ public class PlayerListener implements Listener { Player player = event.getPlayer(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } @@ -682,7 +680,7 @@ public class PlayerListener implements Listener { Player player = event.getPlayer(); /* WORLD GUARD MAIN FLAG CHECK */ - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasMainFlag(player)) return; } diff --git a/src/main/java/com/gmail/nossr50/listeners/SelfListener.java b/src/main/java/com/gmail/nossr50/listeners/SelfListener.java index e49fe3a9c..b0c2f40e0 100644 --- a/src/main/java/com/gmail/nossr50/listeners/SelfListener.java +++ b/src/main/java/com/gmail/nossr50/listeners/SelfListener.java @@ -7,7 +7,6 @@ import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent; import com.gmail.nossr50.events.experience.McMMOPlayerXpGainEvent; import com.gmail.nossr50.events.skills.abilities.McMMOPlayerAbilityActivateEvent; import com.gmail.nossr50.mcMMO; -import com.gmail.nossr50.worldguard.WorldGuardUtils; import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; @@ -72,7 +71,7 @@ public class SelfListener implements Listener { event.getXpGainReason() == XPGainReason.PVP || event.getXpGainReason() == XPGainReason.SHARED_PVE || event.getXpGainReason() == XPGainReason.SHARED_PVP) { - if (WorldGuardUtils.isWorldGuardLoaded()) { + if (pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if (!pluginRef.getWorldGuardManager().hasXPFlag(player)) { event.setRawXpGained(0); event.setCancelled(true); diff --git a/src/main/java/com/gmail/nossr50/mcMMO.java b/src/main/java/com/gmail/nossr50/mcMMO.java index 7eb9a4654..464dff52a 100644 --- a/src/main/java/com/gmail/nossr50/mcMMO.java +++ b/src/main/java/com/gmail/nossr50/mcMMO.java @@ -44,6 +44,7 @@ import com.gmail.nossr50.util.skills.CombatTools; import com.gmail.nossr50.util.skills.RankTools; import com.gmail.nossr50.util.skills.SkillTools; import com.gmail.nossr50.worldguard.WorldGuardManager; +import com.gmail.nossr50.worldguard.WorldGuardUtils; import net.shatteredlands.shatt.backup.ZipLibrary; import org.bstats.bukkit.Metrics; import org.bukkit.Bukkit; @@ -97,6 +98,7 @@ public class mcMMO extends JavaPlugin { private RankTools rankTools; private ItemTools itemTools; private PermissionTools permissionTools; + private WorldGuardUtils worldGuardUtils; /* Never-Ending tasks */ private BleedTimerTask bleedTimerTask; @@ -271,6 +273,9 @@ public class mcMMO extends JavaPlugin { //Init Item Tools itemTools = new ItemTools(this); + + //Init World Guard Utils (Not sure about the order of this one, might need to be loaded earlier) + worldGuardUtils = new WorldGuardUtils(this); } @Override @@ -798,4 +803,8 @@ public class mcMMO extends JavaPlugin { public PermissionTools getPermissionTools() { return permissionTools; } + + public WorldGuardUtils getWorldGuardUtils() { + return worldGuardUtils; + } } diff --git a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java index 76fecef07..53fc8d417 100644 --- a/src/main/java/com/gmail/nossr50/util/HardcoreManager.java +++ b/src/main/java/com/gmail/nossr50/util/HardcoreManager.java @@ -4,7 +4,6 @@ import com.gmail.nossr50.datatypes.interactions.NotificationType; import com.gmail.nossr50.datatypes.player.PlayerProfile; import com.gmail.nossr50.datatypes.skills.PrimarySkillType; import com.gmail.nossr50.worldguard.WorldGuardManager; -import com.gmail.nossr50.worldguard.WorldGuardUtils; import org.bukkit.entity.Player; import java.util.HashMap; @@ -15,7 +14,7 @@ public final class HardcoreManager { public static void invokeStatPenalty(Player player) { - if(WorldGuardUtils.isWorldGuardLoaded()) { + if(pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if(!WorldGuardManager.getInstance().hasHardcoreFlag(player)) { return; } @@ -67,7 +66,7 @@ public final class HardcoreManager { public static void invokeVampirism(Player killer, Player victim) { - if(WorldGuardUtils.isWorldGuardLoaded()) { + if(pluginRef.getWorldGuardUtils().isWorldGuardLoaded()) { if(!WorldGuardManager.getInstance().hasHardcoreFlag(killer) || !WorldGuardManager.getInstance().hasHardcoreFlag(victim)) { return; } diff --git a/src/main/java/com/gmail/nossr50/worldguard/WorldGuardUtils.java b/src/main/java/com/gmail/nossr50/worldguard/WorldGuardUtils.java index 693bafd27..ac6c3f50d 100644 --- a/src/main/java/com/gmail/nossr50/worldguard/WorldGuardUtils.java +++ b/src/main/java/com/gmail/nossr50/worldguard/WorldGuardUtils.java @@ -1,5 +1,6 @@ package com.gmail.nossr50.worldguard; +import com.gmail.nossr50.mcMMO; import com.sk89q.worldguard.bukkit.WorldGuardPlugin; import org.bukkit.plugin.Plugin; @@ -8,10 +9,15 @@ import java.util.ArrayList; import static org.bukkit.Bukkit.getServer; public class WorldGuardUtils { - private static WorldGuardPlugin worldGuardPluginRef; - private static boolean isLoaded = false; - private static boolean detectedIncompatibleWG = false; + private WorldGuardPlugin worldGuardPluginRef; + private boolean isLoaded = false; + private boolean detectedIncompatibleWG = false; private static final ArrayList WGClassList; + private final mcMMO pluginRef; + + public WorldGuardUtils(mcMMO pluginRef) { + this.pluginRef = pluginRef; + } static { /* @@ -37,7 +43,7 @@ public class WorldGuardUtils { WGClassList.add("com.sk89q.worldguard.protection.regions.RegionQuery"); } - public static boolean isWorldGuardLoaded() + public boolean isWorldGuardLoaded() { if(detectedIncompatibleWG) return false; @@ -52,7 +58,7 @@ public class WorldGuardUtils { * Results are cached * @return the instance of WG plugin, null if its not compatible or isn't present */ - private static WorldGuardPlugin getWorldGuard() + private WorldGuardPlugin getWorldGuard() { //WG plugin reference is already cached so just return it if(isLoaded) @@ -90,7 +96,7 @@ public class WorldGuardUtils { * This does not guarantee compatibility, but it should help reduce the chance that mcMMO tries to hook into WG and its not compatible * @return true if the version of WG appears to be compatible */ - private static boolean isCompatibleVersion(Plugin plugin) { + private boolean isCompatibleVersion(Plugin plugin) { //Check that the version of WG is at least version 7.xx if(!plugin.getDescription().getVersion().startsWith("7")) { markWGIncompatible(); @@ -114,7 +120,7 @@ public class WorldGuardUtils { /** * Mark WG as being incompatible to avoid unnecessary operations */ - private static void markWGIncompatible() { + private void markWGIncompatible() { pluginRef.getLogger().severe("You are using a version of WG that is not compatible with mcMMO, " + "WG features for mcMMO will be disabled. mcMMO requires you to be using a new version of WG7 " + "in order for it to use WG features. Not all versions of WG7 are compatible.");