Merge branch 'master' of github.com:mcMMO-Dev/mcMMO into configurable

This commit is contained in:
nossr50
2019-06-26 21:55:41 -07:00
50 changed files with 1771 additions and 1301 deletions

View File

@ -6,7 +6,6 @@ import com.sk89q.worldguard.WorldGuard;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.flags.registry.FlagConflictException;
import com.sk89q.worldguard.protection.flags.registry.FlagRegistry;
import com.sk89q.worldguard.protection.regions.RegionContainer;
import com.sk89q.worldguard.protection.regions.RegionQuery;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
@ -14,31 +13,18 @@ import org.bukkit.plugin.Plugin;
import static org.bukkit.Bukkit.getServer;
public class WorldGuardManager {
private static WorldGuardManager instance;
private WorldGuardPlugin worldGuardPluginRef;
public WorldGuardManager() {
}
public static WorldGuardManager getInstance() {
if (instance == null)
instance = new WorldGuardManager();
return instance;
}
public boolean hasMainFlag(Player player) {
if (player == null)
public boolean hasMainFlag(Player player)
{
if(player == null)
return false;
BukkitPlayer localPlayer = BukkitAdapter.adapt(player);
com.sk89q.worldedit.util.Location loc = localPlayer.getLocation();
//WorldGuardPlugin worldGuard = getWorldGuard();
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionQuery query = container.createQuery();
RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
//ApplicableRegionSet set = query.getApplicableRegions(loc);
@ -53,8 +39,7 @@ public class WorldGuardManager {
com.sk89q.worldedit.util.Location loc = localPlayer.getLocation();
//WorldGuardPlugin worldGuard = getWorldGuard();
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionQuery query = container.createQuery();
RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
//ApplicableRegionSet set = query.getApplicableRegions(loc);
@ -70,8 +55,7 @@ public class WorldGuardManager {
com.sk89q.worldedit.util.Location loc = localPlayer.getLocation();
//WorldGuardPlugin worldGuard = getWorldGuard();
RegionContainer container = WorldGuard.getInstance().getPlatform().getRegionContainer();
RegionQuery query = container.createQuery();
RegionQuery query = WorldGuard.getInstance().getPlatform().getRegionContainer().createQuery();
//ApplicableRegionSet set = query.getApplicableRegions(loc);
@ -90,15 +74,12 @@ public class WorldGuardManager {
return worldGuardPluginRef;
}
public void registerFlags() {
if (getWorldGuard() == null)
return;
public void registerFlags()
{
FlagRegistry registry = WorldGuard.getInstance().getFlagRegistry();
try {
// register our flag with the registry
/*registry.register(WorldGuardFlags.MCMMO_ENABLE_WG_FLAG);
registry.register(WorldGuardFlags.MCMMO_XP_WG_FLAG);*/
registry.register(WorldGuardFlags.MCMMO_ENABLE_WG_FLAG);
registry.register(WorldGuardFlags.MCMMO_XP_WG_FLAG);
registry.register(WorldGuardFlags.MCMMO_HARDCORE_WG_FLAG);
@ -113,5 +94,4 @@ public class WorldGuardManager {
}
}
}

View File

@ -4,53 +4,122 @@ import com.gmail.nossr50.mcMMO;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import org.bukkit.plugin.Plugin;
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 hasWarned = false;
private static boolean detectedIncompatibleWG = false;
private static final ArrayList<String> WGClassList;
public static boolean isWorldGuardLoaded() {
WorldGuardPlugin plugin = getWorldGuard();
static {
/*
These are the classes mcMMO tries to hook into for WG support, if any of them are missing it is safe to consider WG is not compatible
com.sk89q.worldedit.bukkit.BukkitAdapter
com.sk89q.worldedit.bukkit.BukkitPlayer
com.sk89q.worldguard.WorldGuard
com.sk89q.worldguard.bukkit.WorldGuardPlugin
com.sk89q.worldguard.protection.flags.registry.FlagConflictException
com.sk89q.worldguard.protection.flags.registry.FlagRegistry
com.sk89q.worldguard.protection.regions.RegionContainer
com.sk89q.worldguard.protection.regions.RegionQuery
*/
try {
// WorldGuard may not be loaded
if (plugin == null) {
return false; // Maybe you want throw an exception instead
}
} catch (Exception e) {
e.printStackTrace();
//Silently Fail
//mcMMO.p.getLogger().severe("Failed to detect worldguard.");
WGClassList = new ArrayList<>();
WGClassList.add("com.sk89q.worldedit.bukkit.BukkitAdapter");
WGClassList.add("com.sk89q.worldedit.bukkit.BukkitPlayer");
WGClassList.add("com.sk89q.worldguard.WorldGuard");
WGClassList.add("com.sk89q.worldguard.bukkit.WorldGuardPlugin");
WGClassList.add("com.sk89q.worldguard.protection.flags.registry.FlagConflictException");
WGClassList.add("com.sk89q.worldguard.protection.flags.registry.FlagRegistry");
WGClassList.add("com.sk89q.worldguard.protection.regions.RegionContainer");
WGClassList.add("com.sk89q.worldguard.protection.regions.RegionQuery");
}
public static boolean isWorldGuardLoaded()
{
if(detectedIncompatibleWG)
return false;
worldGuardPluginRef = getWorldGuard();
return isLoaded;
}
/**
* Gets the instance of the WG plugin if its compatible
* Results are cached
* @return the instance of WG plugin, null if its not compatible or isn't present
*/
private static WorldGuardPlugin getWorldGuard()
{
//WG plugin reference is already cached so just return it
if(isLoaded)
return worldGuardPluginRef;
//Grab WG if it exists
Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
if(plugin == null) {
//WG is not present
detectedIncompatibleWG = true;
mcMMO.p.getLogger().info("WorldGuard was not detected.");
} else {
//Check that its actually of class WorldGuardPlugin
if(plugin instanceof WorldGuardPlugin)
{
if(isCompatibleVersion(plugin))
{
worldGuardPluginRef = (WorldGuardPlugin) plugin;
isLoaded = true;
}
} else {
//Plugin is not of the expected type
markWGIncompatible();
}
}
return true;
return worldGuardPluginRef;
}
private static WorldGuardPlugin getWorldGuard() {
if (isLoaded)
return worldGuardPluginRef;
Plugin plugin = getServer().getPluginManager().getPlugin("WorldGuard");
if (plugin instanceof WorldGuardPlugin) {
if (plugin.getDescription().getVersion().startsWith("7")) {
worldGuardPluginRef = (WorldGuardPlugin) plugin;
if (worldGuardPluginRef != null)
isLoaded = true;
} else {
if (!hasWarned) {
mcMMO.p.getLogger().severe("mcMMO only supports WorldGuard version 7! Make sure you have WG 7! This warning will not appear again.");
hasWarned = true;
/**
* Checks to make sure the version of WG installed is compatible
* Does this by checking for necessary WG classes via Reflection
* 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) {
//Check that the version of WG is at least version 7.xx
if(!plugin.getDescription().getVersion().startsWith("7")) {
markWGIncompatible();
} else {
//Use Reflection to check for a class not present in all versions of WG7
for(String classString : WGClassList) {
try {
Class<?> checkForClass = Class.forName(classString);
detectedIncompatibleWG = false; //In case this was set to true previously
} catch (ClassNotFoundException e) {
mcMMO.p.getLogger().severe("Missing WorldGuard class - "+classString);
markWGIncompatible();
return false;
}
}
}
return worldGuardPluginRef;
return true;
}
/**
* Mark WG as being incompatible to avoid unnecessary operations
*/
private static void markWGIncompatible() {
mcMMO.p.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.");
mcMMO.p.getLogger().severe("mcMMO will continue to function normally, but if you wish to use WG support you must use a compatible version.");
detectedIncompatibleWG = true;
}
}