diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index 8845e5958..d8e52db11 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -1885,11 +1885,17 @@ public class QuickShop extends JavaPlugin { getConfig().set("config-version", ++selectedVersion); } if (selectedVersion == 137) { - boolean oldValueUntrusted = getConfig().getBoolean("integration.griefprevention.delete-on-untrusted", false); + getConfig().set("integration.griefprevention.create", null); + getConfig().set("integration.griefprevention.create", "INVENTORY"); + + getConfig().set("integration.griefprevention.trade", null); + getConfig().set("integration.griefprevention.trade", Collections.emptyList()); + + boolean oldValueUntrusted = getConfig().getBoolean("integration.griefprevention.delete-on-untrusted", false); getConfig().set("integration.griefprevention.delete-on-untrusted", null); getConfig().set("integration.griefprevention.delete-on-claim-trust-changed", oldValueUntrusted); - boolean oldValueUnclaim = getConfig().getBoolean("integration.griefprevention.delete-on-unclaim", false); + boolean oldValueUnclaim = getConfig().getBoolean("integration.griefprevention.delete-on-unclaim", false); getConfig().set("integration.griefprevention.delete-on-unclaim", null); getConfig().set("integration.griefprevention.delete-on-claim-unclaimed", oldValueUnclaim); diff --git a/src/main/java/org/maxgamer/quickshop/integration/griefprevention/GriefPreventionIntegration.java b/src/main/java/org/maxgamer/quickshop/integration/griefprevention/GriefPreventionIntegration.java index bd8bd08a0..d7b6ff11e 100644 --- a/src/main/java/org/maxgamer/quickshop/integration/griefprevention/GriefPreventionIntegration.java +++ b/src/main/java/org/maxgamer/quickshop/integration/griefprevention/GriefPreventionIntegration.java @@ -20,10 +20,13 @@ package org.maxgamer.quickshop.integration.griefprevention; import me.ryanhamshire.GriefPrevention.Claim; +import me.ryanhamshire.GriefPrevention.ClaimPermission; import me.ryanhamshire.GriefPrevention.GriefPrevention; +import me.ryanhamshire.GriefPrevention.events.TrustChangedEvent; import me.ryanhamshire.GriefPrevention.events.ClaimDeletedEvent; import me.ryanhamshire.GriefPrevention.events.ClaimExpirationEvent; import me.ryanhamshire.GriefPrevention.events.ClaimModifiedEvent; +import me.ryanhamshire.GriefPrevention.events.ClaimCreatedEvent; import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.Material; @@ -37,32 +40,265 @@ import org.maxgamer.quickshop.integration.IntegrateStage; import org.maxgamer.quickshop.integration.IntegrationStage; import org.maxgamer.quickshop.integration.QSIntegratedPlugin; import org.maxgamer.quickshop.shop.Shop; - import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.Map; @IntegrationStage(loadStage = IntegrateStage.onEnableAfter) public class GriefPreventionIntegration extends QSIntegratedPlugin { + final GriefPrevention griefPrevention = GriefPrevention.instance; - private final List createLimits = new ArrayList<>(3); - private final List tradeLimits = new ArrayList<>(3); + private final boolean whiteList; - private final boolean deleteOnUntrusted; - private final boolean deleteOnUnclaim; + + private final boolean deleteOnClaimTrustChanged; + + private final boolean deleteOnClaimUnclaimed; + private final boolean deleteOnClaimExpired; + private final boolean deleteOnClaimResized; + private final boolean deleteOnSubClaimCreated; + + private final Flag createLimit; + + private final List tradeLimits = new ArrayList<>(3); + public GriefPreventionIntegration(QuickShop plugin) { super(plugin); ConfigurationSection configurationSection = plugin.getConfig(); this.whiteList = configurationSection.getBoolean("integration.griefprevention.whitelist-mode"); - deleteOnUntrusted = configurationSection.getBoolean("integration.griefprevention.delete-on-untrusted"); - deleteOnUnclaim = configurationSection.getBoolean("integration.griefprevention.delete-on-unclaim"); - deleteOnClaimExpired = configurationSection.getBoolean("integration.griefprevention.delete-on-claim-expired"); - deleteOnClaimResized = configurationSection.getBoolean("integration.griefprevention.delete-on-claim-resized"); - createLimits.addAll(toFlags(configurationSection.getStringList("integration.griefprevention.create"))); - tradeLimits.addAll(toFlags(configurationSection.getStringList("integration.griefprevention.trade"))); + this.deleteOnClaimTrustChanged = configurationSection.getBoolean("integration.griefprevention.delete-on-claim-trust-changed"); + this.deleteOnClaimUnclaimed = configurationSection.getBoolean("integration.griefprevention.delete-on-claim-unclaimed"); + this.deleteOnClaimExpired = configurationSection.getBoolean("integration.griefprevention.delete-on-claim-expired"); + this.deleteOnClaimResized = configurationSection.getBoolean("integration.griefprevention.delete-on-claim-resized"); + this.deleteOnSubClaimCreated = configurationSection.getBoolean("integration.griefprevention.delete-on-subclaim-created"); + this.createLimit = Flag.getFlag(configurationSection.getString("integration.griefprevention.create")); + this.tradeLimits.addAll(toFlags(configurationSection.getStringList("integration.griefprevention.trade"))); + } + + @Override + public @NotNull String getName() { + return "GriefPrevention"; + } + + @Override + public void load() { + registerListener(); + } + + @Override + public void unload() { + unregisterListener(); + } + + @Override + public boolean canCreateShopHere(@NotNull Player player, @NotNull Location location) { + return checkPermission(player, location, Collections.singletonList(createLimit)); + } + + @Override + public boolean canTradeShopHere(@NotNull Player player, @NotNull Location location) { + return checkPermission(player, location, tradeLimits); + } + + // We will check if the shop belongs to user whose permissions were changed. + // It will remove a shop if the shop owner no longer has permission to build a shop there. + // We will not delete the shops of the claim owner. + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onClaimTrustChanged(TrustChangedEvent event) { + if (!deleteOnClaimTrustChanged) { + return; + } + if (event.isGiven() && event.getClaimPermission() == null) { + return; + } + if (createLimit.toClaimPermission().isGrantedBy(event.getClaimPermission())) { + return; + } + for (Claim claim : event.getClaims()) { + for (Chunk chunk : claim.getChunks()) { + Map shops = plugin.getShopManager().getShops(chunk); + if (shops == null) { + continue; + } + for (Shop shop : shops.values()) { + if (shop.getOwner().equals(claim.getOwnerID())) { + continue; + } + Claim shopClaim = griefPrevention.dataStore.getClaimAt(shop.getLocation(), false, false, null); + if (shopClaim == null || !shopClaim.getID().equals(claim.getID())) { + continue; + } + if (event.getIdentifier().equals(shop.getOwner().toString())) { + plugin.log("[SHOP DELETE] GP Integration: Single delete (Claim/Subclaim Trust Changed) #" + shop.ownerName()); + shop.delete(); + } else if (event.getIdentifier().contains(shop.getOwner().toString())) { + plugin.log("[SHOP DELETE] GP Integration: Group delete (Claim/Subclaim Trust Changed)#" + shop.ownerName()); + shop.delete(); + } else if ("all".equals(event.getIdentifier()) || "public".equals(event.getIdentifier())) { + plugin.log("[SHOP DELETE] GP Integration: All/Public delete (Claim/Subclaim Trust Changed) #" + shop.ownerName()); + shop.delete(); + } + } + } + } + } + + // Player can unclaim the main claim or the subclaim. + // So we need to call either the handleMainClaimUnclaimedOrExpired or the handleSubClaimUnclaimed method. + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onClaimUnclaimed(ClaimDeletedEvent event) { + if (!deleteOnClaimUnclaimed) { + return; + } + if (event.getClaim().parent == null) { + handleMainClaimUnclaimedOrExpired(event.getClaim(), "[SHOP DELETE] GP Integration: Single delete (Claim Unclaimed) #"); + } else { + handleSubClaimUnclaimed(event.getClaim()); + } + } + + // Since only the main claim expires, we will call the handleMainClaimUnclaimedOrExpired method. + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onClaimExpired(ClaimExpirationEvent event) { + if (!deleteOnClaimExpired) { + return; + } + handleMainClaimUnclaimedOrExpired(event.getClaim(),"[SHOP DELETE] GP Integration: Single delete (Claim Expired) #"); + } + + // Player can resize the main claim or the subclaim. + // So we need to call either the handleMainClaimResized or the handleSubClaimResized method. + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onClaimResized(ClaimModifiedEvent event) { + if (!deleteOnClaimResized) { + return; + } + Claim oldClaim = event.getFrom(); + Claim newClaim = event.getTo(); + if (oldClaim.parent == null) { + handleMainClaimResized(oldClaim, newClaim); + } else { + handleSubClaimResized(oldClaim, newClaim); + } + } + + // Player can create subclaims inside a claim. + // So if a subclaim is created that will contain, initially, shops from others players, then we will remove them. + // Because they won't have, initially, permission to create a shop in that subclaim. + @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) + public void onSubClaimCreated(ClaimCreatedEvent event) { + if (!deleteOnSubClaimCreated) { + return; + } + if(event.getClaim().parent == null) { + return; + } + for (Chunk chunk : event.getClaim().getChunks()) { + Map shops = plugin.getShopManager().getShops(chunk); + if (shops != null) { + for (Shop shop : shops.values()) { + if (!shop.getOwner().equals(event.getClaim().getOwnerID()) && + event.getClaim().contains(shop.getLocation(), false, false)) { + plugin.log("[SHOP DELETE] GP Integration: Single delete (SubClaim Created) #" + shop.ownerName()); + shop.delete(); + } + } + } + } + } + + // If it is the main claim, then we will delete all the shops that were inside of it. + private void handleMainClaimUnclaimedOrExpired(Claim claim, String logMessage) { + for (Chunk chunk : claim.getChunks()) { + Map shops = plugin.getShopManager().getShops(chunk); + if (shops != null) { + for (Shop shop : shops.values()) { + if (claim.contains(shop.getLocation(), false, false)) { + plugin.log(logMessage + shop.ownerName()); + shop.delete(); + } + } + } + } + } + + // If it is a subclaim, then we will not remove the shops of the main claim owner. + // But we will remove all the others. + private void handleSubClaimUnclaimed(Claim subClaim) { + for (Chunk chunk : subClaim.getChunks()) { + Map shops = plugin.getShopManager().getShops(chunk); + if (shops != null) { + for (Shop shop : shops.values()) { + if (!shop.getOwner().equals(subClaim.getOwnerID()) && + subClaim.contains(shop.getLocation(), false, false)) { + plugin.log("[SHOP DELETE] GP Integration: Single delete (SubClaim Unclaimed) #" + shop.ownerName()); + shop.delete(); + } + } + } + } + } + + // If it is a main claim, then we will remove the shops if the main claim was resized (size was decreased). + // A shop will be removed if the old claim contains it but the new claim doesn't. + private void handleMainClaimResized(Claim oldClaim, Claim newClaim) { + for (Chunk chunk : oldClaim.getChunks()) { + Map shops = plugin.getShopManager().getShops(chunk); + if (shops != null) { + for (Shop shop : shops.values()) { + if (oldClaim.contains(shop.getLocation(), false, false) && + !newClaim.contains(shop.getLocation(), false, false)) { + plugin.log("[SHOP DELETE] GP Integration: Single delete (Claim Resized) #" + shop.ownerName()); + shop.delete(); + } + } + } + } + } + + // If it is a subclaim, then we will remove the shops in 2 situations. + // We will never remove the shops of the claim owner. + // We will remove a shop if the shop was inside the subclaim but now it is outside the subclaim. + // We will remove a shop if the shop was outside the subclaim but now it is inside the subclaim. + private void handleSubClaimResized(Claim oldClaim, Claim newClaim) { + handleSubClaimResizedHelper(oldClaim, newClaim); + handleSubClaimResizedHelper(newClaim, oldClaim); + } + + private void handleSubClaimResizedHelper(Claim claimVerifyChunks, Claim claimVerifyShop) { + for (Chunk chunk : claimVerifyChunks.getChunks()) { + Map shops = plugin.getShopManager().getShops(chunk); + if (shops != null) { + for (Shop shop : shops.values()) { + if (!shop.getOwner().equals(claimVerifyChunks.getOwnerID()) && + claimVerifyChunks.contains(shop.getLocation(), false, false) && + !claimVerifyShop.contains(shop.getLocation(), false, false)) { + plugin.log("[SHOP DELETE] GP Integration: Single delete (SubClaim Resized) #" + shop.ownerName()); + shop.delete(); + } + } + } + } + } + + private boolean checkPermission(@NotNull Player player, @NotNull Location location, List limits) { + if (!griefPrevention.claimsEnabledForWorld(location.getWorld())) { + return true; + } + Claim claim = griefPrevention.dataStore.getClaimAt(location, false, false, griefPrevention.dataStore.getPlayerData(player.getUniqueId()).lastClaim); + if (claim == null) { + return !whiteList; + } + for (Flag flag : limits) { + if (!flag.check(claim, player)) { + return false; + } + } + return true; } private List toFlags(List flags) { @@ -76,156 +312,49 @@ public class GriefPreventionIntegration extends QSIntegratedPlugin { return result; } - @Override - public @NotNull String getName() { - return "GriefPrevention"; - } - - @Override - public boolean canCreateShopHere(@NotNull Player player, @NotNull Location location) { - return checkPermission(player, location, createLimits); - - } - - @Override - public boolean canTradeShopHere(@NotNull Player player, @NotNull Location location) { - return checkPermission(player, location, tradeLimits); - } - - private boolean checkPermission(@NotNull Player player, @NotNull Location location, List limits) { - if (!griefPrevention.claimsEnabledForWorld(location.getWorld())) { - return true; - } - Claim claim = griefPrevention.dataStore.getClaimAt(location, false, griefPrevention.dataStore.getPlayerData(player.getUniqueId()).lastClaim); - if (claim == null) { - return !whiteList; - } - for (Flag flag : limits) { - if (!flag.check(claim, player)) { - return false; - } - } - return true; - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onUntrusted(me.ryanhamshire.GriefPrevention.events.TrustChangedEvent event) { - if (!deleteOnUntrusted) { - return; - } - for (Claim claim : event.getClaims()) { - for (Chunk chunk : claim.getChunks()) { - Map shops = plugin.getShopManager().getShops(chunk); - if (shops != null) { - for (Shop shop : shops.values()) { - if (!shop.getOwner().equals(claim.getOwnerID())) { - - Claim shopClaim = griefPrevention.dataStore.getClaimAt(shop.getLocation(), true, false, null); - if (shopClaim != null && shopClaim.getID().equals(claim.getID())) { - - if (event.getIdentifier().equals(shop.getOwner().toString())) { - plugin.log("[SHOP DELETE] GP Integration: Single delete #" + event.getIdentifier()); - shop.delete(); - } else if (event.getIdentifier().contains(shop.getOwner().toString())) { - plugin.log("[SHOP DELETE] GP Integration: Group delete #" + event.getIdentifier()); - shop.delete(); - } else if ("all".equals(event.getIdentifier()) || "public".equals(event.getIdentifier())) { - plugin.log("[SHOP DELETE] GP Integration: All/Public delete #" + event.getIdentifier()); - shop.delete(); - } - } - } - } - } - } - } - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onClaimUnclaimed(ClaimDeletedEvent event) { - onUnclaimOrExpiredHelper(event.getClaim(), deleteOnUnclaim); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onClaimExpired(ClaimExpirationEvent event) { - onUnclaimOrExpiredHelper(event.getClaim(), deleteOnClaimExpired); - } - - @EventHandler(priority = EventPriority.MONITOR, ignoreCancelled = true) - public void onClaimResized(ClaimModifiedEvent event) { - if (!deleteOnClaimResized) { - return; - } - Claim oldClaim = event.getFrom(); - Claim newClaim = event.getTo(); - for (Chunk chunk : oldClaim.getChunks()) { - Map shops = plugin.getShopManager().getShops(chunk); - if (shops != null) { - for (Shop shop : shops.values()) { - if (oldClaim.contains(shop.getLocation(), true, false) && !newClaim.contains(shop.getLocation(), true, false)) { - plugin.log("[SHOP DELETE] GP Integration: Single delete (Resized) #" + shop.getOwner()); - shop.delete(); - } - } - } - } - } - - private void onUnclaimOrExpiredHelper(Claim claim, boolean configSectionBoolean) { - if (!configSectionBoolean) { - return; - } - for (Chunk chunk : claim.getChunks()) { - Map shops = plugin.getShopManager().getShops(chunk); - if (shops != null) { - for (Shop shop : shops.values()) { - if (claim.contains(shop.getLocation(), true, false)) { - plugin.log("[SHOP DELETE] GP Integration: Single delete (Unclaimed/Expired) #" + shop.getOwner()); - shop.delete(); - } - } - } - } - } - - @Override - public void load() { - this.registerListener(); - } - - @Override - public void unload() { - this.unregisterListener(); - } - enum Flag { + BUILD { @Override boolean check(Claim claim, Player player) { return claim.allowBuild(player, Material.CHEST) == null; } - }, CONTAINER_ACCESS { + @Override + ClaimPermission toClaimPermission() { + return ClaimPermission.Build; + } + }, INVENTORY { @Override boolean check(Claim claim, Player player) { return claim.allowContainers(player) == null; } + @Override + ClaimPermission toClaimPermission() { + return ClaimPermission.Inventory; + } }, ACCESS { @Override boolean check(Claim claim, Player player) { return claim.allowAccess(player) == null; } + @Override + ClaimPermission toClaimPermission() { + return ClaimPermission.Access; + } }; + abstract boolean check(Claim claim, Player player); + + abstract ClaimPermission toClaimPermission(); + public static Flag getFlag(String flag) { - flag = flag.toUpperCase(); for (Flag value : Flag.values()) { - if (value.name().equals(flag)) { + if (value.name().equalsIgnoreCase(flag)) { return value; } } return null; } - - abstract boolean check(Claim claim, Player player); } + }