Prevent blocks from being placed or pushed above the chest

Fixes #6
This commit is contained in:
Eric 2016-07-01 16:13:44 +02:00
parent 57c6955d6a
commit 8bef79d47f
6 changed files with 89 additions and 17 deletions

View File

@ -1,5 +1,7 @@
message.shop-created=&6Shop erstellt. message.shop-created=&6Shop erstellt.
message.chest-already-shop=&cTruhe ist bereits ein Shop. 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.shop-removed=&6Shop entfernt.
message.chest-no-shop=&cTruhe ist kein Shop. 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. message.shop-create-not-enough-money=&cNicht genug Geld. Du brauchst &6%CREATION-PRICE% &cum einen Shop zu erstellen.

View File

@ -4,6 +4,13 @@ message.shop-created=&6Shop created.
# Set the message when the clicked chest already is a shop. # Set the message when the clicked chest already is a shop.
message.chest-already-shop=&cChest already is 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. # Set the message when the clicked shop is removed.
message.shop-removed=&6Shop removed. message.shop-removed=&6Shop removed.

View File

@ -44,6 +44,8 @@ public class LocalizedMessage {
public enum Message { public enum Message {
SHOP_CREATED, SHOP_CREATED,
CHEST_ALREADY_SHOP, CHEST_ALREADY_SHOP,
CHEST_BLOCKED,
DOUBLE_CHEST_BLOCKED,
SHOP_REMOVED, SHOP_REMOVED,
CHEST_NO_SHOP, CHEST_NO_SHOP,
SHOP_CREATE_NOT_ENOUGH_MONEY, SHOP_CREATE_NOT_ENOUGH_MONEY,

View File

@ -8,6 +8,7 @@ import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.ShopUtils; import de.epiceric.shopchest.utils.ShopUtils;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest; import org.bukkit.block.DoubleChest;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
@ -71,6 +72,7 @@ public class ChestProtectListener implements Listener {
Chest l = (Chest) dc.getLeftSide(); Chest l = (Chest) dc.getLeftSide();
if (ShopUtils.isShop(r.getLocation()) || ShopUtils.isShop(l.getLocation())) { if (ShopUtils.isShop(r.getLocation()) || ShopUtils.isShop(l.getLocation())) {
if (b.getRelative(BlockFace.UP).getType() == Material.AIR) {
Shop shop; Shop shop;
if (b.getLocation().equals(r.getLocation())) { if (b.getLocation().equals(r.getLocation())) {
@ -85,6 +87,10 @@ public class ChestProtectListener implements Listener {
Shop newShop = new Shop(shop.getID(), ShopChest.getInstance(), shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType()); Shop newShop = new Shop(shop.getID(), ShopChest.getInstance(), shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType());
ShopUtils.addShop(newShop, true); ShopUtils.addShop(newShop, true);
} else {
e.setCancelled(true);
e.getPlayer().sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_BLOCKED));
}
} }
} }

View File

@ -1,10 +1,18 @@
package de.epiceric.shopchest.listeners; 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.entity.Item;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority; import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener; 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.entity.ItemDespawnEvent;
import org.bukkit.event.inventory.InventoryPickupItemEvent; import org.bukkit.event.inventory.InventoryPickupItemEvent;
import org.bukkit.event.inventory.InventoryType; 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);
}
}
} }

View File

@ -20,6 +20,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.block.Block; import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest; import org.bukkit.block.Chest;
import org.bukkit.enchantments.Enchantment; import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
@ -92,6 +93,7 @@ public class ShopInteractListener implements Listener {
if (!ShopUtils.isShop(b.getLocation())) { if (!ShopUtils.isShop(b.getLocation())) {
if (b.getRelative(BlockFace.UP).getType() == Material.AIR) {
ClickType clickType = ClickType.getPlayerClickType(p); ClickType clickType = ClickType.getPlayerClickType(p);
ItemStack product = clickType.getProduct(); ItemStack product = clickType.getProduct();
double buyPrice = clickType.getBuyPrice(); double buyPrice = clickType.getBuyPrice();
@ -99,6 +101,9 @@ public class ShopInteractListener implements Listener {
ShopType shopType = clickType.getShopType(); 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 { } else {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_ALREADY_SHOP)); p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.CHEST_ALREADY_SHOP));
} }