rework some stuff
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
package com.alttd.playershops.utils;
|
||||
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.Material;
|
||||
import org.bukkit.configuration.InvalidConfigurationException;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
import org.bukkit.enchantments.Enchantment;
|
||||
import org.bukkit.inventory.Inventory;
|
||||
import org.bukkit.inventory.ItemStack;
|
||||
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
public class ShopUtil {
|
||||
|
||||
/**
|
||||
* Counts the number of items in the given inventory where
|
||||
* Util.matches(inventory item, item) is true.
|
||||
*
|
||||
* @param inv
|
||||
* The inventory to search
|
||||
* @param item
|
||||
* The ItemStack to search for
|
||||
* @return The number of items that match in this inventory.
|
||||
*/
|
||||
public static int countItems(Inventory inv, ItemStack item) {
|
||||
int items = 0;
|
||||
for (ItemStack iStack : inv.getContents()) {
|
||||
if (iStack == null)
|
||||
continue;
|
||||
if (ShopUtil.matches(item, iStack)) {
|
||||
items += iStack.getAmount();
|
||||
}
|
||||
}
|
||||
return items;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns true if the given location is loaded or not.
|
||||
*
|
||||
* @param loc
|
||||
* The location
|
||||
* @return true if the given location is loaded or not.
|
||||
*/
|
||||
public static boolean isLoaded(Location loc) {
|
||||
// System.out.println("Checking isLoaded(Location loc)");
|
||||
if (loc.getWorld() == null) {
|
||||
// System.out.println("Is not loaded. (No world)");
|
||||
return false;
|
||||
}
|
||||
// Calculate the chunks coordinates. These are 1,2,3 for each chunk, NOT
|
||||
// location rounded to the nearest 16.
|
||||
int x = (int) Math.floor((loc.getBlockX()) / 16.0);
|
||||
int z = (int) Math.floor((loc.getBlockZ()) / 16.0);
|
||||
if (loc.getWorld().isChunkLoaded(x, z)) {
|
||||
// System.out.println("Chunk is loaded " + x + ", " + z);
|
||||
return true;
|
||||
} else {
|
||||
// System.out.println("Chunk is NOT loaded " + x + ", " + z);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two items to each other. Returns true if they match.
|
||||
*
|
||||
* @param stack1
|
||||
* The first item stack
|
||||
* @param stack2
|
||||
* The second item stack
|
||||
* @return true if the itemstacks match. (Material, durability, enchants, name)
|
||||
*/
|
||||
public static boolean matches(ItemStack stack1, ItemStack stack2) {
|
||||
if (stack1 == stack2)
|
||||
return true; // Referring to the same thing, or both are null.
|
||||
if (stack1 == null || stack2 == null)
|
||||
return false; // One of them is null (Can't be both, see above)
|
||||
if (stack1.getType() != stack2.getType())
|
||||
return false; // Not the same material
|
||||
if (stack1.getDurability() != stack2.getDurability())
|
||||
return false; // Not the same durability
|
||||
if (!stack1.getEnchantments().equals(stack2.getEnchantments()))
|
||||
return false; // They have the same enchants
|
||||
if (stack1.getItemMeta().hasDisplayName() || stack2.getItemMeta().hasDisplayName()) {
|
||||
if (stack1.getItemMeta().hasDisplayName() && stack2.getItemMeta().hasDisplayName()) {
|
||||
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
|
||||
}
|
||||
}
|
||||
try {
|
||||
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
|
||||
boolean book1 = stack1.getItemMeta() instanceof EnchantmentStorageMeta;
|
||||
boolean book2 = stack2.getItemMeta() instanceof EnchantmentStorageMeta;
|
||||
if (book1 != book2)
|
||||
return false;// One has enchantment meta, the other does not.
|
||||
if (book1 == true) { // They are the same here (both true or both
|
||||
// false). So if one is true, the other is
|
||||
// true.
|
||||
Map<Enchantment, Integer> ench1 = ((EnchantmentStorageMeta) stack1.getItemMeta()).getStoredEnchants();
|
||||
Map<Enchantment, Integer> ench2 = ((EnchantmentStorageMeta) stack2.getItemMeta()).getStoredEnchants();
|
||||
if (!ench1.equals(ench2))
|
||||
return false; // Enchants aren't the same.
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of items that can be given to the inventory safely.
|
||||
*
|
||||
* @param inv
|
||||
* The inventory to count
|
||||
* @param item
|
||||
* The item. Material, durabiltiy and enchants must
|
||||
* match for 'stackability' to occur.
|
||||
* @return The number of items that can be given to the inventory safely.
|
||||
*/
|
||||
public static int countSpace(Inventory inv, ItemStack item) {
|
||||
int space = 0;
|
||||
|
||||
try {
|
||||
ItemStack[] contents = inv.getContents();
|
||||
for (ItemStack iStack : contents) {
|
||||
if (iStack == null || iStack.getType() == Material.AIR) {
|
||||
space += item.getMaxStackSize();
|
||||
} else if (matches(item, iStack)) {
|
||||
space += item.getMaxStackSize() - iStack.getAmount();
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
return space;
|
||||
}
|
||||
|
||||
public static String serialize(ItemStack itemStack) {
|
||||
YamlConfiguration cfg = new YamlConfiguration();
|
||||
cfg.set("item", itemStack);
|
||||
return cfg.saveToString();
|
||||
}
|
||||
|
||||
public static ItemStack deserialize(String config) throws InvalidConfigurationException {
|
||||
YamlConfiguration cfg = new YamlConfiguration();
|
||||
cfg.loadFromString(config);
|
||||
ItemStack stack = cfg.getItemStack("item");
|
||||
return stack;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user