From a524d6cb5046fe7cd956c2c00ec3a0d03c905fd7 Mon Sep 17 00:00:00 2001 From: PhanaticD Date: Tue, 28 Jan 2020 22:28:14 -0500 Subject: [PATCH 1/4] fix error when trying to create a inv with over 54 slots (buying max available) fix unnecessary chunk loads/unloads being checked fix item displays when creating or breaking double chests fix cancelling item despawns creating a new item without metadata (dupe issue for plugins that check for this) minor random performance improvements --- .../prettysimpleshop/command/BuyCommand.java | 2 +- .../prettysimpleshop/feature/ShowoffItem.java | 66 ++++++++++++++----- .../prettysimpleshop/shop/ShopAPI.java | 1 - 3 files changed, 50 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java b/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java index 270476f..91b9dd1 100644 --- a/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java +++ b/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java @@ -154,7 +154,7 @@ public boolean buyCommand(Player player, int amount, boolean confirm) shopInfo = new ShopInfo(shopInfo, itemStack.getAmount()); player.getServer().getPluginManager().callEvent(new ShopBoughtEvent(player, shopInfo)); - int rows = ((itemStack.getAmount() / itemStack.getMaxStackSize()) + 1) / 9 + 1; + int rows = Math.min(((itemStack.getAmount() / itemStack.getMaxStackSize()) + 1) / 9 + 1, 54); ShopInventoryHolder shopInventoryHolder = new ShopInventoryHolder(); Inventory inventory = player.getServer().createInventory(shopInventoryHolder, rows * 9, diff --git a/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java b/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java index 07216b1..9abaee5 100644 --- a/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java +++ b/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java @@ -8,10 +8,7 @@ import com.robomwm.prettysimpleshop.event.ShopSelectEvent; import com.robomwm.prettysimpleshop.shop.ShopAPI; import com.robomwm.prettysimpleshop.shop.ShopInfo; -import org.bukkit.Chunk; -import org.bukkit.ChunkSnapshot; -import org.bukkit.Location; -import org.bukkit.World; +import org.bukkit.*; import org.bukkit.block.Chest; import org.bukkit.block.Container; import org.bukkit.block.DoubleChest; @@ -25,6 +22,7 @@ import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.EntityPickupItemEvent; import org.bukkit.event.entity.ItemDespawnEvent; +import org.bukkit.event.entity.ItemMergeEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.InventoryType; import org.bukkit.event.world.ChunkLoadEvent; @@ -82,7 +80,7 @@ private void saveCache() } } - @EventHandler + @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) private void onChunkLoad(ChunkLoadEvent event) { if (!config.isWhitelistedWorld(event.getWorld())) @@ -127,16 +125,18 @@ public void run() boolean noShops = true; for (Location location : blocksToCheck) { - if (!location.getWorld().isChunkLoaded(location.getBlockX() >> 4, location.getBlockZ() >> 4)) + if (!location.getWorld().isChunkLoaded(location.getBlockX() >> 4, location.getBlockZ() >> 4)) { return; + } Container container = shopAPI.getContainer(location); if (container == null || !shopAPI.isShop(container, false)) continue; ItemStack item = shopAPI.getItemStack(container); if (item == null) continue; - if (spawnItem(new ShopInfo(shopAPI.getLocation(container), item, plugin.getShopAPI().getPrice(container)))) + if (spawnItem(new ShopInfo(shopAPI.getLocation(container), item, shopAPI.getPrice(container)))) { noShops = false; //Shops exist in this chunk + } } if (noShops) removeCachedChunk(chunk); @@ -146,7 +146,7 @@ public void run() }.runTaskAsynchronously(plugin); } - @EventHandler(ignoreCancelled = true) + @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) private void onChunkUnload(ChunkUnloadEvent event) { if (!config.isWhitelistedWorld(event.getWorld())) @@ -157,7 +157,7 @@ private void onChunkUnload(ChunkUnloadEvent event) while (locations.hasNext()) //can optimize later via mapping chunks if needed { Location location = locations.next(); - if (location.getChunk() == event.getChunk()) + if (location.getBlockX() >> 4 == event.getChunk().getX() && location.getBlockZ() >> 4 == event.getChunk().getZ()) { Item item = spawnedItems.get(location); item.remove(); @@ -184,19 +184,23 @@ private void onDoubleChest(BlockPlaceEvent event) return; if (!config.isShopBlock(event.getBlock().getType())) return; - new BukkitRunnable() - { + + if(event.getBlockPlaced().getType() != Material.CHEST && event.getBlockPlaced().getType() != Material.TRAPPED_CHEST) + return; + + new BukkitRunnable() { @Override - public void run() - { - InventoryHolder holder = ((Container)event.getBlock().getState()).getInventory().getHolder(); + public void run() { + Container container = shopAPI.getContainer(event.getBlock().getLocation()); + InventoryHolder holder = container.getInventory().getHolder(); if (!(holder instanceof DoubleChest)) return; DoubleChest doubleChest = (DoubleChest)holder; despawnItem(((Chest)(doubleChest.getLeftSide())).getLocation().add(0.5, 1.2, 0.5)); - despawnItem(((Chest)(doubleChest.getLeftSide())).getLocation().add(0.5, 1.2, 0.5)); + despawnItem(((Chest)(doubleChest.getRightSide())).getLocation().add(0.5, 1.2, 0.5)); + spawnItem(new ShopInfo(shopAPI.getLocation(container), shopAPI.getItemStack(container), shopAPI.getPrice(container))); } - }.runTask(plugin); + }.runTaskLater(plugin, 1L); } @EventHandler(ignoreCancelled = true, priority = EventPriority.LOWEST) @@ -233,9 +237,37 @@ private void onShopOpen(ShopOpenCloseEvent event) private void onShopBreak(ShopBreakEvent event) { despawnItem(event.getShopInfo().getLocation().add(0.5, 1.2, 0.5)); + Container container = shopAPI.getContainer(event.getShopInfo().getLocation()); + InventoryHolder holder = container.getInventory().getHolder(); + if (!(holder instanceof DoubleChest)) { + return; + } + new BukkitRunnable() { + @Override + public void run() { + spawnItem(new ShopInfo(shopAPI.getLocation(container), shopAPI.getItemStack(container), shopAPI.getPrice(container))); + } + }.runTaskLater(plugin, 1L); } @EventHandler private void onItemDespawn(ItemDespawnEvent event) + { + if (event.getEntity().hasMetadata("NO_PICKUP")){ + event.setCancelled(true); + new BukkitRunnable() { + @Override + public void run() { + despawnItem(event.getEntity().getLocation()); + Container container = shopAPI.getContainer(event.getLocation().subtract(0,0.5,0)); + if(container != null) { + spawnItem(new ShopInfo(shopAPI.getLocation(container), shopAPI.getItemStack(container), shopAPI.getPrice(container))); + } + } + }.runTaskLater(plugin, 1L); + } + } + @EventHandler + private void onItemMerge(ItemMergeEvent event) { if (event.getEntity().hasMetadata("NO_PICKUP")) event.setCancelled(true); @@ -256,7 +288,7 @@ private boolean spawnItem(ShopInfo shopInfo) item.setCustomName(name); item.setCustomNameVisible(true); item.setVelocity(new Vector(0, 0.01, 0)); - item.setMetadata("NO_PICKUP", new FixedMetadataValue(plugin, this)); + item.setMetadata("NO_PICKUP", new FixedMetadataValue(plugin, "NO_PICKUP")); spawnedItems.put(location, item); cacheChunk(location.getChunk()); try //spigot compat (switch to Paper!) diff --git a/src/main/java/com/robomwm/prettysimpleshop/shop/ShopAPI.java b/src/main/java/com/robomwm/prettysimpleshop/shop/ShopAPI.java index 1c6fac7..3e2e23b 100644 --- a/src/main/java/com/robomwm/prettysimpleshop/shop/ShopAPI.java +++ b/src/main/java/com/robomwm/prettysimpleshop/shop/ShopAPI.java @@ -186,7 +186,6 @@ private boolean isSimilar(ItemStack item1, ItemStack item2) */ public Container getContainer(Location location) { - location.getBlock(); BlockState state = location.getBlock().getState(); if (state instanceof Container) return (Container)state; From cb811a61209e0f5a805ac0e4387afa26d400906e Mon Sep 17 00:00:00 2001 From: PhanaticD Date: Tue, 28 Jan 2020 23:09:20 -0500 Subject: [PATCH 2/4] turn off import grouping --- .../com/robomwm/prettysimpleshop/feature/ShowoffItem.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java b/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java index 9abaee5..1e3c033 100644 --- a/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java +++ b/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java @@ -8,7 +8,11 @@ import com.robomwm.prettysimpleshop.event.ShopSelectEvent; import com.robomwm.prettysimpleshop.shop.ShopAPI; import com.robomwm.prettysimpleshop.shop.ShopInfo; -import org.bukkit.*; +import org.bukkit.Chunk; +import org.bukkit.ChunkSnapshot; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.block.Chest; import org.bukkit.block.Container; import org.bukkit.block.DoubleChest; From e8dfacd834f51b32d9acc351f7726c75c12850eb Mon Sep 17 00:00:00 2001 From: PhanaticD Date: Mon, 3 Feb 2020 00:42:06 -0500 Subject: [PATCH 3/4] wrong spot --- .../java/com/robomwm/prettysimpleshop/command/BuyCommand.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java b/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java index 91b9dd1..ec11342 100644 --- a/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java +++ b/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java @@ -154,10 +154,10 @@ public boolean buyCommand(Player player, int amount, boolean confirm) shopInfo = new ShopInfo(shopInfo, itemStack.getAmount()); player.getServer().getPluginManager().callEvent(new ShopBoughtEvent(player, shopInfo)); - int rows = Math.min(((itemStack.getAmount() / itemStack.getMaxStackSize()) + 1) / 9 + 1, 54); + int rows = ((itemStack.getAmount() / itemStack.getMaxStackSize()) + 1) / 9 + 1; ShopInventoryHolder shopInventoryHolder = new ShopInventoryHolder(); Inventory inventory = player.getServer().createInventory(shopInventoryHolder, - rows * 9, + Math.min(rows * 9, 54), config.getString("transactionCompletedWindow", Integer.toString(itemStack.getAmount()), PrettySimpleShop.getItemName(itemStack), economy.format(itemStack.getAmount() * shopInfo.getPrice()))); inventory.setMaxStackSize(itemStack.getMaxStackSize()); inventory.addItem(itemStack); //Note: mutates the itemstack's amount From 67858e74eb519592da13053e8666e711d526721a Mon Sep 17 00:00:00 2001 From: PhanaticD Date: Sat, 8 Feb 2020 09:52:33 -0500 Subject: [PATCH 4/4] dont display items for all double chests --- .../java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java b/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java index 1e3c033..8d6a9e1 100644 --- a/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java +++ b/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java @@ -196,6 +196,9 @@ private void onDoubleChest(BlockPlaceEvent event) @Override public void run() { Container container = shopAPI.getContainer(event.getBlock().getLocation()); + if(!shopAPI.isShop(container)){ + return; + } InventoryHolder holder = container.getInventory().getHolder(); if (!(holder instanceof DoubleChest)) return;