From 86322a1198324012f638e6c509439fa66007c3a7 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 3 Aug 2018 18:56:09 +0200 Subject: [PATCH] Fix warnings regarding PlotSquared flags --- .../java/de/epiceric/shopchest/ShopChest.java | 2 +- .../external/PlotSquaredShopFlag.java | 59 ++++++++++++++----- .../listeners/ChestProtectListener.java | 2 +- .../listeners/ShopInteractListener.java | 14 ++--- .../de/epiceric/shopchest/utils/Utils.java | 39 ------------ 5 files changed, 52 insertions(+), 64 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index 2b12458..b63a43c 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -278,7 +278,7 @@ public class ShopChest extends JavaPlugin { } if (hasPlotSquared()) { - new PlotSquaredShopFlag().register(this); + PlotSquaredShopFlag.register(this); } } diff --git a/src/main/java/de/epiceric/shopchest/external/PlotSquaredShopFlag.java b/src/main/java/de/epiceric/shopchest/external/PlotSquaredShopFlag.java index 32ea5fd..d807ac0 100644 --- a/src/main/java/de/epiceric/shopchest/external/PlotSquaredShopFlag.java +++ b/src/main/java/de/epiceric/shopchest/external/PlotSquaredShopFlag.java @@ -2,6 +2,10 @@ package de.epiceric.shopchest.external; import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.flag.Flags; +import com.intellectualcrafters.plot.object.Plot; + +import org.bukkit.entity.Player; + import de.epiceric.shopchest.ShopChest; import java.util.Locale; @@ -14,30 +18,53 @@ public class PlotSquaredShopFlag { OWNERS, MEMBERS, TRUSTED, EVERYONE, NONE } - public static Flag CREATE_SHOP; - public static Flag USE_SHOP; - public static Flag USE_ADMIN_SHOP; + public static GroupFlag CREATE_SHOP = new GroupFlag("create-shop"); + public static GroupFlag USE_SHOP = new GroupFlag("use-shop"); + public static GroupFlag USE_ADMIN_SHOP = new GroupFlag("use-admin-shop"); - private GroupFlag createShop = new GroupFlag("create-shop"); - private GroupFlag useShop = new GroupFlag("use-shop"); - private GroupFlag useAdminShop = new GroupFlag("use-admin-shop"); - - public void register(ShopChest plugin) { + public static void register(ShopChest plugin) { if (registered) return; - CREATE_SHOP = createShop; - USE_SHOP = useShop; - USE_ADMIN_SHOP = useAdminShop; - - Flags.registerFlag(createShop); - Flags.registerFlag(useShop); - Flags.registerFlag(useAdminShop); + Flags.registerFlag(CREATE_SHOP); + Flags.registerFlag(USE_SHOP); + Flags.registerFlag(USE_ADMIN_SHOP); registered = true; plugin.debug("Registered custom PlotSquared flags"); } - public class GroupFlag extends Flag { + /** + * Check if a flag is allowed for a player on a plot from PlotSquared + * @param plot Plot from PlotSquared + * @param flag Flag to check + * @param p Player to check + * @return Whether the flag is allowed for the player + */ + public static boolean isFlagAllowedOnPlot(Plot plot, GroupFlag flag, Player p) { + if (plot != null && flag != null) { + Group group = plot.getFlag(flag, PlotSquaredShopFlag.Group.NONE); + ShopChest.getInstance().debug("Flag " + flag.getName() + " is set to " + group); + + switch (group) { + case OWNERS: + return plot.getOwners().contains(p.getUniqueId()); + case TRUSTED: + return plot.getOwners().contains(p.getUniqueId()) || plot.getTrusted().contains(p.getUniqueId()); + case MEMBERS: + return plot.getOwners().contains(p.getUniqueId()) || plot.getTrusted().contains(p.getUniqueId()) || plot.getMembers().contains(p.getUniqueId()); + case EVERYONE: + return true; + case NONE: + return false; + } + } + + ShopChest.getInstance().debug("Flag or plot is null, or value of flag is not a group"); + + return true; + } + + public static class GroupFlag extends Flag { public GroupFlag(String name) { super(name); diff --git a/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java b/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java index d4a4f82..b42b36f 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java @@ -216,7 +216,7 @@ public class ChestProtectListener implements Listener { com.intellectualcrafters.plot.object.Location loc = new com.intellectualcrafters.plot.object.Location(b.getWorld().getName(), b.getX(), b.getY(), b.getZ()); - externalPluginsAllowed = Utils.isFlagAllowedOnPlot(loc.getOwnedPlot(), PlotSquaredShopFlag.CREATE_SHOP, p); + externalPluginsAllowed = PlotSquaredShopFlag.isFlagAllowedOnPlot(loc.getOwnedPlot(), PlotSquaredShopFlag.CREATE_SHOP, p); } if (externalPluginsAllowed && plugin.hasUSkyBlock() && Config.enableUSkyblockIntegration) { diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java index c577b9d..f2dae78 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java @@ -1,7 +1,6 @@ package de.epiceric.shopchest.listeners; import com.google.gson.JsonPrimitive; -import com.intellectualcrafters.plot.flag.Flag; import com.intellectualcrafters.plot.object.Plot; import com.palmergames.bukkit.towny.object.Resident; import com.palmergames.bukkit.towny.object.Town; @@ -18,6 +17,7 @@ import de.epiceric.shopchest.event.ShopInfoEvent; import de.epiceric.shopchest.event.ShopOpenEvent; import de.epiceric.shopchest.event.ShopRemoveEvent; import de.epiceric.shopchest.external.PlotSquaredShopFlag; +import de.epiceric.shopchest.external.PlotSquaredShopFlag.GroupFlag; import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.Message; import de.epiceric.shopchest.language.Replacement; @@ -216,7 +216,7 @@ public class ShopInteractListener implements Listener { loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); Plot plot = plotLocation.getOwnedPlot(); - externalPluginsAllowed &= Utils.isFlagAllowedOnPlot(plot, PlotSquaredShopFlag.CREATE_SHOP, p); + externalPluginsAllowed &= PlotSquaredShopFlag.isFlagAllowedOnPlot(plot, PlotSquaredShopFlag.CREATE_SHOP, p); } } @@ -450,9 +450,9 @@ public class ShopInteractListener implements Listener { new com.intellectualcrafters.plot.object.Location(b.getWorld().getName(), b.getX(), b.getY(), b.getZ()); Plot plot = plotLocation.getOwnedPlot(); - Flag flag = (shop.getShopType() == Shop.ShopType.ADMIN ? PlotSquaredShopFlag.USE_ADMIN_SHOP : PlotSquaredShopFlag.USE_SHOP); + GroupFlag flag = shop.getShopType() == Shop.ShopType.ADMIN ? PlotSquaredShopFlag.USE_ADMIN_SHOP : PlotSquaredShopFlag.USE_SHOP; - externalPluginsAllowed = Utils.isFlagAllowedOnPlot(plot, flag, p); + externalPluginsAllowed = PlotSquaredShopFlag.isFlagAllowedOnPlot(plot, flag, p); } if (externalPluginsAllowed && plugin.hasWorldGuard() && Config.enableWorldGuardIntegration) { @@ -559,9 +559,9 @@ public class ShopInteractListener implements Listener { new com.intellectualcrafters.plot.object.Location(b.getWorld().getName(), b.getX(), b.getY(), b.getZ()); Plot plot = plotLocation.getOwnedPlot(); - Flag flag = (shop.getShopType() == Shop.ShopType.ADMIN ? PlotSquaredShopFlag.USE_ADMIN_SHOP : PlotSquaredShopFlag.USE_SHOP); - - externalPluginsAllowed = Utils.isFlagAllowedOnPlot(plot, flag, p); + GroupFlag flag = shop.getShopType() == Shop.ShopType.ADMIN ? PlotSquaredShopFlag.USE_ADMIN_SHOP : PlotSquaredShopFlag.USE_SHOP; + + externalPluginsAllowed = PlotSquaredShopFlag.isFlagAllowedOnPlot(plot, flag, p); } if (externalPluginsAllowed && plugin.hasWorldGuard() && Config.enableWorldGuardIntegration) { diff --git a/src/main/java/de/epiceric/shopchest/utils/Utils.java b/src/main/java/de/epiceric/shopchest/utils/Utils.java index f46c328..7ae73a5 100644 --- a/src/main/java/de/epiceric/shopchest/utils/Utils.java +++ b/src/main/java/de/epiceric/shopchest/utils/Utils.java @@ -1,10 +1,7 @@ package de.epiceric.shopchest.utils; -import com.intellectualcrafters.plot.flag.Flag; -import com.intellectualcrafters.plot.object.Plot; import de.epiceric.shopchest.ShopChest; import de.epiceric.shopchest.config.Placeholder; -import de.epiceric.shopchest.external.PlotSquaredShopFlag; import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.Message; import de.epiceric.shopchest.language.Replacement; @@ -225,42 +222,6 @@ public class Utils { return item != null && axes.contains(item.getType().toString()); } - /** - * Check if a flag is allowed for a player on a plot from PlotSquared - * @param plot Plot from PlotSquared - * @param flag Flag to check - * @param p Player to check - * @return Whether the flag is allowed for the player - */ - public static boolean isFlagAllowedOnPlot(Plot plot, Flag flag, Player p) { - if (plot != null && flag != null) { - Object o = plot.getFlag(flag, PlotSquaredShopFlag.Group.NONE); - - if (o instanceof PlotSquaredShopFlag.Group) { - PlotSquaredShopFlag.Group group = (PlotSquaredShopFlag.Group) o; - - ShopChest.getInstance().debug("Flag " + flag.getName() + " is set to " + group); - - switch (group) { - case OWNERS: - return plot.getOwners().contains(p.getUniqueId()); - case TRUSTED: - return plot.getOwners().contains(p.getUniqueId()) || plot.getTrusted().contains(p.getUniqueId()); - case MEMBERS: - return plot.getOwners().contains(p.getUniqueId()) || plot.getTrusted().contains(p.getUniqueId()) || plot.getMembers().contains(p.getUniqueId()); - case EVERYONE: - return true; - case NONE: - return false; - } - } - } - - ShopChest.getInstance().debug("Flag or plot is null, or value of flag is not a group"); - - return true; - } - /** *

Check if a player is allowed to create a shop that sells or buys the given item.

* @param player Player to check