Also keep support for WorldGuard 5

This commit is contained in:
Sn0wStorm 2015-01-09 00:45:26 +01:00
parent 68d5820501
commit 46f9c9dc26
5 changed files with 135 additions and 33 deletions

View File

@ -23,7 +23,6 @@ import org.bukkit.scheduler.BukkitRunnable;
import com.dre.brewery.integration.GriefPreventionBarrel;
import com.dre.brewery.integration.LWCBarrel;
import com.dre.brewery.integration.LogBlockBarrel;
import com.dre.brewery.integration.WGBarrel;
import org.apache.commons.lang.ArrayUtils;
@ -124,12 +123,13 @@ public class Barrel {
Plugin plugin = P.p.getServer().getPluginManager().getPlugin("WorldGuard");
if (plugin != null) {
try {
if (!WGBarrel.checkAccess(player, spigot, plugin)) {
if (!P.p.wg.checkAccess(player, spigot, plugin)) {
return false;
}
} catch (Exception e) {
} catch (Throwable e) {
P.p.errorLog("Failed to Check WorldGuard for Barrel Open Permissions!");
P.p.errorLog("Brewery was tested with version 5.8 of WorldGuard!");
P.p.errorLog("Brewery was tested with version 5.8 to 6.0 of WorldGuard!");
P.p.errorLog("Disable the WorldGuard support in the config and do /brew reload");
e.printStackTrace();
P.p.msg(player, "&cError opening Barrel, please report to an Admin!");
return false;
@ -143,9 +143,10 @@ public class Barrel {
if (!GriefPreventionBarrel.checkAccess(player, spigot)) {
return false;
}
} catch (Exception e) {
} catch (Throwable e) {
P.p.errorLog("Failed to Check GriefPrevention for Barrel Open Permissions!");
P.p.errorLog("Brewery only works with the latest release of GriefPrevention (7.8)");
P.p.errorLog("Disable the GriefPrevention support in the config and do /brew reload");
e.printStackTrace();
P.p.msg(player, "&cError opening Barrel, please report to an Admin!");
return false;
@ -164,9 +165,10 @@ public class Barrel {
if (!sign.equals(event.getClickedBlock())) {
try {
return LWCBarrel.checkAccess(player, sign, event, plugin);
} catch (Exception e) {
} catch (Throwable e) {
P.p.errorLog("Failed to Check LWC for Barrel Open Permissions!");
P.p.errorLog("Brewery was tested with version 4.3.1 of LWC!");
P.p.errorLog("Disable the LWC support in the config and do /brew reload");
e.printStackTrace();
P.p.msg(player, "&cError opening Barrel, please report to an Admin!");
return false;
@ -188,8 +190,10 @@ public class Barrel {
if (P.p.useLWC) {
try {
return LWCBarrel.checkDestroy(player, this);
} catch (Exception e) {
} catch (Throwable e) {
P.p.errorLog("Failed to Check LWC for Barrel Break Permissions!");
P.p.errorLog("Brewery was tested with version 4.3.1 of LWC!");
P.p.errorLog("Disable the LWC support in the config and do /brew reload");
e.printStackTrace();
P.p.msg(player, "&cError breaking Barrel, please report to an Admin!");
return false;
@ -204,8 +208,9 @@ public class Barrel {
if (P.p.useLWC) {
try {
LWCBarrel.remove(this);
} catch (Exception e) {
} catch (Throwable e) {
P.p.errorLog("Failed to Remove LWC Lock from Barrel!");
P.p.errorLog("Brewery was tested with version 4.3.1 of LWC!");
e.printStackTrace();
}
}
@ -248,7 +253,7 @@ public class Barrel {
if (P.p.useLB) {
try {
LogBlockBarrel.openBarrel(player, inventory, spigot.getLocation());
} catch (Exception e) {
} catch (Throwable e) {
P.p.errorLog("Failed to Log Barrel to LogBlock!");
P.p.errorLog("Brewery was tested with version 1.80 of LogBlock!");
e.printStackTrace();
@ -442,7 +447,7 @@ public class Barrel {
if (P.p.useLB && breaker != null) {
try {
LogBlockBarrel.breakBarrel(breaker.getName(), items, spigot.getLocation());
} catch (Exception e) {
} catch (Throwable e) {
P.p.errorLog("Failed to Log Barrel-break to LogBlock!");
P.p.errorLog("Brewery was tested with version 1.80 of LogBlock!");
e.printStackTrace();

View File

@ -8,7 +8,6 @@ import java.util.HashMap;
import java.io.IOException;
import java.io.File;
import java.util.UUID;
import java.util.logging.Level;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -17,6 +16,9 @@ import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.ConfigurationSection;
import com.dre.brewery.integration.WGBarrel;
import com.dre.brewery.integration.WGBarrelNew;
import com.dre.brewery.integration.WGBarrelOld;
import org.apache.commons.lang.math.NumberUtils;
import org.bukkit.event.HandlerList;
import org.bukkit.Bukkit;
@ -39,6 +41,7 @@ public class P extends JavaPlugin {
// Third Party Enabled
public boolean useWG; //WorldGuard
public WGBarrel wg;
public boolean useLWC; //LWC
public boolean useLB; //LogBlock
public boolean useGP; //GriefPrevention
@ -175,7 +178,7 @@ public class P extends JavaPlugin {
}
public void errorLog(String msg) {
Bukkit.getLogger().log(Level.SEVERE, ChatColor.DARK_GREEN + "[Brewery] " + ChatColor.DARK_RED + "ERROR: " + ChatColor.RED + msg);
Bukkit.getConsoleSender().sendMessage(ChatColor.DARK_GREEN + "[Brewery] " + ChatColor.DARK_RED + "ERROR: " + ChatColor.RED + msg);
}
public void readConfig() {
@ -204,6 +207,24 @@ public class P extends JavaPlugin {
// Third-Party
useWG = config.getBoolean("useWorldGuard", true) && getServer().getPluginManager().isPluginEnabled("WorldGuard");
if (useWG) {
try {
try {
Class.forName("com.sk89q.worldguard.bukkit.RegionContainer");
log("Using New WorldGuard!");
wg = new WGBarrelNew();
} catch (ClassNotFoundException e) {
log("Using Old WorldGuard!");
wg = new WGBarrelOld();
}
} catch (Throwable e) {
wg = null;
P.p.errorLog("Failed loading WorldGuard Integration! Opening Barrels will NOT work!");
P.p.errorLog("Brewery was tested with version 5.8 to 6.0 of WorldGuard!");
P.p.errorLog("Disable the WorldGuard support in the config and do /brew reload");
e.printStackTrace();
}
}
useLWC = config.getBoolean("useLWC", true) && getServer().getPluginManager().isPluginEnabled("LWC");
useGP = config.getBoolean("useGriefPrevention", true) && getServer().getPluginManager().isPluginEnabled("GriefPrevention");
useLB = config.getBoolean("useLogBlock", false) && getServer().getPluginManager().isPluginEnabled("LogBlock");

View File

@ -4,25 +4,6 @@ import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import com.dre.brewery.P;
import com.sk89q.worldguard.bukkit.RegionQuery;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
public class WGBarrel {
public static boolean checkAccess(Player player, Block spigot, Plugin plugin) {
WorldGuardPlugin wg = (WorldGuardPlugin) plugin;
if (!wg.getGlobalRegionManager().hasBypass(player, player.getWorld())) {
RegionQuery query = wg.getRegionContainer().createQuery();
if(!query.testState(player.getLocation(), player, DefaultFlag.CHEST_ACCESS, DefaultFlag.BUILD)){
P.p.msg(player, P.p.languageReader.get("Error_NoBarrelAccess"));
return false;
}
}
return true;
}
public interface WGBarrel {
public abstract boolean checkAccess(Player player, Block spigot, Plugin plugin);
}

View File

@ -0,0 +1,32 @@
package com.dre.brewery.integration;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import com.dre.brewery.P;
import com.sk89q.worldguard.bukkit.RegionQuery;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.bukkit.permission.RegionPermissionModel;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
public class WGBarrelNew implements WGBarrel {
public boolean checkAccess(Player player, Block spigot, Plugin plugin) {
WorldGuardPlugin wg = (WorldGuardPlugin) plugin;
if (!wg.getGlobalStateManager().get(spigot.getWorld()).useRegions) return true; // Region support disabled
if (new RegionPermissionModel(wg, player).mayIgnoreRegionProtection(spigot.getWorld())) return true; // Whitelisted cause
RegionQuery query = wg.getRegionContainer().createQuery();
if (!query.testBuild(spigot.getLocation(), player, DefaultFlag.USE, DefaultFlag.CHEST_ACCESS)) {
P.p.msg(player, P.p.languageReader.get("Error_NoBarrelAccess"));
return false;
}
return true;
}
}

View File

@ -0,0 +1,63 @@
package com.dre.brewery.integration;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import com.dre.brewery.P;
import com.sk89q.worldguard.LocalPlayer;
import com.sk89q.worldguard.bukkit.WorldGuardPlugin;
import com.sk89q.worldguard.protection.ApplicableRegionSet;
import com.sk89q.worldguard.protection.flags.DefaultFlag;
import com.sk89q.worldguard.protection.flags.StateFlag;
public class WGBarrelOld implements WGBarrel {
private Method allows;
private Method canBuild;
public WGBarrelOld() {
try {
allows = ApplicableRegionSet.class.getMethod("allows", StateFlag.class, LocalPlayer.class);
canBuild = ApplicableRegionSet.class.getMethod("canBuild", LocalPlayer.class);
} catch (NoSuchMethodException e) {
P.p.errorLog("Failed to Hook WorldGuard for Barrel Open Permissions!");
P.p.errorLog("Brewery was tested with version 5.8 to 6.0 of WorldGuard!");
P.p.errorLog("Disable the WorldGuard support in the config and do /brew reload");
e.printStackTrace();
}
}
public boolean checkAccess(Player player, Block spigot, Plugin plugin) {
WorldGuardPlugin wg = (WorldGuardPlugin) plugin;
if (!wg.getGlobalRegionManager().hasBypass(player, player.getWorld())) {
Object region = wg.getRegionManager(player.getWorld()).getApplicableRegions(spigot.getLocation());
if (region != null) {
LocalPlayer localPlayer = wg.wrapPlayer(player);
try {
if (!(Boolean) allows.invoke(region, DefaultFlag.CHEST_ACCESS, localPlayer)) {
if (!(Boolean) canBuild.invoke(region, localPlayer)) {
P.p.msg(player, P.p.languageReader.get("Error_NoBarrelAccess"));
return false;
}
}
} catch (IllegalAccessException e) {
e.printStackTrace();
return false;
} catch (InvocationTargetException e) {
e.printStackTrace();
return false;
}
}
}
return true;
}
}