From 0f51cc34c2432c34b958773895966ebf01806a7d Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 25 Apr 2020 15:53:58 +0200 Subject: [PATCH] Support PlotSquared v5 Does not support older PlotSquared versions anymore --- pom.xml | 10 +- .../java/de/epiceric/shopchest/ShopChest.java | 20 +++- .../external/PlotSquaredShopFlag.java | 105 ++++++++++++------ .../listeners/PlotSquaredListener.java | 9 +- .../listeners/ShopInteractListener.java | 21 ++-- 5 files changed, 107 insertions(+), 58 deletions(-) diff --git a/pom.xml b/pom.xml index f953819..2a978f5 100644 --- a/pom.xml +++ b/pom.xml @@ -87,6 +87,10 @@ nlthijs48-repo http://maven.wiefferink.me + + plotsquared-repo + https://mvn.intellectualsites.com/content/groups/public/ + @@ -109,9 +113,9 @@ provided - com.github.intellectualsites.plotsquared - PlotSquared-API - 4.226 + com.plotsquared + PlotSquared + 5.1 provided diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index 50462bb..e883e55 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -16,6 +16,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Stream; import com.palmergames.bukkit.towny.Towny; +import com.plotsquared.core.PlotSquared; import com.wasteofplastic.askyblock.ASkyBlock; import org.bstats.bukkit.Metrics; @@ -317,8 +318,15 @@ public class ShopChest extends JavaPlugin { WorldGuardWrapper.getInstance().registerEvents(this); } - if (hasPlotSquared()) { - PlotSquaredShopFlag.register(this); + if (getServer().getPluginManager().isPluginEnabled("PlotSquared")) { + try { + Class.forName("com.plotsquared.core.PlotSquared"); + PlotSquaredShopFlag.register(this); + } catch (ClassNotFoundException ex) { + String ver = getServer().getPluginManager().getPlugin("PlotSquared").getDescription().getVersion(); + debug("PlotSquared v5 required. Installed: " + ver); + getLogger().warning("PlotSquared v5 required. You have version " + ver); + } } if (hasBentoBox()) { @@ -619,6 +627,14 @@ public class ShopChest extends JavaPlugin { // Supported PlotSquared versions don't support versions below 1.13 return false; } + + try { + // Check for PlotSquared v5 + Class.forName("com.plotsquared.core.PlotSquared"); + } catch (ClassNotFoundException ex) { + return false; + } + Plugin p = getServer().getPluginManager().getPlugin("PlotSquared"); return p != null && p.isEnabled(); } diff --git a/src/main/java/de/epiceric/shopchest/external/PlotSquaredShopFlag.java b/src/main/java/de/epiceric/shopchest/external/PlotSquaredShopFlag.java index 4b72463..e0a9ea1 100644 --- a/src/main/java/de/epiceric/shopchest/external/PlotSquaredShopFlag.java +++ b/src/main/java/de/epiceric/shopchest/external/PlotSquaredShopFlag.java @@ -1,33 +1,42 @@ package de.epiceric.shopchest.external; +import java.util.Arrays; +import java.util.Collection; import java.util.Locale; -import com.github.intellectualsites.plotsquared.plot.flag.Flag; -import com.github.intellectualsites.plotsquared.plot.flag.Flags; -import com.github.intellectualsites.plotsquared.plot.object.Plot; +import com.plotsquared.core.configuration.Caption; +import com.plotsquared.core.configuration.Captions; +import com.plotsquared.core.configuration.StaticCaption; +import com.plotsquared.core.plot.Plot; +import com.plotsquared.core.plot.flag.FlagParseException; +import com.plotsquared.core.plot.flag.GlobalFlagContainer; +import com.plotsquared.core.plot.flag.PlotFlag; import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; import de.epiceric.shopchest.ShopChest; public class PlotSquaredShopFlag { - - private static boolean registered = false; - public enum Group { OWNERS, MEMBERS, TRUSTED, EVERYONE, NONE } - 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 static final String[] lowercaseValues = Arrays.asList(Group.values()).stream() + .map(value -> String.valueOf(value).toLowerCase(Locale.ENGLISH)) + .toArray(String[]::new); + + private static boolean registered = false; + + public static final CreateShopFlag CREATE_SHOP = new CreateShopFlag(Group.MEMBERS); + public static final UseShopFlag USE_SHOP = new UseShopFlag(Group.EVERYONE); public static void register(ShopChest plugin) { - if (registered) return; + if (registered) + return; - Flags.registerFlag(CREATE_SHOP); - Flags.registerFlag(USE_SHOP); - Flags.registerFlag(USE_ADMIN_SHOP); + GlobalFlagContainer.getInstance().addFlag(CREATE_SHOP); + GlobalFlagContainer.getInstance().addFlag(USE_SHOP); registered = true; plugin.debug("Registered custom PlotSquared flags"); @@ -35,14 +44,15 @@ public class PlotSquaredShopFlag { /** * 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) { + public static boolean isFlagAllowedOnPlot(Plot plot, GroupFlag flag, Player p) { if (plot != null && flag != null) { - Group group = plot.getFlag(flag, PlotSquaredShopFlag.Group.NONE); + Group group = plot.getFlag(flag); ShopChest.getInstance().debug("Flag " + flag.getName() + " is set to " + group); switch (group) { @@ -64,51 +74,80 @@ public class PlotSquaredShopFlag { return true; } - public static class GroupFlag extends Flag { - - public GroupFlag(String name) { - super(name); + public static class CreateShopFlag extends GroupFlag { + public CreateShopFlag(Group value) { + super(value, new StaticCaption("Set to the group that is allowed to create shops.")); } @Override - public String valueToString(Object value) { - return String.valueOf(value); + protected CreateShopFlag flagOf(@NotNull Group value) { + return new CreateShopFlag(value); + } + } + + public static class UseShopFlag extends GroupFlag { + public UseShopFlag(Group value) { + super(value, new StaticCaption("Set to the group that is allowed to use shops.")); } @Override - public Group parseValue(String s) { - String val = s.toLowerCase(Locale.ENGLISH); + protected UseShopFlag flagOf(@NotNull Group value) { + return new UseShopFlag(value); + } + } - switch (val) { + public abstract static class GroupFlag> extends PlotFlag { + public GroupFlag(Group value, Caption description) { + super(value, Captions.FLAG_CATEGORY_ENUM, description); + } + + @Override + public String toString() { + return String.valueOf(getValue()).toLowerCase(Locale.ENGLISH); + } + + @Override + public String getExample() { + return "members"; + } + + @Override + public F merge(@NotNull Group newValue) { + return flagOf(newValue); + } + + @Override + public F parse(@NotNull String input) throws FlagParseException { + switch (input.toLowerCase(Locale.ENGLISH)) { case "owners": case "owner": - return Group.OWNERS; + return this.flagOf(Group.OWNERS); case "members": case "member": case "helpers": case "helper": - return Group.MEMBERS; + return this.flagOf(Group.MEMBERS); case "trusted": - return Group.TRUSTED; + return this.flagOf(Group.TRUSTED); case "everyone": case "all": - return Group.EVERYONE; + return this.flagOf(Group.EVERYONE); case "deny": + case "disallow": case "false": case "no": case "0": case "none": case "noone": - return Group.NONE; + return this.flagOf(Group.NONE); } - return null; + throw new FlagParseException(this, input, Captions.FLAG_ERROR_ENUM, (Object[]) lowercaseValues); } @Override - public String getValueDescription() { - return "Flag value must be a group: 'owner' , 'members', 'trusted', 'everyone' or 'none'"; + public Collection getTabCompletions() { + return Arrays.asList(lowercaseValues); } } - } diff --git a/src/main/java/de/epiceric/shopchest/external/listeners/PlotSquaredListener.java b/src/main/java/de/epiceric/shopchest/external/listeners/PlotSquaredListener.java index 9aab2b6..ef58bf1 100644 --- a/src/main/java/de/epiceric/shopchest/external/listeners/PlotSquaredListener.java +++ b/src/main/java/de/epiceric/shopchest/external/listeners/PlotSquaredListener.java @@ -2,8 +2,8 @@ package de.epiceric.shopchest.external.listeners; import java.util.Set; -import com.github.intellectualsites.plotsquared.plot.object.Location; -import com.github.intellectualsites.plotsquared.plot.object.Plot; +import com.plotsquared.core.location.Location; +import com.plotsquared.core.plot.Plot; import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; @@ -51,15 +51,12 @@ public class PlotSquaredListener implements Listener { // public void onBuySell(ShopBuySellEvent e) { // if (!Config.enablePlotsquaredIntegration) // return; - - // ShopType shopType = e.getShop().getShopType(); - // GroupFlag flag = shopType == ShopType.ADMIN ? PlotSquaredShopFlag.USE_ADMIN_SHOP : PlotSquaredShopFlag.USE_SHOP; // Set chestLocations = Utils.getChestLocations(e.getShop()); // for (org.bukkit.Location loc : chestLocations) { // Location plotLocation = new Location(loc.getWorld().getName(), loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); // Plot plot = plotLocation.getOwnedPlot(); - // if (!isFlagAllowed(plot, flag, e.getPlayer())) { + // if (!isFlagAllowed(plot, PlotSquaredShopFlag.USE_SHOP, e.getPlayer())) { // e.setCancelled(true); // plugin.debug("Cancel Reason: PlotSquared"); // return; diff --git a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java index ec187be..bd85053 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ShopInteractListener.java @@ -9,8 +9,8 @@ import java.util.UUID; import java.util.regex.Matcher; import java.util.regex.Pattern; -import com.github.intellectualsites.plotsquared.plot.object.Plot; import com.google.gson.JsonPrimitive; +import com.plotsquared.core.plot.Plot; import org.bukkit.Bukkit; import org.bukkit.GameMode; @@ -45,7 +45,6 @@ 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; @@ -255,13 +254,10 @@ public class ShopInteractListener implements Listener { boolean externalPluginsAllowed = true; if (plugin.hasPlotSquared() && Config.enablePlotsquaredIntegration) { - com.github.intellectualsites.plotsquared.plot.object.Location plotLocation = - new com.github.intellectualsites.plotsquared.plot.object.Location(b.getWorld().getName(), b.getX(), b.getY(), b.getZ()); - + com.plotsquared.core.location.Location plotLocation = + new com.plotsquared.core.location.Location(b.getWorld().getName(), b.getX(), b.getY(), b.getZ()); Plot plot = plotLocation.getOwnedPlot(); - GroupFlag flag = shop.getShopType() == Shop.ShopType.ADMIN ? PlotSquaredShopFlag.USE_ADMIN_SHOP : PlotSquaredShopFlag.USE_SHOP; - - externalPluginsAllowed = PlotSquaredShopFlag.isFlagAllowedOnPlot(plot, flag, p); + externalPluginsAllowed = PlotSquaredShopFlag.isFlagAllowedOnPlot(plot, PlotSquaredShopFlag.USE_SHOP, p); } if (externalPluginsAllowed && plugin.hasWorldGuard() && Config.enableWorldGuardIntegration) { @@ -370,13 +366,10 @@ public class ShopInteractListener implements Listener { boolean externalPluginsAllowed = true; if (plugin.hasPlotSquared() && Config.enablePlotsquaredIntegration) { - com.github.intellectualsites.plotsquared.plot.object.Location plotLocation = - new com.github.intellectualsites.plotsquared.plot.object.Location(b.getWorld().getName(), b.getX(), b.getY(), b.getZ()); - + com.plotsquared.core.location.Location plotLocation = + new com.plotsquared.core.location.Location(b.getWorld().getName(), b.getX(), b.getY(), b.getZ()); Plot plot = plotLocation.getOwnedPlot(); - GroupFlag flag = shop.getShopType() == Shop.ShopType.ADMIN ? PlotSquaredShopFlag.USE_ADMIN_SHOP : PlotSquaredShopFlag.USE_SHOP; - - externalPluginsAllowed = PlotSquaredShopFlag.isFlagAllowedOnPlot(plot, flag, p); + externalPluginsAllowed = PlotSquaredShopFlag.isFlagAllowedOnPlot(plot, PlotSquaredShopFlag.USE_SHOP, p); } if (externalPluginsAllowed && plugin.hasWorldGuard() && Config.enableWorldGuardIntegration) {