Allow configuring what containers can be used for shops (Fixes #175)

This commit is contained in:
Phoenix616 2018-10-23 19:04:18 +01:00
parent b5f77d67b5
commit fe85dafec5
24 changed files with 307 additions and 93 deletions

View File

@ -0,0 +1,22 @@
package com.Acrobot.Breeze.Configuration.Annotations;
import com.Acrobot.Breeze.Configuration.ValueParser;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* Annotation for a parser
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.FIELD)
public @interface Parser {
/**
* This option's comment
*
* @return Comment
*/
public String value();
}

View File

@ -7,8 +7,11 @@ import java.io.FileWriter;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import com.Acrobot.Breeze.Configuration.Annotations.Parser;
import org.bukkit.ChatColor;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
@ -21,6 +24,9 @@ import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace;
* @author Acrobot
*/
public class Configuration {
private static Map<String, ValueParser> parsers = new HashMap<>();
public static ValueParser DEFAULT_PARSER = new ValueParser();
/**
* Loads a YAML-formatted file into a class and modifies the file if some of class's fields are missing
*
@ -46,9 +52,9 @@ public class Configuration {
try {
if (config.isSet(path)) {
field.set(null, ValueParser.parseToJava(config.get(path)));
field.set(null, getParser(field).parseToJava(config.get(path)));
} else if (config.isSet(path.toLowerCase())) {
field.set(null, ValueParser.parseToJava(config.get(path.toLowerCase())));
field.set(null, getParser(field).parseToJava(config.get(path.toLowerCase())));
} else {
if (field.isAnnotationPresent(PrecededBySpace.class)) {
writer.newLine();
@ -99,7 +105,7 @@ public class Configuration {
* @return Parsed output
*/
public static String parseToConfig(Object value) {
return ValueParser.parseToYAML(value);
return DEFAULT_PARSER.parseToYAML(value);
}
/**
@ -111,4 +117,38 @@ public class Configuration {
public static String getColoured(String string) {
return ChatColor.translateAlternateColorCodes('&', string);
}
/**
* Register a parser
* @param name The name of the parser
* @param valueParser The parser itself
*/
public static void registerParser(String name, ValueParser valueParser) {
parsers.put(name.toLowerCase(), valueParser);
}
/**
* Get a registered parser
* @param name The name of the parser
* @return The parser or null if it doesn't exist
*/
public static ValueParser getParser(String name) {
return parsers.get(name.toLowerCase());
}
/**
* Get the parser that should be used for a field
* @param field The field
* @return The registered parser if the field has a Parser annotation or the default one
*/
public static ValueParser getParser(Field field) {
ValueParser parser = null;
if (field.isAnnotationPresent(Parser.class)) {
parser = Configuration.getParser(field.getAnnotation(Parser.class).value());
}
if (parser == null) {
parser = Configuration.DEFAULT_PARSER;
}
return parser;
}
}

View File

@ -1,6 +1,7 @@
package com.Acrobot.Breeze.Configuration;
import com.Acrobot.Breeze.Configuration.Annotations.ConfigurationComment;
import com.Acrobot.Breeze.Configuration.Annotations.Parser;
import java.lang.reflect.Field;
@ -21,8 +22,10 @@ public class FieldParser {
builder.append('#').append(field.getAnnotation(ConfigurationComment.class).value()).append('\n');
}
ValueParser parser = Configuration.getParser(field);
try {
builder.append(field.getName()).append(": ").append(ValueParser.parseToYAML(field.get(null)));
builder.append(field.getName()).append(": ").append(parser.parseToYAML(field.get(null)));
} catch (IllegalAccessException e) {
e.printStackTrace();
return "";

View File

@ -2,6 +2,7 @@ package com.Acrobot.Breeze.Configuration;
import org.bukkit.configuration.ConfigurationSection;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@ -16,12 +17,12 @@ public class ValueParser {
* @param object Object to parse
* @return YAML string
*/
public static String parseToYAML(Object object) {
public String parseToYAML(Object object) {
if (object instanceof Number || object instanceof Boolean) {
return String.valueOf(object);
} else if (object instanceof List) {
} else if (object instanceof Collection) {
StringBuilder sb = new StringBuilder();
for (Object o : (List) object) {
for (Object o : (Collection) object) {
sb.append("\n- ").append(parseToYAML(o));
}
return sb.toString();
@ -36,7 +37,7 @@ public class ValueParser {
* @param object Object to parse
* @return Java-compatible object
*/
public static Object parseToJava(Object object) {
public Object parseToJava(Object object) {
if (object instanceof ConfigurationSection) {
Map<String, List<String>> map = new HashMap<String, List<String>>();

View File

@ -1,15 +1,57 @@
package com.Acrobot.ChestShop.Configuration;
import com.Acrobot.Breeze.Configuration.Annotations.ConfigurationComment;
import com.Acrobot.Breeze.Configuration.Annotations.Parser;
import com.Acrobot.Breeze.Configuration.Annotations.PrecededBySpace;
import com.Acrobot.Breeze.Configuration.Configuration;
import com.Acrobot.Breeze.Configuration.ValueParser;
import com.Acrobot.ChestShop.ChestShop;
import org.bukkit.Material;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
/**
* @author Acrobot
*/
public class Properties {
static {
Configuration.registerParser("StringSet", new ValueParser(){
public Object parseToJava(Object object) {
if (object instanceof Collection) {
return new LinkedHashSet<>((Collection<String>) object);
}
return object;
}
});
Configuration.registerParser("MaterialSet", new ValueParser(){
public Object parseToJava(Object object) {
if (object instanceof Collection) {
EnumSet<Material> set = EnumSet.noneOf(Material.class);
for (Object o : (Collection) object) {
if (o instanceof Material) {
set.add((Material) o);
} else if (o instanceof String) {
try {
set.add(Material.getMaterial(((String) o).toUpperCase()));
} catch (IllegalArgumentException e) {
ChestShop.getBukkitLogger().log(Level.WARNING, o + " is not a valid Material name in the config!");
}
}
}
return set;
}
return object;
}
});
}
@ConfigurationComment("Do you want to turn off the automatic updates of ChestShop?")
public static boolean TURN_OFF_UPDATES = false;
@ -17,6 +59,14 @@ public class Properties {
@ConfigurationComment("How large should the internal caches be?")
public static int CACHE_SIZE = 1000;
@PrecededBySpace
@ConfigurationComment("What containers are allowed to hold a shop? (Only blocks with inventories work!)")
@Parser("MaterialSet")
public static Set<Material> SHOP_CONTAINERS = EnumSet.of(
Material.CHEST,
Material.TRAPPED_CHEST
);
@PrecededBySpace
@ConfigurationComment("(In 1/1000th of a second) How often can a player use the shop sign?")
public static int SHOP_INTERACTION_INTERVAL = 250;
@ -47,7 +97,8 @@ public class Properties {
public static boolean REMOVE_EMPTY_CHESTS = false;
@ConfigurationComment("A list of worlds in which to remove empty shops with the previous config. Case sensitive. An empty list means all worlds.")
public static List<String> REMOVE_EMPTY_WORLDS = Arrays.asList("world1", "world2");
@Parser("StringSet")
public static Set<String> REMOVE_EMPTY_WORLDS = new LinkedHashSet<>(Arrays.asList("world1", "world2"));
@PrecededBySpace
@ConfigurationComment("First line of your Admin Shop's sign should look like this:")

View File

@ -1,6 +1,7 @@
package com.Acrobot.ChestShop.Events;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
@ -20,12 +21,17 @@ public class ShopCreatedEvent extends Event {
private final Sign sign;
private final String[] signLines;
@Nullable private final Chest chest;
@Nullable private final Container container;
@Deprecated
public ShopCreatedEvent(Player creator, Sign sign, @Nullable Chest chest, String[] signLines) {
this(creator, sign, (Container) chest, signLines);
}
public ShopCreatedEvent(Player creator, Sign sign, @Nullable Container container, String[] signLines) {
this.creator = creator;
this.sign = sign;
this.chest = chest;
this.container = container;
this.signLines = signLines.clone();
}
@ -67,12 +73,20 @@ public class ShopCreatedEvent extends Event {
}
/**
* Returns the shop's chest (if applicable)
* Returns the shop's container (if applicable)
*
* @return Shop's chest
* @return Shop's container
*/
@Nullable public Container getContainer() {
return container;
}
/**
* @deprecated Use {@link #getContainer()}
*/
@Deprecated
@Nullable public Chest getChest() {
return chest;
return container instanceof Chest ? (Chest) container : null;
}
public HandlerList getHandlers() {

View File

@ -1,6 +1,7 @@
package com.Acrobot.ChestShop.Events;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
@ -19,12 +20,17 @@ public class ShopDestroyedEvent extends Event {
private final Player destroyer;
private final Sign sign;
private final Chest chest;
private final Container container;
@Deprecated
public ShopDestroyedEvent(@Nullable Player destroyer, Sign sign, @Nullable Chest chest) {
this(destroyer, sign, (Container) chest);
}
public ShopDestroyedEvent(@Nullable Player destroyer, Sign sign, @Nullable Container container) {
this.destroyer = destroyer;
this.sign = sign;
this.chest = chest;
this.container = container;
}
/**
@ -37,8 +43,16 @@ public class ShopDestroyedEvent extends Event {
/**
* @return Shop's chest
*/
@Nullable public Container getContainer() {
return container;
}
/**
* @deprecated Use {@link #getContainer()}
*/
@Deprecated
@Nullable public Chest getChest() {
return chest;
return container instanceof Chest ? (Chest) container : null;
}
/**

View File

@ -1,6 +1,5 @@
package com.Acrobot.ChestShop.Listeners.Block;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Security;
@ -21,10 +20,10 @@ public class BlockPlace implements Listener {
private static BlockFace[] SEARCH_DIRECTIONS = {BlockFace.EAST, BlockFace.WEST, BlockFace.NORTH, BlockFace.SOUTH, BlockFace.UP, BlockFace.DOWN};
@EventHandler(ignoreCancelled = true)
public static void onChestPlace(BlockPlaceEvent event) {
public static void onContainerPlace(BlockPlaceEvent event) {
Block placed = event.getBlockPlaced();
if (!BlockUtil.isChest(placed)) {
if (!uBlock.couldBeShopContainer(placed)) {
return;
}
@ -70,7 +69,7 @@ public class BlockPlace implements Listener {
for (BlockFace face : SEARCH_DIRECTIONS) {
Block relative = placed.getRelative(face);
if (!BlockUtil.isChest(relative)) {
if (!uBlock.couldBeShopContainer(relative)) {
continue;
}

View File

@ -1,10 +1,10 @@
package com.Acrobot.ChestShop.Listeners.Block.Break;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Listeners.Player.PlayerInteract;
import com.Acrobot.ChestShop.Plugins.ChestShop;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -18,7 +18,7 @@ import org.bukkit.event.entity.EntityExplodeEvent;
public class ChestBreak implements Listener {
@EventHandler(ignoreCancelled = true)
public static void onChestBreak(BlockBreakEvent event) {
if (!canChestBeBroken(event.getBlock(), event.getPlayer())) {
if (!canBeBroken(event.getBlock(), event.getPlayer())) {
event.setCancelled(true);
}
}
@ -30,18 +30,18 @@ public class ChestBreak implements Listener {
}
for (Block block : event.blockList()) {
if (!canChestBeBroken(block, null)) {
if (!canBeBroken(block, null)) {
event.setCancelled(true);
return;
}
}
}
private static boolean canChestBeBroken(Block chest, Player breaker) {
if (!BlockUtil.isChest(chest) || !Properties.USE_BUILT_IN_PROTECTION || !ChestShopSign.isShopChest(chest)) {
private static boolean canBeBroken(Block block, Player breaker) {
if (!uBlock.couldBeShopContainer(block) || !Properties.USE_BUILT_IN_PROTECTION || !ChestShopSign.isShopChest(block)) {
return true;
}
return breaker != null && (PlayerInteract.canOpenOtherShops(breaker) || ChestShop.canAccess(breaker, chest));
return breaker != null && (PlayerInteract.canOpenOtherShops(breaker) || ChestShop.canAccess(breaker, block));
}
}

View File

@ -10,7 +10,7 @@ import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
@ -159,13 +159,13 @@ public class SignBreak implements Listener {
}
private static void sendShopDestroyedEvent(Sign sign, Player player) {
Chest connectedChest = null;
Container connectedContainer = null;
if (!ChestShopSign.isAdminShop(sign)) {
connectedChest = uBlock.findConnectedChest(sign.getBlock());
connectedContainer = uBlock.findConnectedContainer(sign.getBlock());
}
Event event = new ShopDestroyedEvent(player, sign, connectedChest);
Event event = new ShopDestroyedEvent(player, sign, connectedContainer);
ChestShop.callEvent(event);
}

View File

@ -42,7 +42,7 @@ public class SignCreate implements Listener {
return;
}
ShopCreatedEvent postEvent = new ShopCreatedEvent(preEvent.getPlayer(), preEvent.getSign(), uBlock.findConnectedChest(preEvent.getSign()), preEvent.getSignLines());
ShopCreatedEvent postEvent = new ShopCreatedEvent(preEvent.getPlayer(), preEvent.getSign(), uBlock.findConnectedContainer(preEvent.getSign()), preEvent.getSignLines());
ChestShop.callEvent(postEvent);
}
}

View File

@ -80,7 +80,7 @@ public class VaultListener implements Listener {
double balance = 0;
//String lastSeen = NameManager.getLastSeenName(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
if (lastSeen != null) {
balance = provider.getBalance(lastSeen, event.getWorld().getName());
@ -101,7 +101,7 @@ public class VaultListener implements Listener {
World world = event.getWorld();
//String lastSeen = NameManager.getLastSeenName(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
if (lastSeen != null) {
if (provider.has(lastSeen, world.getName(), event.getDoubleAmount())) {
@ -120,7 +120,7 @@ public class VaultListener implements Listener {
World world = event.getWorld();
//String lastSeen = NameManager.getLastSeenName(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
event.hasAccount(lastSeen != null && provider.hasAccount(lastSeen, world.getName()));
}
@ -143,7 +143,7 @@ public class VaultListener implements Listener {
World world = event.getWorld();
//String lastSeen = NameManager.getLastSeenName(event.getTarget());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget());
if (lastSeen != null) {
EconomyResponse response = provider.depositPlayer(lastSeen, world.getName(), event.getDoubleAmount());
@ -159,7 +159,7 @@ public class VaultListener implements Listener {
World world = event.getWorld();
//String lastSeen = NameManager.getLastSeenName(event.getTarget());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getTarget());
if (lastSeen != null) {
EconomyResponse response = provider.withdrawPlayer(lastSeen, world.getName(), event.getDoubleAmount());
@ -193,7 +193,7 @@ public class VaultListener implements Listener {
}
//String lastSeen = NameManager.getLastSeenName(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
OfflinePlayer lastSeen = Bukkit.getOfflinePlayer(event.getAccount());
String world = event.getWorld().getName();
if (lastSeen == null) {

View File

@ -1,6 +1,5 @@
package com.Acrobot.ChestShop.Listeners.Item;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -14,11 +13,11 @@ public class ItemMoveListener implements Listener {
@EventHandler(ignoreCancelled = true, priority = EventPriority.HIGHEST)
public static void onItemMove(InventoryMoveItemEvent event) {
if (event.getSource() == null || !BlockUtil.isChest(event.getSource().getHolder())) {
if (event.getSource() == null) {
return;
}
if (!ChestShopSign.isShopChest(event.getSource().getHolder())) {
if (!ChestShopSign.isShopBlock(event.getSource().getHolder())) {
return;
}

View File

@ -19,7 +19,7 @@ import org.bukkit.ChatColor;
import org.bukkit.GameMode;
import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
@ -31,7 +31,6 @@ import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import static com.Acrobot.Breeze.Utils.BlockUtil.isChest;
import static com.Acrobot.Breeze.Utils.BlockUtil.isSign;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType;
import static com.Acrobot.ChestShop.Events.TransactionEvent.TransactionType.BUY;
@ -54,7 +53,7 @@ public class PlayerInteract implements Listener {
Action action = event.getAction();
Player player = event.getPlayer();
if (Properties.USE_BUILT_IN_PROTECTION && isChest(block)) {
if (Properties.USE_BUILT_IN_PROTECTION && isShopBlock(block)) {
if (Properties.TURN_OFF_DEFAULT_PROTECTION_WHEN_PROTECTED_EXTERNALLY) {
return;
}
@ -161,8 +160,8 @@ public class PlayerInteract implements Listener {
Action buy = Properties.REVERSE_BUTTONS ? LEFT_CLICK_BLOCK : RIGHT_CLICK_BLOCK;
double price = (action == buy ? PriceUtil.getBuyPrice(prices) : PriceUtil.getSellPrice(prices));
Chest chest = uBlock.findConnectedChest(sign);
Inventory ownerInventory = (adminShop ? new AdminInventory() : chest != null ? chest.getInventory() : null);
Container shopBlock = uBlock.findConnectedContainer(sign);
Inventory ownerInventory = (adminShop ? new AdminInventory() : shopBlock != null ? shopBlock.getInventory() : null);
ItemStack item = MaterialUtil.getItem(material);
if (item == null || !NumberUtil.isInteger(quantity)) {
@ -218,9 +217,9 @@ public class PlayerInteract implements Listener {
}
private static void showChestGUI(Player player, Block signBlock) {
Chest chest = uBlock.findConnectedChest(signBlock);
Container container = uBlock.findConnectedContainer(signBlock);
if (chest == null) {
if (container == null) {
player.sendMessage(Messages.prefix(Messages.NO_CHEST_DETECTED));
return;
}
@ -229,6 +228,6 @@ public class PlayerInteract implements Listener {
return;
}
BlockUtil.openBlockGUI(chest, player);
BlockUtil.openBlockGUI(container, player);
}
}

View File

@ -1,6 +1,5 @@
package com.Acrobot.ChestShop.Listeners.PostShopCreation;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Events.ShopCreatedEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
@ -38,16 +37,16 @@ public class SignSticker implements Listener {
return;
}
BlockFace chestFace = null;
BlockFace shopBlockFace = null;
for (BlockFace face : uBlock.CHEST_EXTENSION_FACES) {
if (BlockUtil.isChest(signBlock.getRelative(face))) {
chestFace = face;
if (uBlock.couldBeShopContainer(signBlock.getRelative(face))) {
shopBlockFace = face;
break;
}
}
if (chestFace == null) {
if (shopBlockFace == null) {
return;
}
@ -57,7 +56,7 @@ public class SignSticker implements Listener {
org.bukkit.block.Sign sign = (org.bukkit.block.Sign) signBlock.getState();
Sign signMaterial = (Sign) Bukkit.createBlockData(Material.WALL_SIGN);
signMaterial.setRotation(chestFace.getOppositeFace());
signMaterial.setRotation(shopBlockFace.getOppositeFace());
sign.setBlockData(signMaterial);
for (int i = 0; i < lines.length; ++i) {

View File

@ -8,7 +8,7 @@ import com.Acrobot.ChestShop.Events.TransactionEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Material;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -29,7 +29,7 @@ public class EmptyShopDeleter implements Listener {
Inventory ownerInventory = event.getOwnerInventory();
Sign sign = event.getSign();
Chest connectedChest = uBlock.findConnectedChest(sign);
Container connectedContainer = uBlock.findConnectedContainer(sign);
if (!shopShouldBeRemoved(ownerInventory, event.getStock())) {
return;
@ -39,13 +39,13 @@ public class EmptyShopDeleter implements Listener {
return;
}
ShopDestroyedEvent destroyedEvent = new ShopDestroyedEvent(null, event.getSign(), connectedChest);
ShopDestroyedEvent destroyedEvent = new ShopDestroyedEvent(null, event.getSign(), connectedContainer);
ChestShop.callEvent(destroyedEvent);
sign.getBlock().setType(Material.AIR);
if (Properties.REMOVE_EMPTY_CHESTS && !ChestShopSign.isAdminShop(ownerInventory) && InventoryUtil.isEmpty(ownerInventory)) {
connectedChest.getBlock().setType(Material.AIR);
connectedContainer.getBlock().setType(Material.AIR);
} else {
ownerInventory.addItem(new ItemStack(Material.SIGN, 1));
}

View File

@ -5,7 +5,7 @@ import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
@ -29,9 +29,9 @@ public class ChestChecker implements Listener {
return;
}
Chest connectedChest = uBlock.findConnectedChest(event.getSign().getBlock());
Container connectedContainer = uBlock.findConnectedContainer(event.getSign().getBlock());
if (connectedChest == null) {
if (connectedContainer == null) {
event.setOutcome(NO_CHEST);
return;
}
@ -42,7 +42,7 @@ public class ChestChecker implements Listener {
return;
}
if (!Security.canAccess(player, connectedChest.getBlock())) {
if (!Security.canAccess(player, connectedContainer.getBlock())) {
event.setOutcome(NO_PERMISSION_FOR_CHEST);
}
}

View File

@ -8,7 +8,7 @@ import com.Acrobot.ChestShop.Events.PreShopCreationEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.ChatColor;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.event.EventHandler;
import org.bukkit.event.EventPriority;
import org.bukkit.event.Listener;
@ -34,9 +34,9 @@ public class ItemChecker implements Listener {
if (item == null) {
if (Properties.ALLOW_AUTO_ITEM_FILL && itemCode.equals(AUTOFILL_CODE)) {
Chest chest = uBlock.findConnectedChest(event.getSign());
if (chest != null) {
for (ItemStack stack : chest.getInventory().getContents()) {
Container container = uBlock.findConnectedContainer(event.getSign());
if (container != null) {
for (ItemStack stack : container.getInventory().getContents()) {
if (!MaterialUtil.isEmpty(stack)) {
item = stack;
break;

View File

@ -8,7 +8,7 @@ import com.Acrobot.ChestShop.Security;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.Location;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -41,10 +41,10 @@ public class TerrainChecker implements Listener {
return;
}
Chest connectedChest = uBlock.findConnectedChest(event.getSign().getBlock());
Location chestLocation = (connectedChest != null ? connectedChest.getLocation() : null);
Container connectedContainer = uBlock.findConnectedContainer(event.getSign().getBlock());
Location containerLocation = (connectedContainer != null ? connectedContainer.getLocation() : null);
BuildPermissionEvent bEvent = new BuildPermissionEvent(player, chestLocation, event.getSign().getLocation());
BuildPermissionEvent bEvent = new BuildPermissionEvent(player, containerLocation, event.getSign().getLocation());
ChestShop.callEvent(bEvent);
if (!bEvent.isAllowed()) {

View File

@ -6,14 +6,12 @@ import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import static com.Acrobot.Breeze.Utils.BlockUtil.isChest;
import static com.Acrobot.Breeze.Utils.BlockUtil.isSign;
/**
@ -51,8 +49,8 @@ public class ChestShop implements Listener {
}
}
if (isChest(block)) {
Sign sign = uBlock.getConnectedSign((Chest) block.getState());
if (uBlock.couldBeShopContainer(block)) {
Sign sign = uBlock.getConnectedSign(block);
if (sign != null && !isShopMember(player, sign)) {
return false;
@ -63,7 +61,7 @@ public class ChestShop implements Listener {
}
private static boolean canBeProtected(Block block) {
return isSign(block) || isChest(block);
return isSign(block) || uBlock.couldBeShopContainer(block);
}
private static boolean isShopMember(Player player, Sign sign) {

View File

@ -12,6 +12,7 @@ import com.griefcraft.model.Protection;
import com.griefcraft.scripting.event.LWCProtectionRegisterEvent;
import org.bukkit.block.Block;
import org.bukkit.block.Chest;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.entity.Player;
import org.bukkit.event.Event;
@ -32,7 +33,7 @@ public class LightweightChestProtection implements Listener {
public static void onShopCreation(ShopCreatedEvent event) {
Player player = event.getPlayer();
Sign sign = event.getSign();
Chest connectedChest = event.getChest();
Container connectedContainer = event.getContainer();
if (Properties.PROTECT_SIGN_WITH_LWC) {
if (!Security.protect(player, sign.getBlock())) {
@ -40,7 +41,7 @@ public class LightweightChestProtection implements Listener {
}
}
if (Properties.PROTECT_CHEST_WITH_LWC && connectedChest != null && Security.protect(player, connectedChest.getBlock())) {
if (Properties.PROTECT_CHEST_WITH_LWC && connectedContainer != null && Security.protect(player, connectedContainer.getBlock())) {
player.sendMessage(Messages.prefix(Messages.PROTECTED_SHOP));
}
}
@ -114,11 +115,11 @@ public class LightweightChestProtection implements Listener {
signProtection.remove();
}
if (event.getChest() == null || !Properties.REMOVE_LWC_PROTECTION_AUTOMATICALLY) {
if (event.getContainer() == null || !Properties.REMOVE_LWC_PROTECTION_AUTOMATICALLY) {
return;
}
Protection chestProtection = lwc.findProtection(event.getChest().getBlock());
Protection chestProtection = lwc.findProtection(event.getContainer().getBlock());
if (chestProtection != null) {
chestProtection.remove();

View File

@ -1,13 +1,13 @@
package com.Acrobot.ChestShop;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.Breeze.Utils.NameUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Database.Account;
import com.Acrobot.ChestShop.Events.Protection.ProtectBlockEvent;
import com.Acrobot.ChestShop.Events.Protection.ProtectionCheckEvent;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.Sign;
@ -53,7 +53,7 @@ public class Security {
for (BlockFace face : BLOCKS_AROUND) {
Block block = sign.getRelative(face);
if (!BlockUtil.isChest(block)) {
if (!uBlock.couldBeShopContainer(block)) {
continue;
}
if (!canAccess(player, block)) {

View File

@ -7,6 +7,7 @@ import com.Acrobot.ChestShop.Containers.AdminInventory;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import com.Acrobot.ChestShop.Utils.uBlock;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.block.Chest;
import org.bukkit.block.DoubleChest;
import org.bukkit.block.Sign;
@ -58,6 +59,10 @@ public class ChestShopSign {
return BlockUtil.isSign(sign) && isValid((Sign) sign.getState());
}
/**
* @deprecated Use {@link #isShopBlock(Block}
*/
@Deprecated
public static boolean isShopChest(Block chest) {
if (!BlockUtil.isChest(chest)) {
return false;
@ -66,6 +71,18 @@ public class ChestShopSign {
return uBlock.getConnectedSign((Chest) chest.getState()) != null;
}
public static boolean isShopBlock(Block block) {
if (!uBlock.couldBeShopContainer(block)) {
return false;
}
return uBlock.getConnectedSign(block) != null;
}
/**
* @deprecated Use {@link #isShopBlock(InventoryHolder}
*/
@Deprecated
public static boolean isShopChest(InventoryHolder holder) {
if (!BlockUtil.isChest(holder)) {
return false;
@ -80,6 +97,10 @@ public class ChestShopSign {
}
}
public static boolean isShopBlock(InventoryHolder holder) {
return holder instanceof BlockState && isShopBlock(((BlockState) holder).getBlock());
}
public static boolean canAccess(Player player, Sign sign) {
if (player == null) return false;
if (sign == null) return true;

View File

@ -1,12 +1,16 @@
package com.Acrobot.ChestShop.Utils;
import com.Acrobot.Breeze.Utils.BlockUtil;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Signs.ChestShopSign;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.BlockState;
import org.bukkit.block.Container;
import org.bukkit.block.Sign;
import org.bukkit.block.data.BlockData;
import org.bukkit.block.data.type.Chest;
import org.bukkit.inventory.InventoryHolder;
import org.bukkit.material.Attachable;
/**
@ -18,17 +22,8 @@ public class uBlock {
@Deprecated
public static final BlockFace[] NEIGHBOR_FACES = {BlockFace.EAST, BlockFace.NORTH, BlockFace.WEST, BlockFace.SOUTH};
public static Sign getConnectedSign(org.bukkit.block.Chest chest) {
Sign sign = uBlock.findAnyNearbyShopSign(chest.getBlock());
if (sign == null) {
Block neighbor = findNeighbor(chest.getBlock());
if (neighbor != null) {
sign = uBlock.findAnyNearbyShopSign(neighbor);
}
}
return sign;
public static Sign getConnectedSign(BlockState blockState) {
return getConnectedSign(blockState.getBlock());
}
public static Sign getConnectedSign(Block block) {
@ -44,6 +39,10 @@ public class uBlock {
return sign;
}
/**
* @deprecated Use {@link #findConnectedContainer(Sign)}
*/
@Deprecated
public static org.bukkit.block.Chest findConnectedChest(Sign sign) {
BlockFace signFace = null;
if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) {
@ -52,6 +51,10 @@ public class uBlock {
return findConnectedChest(sign.getBlock(), signFace);
}
/**
* @deprecated Use {@link #findConnectedContainer(Block)}
*/
@Deprecated
public static org.bukkit.block.Chest findConnectedChest(Block block) {
BlockFace signFace = null;
if (BlockUtil.isSign(block)) {
@ -63,6 +66,10 @@ public class uBlock {
return findConnectedChest(block, signFace);
}
/**
* @deprecated Use {@link #findConnectedContainer(Block, BlockFace)}
*/
@Deprecated
private static org.bukkit.block.Chest findConnectedChest(Block block, BlockFace signFace) {
if (signFace != null) {
Block faceBlock = block.getRelative(signFace);
@ -82,6 +89,44 @@ public class uBlock {
return null;
}
public static Container findConnectedContainer(Sign sign) {
BlockFace signFace = null;
if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) {
signFace = ((Attachable) sign.getData()).getAttachedFace();
}
return findConnectedContainer(sign.getBlock(), signFace);
}
public static Container findConnectedContainer(Block block) {
BlockFace signFace = null;
if (BlockUtil.isSign(block)) {
Sign sign = (Sign) block.getState();
if (((org.bukkit.material.Sign) sign.getData()).isWallSign()) {
signFace = ((Attachable) sign.getData()).getAttachedFace();
}
}
return findConnectedContainer(block, signFace);
}
private static Container findConnectedContainer(Block block, BlockFace signFace) {
if (signFace != null) {
Block faceBlock = block.getRelative(signFace);
if (uBlock.couldBeShopContainer(faceBlock)) {
return (Container) faceBlock.getState();
}
}
for (BlockFace bf : SHOP_FACES) {
if (bf != signFace) {
Block faceBlock = block.getRelative(bf);
if (uBlock.couldBeShopContainer(faceBlock)) {
return (Container) faceBlock.getState();
}
}
}
return null;
}
public static Sign findValidShopSign(Block block, String originalName) {
Sign ownerShopSign = null;
@ -167,4 +212,12 @@ public class uBlock {
private static boolean signIsAttachedToBlock(Sign sign, Block block) {
return sign.getBlock().equals(block) || BlockUtil.getAttachedBlock(sign).equals(block);
}
public static boolean couldBeShopContainer(Block block) {
return block != null && Properties.SHOP_CONTAINERS.contains(block.getType());
}
public static boolean couldBeShopContainer(InventoryHolder holder) {
return holder instanceof Container && couldBeShopContainer(((Container) holder).getBlock());
}
}