From 5b4530f8276ac77d33b255d303fff469c39e8d92 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Wed, 28 Jul 2021 13:15:08 +0800 Subject: [PATCH 01/18] Fixed #1351, Try search the quickshop jar if old jar not found --- .../java/org/maxgamer/quickshop/QuickShop.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index d7eed103d..a7732cc7f 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -672,6 +672,22 @@ public class QuickShop extends JavaPlugin { throw new IllegalStateException("Failed to reload QuickShop! Please consider restarting the server. (Plugin was updated)"); } } + // Maybe name changed, try search globally + if (!file.exists()) { + File pluginFolder = new File("plugins"); + if (pluginFolder.isDirectory()) { + //noinspection ConstantConditions + for (File listFile : pluginFolder.listFiles()) { + if (listFile.getName().endsWith(".jar")) { + if (getPluginLoader().loadPlugin(listFile).getName().equals(getDescription().getName())) { + file = listFile; + break; + } + } + } + } + } + if (!file.exists()) { throw new IllegalStateException("Failed to reload QuickShop! Please consider restarting the server. (Failed to find plugin jar)"); } From a8cdad72646f7686ee7b1617758fa8de72ba77be Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Wed, 28 Jul 2021 13:15:24 +0800 Subject: [PATCH 02/18] Add more details while loading shops --- src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java index 658b05491..91b385424 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java @@ -25,6 +25,7 @@ import com.google.gson.JsonSyntaxException; import lombok.Getter; import lombok.Setter; import org.apache.commons.lang.StringUtils; +import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.World; @@ -84,7 +85,7 @@ public class ShopLoader { */ public void loadShops(@Nullable String worldName) { //boolean backupedDatabaseInDeleteProcess = false; - this.plugin.getLogger().info("Loading shops from the database..."); + this.plugin.getLogger().info("Loading shops from the database... If plugin stuck there, check your database connection."); int loadAfterChunkLoaded = 0; int loadAfterWorldLoaded = 0; List pendingLoadShops = new ArrayList<>(); @@ -146,9 +147,11 @@ public class ShopLoader { loadAfterChunkLoaded++; } } + this.plugin.getLogger().info("Enabling the shops in worlds..."); for (Shop shop : pendingLoadShops) { shop.onLoad(); - shop.update(); + // Delay update shop data + Bukkit.getScheduler().runTaskLater(plugin, shop::update, 1L); } this.plugin .getLogger() From 35836c60bcd7e988d14018c2b54656d0d50907a5 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Wed, 28 Jul 2021 13:18:45 +0800 Subject: [PATCH 03/18] Force shop database updating in async thread --- .../maxgamer/quickshop/shop/ContainerShop.java | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java index ba148d46f..6cc07601d 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java @@ -711,7 +711,13 @@ public class ContainerShop implements Shop { */ @Override public synchronized void update() { - Util.mainThreadRun(this::update0); + Util.ensureThread(false); + ShopUpdateEvent shopUpdateEvent = new ShopUpdateEvent(this); + if (Util.fireCancellableEvent(shopUpdateEvent)) { + Util.debugLog("The Shop update action was canceled by a plugin."); + return; + } + Bukkit.getScheduler().runTaskAsynchronously(plugin, this::update0); } private void notifyDisplayItemChange() { @@ -722,12 +728,8 @@ public class ContainerShop implements Shop { } private void update0() { - Util.ensureThread(false); - ShopUpdateEvent shopUpdateEvent = new ShopUpdateEvent(this); - if (Util.fireCancellableEvent(shopUpdateEvent)) { - Util.debugLog("The Shop update action was canceled by a plugin."); - return; - } + // Make sure this only trigged in async thread, we will start the I/O actions + Util.ensureThread(true); int x = this.getLocation().getBlockX(); int y = this.getLocation().getBlockY(); int z = this.getLocation().getBlockZ(); From 389d00823cd6e8ef5a2adf3c72e8737548ec41d3 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Wed, 28 Jul 2021 13:22:48 +0800 Subject: [PATCH 04/18] Prevent multi updates while shop loading --- .../maxgamer/quickshop/shop/ContainerShop.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java index 6cc07601d..b8c7f547d 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java @@ -707,10 +707,11 @@ public class ContainerShop implements Shop { } /** - * Upates the shop into the database. + * Updates the shop into the database. */ @Override public synchronized void update() { + //TODO: check isDirty() Util.ensureThread(false); ShopUpdateEvent shopUpdateEvent = new ShopUpdateEvent(this); if (Util.fireCancellableEvent(shopUpdateEvent)) { @@ -833,24 +834,28 @@ public class ContainerShop implements Shop { // check price restriction PriceLimiter.CheckResult priceRestriction = plugin.getShopManager().getPriceLimiter().check(item, price); + boolean markUpdate = false; if (priceRestriction.getStatus() != PriceLimiter.Status.PASS) { if (priceRestriction.getStatus() == PriceLimiter.Status.NOT_A_WHOLE_NUMBER) { setDirty(); price = Math.floor(price); - this.update(); + markUpdate = true; } else if (priceRestriction.getStatus() == PriceLimiter.Status.NOT_VALID) { setDirty(); price = priceRestriction.getMin(); - this.update(); + markUpdate = true; } if (price < priceRestriction.getMin()) { setDirty(); price = priceRestriction.getMin(); - this.update(); + markUpdate = true; } else if (price > priceRestriction.getMax()) { setDirty(); price = priceRestriction.getMax(); - this.update(); + markUpdate = true; + } + if (markUpdate) { + update(); } } checkDisplay(); From 9599494df1bb11e3b75e51387a5fa972e195263e Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Wed, 28 Jul 2021 13:25:38 +0800 Subject: [PATCH 05/18] Cleanup --- .../quickshop/shop/ContainerShop.java | 1 + .../quickshop/shop/RealDisplayItem.java | 26 +++++++------------ 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java index b8c7f547d..688d258b4 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java @@ -303,6 +303,7 @@ public class ContainerShop implements Shop { return; } + //FIXME: This may affect the performance updateAttachedShop(); if (isLeftShop) { diff --git a/src/main/java/org/maxgamer/quickshop/shop/RealDisplayItem.java b/src/main/java/org/maxgamer/quickshop/shop/RealDisplayItem.java index 45f875f21..159386e4d 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/RealDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/shop/RealDisplayItem.java @@ -35,6 +35,7 @@ import org.maxgamer.quickshop.util.MsgUtil; import org.maxgamer.quickshop.util.Util; import java.util.ArrayList; +import java.util.List; import java.util.Objects; import java.util.UUID; @@ -113,9 +114,7 @@ public class RealDisplayItem extends DisplayItem { if (shop.isLeftShop()) { return; } - Util.debugLog( - "Fixing moved Item displayItem " + eItem.getUniqueId() + " at " + eItem - .getLocation()); + Util.debugLog("Fixing moved Item displayItem " + eItem.getUniqueId() + " at " + eItem.getLocation()); PaperLib.teleportAsync(entity, Objects.requireNonNull(getDisplayLocation()), PlayerTeleportEvent.TeleportCause.UNKNOWN); return; } @@ -132,8 +131,7 @@ public class RealDisplayItem extends DisplayItem { public void remove() { Util.ensureThread(false); if (this.item == null) { - Util.debugLog( - "Ignore the Item removing because the Item is already gone or it's a left shop."); + Util.debugLog("Ignore the Item removing because the Item is already gone or it's a left shop."); return; } this.item.remove(); @@ -157,7 +155,7 @@ public class RealDisplayItem extends DisplayItem { boolean removed = false; - ArrayList elist = new ArrayList<>(item.getNearbyEntities(1.5, 1.5, 1.5)); + List elist = new ArrayList<>(item.getNearbyEntities(1.5, 1.5, 1.5)); if (shop.isRealDouble()) { elist.addAll(item.getWorld() .getNearbyEntities(Objects.requireNonNull(getDoubleShopDisplayLocations(true)), 1.5, @@ -175,9 +173,7 @@ public class RealDisplayItem extends DisplayItem { UUID displayUUID = this.item.getUniqueId(); if (!eItem.getUniqueId().equals(displayUUID)) { if (DisplayItem.checkIsTargetShopDisplay(eItem.getItemStack(), this.shop)) { - Util.debugLog( - "Removing a duped ItemEntity " + eItem.getUniqueId() + " at " + eItem - .getLocation()); + Util.debugLog("Removing a duped ItemEntity " + eItem.getUniqueId() + " at " + eItem.getLocation()); entity.remove(); removed = true; } @@ -226,8 +222,7 @@ public class RealDisplayItem extends DisplayItem { return; } if (shop.getLocation().getWorld() == null) { - Util.debugLog( - "Canceled the displayItem spawning because the location in the world is null."); + Util.debugLog("Canceled the displayItem spawning because the location in the world is null."); return; } @@ -236,14 +231,12 @@ public class RealDisplayItem extends DisplayItem { return; } if (item != null && item.isValid()) { - Util.debugLog( - "Warning: Spawning the Dropped Item for DisplayItem when there is already an existing Dropped Item, May cause a duplicated Dropped Item!"); + Util.debugLog("Warning: Spawning the Dropped Item for DisplayItem when there is already an existing Dropped Item, May cause a duplicated Dropped Item!"); MsgUtil.debugStackTrace(Thread.currentThread().getStackTrace()); } if (!Util.isDisplayAllowBlock( Objects.requireNonNull(getDisplayLocation()).getBlock().getType())) { - Util.debugLog( - "Can't spawn the displayItem because there is not an AIR block above the shopblock."); + Util.debugLog("Can't spawn the displayItem because there is not an AIR block above the shopblock."); return; } @@ -252,8 +245,7 @@ public class RealDisplayItem extends DisplayItem { plugin.getServer().getPluginManager().callEvent(shopDisplayItemSpawnEvent); if (shopDisplayItemSpawnEvent.isCancelled()) { - Util.debugLog( - "Canceled the displayItem spawning because a plugin setCancelled the spawning event, usually this is a QuickShop Add on"); + Util.debugLog("Canceled the displayItem spawning because a plugin setCancelled the spawning event, usually this is a QuickShop Add on"); return; } this.guardedIstack = DisplayItem.createGuardItemStack(this.originalItemStack, this.shop); From 2f1bb148d47f31399e184ddeee8ac1be3efb3e22 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Wed, 28 Jul 2021 13:30:54 +0800 Subject: [PATCH 06/18] Thread-safe update0() method --- .../maxgamer/quickshop/shop/ContainerShop.java | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java index 688d258b4..036f90bc0 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java @@ -719,7 +719,13 @@ public class ContainerShop implements Shop { Util.debugLog("The Shop update action was canceled by a plugin."); return; } - Bukkit.getScheduler().runTaskAsynchronously(plugin, this::update0); + int x = this.getLocation().getBlockX(); + int y = this.getLocation().getBlockY(); + int z = this.getLocation().getBlockZ(); + String world = Objects.requireNonNull(this.getLocation().getWorld()).getName(); + Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { + update0(world, x, y, z); + }); } private void notifyDisplayItemChange() { @@ -729,13 +735,10 @@ public class ContainerShop implements Shop { } } - private void update0() { + private void update0(String world, int x, int y, int z) { // Make sure this only trigged in async thread, we will start the I/O actions Util.ensureThread(true); - int x = this.getLocation().getBlockX(); - int y = this.getLocation().getBlockY(); - int z = this.getLocation().getBlockZ(); - String world = Objects.requireNonNull(this.getLocation().getWorld()).getName(); + int unlimited = this.isUnlimited() ? 1 : 0; try { plugin.getDatabaseHelper() From 7fd1c5cc097e817a3881c12b43d0ef86d1265f13 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Wed, 28 Jul 2021 13:37:57 +0800 Subject: [PATCH 07/18] update0 actually can safety executing on any threads --- src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java | 3 --- 1 file changed, 3 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java index 036f90bc0..c6772d3eb 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java @@ -736,9 +736,6 @@ public class ContainerShop implements Shop { } private void update0(String world, int x, int y, int z) { - // Make sure this only trigged in async thread, we will start the I/O actions - Util.ensureThread(true); - int unlimited = this.isUnlimited() ? 1 : 0; try { plugin.getDatabaseHelper() From 60b1df7214180639db0298e9702b838b70dd5d9d Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Wed, 28 Jul 2021 13:54:28 +0800 Subject: [PATCH 08/18] try to resolve npe issue --- .../java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java index 3afd7716a..3d2dbba78 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java @@ -266,7 +266,7 @@ public class VirtualDisplayItem extends DisplayItem { private void unload() { packetSenders.clear(); if (packetAdapter != null) { - asyncListenerHandler.stop(); + //asyncListenerHandler.stop(); protocolManager.getAsynchronousManager().unregisterAsyncHandler(packetAdapter); //protocolManager.removePacketListener(packetAdapter); //Prevent memory leak From 47678e3e88b5f3458a53a5a6fe936edf59d10208 Mon Sep 17 00:00:00 2001 From: Ghost_chu <2908803755@qq.com> Date: Wed, 28 Jul 2021 14:07:07 +0800 Subject: [PATCH 09/18] Fix npe while disabling --- .../java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java index 3d2dbba78..ba090fd19 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java @@ -266,8 +266,8 @@ public class VirtualDisplayItem extends DisplayItem { private void unload() { packetSenders.clear(); if (packetAdapter != null) { - //asyncListenerHandler.stop(); - protocolManager.getAsynchronousManager().unregisterAsyncHandler(packetAdapter); + asyncListenerHandler.stop(); + protocolManager.getAsynchronousManager().unregisterAsyncHandler(asyncListenerHandler); //protocolManager.removePacketListener(packetAdapter); //Prevent memory leak asyncListenerHandler = null; From c8c1bfe463a480861d70593f51bcc133ef9175ed Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Wed, 28 Jul 2021 22:39:58 +0800 Subject: [PATCH 10/18] Improve shop loading and logging --- .../maxgamer/quickshop/shop/ShopLoader.java | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java index 91b385424..2300e190c 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java @@ -61,7 +61,6 @@ public class ShopLoader { private final List shopsInDatabase = new CopyOnWriteArrayList<>(); private final List shopRawDatabaseInfoList = new CopyOnWriteArrayList<>(); private int errors; - private int totalLoaded = 0; //private final WarningSender warningSender; /** @@ -89,6 +88,7 @@ public class ShopLoader { int loadAfterChunkLoaded = 0; int loadAfterWorldLoaded = 0; List pendingLoadShops = new ArrayList<>(); + this.plugin.getLogger().info("Loading shops from the database..."); try (WarpedResultSet warpRS = plugin.getDatabaseHelper().selectAllShops(); ResultSet rs = warpRS.getResultSet()) { while (rs.next()) { ShopRawDatabaseInfo origin = new ShopRawDatabaseInfo(rs); @@ -147,19 +147,21 @@ public class ShopLoader { loadAfterChunkLoaded++; } } - this.plugin.getLogger().info("Enabling the shops in worlds..."); - for (Shop shop : pendingLoadShops) { - shop.onLoad(); - // Delay update shop data - Bukkit.getScheduler().runTaskLater(plugin, shop::update, 1L); - } - this.plugin - .getLogger() - .info( - "Successfully loaded " - + totalLoaded - + " shops!"); - this.plugin.getLogger().info(loadAfterChunkLoaded + + Bukkit.getScheduler().runTaskLater(plugin, () -> { + this.plugin.getLogger().info("Loading the shops in worlds..."); + for (Shop shop : pendingLoadShops) { + shop.onLoad(); + shop.update(); + } + this.plugin + .getLogger() + .info( + "Successfully loaded " + + pendingLoadShops.size() + + " shops!"); + }, 1); + this.plugin.getLogger().info("Scheduled " + pendingLoadShops.size() + " shops to load in next tick, " + loadAfterChunkLoaded + " shops will load after chunk have loaded, " + loadAfterWorldLoaded + " shops will load after the world has loaded."); @@ -168,7 +170,7 @@ public class ShopLoader { } } - private void singleShopLoaded(@NotNull Timer singleShopLoadTimer) { + /* private void singleShopLoaded(@NotNull Timer singleShopLoadTimer) { totalLoaded++; long singleShopLoadTime = singleShopLoadTimer.stopAndGetTimePassed(); loadTimes.add(singleShopLoadTime); @@ -181,7 +183,7 @@ public class ShopLoader { private double calcTimeCost(@NotNull Timer timer) { timeCostCache.putIfAbsent(timer, (double) timer.getPassedTime()); return timer.getPassedTime() - timeCostCache.get(timer); - } + }*/ @SuppressWarnings("ConstantConditions") private boolean shopNullCheck(@Nullable Shop shop) { From c8bc3ab422769b5e4cf5511fae78179267a438e2 Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Wed, 28 Jul 2021 22:41:21 +0800 Subject: [PATCH 11/18] Fix duplicated updating correctly, fix error on plugin disable --- .../quickshop/shop/ContainerShop.java | 38 ++++++++++--------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java index c6772d3eb..244689670 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java @@ -707,25 +707,41 @@ public class ContainerShop implements Shop { this.setSignText(getSignText()); } + private volatile boolean updating = false; + /** * Updates the shop into the database. */ @Override - public synchronized void update() { + public void update() { //TODO: check isDirty() Util.ensureThread(false); + if (updating) { + return; + } ShopUpdateEvent shopUpdateEvent = new ShopUpdateEvent(this); if (Util.fireCancellableEvent(shopUpdateEvent)) { Util.debugLog("The Shop update action was canceled by a plugin."); return; } + updating = true; int x = this.getLocation().getBlockX(); int y = this.getLocation().getBlockY(); int z = this.getLocation().getBlockZ(); String world = Objects.requireNonNull(this.getLocation().getWorld()).getName(); - Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> { - update0(world, x, y, z); - }); + int unlimited = this.isUnlimited() ? 1 : 0; + try { + plugin.getDatabaseHelper() + .updateShop(ShopModerator.serialize(this.moderator), this.getItem(), + unlimited, shopType.toID(), this.getPrice(), x, y, z, world, + this.saveExtraToYaml()); + this.dirty = false; + } catch (Exception e) { + plugin.getLogger().log(Level.WARNING, + "Could not update a shop in the database! Changes will revert after a reboot!", e); + } finally { + updating = false; + } } private void notifyDisplayItemChange() { @@ -735,20 +751,6 @@ public class ContainerShop implements Shop { } } - private void update0(String world, int x, int y, int z) { - int unlimited = this.isUnlimited() ? 1 : 0; - try { - plugin.getDatabaseHelper() - .updateShop(ShopModerator.serialize(this.moderator), this.getItem(), - unlimited, shopType.toID(), this.getPrice(), x, y, z, world, - this.saveExtraToYaml()); - } catch (Exception e) { - plugin.getLogger().log(Level.WARNING, - "Could not update a shop in the database! Changes will revert after a reboot!", e); - } - this.dirty = false; - } - /** * @return The durability of the item */ From c4b1f47450b7052d0bddc41cbce78d8ee302662b Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Wed, 28 Jul 2021 23:15:44 +0800 Subject: [PATCH 12/18] Use the right set implementation --- .../java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java index ba090fd19..c4ecea3e0 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java @@ -46,7 +46,7 @@ import org.maxgamer.quickshop.util.Util; import java.lang.reflect.InvocationTargetException; import java.util.*; -import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentSkipListSet; import java.util.concurrent.atomic.AtomicInteger; public class VirtualDisplayItem extends DisplayItem { @@ -62,7 +62,7 @@ public class VirtualDisplayItem extends DisplayItem { private final int entityID = counter.decrementAndGet(); //The List which store packet sender - private final Set packetSenders = ConcurrentHashMap.newKeySet(); + private final Set packetSenders = new ConcurrentSkipListSet<>(); private volatile boolean isDisplay; From f542340782200164c2d3fb07cc374120904730e2 Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Wed, 28 Jul 2021 23:31:10 +0800 Subject: [PATCH 13/18] Fix wording --- src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java index 2300e190c..c3d35eed5 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ShopLoader.java @@ -84,12 +84,12 @@ public class ShopLoader { */ public void loadShops(@Nullable String worldName) { //boolean backupedDatabaseInDeleteProcess = false; - this.plugin.getLogger().info("Loading shops from the database... If plugin stuck there, check your database connection."); + this.plugin.getLogger().info("Fetching shops from the database...If plugin stuck there, check your database connection."); int loadAfterChunkLoaded = 0; int loadAfterWorldLoaded = 0; List pendingLoadShops = new ArrayList<>(); - this.plugin.getLogger().info("Loading shops from the database..."); try (WarpedResultSet warpRS = plugin.getDatabaseHelper().selectAllShops(); ResultSet rs = warpRS.getResultSet()) { + this.plugin.getLogger().info("Loading shops from the database..."); while (rs.next()) { ShopRawDatabaseInfo origin = new ShopRawDatabaseInfo(rs); shopRawDatabaseInfoList.add(origin); From 68d743e7231624973c5d0e24fc04fd4413b2594c Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Thu, 29 Jul 2021 00:52:58 +0800 Subject: [PATCH 14/18] Fix VirtualDisplayItem disappear bug --- src/main/java/org/maxgamer/quickshop/shop/DisplayItem.java | 6 +++--- .../org/maxgamer/quickshop/shop/VirtualDisplayItem.java | 3 --- 2 files changed, 3 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/shop/DisplayItem.java index 23eda4915..686dc312b 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/DisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/shop/DisplayItem.java @@ -52,9 +52,9 @@ public abstract class DisplayItem { @Nullable protected ItemStack guardedIstack; private boolean pendingRemoval; - protected int chunkX; - protected int chunkZ; - protected String worldName; + protected final int chunkX; + protected final int chunkZ; + protected final String worldName; protected DisplayItem(Shop shop, String worldName, int chunkX, int chunkZ) { this.shop = shop; diff --git a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java index c4ecea3e0..479df14d3 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java @@ -84,9 +84,6 @@ public class VirtualDisplayItem extends DisplayItem { //cache chunk x and z private volatile ShopChunk chunkLocation; - private volatile int chunkX; - private volatile int chunkZ; - private AsyncListenerHandler asyncListenerHandler; From cdf5f8ba560587b41d2cf7672e1024f7506daefb Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Thu, 29 Jul 2021 03:23:49 +0800 Subject: [PATCH 15/18] Fix another display disappear bug --- .../java/org/maxgamer/quickshop/shop/ContainerShop.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java index 244689670..7591b5ab4 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java @@ -167,15 +167,15 @@ public class ContainerShop implements Shop { Chunk chunk = getLocation().getChunk(); switch (DisplayItem.getNowUsing()) { case REALITEM: - this.displayItem = new RealDisplayItem(this, chunk.getWorld().getName(), chunk.getX(), chunk.getX()); + this.displayItem = new RealDisplayItem(this, chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); break; case VIRTUALITEM: - this.displayItem = new VirtualDisplayItem(this, chunk.getWorld().getName(), chunk.getX(), chunk.getX()); + this.displayItem = new VirtualDisplayItem(this, chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); break; default: Util.debugLog( "Warning: Failed to create a ContainerShop displayItem, the type we didn't know, fallback to RealDisplayItem"); - this.displayItem = new RealDisplayItem(this, chunk.getWorld().getName(), chunk.getX(), chunk.getX()); + this.displayItem = new RealDisplayItem(this, chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); break; } } From bd9477e335cb208700933595c83837cc5ed61a19 Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Thu, 29 Jul 2021 19:21:34 +0800 Subject: [PATCH 16/18] Do not load chunk in shop init process and deduplicate loading, fix #1360 --- .../quickshop/shop/ContainerShop.java | 7 +++--- .../maxgamer/quickshop/shop/DisplayItem.java | 8 +------ .../quickshop/shop/RealDisplayItem.java | 4 ++-- .../quickshop/shop/VirtualDisplayItem.java | 23 ++++++++++--------- 4 files changed, 18 insertions(+), 24 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java index 7591b5ab4..d2641a539 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java +++ b/src/main/java/org/maxgamer/quickshop/shop/ContainerShop.java @@ -164,18 +164,17 @@ public class ContainerShop implements Shop { private void initDisplayItem() { Util.ensureThread(false); if (plugin.isDisplay()) { - Chunk chunk = getLocation().getChunk(); switch (DisplayItem.getNowUsing()) { case REALITEM: - this.displayItem = new RealDisplayItem(this, chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); + this.displayItem = new RealDisplayItem(this); break; case VIRTUALITEM: - this.displayItem = new VirtualDisplayItem(this, chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); + this.displayItem = new VirtualDisplayItem(this); break; default: Util.debugLog( "Warning: Failed to create a ContainerShop displayItem, the type we didn't know, fallback to RealDisplayItem"); - this.displayItem = new RealDisplayItem(this, chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); + this.displayItem = new RealDisplayItem(this); break; } } diff --git a/src/main/java/org/maxgamer/quickshop/shop/DisplayItem.java b/src/main/java/org/maxgamer/quickshop/shop/DisplayItem.java index 686dc312b..be9737e87 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/DisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/shop/DisplayItem.java @@ -52,15 +52,9 @@ public abstract class DisplayItem { @Nullable protected ItemStack guardedIstack; private boolean pendingRemoval; - protected final int chunkX; - protected final int chunkZ; - protected final String worldName; - protected DisplayItem(Shop shop, String worldName, int chunkX, int chunkZ) { + protected DisplayItem(Shop shop) { this.shop = shop; - this.worldName = worldName; - this.chunkX = chunkX; - this.chunkZ = chunkZ; this.originalItemStack = shop.getItem().clone(); if (displayAllowStacks) { //Prevent stack over the normal size diff --git a/src/main/java/org/maxgamer/quickshop/shop/RealDisplayItem.java b/src/main/java/org/maxgamer/quickshop/shop/RealDisplayItem.java index 159386e4d..0dcfb9ae4 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/RealDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/shop/RealDisplayItem.java @@ -50,8 +50,8 @@ public class RealDisplayItem extends DisplayItem { * * @param shop The shop (See Shop) */ - RealDisplayItem(@NotNull Shop shop, @NotNull String worldName, int chunkX, int chunkZ) { - super(shop, worldName, chunkX, chunkZ); + RealDisplayItem(@NotNull Shop shop) { + super(shop); // this.displayLoc = shop.getLocation().clone().add(0.5, 1.2, 0.5); } diff --git a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java index 479df14d3..dbc01cc52 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java @@ -31,6 +31,7 @@ import com.comphenix.protocol.reflect.StructureModifier; import com.comphenix.protocol.utility.MinecraftVersion; import com.comphenix.protocol.wrappers.WrappedChatComponent; import com.comphenix.protocol.wrappers.WrappedDataWatcher; +import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.entity.Entity; @@ -87,8 +88,8 @@ public class VirtualDisplayItem extends DisplayItem { private AsyncListenerHandler asyncListenerHandler; - public VirtualDisplayItem(@NotNull Shop shop, @NotNull String worldName, int chunkX, int chunkZ) throws RuntimeException { - super(shop, worldName, chunkX, chunkZ); + public VirtualDisplayItem(@NotNull Shop shop) throws RuntimeException { + super(shop); } private void initFakeDropItemPacket() { @@ -126,9 +127,11 @@ public class VirtualDisplayItem extends DisplayItem { @Override public void remove() { - sendPacketToAll(fakeItemDestroyPacket); - unload(); - isDisplay = false; + if (isDisplay) { + sendPacketToAll(fakeItemDestroyPacket); + unload(); + isDisplay = false; + } } private void sendPacketToAll(@NotNull PacketContainer packet) { @@ -177,15 +180,16 @@ public class VirtualDisplayItem extends DisplayItem { @Override public void spawn() { Util.ensureThread(false); - if (shop.isLeftShop()) { + if (shop.isLeftShop() || isDisplay || shop.isDeleted() || !shop.isLoaded()) { return; } //lazy initialize if (!initialized) { initFakeDropItemPacket(); } - if (shop.isDeleted() || !shop.isLoaded()) { - return; + if (chunkLocation == null) { + Chunk chunk = shop.getLocation().getChunk(); + chunkLocation = new ShopChunk(chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); } ShopDisplayItemSpawnEvent shopDisplayItemSpawnEvent = new ShopDisplayItemSpawnEvent(shop, originalItemStack, DisplayType.VIRTUALITEM); plugin.getServer().getPluginManager().callEvent(shopDisplayItemSpawnEvent); @@ -246,9 +250,6 @@ public class VirtualDisplayItem extends DisplayItem { int x = integerStructureModifier.read(0); //chunk z int z = integerStructureModifier.read(1); - if (chunkLocation == null) { - chunkLocation = new ShopChunk(worldName, chunkX, chunkZ); - } if (chunkLocation.isSame(player.getWorld().getName(), x, z)) { packetSenders.add(player.getUniqueId()); sendFakeItem(player); From f4ffb375badf93161777ad20639c1709a273a5e7 Mon Sep 17 00:00:00 2001 From: sandtechnology Date: Thu, 29 Jul 2021 19:34:18 +0800 Subject: [PATCH 17/18] Tweak calling event to before loading, save resources --- .../quickshop/shop/VirtualDisplayItem.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java index dbc01cc52..c2220239b 100644 --- a/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java +++ b/src/main/java/org/maxgamer/quickshop/shop/VirtualDisplayItem.java @@ -183,6 +183,14 @@ public class VirtualDisplayItem extends DisplayItem { if (shop.isLeftShop() || isDisplay || shop.isDeleted() || !shop.isLoaded()) { return; } + ShopDisplayItemSpawnEvent shopDisplayItemSpawnEvent = new ShopDisplayItemSpawnEvent(shop, originalItemStack, DisplayType.VIRTUALITEM); + plugin.getServer().getPluginManager().callEvent(shopDisplayItemSpawnEvent); + if (shopDisplayItemSpawnEvent.isCancelled()) { + Util.debugLog( + "Canceled the displayItem spawning because a plugin setCancelled the spawning event, usually this is a QuickShop Add on"); + return; + } + //lazy initialize if (!initialized) { initFakeDropItemPacket(); @@ -191,13 +199,6 @@ public class VirtualDisplayItem extends DisplayItem { Chunk chunk = shop.getLocation().getChunk(); chunkLocation = new ShopChunk(chunk.getWorld().getName(), chunk.getX(), chunk.getZ()); } - ShopDisplayItemSpawnEvent shopDisplayItemSpawnEvent = new ShopDisplayItemSpawnEvent(shop, originalItemStack, DisplayType.VIRTUALITEM); - plugin.getServer().getPluginManager().callEvent(shopDisplayItemSpawnEvent); - if (shopDisplayItemSpawnEvent.isCancelled()) { - Util.debugLog( - "Canceled the displayItem spawning because a plugin setCancelled the spawning event, usually this is a QuickShop Add on"); - return; - } load(); // Can't rely on the attachedShop cache to be accurate From fc17493d82916c31e7565f498570c304eb02f76c Mon Sep 17 00:00:00 2001 From: raphtaliapt <64327988+raphtaliapt@users.noreply.github.com> Date: Fri, 30 Jul 2021 05:29:52 +0100 Subject: [PATCH 18/18] Big changes/improvements/bug fixes to the Grief Prevention Integration (#1352) * Big improvements to the Grief Prevention Integration. Fixed some bugs. Refactored some things. * Deleted some features/validation because of some problems found. * Minor fix in the trust changed event. * Improved the Grief Prevention Integration by changing the config file. * Fixed/Changed some things, in order for the pull request to be approved. Co-authored-by: 1171066 <1171066@isep.ipp.pt> --- .../org/maxgamer/quickshop/QuickShop.java | 17 + .../GriefPreventionIntegration.java | 404 ++++++++++++------ src/main/resources/config.yml | 45 +- 3 files changed, 311 insertions(+), 155 deletions(-) diff --git a/src/main/java/org/maxgamer/quickshop/QuickShop.java b/src/main/java/org/maxgamer/quickshop/QuickShop.java index a7732cc7f..fd106f750 100644 --- a/src/main/java/org/maxgamer/quickshop/QuickShop.java +++ b/src/main/java/org/maxgamer/quickshop/QuickShop.java @@ -1884,7 +1884,24 @@ public class QuickShop extends JavaPlugin { getConfig().set("shop.use-global-virtual-item-queue", null); getConfig().set("config-version", ++selectedVersion); } + if (selectedVersion == 137) { + 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); + getConfig().set("integration.griefprevention.delete-on-unclaim", null); + getConfig().set("integration.griefprevention.delete-on-claim-unclaimed", oldValueUnclaim); + + getConfig().set("integration.griefprevention.delete-on-subclaim-created", false); + getConfig().set("config-version", ++selectedVersion); + } if (getConfig().getInt("matcher.work-type") != 0 && GameVersion.get(ReflectFactory.getServerVersion()).name().contains("1_16")) { getLogger().warning("You are not using QS Matcher, it may meeting item comparing issue mentioned there: https://hub.spigotmc.org/jira/browse/SPIGOT-5063"); 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 ee34782e6..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; @@ -38,30 +41,264 @@ 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) { @@ -75,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() == 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); } + } diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index a6f4eacc1..3a0179d56 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -22,7 +22,7 @@ # TO EDIT QUICKSHOP'S CONFIGURATION, USE THE "config.yml" FILE! #Do not touch this if you don't know what you're doing! -config-version: 137 +config-version: 138 #Select the language you want to use, (e.g de), use only supported language codes from the list below. #If you use a not existant/not supported language, then QuickShop will use en_US. @@ -663,35 +663,44 @@ integration: #GriefPrevention Integration griefprevention: enable: false - #Should we prevent the creation of a shop or trade with a shop outside GriefPrevetion's area? - whitelist-mode: false - #Which checks does a player need to pass to create a shop? - #You can use multiple checks at the same time. - #Priority: UP > DOWN - # + #Should we prevent the creation of a shop or trade with a shop outside GriefPrevention's area? + whitelist-mode: true + #Which check does a player need to pass to create a shop? #Available fields: #BUILD = The player must have the ability to build in the target claim. - #CONTAINER_ACCESS = The player must be able to access containers. + #INVENTORY = The player must be able to access containers. #ACCESS = The Player must have the ability to access the target claim. - create: - - CONTAINER_ACCESS + create: INVENTORY #Which checks does a player need to pass to trade with a shop? #You can use multiple checks at the same time. #Priority: UP > DOWN - # #Available fields: #BUILD = The player must have the ability to build in the target claim. - #CONTAINER_ACCESS = The player must be able to access containers. + #INVENTORY = The player must be able to access containers. #ACCESS = The Player must have the ability to access the target claim. trade: [ ] - #Should QuickShop delete shops if the shop owner has been untrusted on the claim? - delete-on-untrusted: true - #Should QuickShop delete shops if claim has been deleted? - delete-on-unclaim: true - #Should QuickShop delete shops if claim has been expired? + #Should QuickShop delete shops if the trust permission of the shop owner has been changed on the claim/subclaim? + #Removes a shop if the shop owner no longer has permission to build a shop there. + #Never removes a shop of the claim owner. + delete-on-claim-trust-changed: true + #Should QuickShop delete shops if the claim/subclaim has been unclaimed? + #Removes all the shops that were inside the claim/subclaim that was unclaimed. + #Never removes a shop of the claim owner when unclaiming a subclaim. + delete-on-claim-unclaimed: true + #Should QuickShop delete shops if the claim has been expired? + #Removes all the shops that were inside the claim. delete-on-claim-expired: true - #Should QuickShop delete shops if claim has been resized (claim has become smaller and the shop is now outside claim)? + #Should QuickShop delete shops if claim/subclaim has been resized? + #Removes a shop that the old claim contains but the new claim doesn't. + #Removes a shop if the shop was inside the subclaim but now it is outside the subclaim. + #Removes a shop if the shop was outside the subclaim but now it is inside the subclaim. + #Never removes a shop of the claim owner when resizing a subclaim. delete-on-claim-resized: true + #Should Quickshop delete shops if a subclaim has been created? + #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. + #Never removes a shop of the claim owner. + delete-on-subclaim-created: true #Residence Integration residence: enable: false