1
0
mirror of https://e.coding.net/circlecloud/QuickShop.git synced 2024-11-22 01:58:54 +00:00

修复满背包依然可以购买的Bug

玩家背包getContent获取的内容包括了防具栏等
也就是说,即便背包是满的,只要没穿防具就依然可以购买物品
但是因为没有储存空间,导致花了钱,也无法获得该物品
This commit is contained in:
17jiong 2018-01-31 18:49:19 +08:00
parent 4a18053026
commit 2b82a8430d

View File

@ -18,7 +18,9 @@ import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.Inventory; import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.InventoryHolder; import org.bukkit.inventory.InventoryHolder;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.BookMeta;
import org.bukkit.inventory.meta.EnchantmentStorageMeta; import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.material.MaterialData; import org.bukkit.material.MaterialData;
import org.bukkit.material.Sign; import org.bukkit.material.Sign;
import org.maxgamer.QuickShop.QuickShop; import org.maxgamer.QuickShop.QuickShop;
@ -27,454 +29,467 @@ import pw.yumc.YumCore.global.L10N;
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class Util { public class Util {
private static HashSet<Material> blacklist = new HashSet<>(); private static HashSet<Material> blacklist = new HashSet<>();
private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##"); private static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("#.##");
private static QuickShop plugin; private static QuickShop plugin;
private static HashSet<Material> shoppables = new HashSet<>(); private static HashSet<Material> shoppables = new HashSet<>();
private static HashSet<Material> tools = new HashSet<>(); private static HashSet<Material> tools = new HashSet<>();
private static HashSet<Material> transparent = new HashSet<>(); private static HashSet<Material> transparent = new HashSet<>();
public static void addTransparentBlock(final Material m) { public static void addTransparentBlock(final Material m) {
if (!transparent.add(m)) { if (!transparent.add(m)) {
System.out.println("已添加透明方块: " + m.toString()); System.out.println("已添加透明方块: " + m.toString());
} }
if (!m.isBlock()) { if (!m.isBlock()) {
System.out.println(m + " 不是一个方块!"); System.out.println(m + " 不是一个方块!");
} }
} }
/** /**
* Returns true if the given block could be used to make a shop out of. * Returns true if the given block could be used to make a shop out of.
* *
* @param b * @param b
* The block to check. Possibly a chest, dispenser, etc. * The block to check. Possibly a chest, dispenser, etc.
* @return True if it can be made into a shop, otherwise false. * @return True if it can be made into a shop, otherwise false.
*/ */
public static boolean canBeShop(final Block b) { public static boolean canBeShop(final Block b) {
try { try {
final BlockState bs = b.getState(); final BlockState bs = b.getState();
return bs instanceof InventoryHolder && shoppables.contains(bs.getType()); return bs instanceof InventoryHolder && shoppables.contains(bs.getType());
} catch (final Exception e) { } catch (final Exception e) {
return false; return false;
} }
} }
/** /**
* Counts the number of items in the given inventory where * Counts the number of items in the given inventory where
* Util.matches(inventory item, item) is true. * Util.matches(inventory item, item) is true.
* *
* @param inv * @param inv
* The inventory to search * The inventory to search
* @param item * @param item
* The ItemStack to search for * The ItemStack to search for
* @return The number of items that match in this inventory. * @return The number of items that match in this inventory.
*/ */
public static int countItems(final Inventory inv, final ItemStack item) { public static int countItems(final Inventory inv, final ItemStack item) {
int items = 0; int items = 0;
for (final ItemStack iStack : inv.getContents()) { for (final ItemStack iStack : inv.getStorageContents()) {
if (iStack == null) { if (iStack == null) {
continue; continue;
} }
if (Util.matches(item, iStack)) { if (Util.matches(item, iStack)) {
items += iStack.getAmount(); items += iStack.getAmount();
} }
} }
return items; return items;
} }
/** /**
* Returns the number of items that can be given to the inventory safely. * Returns the number of items that can be given to the inventory safely.
* *
* @param inv * @param inv
* The inventory to count * The inventory to count
* @param item * @param item
* The item prototype. Material, durabiltiy and enchants must * The item prototype. Material, durabiltiy and enchants must
* match for 'stackability' to occur. * match for 'stackability' to occur.
* @return The number of items that can be given to the inventory safely. * @return The number of items that can be given to the inventory safely.
*/ */
public static int countSpace(final Inventory inv, final ItemStack item) { public static int countSpace(final Inventory inv, final ItemStack item) {
int space = 0; int space = 0;
for (final ItemStack iStack : inv.getContents()) { for (final ItemStack iStack : inv.getStorageContents()) {
if (iStack == null || iStack.getType() == Material.AIR) { if (iStack == null || iStack.getType() == Material.AIR) {
space += item.getMaxStackSize(); space += item.getMaxStackSize();
} else if (matches(item, iStack)) { } else if (matches(item, iStack)) {
space += item.getMaxStackSize() - iStack.getAmount(); space += item.getMaxStackSize() - iStack.getAmount();
} }
} }
return space; return space;
} }
public static ItemStack deserialize(final String config) throws InvalidConfigurationException { public static ItemStack deserialize(final String config) throws InvalidConfigurationException {
final YamlConfiguration cfg = new YamlConfiguration(); final YamlConfiguration cfg = new YamlConfiguration();
cfg.loadFromString(config); cfg.loadFromString(config);
return cfg.getItemStack("item"); return cfg.getItemStack("item");
} }
public static String firstUppercase(final String string) { public static String firstUppercase(final String string) {
if (string.length() > 1) { return Character.toUpperCase(string.charAt(0)) + string.substring(1).toLowerCase(); } if (string.length() > 1) {
return string.toUpperCase(); return Character.toUpperCase(string.charAt(0)) + string.substring(1).toLowerCase();
} }
return string.toUpperCase();
}
/** /**
* Formats the given number according to how vault would like it. E.g. $50 * Formats the given number according to how vault would like it. E.g. $50
* or 5 dollars. * or 5 dollars.
* *
* @return The formatted string. * @return The formatted string.
*/ */
public static String format(final double n) { public static String format(final double n) {
try { try {
return DECIMAL_FORMAT.format(n) + plugin.getEcon().currencyNamePlural(); return DECIMAL_FORMAT.format(n) + plugin.getEcon().currencyNamePlural();
} catch (final NumberFormatException e) { } catch (final NumberFormatException e) {
return n + ""; return n + "";
} }
} }
/** /**
* Fetches the block which the given sign is attached to * Fetches the block which the given sign is attached to
* *
* @param b * @param b
* The sign which is attached * The sign which is attached
* @return The block the sign is attached to * @return The block the sign is attached to
*/ */
public static Block getAttached(final Block b) { public static Block getAttached(final Block b) {
try { try {
final Sign sign = (Sign) b.getState().getData(); // Throws a NPE final Sign sign = (Sign) b.getState().getData(); // Throws a NPE
// sometimes?? // sometimes??
final BlockFace attached = sign.getAttachedFace(); final BlockFace attached = sign.getAttachedFace();
return attached == null ? null : b.getRelative(attached); return attached == null ? null : b.getRelative(attached);
} catch (final NullPointerException e) { } catch (final NullPointerException e) {
return null; // /Not sure what causes this. return null; // /Not sure what causes this.
} }
} }
/** /**
* Fetches an ItemStack's name - For example, converting INK_SAC:11 to * Fetches an ItemStack's name - For example, converting INK_SAC:11 to
* Dandellion Yellow, or WOOL:14 to Red Wool * Dandellion Yellow, or WOOL:14 to Red Wool
* *
* @param i * @param i
* The itemstack to fetch the name of * The itemstack to fetch the name of
* @return The human readable item name. * @return The human readable item name.
*/ */
public static String getName(final ItemStack i) { public static String getName(final ItemStack i) {
return L10N.getItemName(i); return L10N.getItemName(i);
} }
// Let's make very long names shorter for our sign // Let's make very long names shorter for our sign
public static String getNameForSign(final ItemStack itemStack) { public static String getNameForSign(final ItemStack itemStack) {
String name = getName(itemStack); String name = getName(itemStack);
if (name.length() > 16) { if (name.length() > 16) {
name = name.substring(0, 16); name = name.substring(0, 16);
} }
return name; return name;
} }
/** /**
* Returns the chest attached to the given chest. The given block must be a * Returns the chest attached to the given chest. The given block must be a
* chest. * chest.
* *
* @param b * @param b
* The chest to check. * The chest to check.
* @return the block which is also a chest and connected to b. * @return the block which is also a chest and connected to b.
*/ */
public static Block getSecondHalf(final Block b) { public static Block getSecondHalf(final Block b) {
if (!b.getType().toString().contains("CHEST")) { return null; } if (!b.getType().toString().contains("CHEST")) {
final Block[] blocks = new Block[4]; return null;
blocks[0] = b.getRelative(1, 0, 0); }
blocks[1] = b.getRelative(-1, 0, 0); final Block[] blocks = new Block[4];
blocks[2] = b.getRelative(0, 0, 1); blocks[0] = b.getRelative(1, 0, 0);
blocks[3] = b.getRelative(0, 0, -1); blocks[1] = b.getRelative(-1, 0, 0);
for (final Block c : blocks) { blocks[2] = b.getRelative(0, 0, 1);
if (c.getType() == b.getType()) { return c; } blocks[3] = b.getRelative(0, 0, -1);
} for (final Block c : blocks) {
return null; if (c.getType() == b.getType()) {
} return c;
}
}
return null;
}
/** /**
* Gets the percentage (Without trailing %) damage on a tool. * Gets the percentage (Without trailing %) damage on a tool.
* *
* @param item * @param item
* The ItemStack of tools to check * The ItemStack of tools to check
* @return The percentage 'health' the tool has. (Opposite of total damage) * @return The percentage 'health' the tool has. (Opposite of total damage)
*/ */
public static String getToolPercentage(final ItemStack item) { public static String getToolPercentage(final ItemStack item) {
final double dura = item.getDurability(); final double dura = item.getDurability();
final double max = item.getType().getMaxDurability(); final double max = item.getType().getMaxDurability();
return String.format("%.2f%%(剩余耐久%s/总耐久%s)", (1 - dura / max) * 100.0, max - dura, max); return String.format("%.2f%%(剩余耐久%s/总耐久%s)", (1 - dura / max) * 100.0, max - dura, max);
} }
public static void initialize() { public static void initialize() {
tools.clear(); tools.clear();
blacklist.clear(); blacklist.clear();
shoppables.clear(); shoppables.clear();
transparent.clear(); transparent.clear();
plugin = QuickShop.instance; plugin = QuickShop.instance;
for (final String s : plugin.getConfig().getStringList("shop-blocks")) { for (final String s : plugin.getConfig().getStringList("shop-blocks")) {
Material mat = Material.getMaterial(s.toUpperCase()); Material mat = Material.getMaterial(s.toUpperCase());
if (mat == null) { if (mat == null) {
try { try {
mat = Material.getMaterial(Integer.parseInt(s)); mat = Material.getMaterial(Integer.parseInt(s));
} catch (final NumberFormatException ignored) { } catch (final NumberFormatException ignored) {
} }
} }
if (mat == null) { if (mat == null) {
plugin.getLogger().info("Invalid shop-block: " + s); plugin.getLogger().info("Invalid shop-block: " + s);
} else { } else {
shoppables.add(mat); shoppables.add(mat);
} }
} }
tools.add(Material.BOW); tools.add(Material.BOW);
tools.add(Material.SHEARS); tools.add(Material.SHEARS);
tools.add(Material.FISHING_ROD); tools.add(Material.FISHING_ROD);
tools.add(Material.FLINT_AND_STEEL); tools.add(Material.FLINT_AND_STEEL);
tools.add(Material.CHAINMAIL_BOOTS); tools.add(Material.CHAINMAIL_BOOTS);
tools.add(Material.CHAINMAIL_CHESTPLATE); tools.add(Material.CHAINMAIL_CHESTPLATE);
tools.add(Material.CHAINMAIL_HELMET); tools.add(Material.CHAINMAIL_HELMET);
tools.add(Material.CHAINMAIL_LEGGINGS); tools.add(Material.CHAINMAIL_LEGGINGS);
tools.add(Material.WOOD_AXE); tools.add(Material.WOOD_AXE);
tools.add(Material.WOOD_HOE); tools.add(Material.WOOD_HOE);
tools.add(Material.WOOD_PICKAXE); tools.add(Material.WOOD_PICKAXE);
tools.add(Material.WOOD_SPADE); tools.add(Material.WOOD_SPADE);
tools.add(Material.WOOD_SWORD); tools.add(Material.WOOD_SWORD);
tools.add(Material.LEATHER_BOOTS); tools.add(Material.LEATHER_BOOTS);
tools.add(Material.LEATHER_CHESTPLATE); tools.add(Material.LEATHER_CHESTPLATE);
tools.add(Material.LEATHER_HELMET); tools.add(Material.LEATHER_HELMET);
tools.add(Material.LEATHER_LEGGINGS); tools.add(Material.LEATHER_LEGGINGS);
tools.add(Material.DIAMOND_AXE); tools.add(Material.DIAMOND_AXE);
tools.add(Material.DIAMOND_HOE); tools.add(Material.DIAMOND_HOE);
tools.add(Material.DIAMOND_PICKAXE); tools.add(Material.DIAMOND_PICKAXE);
tools.add(Material.DIAMOND_SPADE); tools.add(Material.DIAMOND_SPADE);
tools.add(Material.DIAMOND_SWORD); tools.add(Material.DIAMOND_SWORD);
tools.add(Material.DIAMOND_BOOTS); tools.add(Material.DIAMOND_BOOTS);
tools.add(Material.DIAMOND_CHESTPLATE); tools.add(Material.DIAMOND_CHESTPLATE);
tools.add(Material.DIAMOND_HELMET); tools.add(Material.DIAMOND_HELMET);
tools.add(Material.DIAMOND_LEGGINGS); tools.add(Material.DIAMOND_LEGGINGS);
tools.add(Material.STONE_AXE); tools.add(Material.STONE_AXE);
tools.add(Material.STONE_HOE); tools.add(Material.STONE_HOE);
tools.add(Material.STONE_PICKAXE); tools.add(Material.STONE_PICKAXE);
tools.add(Material.STONE_SPADE); tools.add(Material.STONE_SPADE);
tools.add(Material.STONE_SWORD); tools.add(Material.STONE_SWORD);
tools.add(Material.GOLD_AXE); tools.add(Material.GOLD_AXE);
tools.add(Material.GOLD_HOE); tools.add(Material.GOLD_HOE);
tools.add(Material.GOLD_PICKAXE); tools.add(Material.GOLD_PICKAXE);
tools.add(Material.GOLD_SPADE); tools.add(Material.GOLD_SPADE);
tools.add(Material.GOLD_SWORD); tools.add(Material.GOLD_SWORD);
tools.add(Material.GOLD_BOOTS); tools.add(Material.GOLD_BOOTS);
tools.add(Material.GOLD_CHESTPLATE); tools.add(Material.GOLD_CHESTPLATE);
tools.add(Material.GOLD_HELMET); tools.add(Material.GOLD_HELMET);
tools.add(Material.GOLD_LEGGINGS); tools.add(Material.GOLD_LEGGINGS);
tools.add(Material.IRON_AXE); tools.add(Material.IRON_AXE);
tools.add(Material.IRON_HOE); tools.add(Material.IRON_HOE);
tools.add(Material.IRON_PICKAXE); tools.add(Material.IRON_PICKAXE);
tools.add(Material.IRON_SPADE); tools.add(Material.IRON_SPADE);
tools.add(Material.IRON_SWORD); tools.add(Material.IRON_SWORD);
tools.add(Material.IRON_BOOTS); tools.add(Material.IRON_BOOTS);
tools.add(Material.IRON_CHESTPLATE); tools.add(Material.IRON_CHESTPLATE);
tools.add(Material.IRON_HELMET); tools.add(Material.IRON_HELMET);
tools.add(Material.IRON_LEGGINGS); tools.add(Material.IRON_LEGGINGS);
final List<String> configBlacklist = plugin.getConfig().getStringList("blacklist"); final List<String> configBlacklist = plugin.getConfig().getStringList("blacklist");
for (final String s : configBlacklist) { for (final String s : configBlacklist) {
Material mat = Material.getMaterial(s.toUpperCase()); Material mat = Material.getMaterial(s.toUpperCase());
if (mat == null) { if (mat == null) {
mat = Material.getMaterial(Integer.parseInt(s)); mat = Material.getMaterial(Integer.parseInt(s));
if (mat == null) { if (mat == null) {
plugin.getLogger().info(s + " is not a valid material. Check your spelling or ID"); plugin.getLogger().info(s + " is not a valid material. Check your spelling or ID");
continue; continue;
} }
} }
blacklist.add(mat); blacklist.add(mat);
} }
// ToDo: add extras to config file // ToDo: add extras to config file
addTransparentBlock(Material.AIR); addTransparentBlock(Material.AIR);
/* Misc */ /* Misc */
addTransparentBlock(Material.CAKE_BLOCK); addTransparentBlock(Material.CAKE_BLOCK);
/* Redstone Material */ /* Redstone Material */
addTransparentBlock(Material.REDSTONE_WIRE); addTransparentBlock(Material.REDSTONE_WIRE);
/* Redstone Torches */ /* Redstone Torches */
addTransparentBlock(Material.REDSTONE_TORCH_OFF); addTransparentBlock(Material.REDSTONE_TORCH_OFF);
addTransparentBlock(Material.REDSTONE_TORCH_ON); addTransparentBlock(Material.REDSTONE_TORCH_ON);
/* Diodes (Repeaters) */ /* Diodes (Repeaters) */
addTransparentBlock(Material.DIODE_BLOCK_OFF); addTransparentBlock(Material.DIODE_BLOCK_OFF);
addTransparentBlock(Material.DIODE_BLOCK_ON); addTransparentBlock(Material.DIODE_BLOCK_ON);
/* Power Sources */ /* Power Sources */
addTransparentBlock(Material.DETECTOR_RAIL); addTransparentBlock(Material.DETECTOR_RAIL);
addTransparentBlock(Material.LEVER); addTransparentBlock(Material.LEVER);
addTransparentBlock(Material.STONE_BUTTON); addTransparentBlock(Material.STONE_BUTTON);
addTransparentBlock(Material.WOOD_BUTTON); addTransparentBlock(Material.WOOD_BUTTON);
addTransparentBlock(Material.STONE_PLATE); addTransparentBlock(Material.STONE_PLATE);
addTransparentBlock(Material.WOOD_PLATE); addTransparentBlock(Material.WOOD_PLATE);
/* Nature Material */ /* Nature Material */
addTransparentBlock(Material.RED_MUSHROOM); addTransparentBlock(Material.RED_MUSHROOM);
addTransparentBlock(Material.BROWN_MUSHROOM); addTransparentBlock(Material.BROWN_MUSHROOM);
addTransparentBlock(Material.RED_ROSE); addTransparentBlock(Material.RED_ROSE);
addTransparentBlock(Material.YELLOW_FLOWER); addTransparentBlock(Material.YELLOW_FLOWER);
addTransparentBlock(Material.FLOWER_POT); addTransparentBlock(Material.FLOWER_POT);
/* Greens */ /* Greens */
addTransparentBlock(Material.LONG_GRASS); addTransparentBlock(Material.LONG_GRASS);
addTransparentBlock(Material.VINE); addTransparentBlock(Material.VINE);
addTransparentBlock(Material.WATER_LILY); addTransparentBlock(Material.WATER_LILY);
/* Seedy things */ /* Seedy things */
addTransparentBlock(Material.MELON_STEM); addTransparentBlock(Material.MELON_STEM);
addTransparentBlock(Material.PUMPKIN_STEM); addTransparentBlock(Material.PUMPKIN_STEM);
addTransparentBlock(Material.CROPS); addTransparentBlock(Material.CROPS);
addTransparentBlock(Material.NETHER_WARTS); addTransparentBlock(Material.NETHER_WARTS);
/* Semi-nature */ /* Semi-nature */
addTransparentBlock(Material.SNOW); addTransparentBlock(Material.SNOW);
addTransparentBlock(Material.FIRE); addTransparentBlock(Material.FIRE);
addTransparentBlock(Material.WEB); addTransparentBlock(Material.WEB);
addTransparentBlock(Material.TRIPWIRE); addTransparentBlock(Material.TRIPWIRE);
addTransparentBlock(Material.TRIPWIRE_HOOK); addTransparentBlock(Material.TRIPWIRE_HOOK);
/* Stairs */ /* Stairs */
addTransparentBlock(Material.COBBLESTONE_STAIRS); addTransparentBlock(Material.COBBLESTONE_STAIRS);
addTransparentBlock(Material.BRICK_STAIRS); addTransparentBlock(Material.BRICK_STAIRS);
addTransparentBlock(Material.SANDSTONE_STAIRS); addTransparentBlock(Material.SANDSTONE_STAIRS);
addTransparentBlock(Material.NETHER_BRICK_STAIRS); addTransparentBlock(Material.NETHER_BRICK_STAIRS);
addTransparentBlock(Material.SMOOTH_STAIRS); addTransparentBlock(Material.SMOOTH_STAIRS);
/* Wood Stairs */ /* Wood Stairs */
addTransparentBlock(Material.BIRCH_WOOD_STAIRS); addTransparentBlock(Material.BIRCH_WOOD_STAIRS);
addTransparentBlock(Material.WOOD_STAIRS); addTransparentBlock(Material.WOOD_STAIRS);
addTransparentBlock(Material.JUNGLE_WOOD_STAIRS); addTransparentBlock(Material.JUNGLE_WOOD_STAIRS);
addTransparentBlock(Material.SPRUCE_WOOD_STAIRS); addTransparentBlock(Material.SPRUCE_WOOD_STAIRS);
/* Lava & Water */ /* Lava & Water */
addTransparentBlock(Material.LAVA); addTransparentBlock(Material.LAVA);
addTransparentBlock(Material.STATIONARY_LAVA); addTransparentBlock(Material.STATIONARY_LAVA);
addTransparentBlock(Material.WATER); addTransparentBlock(Material.WATER);
addTransparentBlock(Material.STATIONARY_WATER); addTransparentBlock(Material.STATIONARY_WATER);
/* Saplings and bushes */ /* Saplings and bushes */
addTransparentBlock(Material.SAPLING); addTransparentBlock(Material.SAPLING);
addTransparentBlock(Material.DEAD_BUSH); addTransparentBlock(Material.DEAD_BUSH);
/* Construction Material */ /* Construction Material */
/* Fences */ /* Fences */
addTransparentBlock(Material.FENCE); addTransparentBlock(Material.FENCE);
addTransparentBlock(Material.FENCE_GATE); addTransparentBlock(Material.FENCE_GATE);
addTransparentBlock(Material.IRON_FENCE); addTransparentBlock(Material.IRON_FENCE);
addTransparentBlock(Material.NETHER_FENCE); addTransparentBlock(Material.NETHER_FENCE);
/* Ladders, Signs */ /* Ladders, Signs */
addTransparentBlock(Material.LADDER); addTransparentBlock(Material.LADDER);
addTransparentBlock(Material.SIGN_POST); addTransparentBlock(Material.SIGN_POST);
addTransparentBlock(Material.WALL_SIGN); addTransparentBlock(Material.WALL_SIGN);
/* Bed */ /* Bed */
addTransparentBlock(Material.BED_BLOCK); addTransparentBlock(Material.BED_BLOCK);
/* Pistons */ /* Pistons */
addTransparentBlock(Material.PISTON_EXTENSION); addTransparentBlock(Material.PISTON_EXTENSION);
addTransparentBlock(Material.PISTON_MOVING_PIECE); addTransparentBlock(Material.PISTON_MOVING_PIECE);
addTransparentBlock(Material.RAILS); addTransparentBlock(Material.RAILS);
/* Torch & Trapdoor */ /* Torch & Trapdoor */
addTransparentBlock(Material.TORCH); addTransparentBlock(Material.TORCH);
addTransparentBlock(Material.TRAP_DOOR); addTransparentBlock(Material.TRAP_DOOR);
/* New */ /* New */
addTransparentBlock(Material.BREWING_STAND); addTransparentBlock(Material.BREWING_STAND);
addTransparentBlock(Material.WOODEN_DOOR); addTransparentBlock(Material.WOODEN_DOOR);
addTransparentBlock(Material.WOOD_STEP); addTransparentBlock(Material.WOOD_STEP);
} }
/** /**
* @param m * @param m
* The material to check if it is blacklisted * The material to check if it is blacklisted
* @return true if the material is black listed. False if not. * @return true if the material is black listed. False if not.
*/ */
public static boolean isBlacklisted(final Material m) { public static boolean isBlacklisted(final Material m) {
return blacklist.contains(m); return blacklist.contains(m);
} }
/** /**
* Returns true if the given location is loaded or not. * Returns true if the given location is loaded or not.
* *
* @param loc * @param loc
* The location * The location
* @return true if the given location is loaded or not. * @return true if the given location is loaded or not.
*/ */
public static boolean isLoaded(final Location loc) { public static boolean isLoaded(final Location loc) {
// System.out.println("Checking isLoaded(Location loc)"); // System.out.println("Checking isLoaded(Location loc)");
if (loc.getWorld() == null) { if (loc.getWorld() == null) {
// System.out.println("Is not loaded. (No world)"); // System.out.println("Is not loaded. (No world)");
return false; return false;
} }
// Calculate the chunks coordinates. These are 1,2,3 for each chunk, NOT // Calculate the chunks coordinates. These are 1,2,3 for each chunk, NOT
// location rounded to the nearest 16. // location rounded to the nearest 16.
if (loc.getWorld().isChunkLoaded(loc.getBlockX() >> 4, loc.getBlockZ() >> 4)) { if (loc.getWorld().isChunkLoaded(loc.getBlockX() >> 4, loc.getBlockZ() >> 4)) {
// System.out.println("Chunk is loaded " + x + ", " + z); // System.out.println("Chunk is loaded " + x + ", " + z);
return true; return true;
} }
// System.out.println("Chunk is NOT loaded " + x + ", " + z); // System.out.println("Chunk is NOT loaded " + x + ", " + z);
return false; return false;
} }
/** /**
* @param mat * @param mat
* The material to check * The material to check
* @return Returns true if the item is a tool (Has durability) or false if * @return Returns true if the item is a tool (Has durability) or false if
* it doesn't. * it doesn't.
*/ */
public static boolean isTool(final Material mat) { public static boolean isTool(final Material mat) {
return tools.contains(mat); return tools.contains(mat);
} }
public static boolean isTransparent(final Material m) { public static boolean isTransparent(final Material m) {
return transparent.contains(m); return transparent.contains(m);
} }
/** /**
* Converts a string into an item from the database. * Converts a string into an item from the database.
* *
* @param itemString * @param itemString
* The database string. Is the result of makeString(ItemStack * The database string. Is the result of makeString(ItemStack
* item). * item).
* @return A new itemstack, with the properties given in the string * @return A new itemstack, with the properties given in the string
*/ */
public static ItemStack makeItem(final String itemString) { public static ItemStack makeItem(final String itemString) {
final String[] itemInfo = itemString.split(":"); final String[] itemInfo = itemString.split(":");
final ItemStack item = new ItemStack(Material.getMaterial(itemInfo[0])); final ItemStack item = new ItemStack(Material.getMaterial(itemInfo[0]));
final MaterialData data = new MaterialData(Integer.parseInt(itemInfo[1])); final MaterialData data = new MaterialData(Integer.parseInt(itemInfo[1]));
item.setData(data); item.setData(data);
item.setDurability(Short.parseShort(itemInfo[2])); item.setDurability(Short.parseShort(itemInfo[2]));
item.setAmount(Integer.parseInt(itemInfo[3])); item.setAmount(Integer.parseInt(itemInfo[3]));
for (int i = 4; i < itemInfo.length; i = i + 2) { for (int i = 4; i < itemInfo.length; i = i + 2) {
int level = Integer.parseInt(itemInfo[i + 1]); int level = Integer.parseInt(itemInfo[i + 1]);
final Enchantment ench = Enchantment.getByName(itemInfo[i]); final Enchantment ench = Enchantment.getByName(itemInfo[i]);
if (ench == null) { if (ench == null) {
continue; // Invalid continue; // Invalid
} }
if (ench.canEnchantItem(item)) { if (ench.canEnchantItem(item)) {
if (level <= 0) { if (level <= 0) {
continue; continue;
} }
level = Math.min(ench.getMaxLevel(), level); level = Math.min(ench.getMaxLevel(), level);
item.addEnchantment(ench, level); item.addEnchantment(ench, level);
} }
} }
return item; return item;
} }
/** /**
* Compares two items to each other. Returns true if they match. * Compares two items to each other. Returns true if they match.
* *
* @param stack1 * @param stack1
* The first item stack * The first item stack
* @param stack2 * @param stack2
* The second item stack * The second item stack
* @return true if the itemstacks match. (Material, durability, enchants, name) * @return true if the itemstacks match. (Material, durability, enchants,
*/ * name)
public static boolean matches(final ItemStack stack1, final ItemStack stack2) { */
if (stack1 == stack2) { return true; // Referring to the same thing, or both are null. public static boolean matches(final ItemStack stack1, final ItemStack stack2) {
} if (stack1 == stack2) {
if (stack1 == null || stack2 == null) { return false; // One of them is null (Can't be both, see above) return true; // Referring to the same thing, or both are null.
} }
if (stack1.getType() != stack2.getType()) { return false; // Not the same material if (stack1 == null || stack2 == null) {
} return false; // One of them is null (Can't be both, see above)
if (stack1.getDurability() != stack2.getDurability()) { return false; // Not the same durability }
} if (stack1.getType() != stack2.getType()) {
if (!stack1.getEnchantments().equals(stack2.getEnchantments())) { return false; // They have the same enchants return false; // Not the same material
} }
if (stack1.getItemMeta().hasDisplayName() || stack2.getItemMeta().hasDisplayName()) { if (stack1.getDurability() != stack2.getDurability()) {
if (stack1.getItemMeta().hasDisplayName() && stack2.getItemMeta().hasDisplayName()) { return false; // Not the same durability
if (!stack1.getItemMeta().getDisplayName().equals(stack2.getItemMeta().getDisplayName())) { return false; // items have different display name }
} if (!stack1.getEnchantments().equals(stack2.getEnchantments())) {
} else { return false; // They have the same enchants
return false; // one of the item stacks have a display name }
} if (stack1.getItemMeta().hasDisplayName() || stack2.getItemMeta().hasDisplayName()) {
} if (stack1.getItemMeta().hasDisplayName() && stack2.getItemMeta().hasDisplayName()) {
if (stack1.getItemMeta().hasLore() || stack2.getItemMeta().hasLore()) { if (!stack1.getItemMeta().getDisplayName().equals(stack2.getItemMeta().getDisplayName())) {
return false; // items have different display name
}
} else {
return false; // one of the item stacks have a display name
}
}
if (stack1.getItemMeta().hasLore() || stack2.getItemMeta().hasLore()) {
if (stack1.getItemMeta().hasLore() && stack2.getItemMeta().hasLore()) { if (stack1.getItemMeta().hasLore() && stack2.getItemMeta().hasLore()) {
if (!stack1.getItemMeta().getLore().equals(stack2.getItemMeta().getLore())) { if (!stack1.getItemMeta().getLore().equals(stack2.getItemMeta().getLore())) {
return false; // items have different lore return false; // items have different lore
@ -540,41 +555,43 @@ public class Util {
} }
} }
} }
try { try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta"); Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
final boolean book1 = stack1.getItemMeta() instanceof EnchantmentStorageMeta; final boolean book1 = stack1.getItemMeta() instanceof EnchantmentStorageMeta;
final boolean book2 = stack2.getItemMeta() instanceof EnchantmentStorageMeta; final boolean book2 = stack2.getItemMeta() instanceof EnchantmentStorageMeta;
if (book1 != book2) { return false;// One has enchantment meta, the other does not. if (book1 != book2) {
} return false;// One has enchantment meta, the other does not.
if (book1) { // They are the same here (both true or both }
// false). So if one is true, the other is if (book1) { // They are the same here (both true or both
// true. // false). So if one is true, the other is
final Map<Enchantment, Integer> ench1 = ((EnchantmentStorageMeta) stack1.getItemMeta()).getStoredEnchants(); // true.
final Map<Enchantment, Integer> ench2 = ((EnchantmentStorageMeta) stack2.getItemMeta()).getStoredEnchants(); final Map<Enchantment, Integer> ench1 = ((EnchantmentStorageMeta) stack1.getItemMeta()).getStoredEnchants();
if (!ench1.equals(ench2)) { return false; // Enchants aren't the same. final Map<Enchantment, Integer> ench2 = ((EnchantmentStorageMeta) stack2.getItemMeta()).getStoredEnchants();
} if (!ench1.equals(ench2)) {
} return false; // Enchants aren't the same.
} catch (final ClassNotFoundException e) { }
// Nothing. They dont have a build high enough to support this. }
} } catch (final ClassNotFoundException e) {
return true; // Nothing. They dont have a build high enough to support this.
} }
return true;
}
public static void parseColours(final YamlConfiguration config) { public static void parseColours(final YamlConfiguration config) {
final Set<String> keys = config.getKeys(true); final Set<String> keys = config.getKeys(true);
for (final String key : keys) { for (final String key : keys) {
String filtered = config.getString(key); String filtered = config.getString(key);
if (filtered.startsWith("MemorySection")) { if (filtered.startsWith("MemorySection")) {
continue; continue;
} }
filtered = ChatColor.translateAlternateColorCodes('&', filtered); filtered = ChatColor.translateAlternateColorCodes('&', filtered);
config.set(key, filtered); config.set(key, filtered);
} }
} }
public static String serialize(final ItemStack iStack) { public static String serialize(final ItemStack iStack) {
final YamlConfiguration cfg = new YamlConfiguration(); final YamlConfiguration cfg = new YamlConfiguration();
cfg.set("item", iStack); cfg.set("item", iStack);
return cfg.saveToString(); return cfg.saveToString();
} }
} }