From 3eef8250d2770c733e3b6d6a9299b6cda16dc824 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 6 Jun 2017 18:13:08 +0200 Subject: [PATCH] Fixed shops duplicating after extending --- .../listeners/ChestProtectListener.java | 62 ++++++++++--------- .../de/epiceric/shopchest/sql/Database.java | 52 ++++++++++------ .../epiceric/shopchest/utils/ShopUtils.java | 39 +++++++++--- 3 files changed, 99 insertions(+), 54 deletions(-) diff --git a/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java b/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java index b33ad60..76edcaf 100644 --- a/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java +++ b/src/main/java/de/epiceric/shopchest/listeners/ChestProtectListener.java @@ -17,11 +17,11 @@ import de.epiceric.shopchest.language.LanguageUtils; import de.epiceric.shopchest.language.LocalizedMessage; import de.epiceric.shopchest.nms.Hologram; import de.epiceric.shopchest.shop.Shop; +import de.epiceric.shopchest.utils.Callback; import de.epiceric.shopchest.utils.Permissions; import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.Utils; import me.ryanhamshire.GriefPrevention.Claim; -import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; @@ -59,28 +59,27 @@ public class ChestProtectListener implements Listener { this.worldGuard = worldGuard; } - private void remove(final Shop shop, final Block b, Player p) { - shopUtils.removeShop(shop, true); - + private void remove(final Shop shop, final Block b, final Player p) { if (shop.getInventoryHolder() instanceof DoubleChest) { DoubleChest dc = (DoubleChest) shop.getInventoryHolder(); final Chest l = (Chest) dc.getLeftSide(); final Chest r = (Chest) dc.getRightSide(); - Bukkit.getScheduler().runTaskLater(plugin, new Runnable() { + Location loc = (b.getLocation().equals(l.getLocation()) ? r.getLocation() : l.getLocation()); + final Shop newShop = new Shop(shop.getID(), plugin, shop.getVendor(), shop.getProduct(), loc, shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); + + shopUtils.removeShop(shop, true, new Callback(plugin) { @Override - public void run() { - Location loc = (b.getLocation().equals(l.getLocation()) ? r.getLocation() : l.getLocation()); - Shop newShop = new Shop(shop.getID(), plugin, shop.getVendor(), shop.getProduct(), loc, shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); + public void onResult(Object result) { newShop.create(true); shopUtils.addShop(newShop, true); } - }, 1L); - return; + }); + } else { + shopUtils.removeShop(shop, true); + plugin.debug(String.format("%s broke %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID())); + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_REMOVED)); } - - plugin.debug(String.format("%s broke %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID())); - p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.SHOP_REMOVED)); } @EventHandler(ignoreCancelled = true) @@ -130,7 +129,7 @@ public class ChestProtectListener implements Listener { @EventHandler(ignoreCancelled = true) public void onBlockPlace(BlockPlaceEvent e) { - Player p = e.getPlayer(); + final Player p = e.getPlayer(); Block b = e.getBlockPlaced(); if (b.getType().equals(Material.CHEST) || b.getType().equals(Material.TRAPPED_CHEST)) { @@ -143,7 +142,7 @@ public class ChestProtectListener implements Listener { Chest l = (Chest) dc.getLeftSide(); if (shopUtils.isShop(r.getLocation()) || shopUtils.isShop(l.getLocation())) { - Shop shop; + final Shop shop; if (b.getLocation().equals(r.getLocation())) { shop = shopUtils.getShop(l.getLocation()); @@ -171,12 +170,13 @@ public class ChestProtectListener implements Listener { for (Resident resident : town.getResidents()) { if (resident.getName().equals(p.getName())) { if (resident.isMayor()) { - externalPluginsAllowed &= (config.towny_shop_plots_mayor.contains(townBlock.getType().name())); + externalPluginsAllowed = (config.towny_shop_plots_mayor.contains(townBlock.getType().name())); } else if (resident.isKing()) { - externalPluginsAllowed &= (config.towny_shop_plots_king.contains(townBlock.getType().name())); + externalPluginsAllowed = (config.towny_shop_plots_king.contains(townBlock.getType().name())); } else { - externalPluginsAllowed &= (config.towny_shop_plots_residents.contains(townBlock.getType().name())); + externalPluginsAllowed = (config.towny_shop_plots_residents.contains(townBlock.getType().name())); } + break; } } } catch (Exception ex) { @@ -189,45 +189,49 @@ public class ChestProtectListener implements Listener { com.intellectualcrafters.plot.object.Location loc = new com.intellectualcrafters.plot.object.Location(b.getWorld().getName(), b.getX(), b.getY(), b.getZ()); - externalPluginsAllowed &= Utils.isFlagAllowedOnPlot(loc.getOwnedPlot(), PlotSquaredShopFlag.CREATE_SHOP, p); + externalPluginsAllowed = Utils.isFlagAllowedOnPlot(loc.getOwnedPlot(), PlotSquaredShopFlag.CREATE_SHOP, p); } if (externalPluginsAllowed && plugin.hasUSkyBlock() && config.enable_uskyblock_integration) { IslandInfo islandInfo = plugin.getUSkyBlock().getIslandInfo(b.getLocation()); if (islandInfo != null) { - externalPluginsAllowed &= islandInfo.getMembers().contains(p.getName()) || islandInfo.getLeader().equals(p.getName()); + externalPluginsAllowed = islandInfo.getMembers().contains(p.getName()) || islandInfo.getLeader().equals(p.getName()); } } if (externalPluginsAllowed && plugin.hasASkyBlock() && config.enable_askyblock_integration) { Island island = ASkyBlockAPI.getInstance().getIslandAt(b.getLocation()); if (island != null) { - externalPluginsAllowed &= island.getMembers().contains(p.getUniqueId()) || island.getOwner().equals(p.getUniqueId()); + externalPluginsAllowed = island.getMembers().contains(p.getUniqueId()) || island.getOwner().equals(p.getUniqueId()); } } if (externalPluginsAllowed && plugin.hasIslandWorld() && config.enable_islandworld_integration && IslandWorldApi.isInitialized()) { if (b.getWorld().getName().equals(IslandWorldApi.getIslandWorld().getName())) { - externalPluginsAllowed &= IslandWorldApi.canBuildOnLocation(p, b.getLocation(), true); + externalPluginsAllowed = IslandWorldApi.canBuildOnLocation(p, b.getLocation(), true); } } if (externalPluginsAllowed && plugin.hasGriefPrevention() && config.enable_griefprevention_integration) { Claim claim = plugin.getGriefPrevention().dataStore.getClaimAt(b.getLocation(), false, null); if (claim != null) { - externalPluginsAllowed &= claim.allowContainers(p) == null; + externalPluginsAllowed = claim.allowContainers(p) == null; } } if (externalPluginsAllowed || p.hasPermission(Permissions.EXTEND_PROTECTED)) { if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || p.hasPermission(Permissions.EXTEND_OTHER)) { - if (b.getRelative(BlockFace.UP).getType() == Material.AIR) { - shopUtils.removeShop(shop, true); - Shop newShop = new Shop(shop.getID(), plugin, shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); - newShop.create(true); - shopUtils.addShop(newShop, true); - plugin.debug(String.format("%s extended %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID())); + final Shop newShop = new Shop(shop.getID(), plugin, shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); + + shopUtils.removeShop(shop, true, new Callback(plugin) { + @Override + public void onResult(Object result) { + newShop.create(true); + shopUtils.addShop(newShop, true); + plugin.debug(String.format("%s extended %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID())); + } + }); } else { e.setCancelled(true); p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_BLOCKED)); diff --git a/src/main/java/de/epiceric/shopchest/sql/Database.java b/src/main/java/de/epiceric/shopchest/sql/Database.java index 9b6c8e5..b72a53b 100644 --- a/src/main/java/de/epiceric/shopchest/sql/Database.java +++ b/src/main/java/de/epiceric/shopchest/sql/Database.java @@ -304,28 +304,44 @@ public abstract class Database { ResultSet rs = null; try { - ps = connection.prepareStatement("REPLACE INTO shops (vendor,product,world,x,y,z,buyprice,sellprice,shoptype) VALUES(?,?,?,?,?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS); + if (!shop.hasId()) { + ps = connection.prepareStatement("REPLACE INTO shops (vendor,product,world,x,y,z,buyprice,sellprice,shoptype) VALUES(?,?,?,?,?,?,?,?,?)", Statement.RETURN_GENERATED_KEYS); - ps.setString(1, shop.getVendor().getUniqueId().toString()); - ps.setString(2, Utils.encode(shop.getProduct())); - ps.setString(3, shop.getLocation().getWorld().getName()); - ps.setInt(4, shop.getLocation().getBlockX()); - ps.setInt(5, shop.getLocation().getBlockY()); - ps.setInt(6, shop.getLocation().getBlockZ()); - ps.setDouble(7, shop.getBuyPrice()); - ps.setDouble(8, shop.getSellPrice()); - ps.setString(9, shop.getShopType().toString()); - ps.executeUpdate(); + ps.setString(1, shop.getVendor().getUniqueId().toString()); + ps.setString(2, Utils.encode(shop.getProduct())); + ps.setString(3, shop.getLocation().getWorld().getName()); + ps.setInt(4, shop.getLocation().getBlockX()); + ps.setInt(5, shop.getLocation().getBlockY()); + ps.setInt(6, shop.getLocation().getBlockZ()); + ps.setDouble(7, shop.getBuyPrice()); + ps.setDouble(8, shop.getSellPrice()); + ps.setString(9, shop.getShopType().toString()); + ps.executeUpdate(); - int shopId = -1; - rs = ps.getGeneratedKeys(); - if (rs.next()) { - shopId = rs.getInt(1); + int shopId = -1; + rs = ps.getGeneratedKeys(); + if (rs.next()) { + shopId = rs.getInt(1); + } + + shop.setId(shopId); + } else { + ps = connection.prepareStatement("REPLACE INTO shops (id,vendor,product,world,x,y,z,buyprice,sellprice,shoptype) VALUES(?,?,?,?,?,?,?,?,?,?)"); + + ps.setInt(1, shop.getID()); + ps.setString(2, shop.getVendor().getUniqueId().toString()); + ps.setString(3, Utils.encode(shop.getProduct())); + ps.setString(4, shop.getLocation().getWorld().getName()); + ps.setInt(5, shop.getLocation().getBlockX()); + ps.setInt(6, shop.getLocation().getBlockY()); + ps.setInt(7, shop.getLocation().getBlockZ()); + ps.setDouble(8, shop.getBuyPrice()); + ps.setDouble(9, shop.getSellPrice()); + ps.setString(10, shop.getShopType().toString()); + ps.executeUpdate(); } - shop.setId(shopId); - - if (callback != null) callback.callSyncResult(shopId); + if (callback != null) callback.callSyncResult(shop.getID()); plugin.debug("Adding shop to database (#" + shop.getID() + ")"); } catch (SQLException ex) { if (callback != null) callback.callSyncError(ex); diff --git a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java index 045361b..5f09f80 100644 --- a/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java +++ b/src/main/java/de/epiceric/shopchest/utils/ShopUtils.java @@ -61,8 +61,9 @@ public class ShopUtils { * Add a shop * @param shop Shop to add * @param addToDatabase Whether the shop should also be added to the database + * @param callback Callback that - if succeeded - returns the ID the shop had or was given (as {@code int}) */ - public void addShop(Shop shop, boolean addToDatabase) { + public void addShop(Shop shop, boolean addToDatabase, Callback callback) { InventoryHolder ih = shop.getInventoryHolder(); plugin.debug("Adding shop... (#" + shop.getID() + ")"); @@ -81,17 +82,29 @@ public class ShopUtils { shopLocation.put(shop.getLocation(), shop); } - if (addToDatabase) - plugin.getShopDatabase().addShop(shop, null); + if (addToDatabase) { + plugin.getShopDatabase().addShop(shop, callback); + } else { + if (callback != null) callback.callSyncResult(shop.getID()); + } } /** - * Remove a shop + * Add a shop + * @param shop Shop to add + * @param addToDatabase Whether the shop should also be added to the database + */ + public void addShop(Shop shop, boolean addToDatabase) { + addShop(shop, addToDatabase, null); + } + + /** Remove a shop * @param shop Shop to remove * @param removeFromDatabase Whether the shop should also be removed from the database + * @param callback Callback that - if succeeded - returns null */ - public void removeShop(Shop shop, boolean removeFromDatabase) { + public void removeShop(Shop shop, boolean removeFromDatabase, Callback callback) { plugin.debug("Removing shop (#" + shop.getID() + ")"); InventoryHolder ih = shop.getInventoryHolder(); @@ -110,8 +123,20 @@ public class ShopUtils { shop.removeItem(); shop.removeHologram(); - if (removeFromDatabase) - plugin.getShopDatabase().removeShop(shop, null); + if (removeFromDatabase) { + plugin.getShopDatabase().removeShop(shop, callback); + } else { + if (callback != null) callback.callSyncResult(null); + } + } + + /** + * Remove a shop + * @param shop Shop to remove + * @param removeFromDatabase Whether the shop should also be removed from the database + */ + public void removeShop(Shop shop, boolean removeFromDatabase) { + removeShop(shop, removeFromDatabase, null); } /**