From 8bef79d47f087b2ca234d44bf72ad7a8a29e81a6 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 1 Jul 2016 16:13:44 +0200 Subject: [PATCH] Prevent blocks from being placed or pushed above the chest Fixes #6 --- lang/de_DE.lang | 2 + lang/en_US.lang | 7 +++ .../shopchest/language/LocalizedMessage.java | 2 + .../listeners/ChestProtectListener.java | 28 +++++++---- .../listeners/ItemProtectListener.java | 50 +++++++++++++++++++ .../listeners/ShopInteractListener.java | 17 ++++--- 6 files changed, 89 insertions(+), 17 deletions(-) diff --git a/lang/de_DE.lang b/lang/de_DE.lang index d24d8d8..2e28a45 100644 --- a/lang/de_DE.lang +++ b/lang/de_DE.lang @@ -1,5 +1,7 @@ message.shop-created=&6Shop erstellt. message.chest-already-shop=&cTruhe ist bereits ein Shop. +message.chest-blocked=&cÜber der Truhe ist kein Platz. +message.double-chest-blocked=&cÜber der Truhe ist kein Platz. message.shop-removed=&6Shop entfernt. message.chest-no-shop=&cTruhe ist kein Shop. message.shop-create-not-enough-money=&cNicht genug Geld. Du brauchst &6%CREATION-PRICE% &cum einen Shop zu erstellen. diff --git a/lang/en_US.lang b/lang/en_US.lang index 9665a76..9f5c63e 100644 --- a/lang/en_US.lang +++ b/lang/en_US.lang @@ -4,6 +4,13 @@ message.shop-created=&6Shop created. # Set the message when the clicked chest already is a shop. message.chest-already-shop=&cChest already is shop. +# Set the message when there is a block above the clicked chest. +message.chest-blocked=&cThere must not be a block above the chest. + +# Set the message when a player tries to place a chest next to a shop's chest +# to create a double chest shop, but there is a block above the placed chest. +message.double-chest-blocked=&cThere must not be a block above the chest. + # Set the message when the clicked shop is removed. message.shop-removed=&6Shop removed. diff --git a/src/de/epiceric/shopchest/language/LocalizedMessage.java b/src/de/epiceric/shopchest/language/LocalizedMessage.java index 67fb595..7a75d4f 100644 --- a/src/de/epiceric/shopchest/language/LocalizedMessage.java +++ b/src/de/epiceric/shopchest/language/LocalizedMessage.java @@ -44,6 +44,8 @@ public class LocalizedMessage { public enum Message { SHOP_CREATED, CHEST_ALREADY_SHOP, + CHEST_BLOCKED, + DOUBLE_CHEST_BLOCKED, SHOP_REMOVED, CHEST_NO_SHOP, SHOP_CREATE_NOT_ENOUGH_MONEY, diff --git a/src/de/epiceric/shopchest/listeners/ChestProtectListener.java b/src/de/epiceric/shopchest/listeners/ChestProtectListener.java index f4b32bb..a7e423c 100644 --- a/src/de/epiceric/shopchest/listeners/ChestProtectListener.java +++ b/src/de/epiceric/shopchest/listeners/ChestProtectListener.java @@ -8,6 +8,7 @@ import de.epiceric.shopchest.shop.Shop; import de.epiceric.shopchest.utils.ShopUtils; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.block.Chest; import org.bukkit.block.DoubleChest; import org.bukkit.event.EventHandler; @@ -71,20 +72,25 @@ public class ChestProtectListener implements Listener { Chest l = (Chest) dc.getLeftSide(); if (ShopUtils.isShop(r.getLocation()) || ShopUtils.isShop(l.getLocation())) { - Shop shop; + if (b.getRelative(BlockFace.UP).getType() == Material.AIR) { + Shop shop; - if (b.getLocation().equals(r.getLocation())) { - shop = ShopUtils.getShop(l.getLocation()); - } else if (b.getLocation().equals(l.getLocation())) { - shop = ShopUtils.getShop(r.getLocation()); + if (b.getLocation().equals(r.getLocation())) { + shop = ShopUtils.getShop(l.getLocation()); + } else if (b.getLocation().equals(l.getLocation())) { + shop = ShopUtils.getShop(r.getLocation()); + } else { + return; + } + + ShopUtils.removeShop(shop, true); + + Shop newShop = new Shop(shop.getID(), ShopChest.getInstance(), shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); + ShopUtils.addShop(newShop, true); } else { - return; + e.setCancelled(true); + e.getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_BLOCKED)); } - - ShopUtils.removeShop(shop, true); - - Shop newShop = new Shop(shop.getID(), ShopChest.getInstance(), shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); - ShopUtils.addShop(newShop, true); } } diff --git a/src/de/epiceric/shopchest/listeners/ItemProtectListener.java b/src/de/epiceric/shopchest/listeners/ItemProtectListener.java index 5502ee3..3267d92 100644 --- a/src/de/epiceric/shopchest/listeners/ItemProtectListener.java +++ b/src/de/epiceric/shopchest/listeners/ItemProtectListener.java @@ -1,10 +1,18 @@ package de.epiceric.shopchest.listeners; +import de.epiceric.shopchest.utils.ShopUtils; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.block.BlockState; import org.bukkit.entity.Item; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.block.BlockMultiPlaceEvent; +import org.bukkit.event.block.BlockPistonExtendEvent; +import org.bukkit.event.block.BlockPistonRetractEvent; +import org.bukkit.event.block.BlockPlaceEvent; import org.bukkit.event.entity.ItemDespawnEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.InventoryType; @@ -30,4 +38,46 @@ public class ItemProtectListener implements Listener { } } + @EventHandler(priority = EventPriority.HIGH) + public void onBlockPlace(BlockPlaceEvent e) { + Block b = e.getBlockPlaced(); + Block below = b.getRelative(BlockFace.DOWN); + + if (ShopUtils.isShop(below.getLocation())) e.setCancelled(true); + } + + @EventHandler(priority = EventPriority.HIGH) + public void onMultiBlockPlace(BlockMultiPlaceEvent e) { + for (BlockState blockState : e.getReplacedBlockStates()) { + Block below = blockState.getBlock().getRelative(BlockFace.DOWN); + if (ShopUtils.isShop(below.getLocation())) e.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGH) + public void onPistonExtend(BlockPistonExtendEvent e) { + // If the piston would only move itself + Block airAfterPiston = e.getBlock().getRelative(e.getDirection()); + Block belowAir = airAfterPiston.getRelative(BlockFace.DOWN); + if (ShopUtils.isShop(belowAir.getLocation())) { + e.setCancelled(true); + return; + } + + for (Block b : e.getBlocks()) { + Block newBlock = b.getRelative(e.getDirection()); + Block belowNewBlock = newBlock.getRelative(BlockFace.DOWN); + if (ShopUtils.isShop(belowNewBlock.getLocation())) e.setCancelled(true); + } + } + + @EventHandler(priority = EventPriority.HIGH) + public void onPistonRetract(BlockPistonRetractEvent e) { + for (Block b : e.getBlocks()) { + Block newBlock = b.getRelative(e.getDirection()); + Block belowNewBlock = newBlock.getRelative(BlockFace.DOWN); + if (ShopUtils.isShop(belowNewBlock.getLocation())) e.setCancelled(true); + } + } + } diff --git a/src/de/epiceric/shopchest/listeners/ShopInteractListener.java b/src/de/epiceric/shopchest/listeners/ShopInteractListener.java index 536b3fc..3334f4c 100644 --- a/src/de/epiceric/shopchest/listeners/ShopInteractListener.java +++ b/src/de/epiceric/shopchest/listeners/ShopInteractListener.java @@ -20,6 +20,7 @@ import org.bukkit.Bukkit; import org.bukkit.Location; import org.bukkit.Material; import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; import org.bukkit.block.Chest; import org.bukkit.enchantments.Enchantment; import org.bukkit.entity.Player; @@ -92,13 +93,17 @@ public class ShopInteractListener implements Listener { if (!ShopUtils.isShop(b.getLocation())) { - ClickType clickType = ClickType.getPlayerClickType(p); - ItemStack product = clickType.getProduct(); - double buyPrice = clickType.getBuyPrice(); - double sellPrice = clickType.getSellPrice(); - ShopType shopType = clickType.getShopType(); + if (b.getRelative(BlockFace.UP).getType() == Material.AIR) { + ClickType clickType = ClickType.getPlayerClickType(p); + ItemStack product = clickType.getProduct(); + double buyPrice = clickType.getBuyPrice(); + double sellPrice = clickType.getSellPrice(); + ShopType shopType = clickType.getShopType(); - create(p, b.getLocation(), product, buyPrice, sellPrice, shopType); + create(p, b.getLocation(), product, buyPrice, sellPrice, shopType); + } else { + p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_BLOCKED)); + } } else { p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_ALREADY_SHOP)); }