From 8ac7d85f1e4d3b23ca03372ecdb7339a9468d0f1 Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 16 Mar 2020 13:35:22 +0100 Subject: [PATCH] Add BentoBox integration Added a custom protection flag CREATE_SHOP that defaults to trusted members Shops are removed if an island is deleted or reset, or if a player is banned or expelled from the island --- pom.xml | 6 ++ .../java/de/epiceric/shopchest/ShopChest.java | 26 ++++++ .../de/epiceric/shopchest/config/Config.java | 6 ++ .../shopchest/external/BentoBoxShopFlag.java | 25 ++++++ .../external/listeners/BentoBoxListener.java | 57 ++++++++++++ .../shopchest/listeners/BentoBoxListener.java | 90 +++++++++++++++++++ src/main/resources/config.yml | 1 + src/main/resources/plugin.yml | 2 +- 8 files changed, 212 insertions(+), 1 deletion(-) create mode 100644 src/main/java/de/epiceric/shopchest/external/BentoBoxShopFlag.java create mode 100644 src/main/java/de/epiceric/shopchest/external/listeners/BentoBoxListener.java create mode 100644 src/main/java/de/epiceric/shopchest/listeners/BentoBoxListener.java diff --git a/pom.xml b/pom.xml index 5722ce4..1dabb4f 100644 --- a/pom.xml +++ b/pom.xml @@ -138,6 +138,12 @@ 2.6.0 provided + + world.bentobox + bentobox + 1.11.1 + provided + com.palmergames Towny diff --git a/src/main/java/de/epiceric/shopchest/ShopChest.java b/src/main/java/de/epiceric/shopchest/ShopChest.java index c5065d7..9b2be51 100644 --- a/src/main/java/de/epiceric/shopchest/ShopChest.java +++ b/src/main/java/de/epiceric/shopchest/ShopChest.java @@ -6,6 +6,7 @@ import de.epiceric.shopchest.command.ShopCommand; import de.epiceric.shopchest.config.Config; import de.epiceric.shopchest.config.HologramFormat; import de.epiceric.shopchest.event.ShopInitializedEvent; +import de.epiceric.shopchest.external.BentoBoxShopFlag; import de.epiceric.shopchest.external.PlotSquaredShopFlag; import de.epiceric.shopchest.external.WorldGuardShopFlag; import de.epiceric.shopchest.external.listeners.ASkyBlockListener; @@ -16,6 +17,7 @@ import de.epiceric.shopchest.external.listeners.TownyListener; import de.epiceric.shopchest.external.listeners.USkyBlockListener; import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.listeners.AreaShopListener; +import de.epiceric.shopchest.listeners.BentoBoxListener; import de.epiceric.shopchest.listeners.BlockExplodeListener; import de.epiceric.shopchest.listeners.ChestProtectListener; import de.epiceric.shopchest.listeners.CreativeModeListener; @@ -54,6 +56,7 @@ import org.codemc.worldguardwrapper.WorldGuardWrapper; import pl.islandworld.IslandWorld; import us.talabrek.ultimateskyblock.api.uSkyBlockAPI; +import world.bentobox.bentobox.BentoBox; import java.io.File; import java.io.FileWriter; @@ -92,6 +95,7 @@ public class ShopChest extends JavaPlugin { private IslandWorld islandWorld; private GriefPrevention griefPrevention; private AreaShop areaShop; + private BentoBox bentoBox; private ShopUpdater updater; private ExecutorService shopCreationThreadPool; @@ -303,6 +307,11 @@ public class ShopChest extends JavaPlugin { areaShop = (AreaShop) areaShopPlugin; } + Plugin bentoBoxPlugin = getServer().getPluginManager().getPlugin("BentoBox"); + if (bentoBoxPlugin instanceof BentoBox) { + bentoBox = (BentoBox) bentoBoxPlugin; + } + if (hasWorldGuard()) { WorldGuardWrapper.getInstance().registerEvents(this); } @@ -310,6 +319,10 @@ public class ShopChest extends JavaPlugin { if (hasPlotSquared()) { PlotSquaredShopFlag.register(this); } + + if (hasBentoBox()) { + BentoBoxShopFlag.register(this); + } } private void loadMetrics() { @@ -422,6 +435,10 @@ public class ShopChest extends JavaPlugin { getServer().getPluginManager().registerEvents(new AreaShopListener(this), this); } } + + if (hasBentoBox()) { + getServer().getPluginManager().registerEvents(new BentoBoxListener(this), this); + } } private void registerExternalListeners() { @@ -439,6 +456,8 @@ public class ShopChest extends JavaPlugin { getServer().getPluginManager().registerEvents(new USkyBlockListener(this), this); if (hasWorldGuard()) getServer().getPluginManager().registerEvents(new de.epiceric.shopchest.external.listeners.WorldGuardListener(this), this); + if (hasBentoBox()) + getServer().getPluginManager().registerEvents(new de.epiceric.shopchest.external.listeners.BentoBoxListener(this), this); } /** @@ -623,6 +642,13 @@ public class ShopChest extends JavaPlugin { return worldGuard != null && worldGuard.isEnabled(); } + /** + * @return Whether the plugin 'WorldGuard' is enabled + */ + public boolean hasBentoBox() { + return bentoBox != null && bentoBox.isEnabled(); + } + /** * @return ShopChest's {@link ShopUtils} containing some important methods */ diff --git a/src/main/java/de/epiceric/shopchest/config/Config.java b/src/main/java/de/epiceric/shopchest/config/Config.java index ea8f092..e3c57d7 100644 --- a/src/main/java/de/epiceric/shopchest/config/Config.java +++ b/src/main/java/de/epiceric/shopchest/config/Config.java @@ -194,6 +194,11 @@ public class Config { **/ public static boolean enableASkyblockIntegration; + /** + * Whether BentoBox integration should be enabled + **/ + public static boolean enableBentoBoxIntegration; + /** * Whether IslandWorld integration should be enabled **/ @@ -476,6 +481,7 @@ public class Config { enablePlotsquaredIntegration = plugin.getConfig().getBoolean("enable-plotsquared-integration"); enableUSkyblockIntegration = plugin.getConfig().getBoolean("enable-uskyblock-integration"); enableASkyblockIntegration = plugin.getConfig().getBoolean("enable-askyblock-integration"); + enableBentoBoxIntegration = plugin.getConfig().getBoolean("enable-bentobox-integration"); enableIslandWorldIntegration = plugin.getConfig().getBoolean("enable-islandworld-integration"); enableGriefPreventionIntegration = plugin.getConfig().getBoolean("enable-griefprevention-integration"); enableAreaShopIntegration = plugin.getConfig().getBoolean("enable-areashop-integration"); diff --git a/src/main/java/de/epiceric/shopchest/external/BentoBoxShopFlag.java b/src/main/java/de/epiceric/shopchest/external/BentoBoxShopFlag.java new file mode 100644 index 0000000..de47c60 --- /dev/null +++ b/src/main/java/de/epiceric/shopchest/external/BentoBoxShopFlag.java @@ -0,0 +1,25 @@ +package de.epiceric.shopchest.external; + +import org.bukkit.Material; + +import de.epiceric.shopchest.ShopChest; +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.flags.Flag; +import world.bentobox.bentobox.managers.RanksManager; + +public class BentoBoxShopFlag { + public static final Flag SHOP_FLAG = new Flag.Builder("CREATE_SHOPS", Material.CHEST) + .type(Flag.Type.PROTECTION) + .mode(Flag.Mode.BASIC) + .defaultRank(RanksManager.TRUSTED_RANK) + .build(); + + public static void register(ShopChest plugin) { + if (BentoBox.getInstance().getFlagsManager().registerFlag(SHOP_FLAG)) { + plugin.debug("Registered BentoBox shop flag"); + } else { + plugin.getLogger().warning("Failed to register BentoBox shop flag"); + plugin.debug("Failed to register BentoBox shop flag"); + } + } +} \ No newline at end of file diff --git a/src/main/java/de/epiceric/shopchest/external/listeners/BentoBoxListener.java b/src/main/java/de/epiceric/shopchest/external/listeners/BentoBoxListener.java new file mode 100644 index 0000000..b28c7cf --- /dev/null +++ b/src/main/java/de/epiceric/shopchest/external/listeners/BentoBoxListener.java @@ -0,0 +1,57 @@ +package de.epiceric.shopchest.external.listeners; + +import java.util.Set; + +import org.bukkit.Location; +import org.bukkit.entity.Player; +import org.bukkit.event.Cancellable; +import org.bukkit.event.Event; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; + +import de.epiceric.shopchest.ShopChest; +import de.epiceric.shopchest.config.Config; +import de.epiceric.shopchest.event.ShopCreateEvent; +import de.epiceric.shopchest.event.ShopExtendEvent; +import de.epiceric.shopchest.external.BentoBoxShopFlag; +import de.epiceric.shopchest.utils.Utils; +import world.bentobox.bentobox.api.flags.FlagListener; + +public class BentoBoxListener extends FlagListener { + private ShopChest plugin; + + public BentoBoxListener(ShopChest plugin) { + this.plugin = plugin; + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onCreateShop(ShopCreateEvent e) { + if (!Config.enableBentoBoxIntegration) + return; + + Set chestLocations = Utils.getChestLocations(e.getShop()); + for (Location loc : chestLocations) { + if (handleForLocation(e.getPlayer(), loc, e)) + return; + } + } + + @EventHandler(priority = EventPriority.LOWEST, ignoreCancelled = true) + public void onExtendShop(ShopExtendEvent e) { + if (!Config.enableBentoBoxIntegration) + return; + + handleForLocation(e.getPlayer(), e.getNewChestLocation(), e); + } + + private boolean handleForLocation(Player player, Location loc, Cancellable e) { + boolean allowed = checkIsland((Event) e, player, loc, BentoBoxShopFlag.SHOP_FLAG, true); + if (!allowed) { + e.setCancelled(true); + plugin.debug("Cancel Reason: BentoBox"); + return true; + } + + return false; + } +} \ No newline at end of file diff --git a/src/main/java/de/epiceric/shopchest/listeners/BentoBoxListener.java b/src/main/java/de/epiceric/shopchest/listeners/BentoBoxListener.java new file mode 100644 index 0000000..3e3a59e --- /dev/null +++ b/src/main/java/de/epiceric/shopchest/listeners/BentoBoxListener.java @@ -0,0 +1,90 @@ +package de.epiceric.shopchest.listeners; + +import java.util.Collection; +import java.util.UUID; + +import org.bukkit.World; +import org.bukkit.event.EventHandler; +import org.bukkit.event.EventPriority; +import org.bukkit.event.Listener; +import org.bukkit.util.BoundingBox; + +import de.epiceric.shopchest.ShopChest; +import de.epiceric.shopchest.config.Config; +import de.epiceric.shopchest.shop.Shop; +import world.bentobox.bentobox.api.events.island.IslandEvent.IslandBanEvent; +import world.bentobox.bentobox.api.events.island.IslandEvent.IslandDeleteChunksEvent; +import world.bentobox.bentobox.api.events.island.IslandEvent.IslandDeletedEvent; +import world.bentobox.bentobox.api.events.island.IslandEvent.IslandExpelEvent; +import world.bentobox.bentobox.api.events.island.IslandEvent.IslandResettedEvent; +import world.bentobox.bentobox.database.objects.Island; + +public class BentoBoxListener implements Listener { + private ShopChest plugin; + + public BentoBoxListener(ShopChest plugin) { + this.plugin = plugin; + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onIslandDeleted(IslandDeletedEvent e) { + deleteShops(e.getIsland().getWorld(), e.getDeletedIslandInfo().getBox()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onIslandBan(IslandBanEvent e) { + deleteShops(e.getIsland(), e.getPlayerUUID()); + } + + @EventHandler(priority = EventPriority.MONITOR) + public void onIslandResetted(IslandResettedEvent e) { + deleteShops(e.getIsland()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onIslandResettead(IslandDeleteChunksEvent e) { + deleteShops(e.getIsland().getWorld(), e.getDeletedIslandInfo().getBox()); + } + + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onIslandResettead(IslandExpelEvent e) { + deleteShops(e.getIsland(), e.getPlayerUUID()); + } + + // Utility methods + + private void deleteShops(Island island) { + deleteShops(island.getWorld(), island.getBoundingBox(), null); + } + + private void deleteShops(Island island, UUID vendorUuid) { + deleteShops(island.getWorld(), island.getBoundingBox(), vendorUuid); + } + + private void deleteShops(World world, BoundingBox box) { + deleteShops(world, box, null); + } + + private void deleteShops(World world, BoundingBox box, UUID vendorUuid) { + if (!Config.enableBentoBoxIntegration) + return; + + Collection shops = plugin.getShopUtils().getShopsCopy(); + for (Shop shop : shops) { + if (!shop.getLocation().getWorld().getName().equals(world.getName())) { + continue; + } + + if (vendorUuid != null && !shop.getVendor().getUniqueId().equals(vendorUuid)) { + continue; + } + + int x = shop.getLocation().getBlockX(); + int z = shop.getLocation().getBlockZ(); + if (box.contains(x, 0, z)) { + plugin.getShopUtils().removeShop(shop, true); + } + } + } + +} \ No newline at end of file diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index b009717..2dafa5b 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -57,6 +57,7 @@ enable-authme-integration: true enable-plotsquared-integration: true enable-uskyblock-integration: true enable-askyblock-integration: true +enable-bentobox-integration: true enable-islandworld-integration: true enable-griefprevention-integration: true enable-areashop-integration: true diff --git a/src/main/resources/plugin.yml b/src/main/resources/plugin.yml index b7bec1c..dbf6e67 100644 --- a/src/main/resources/plugin.yml +++ b/src/main/resources/plugin.yml @@ -6,7 +6,7 @@ version: ${project.version} author: EpicEric website: ${project.url} description: Create your own nice-looking chest shops and sell your stuff to other players! -softdepend: [WorldGuard, Towny, AuthMe, PlotSquared, uSkyBlock, ASkyBlock, IslandWorld, GriefPrevention, AreaShop, Multiverse-Core, MultiWorld] +softdepend: [WorldGuard, Towny, AuthMe, PlotSquared, uSkyBlock, ASkyBlock, IslandWorld, GriefPrevention, AreaShop, Multiverse-Core, MultiWorld, BentoBox] depend: [Vault] api-version: 1.13