diff --git a/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java b/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java index 270476f..ec11342 100644 --- a/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java +++ b/src/main/java/com/robomwm/prettysimpleshop/command/BuyCommand.java @@ -157,7 +157,7 @@ public boolean buyCommand(Player player, int amount, boolean confirm) 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 diff --git a/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java b/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java index 07216b1..8d6a9e1 100644 --- a/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java +++ b/src/main/java/com/robomwm/prettysimpleshop/feature/ShowoffItem.java @@ -11,6 +11,7 @@ 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; @@ -25,6 +26,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 +84,7 @@ private void saveCache() } } - @EventHandler + @EventHandler(ignoreCancelled = true, priority = EventPriority.MONITOR) private void onChunkLoad(ChunkLoadEvent event) { if (!config.isWhitelistedWorld(event.getWorld())) @@ -127,16 +129,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 +150,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 +161,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 +188,26 @@ 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()); + if(!shopAPI.isShop(container)){ + return; + } + 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 +244,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 +295,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;