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.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.

View File

@ -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.

View File

@ -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,

View File

@ -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);
}
}

View File

@ -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);
}
}
}

View File

@ -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));
}