Update old item matching to use the new ItemMatcher class
This commit is contained in:
parent
bcba3413c5
commit
ffdafd07d2
|
|
@ -110,7 +110,7 @@ public class PlayerShop {
|
|||
}
|
||||
|
||||
public boolean matches(ItemStack item) {
|
||||
return ShopUtil.matches(getItemStack(), item);
|
||||
return ItemMatcher.matches(getItemStack(), item);
|
||||
}
|
||||
|
||||
public void remove(ItemStack item, int amount) {
|
||||
|
|
|
|||
|
|
@ -25,7 +25,7 @@ public class InventoryUtils {
|
|||
for (ItemStack iStack : inv.getContents()) {
|
||||
if (iStack == null)
|
||||
continue;
|
||||
if (ShopUtil.matches(item, iStack)) {
|
||||
if (ItemMatcher.matches(item, iStack)) {
|
||||
items += iStack.getAmount();
|
||||
}
|
||||
}
|
||||
|
|
@ -46,7 +46,7 @@ public class InventoryUtils {
|
|||
int amount = itemStack.getAmount();
|
||||
for (ItemStack stack : contents) {
|
||||
if (stack != null) {
|
||||
if (ShopUtil.matches(stack, itemStack)) {
|
||||
if (ItemMatcher.matches(stack, itemStack)) {
|
||||
if (stack.getAmount() > amount) {
|
||||
stack.setAmount(stack.getAmount() - amount);
|
||||
inventory.setContents(contents);
|
||||
|
|
|
|||
|
|
@ -37,167 +37,6 @@ public class ShopUtil {
|
|||
return (loc.getWorld().isChunkLoaded(x, z));
|
||||
}
|
||||
|
||||
/**
|
||||
* 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().equals(stack2.getItemMeta()))
|
||||
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 matches2(stack1, stack2);
|
||||
}
|
||||
|
||||
public static boolean matches2(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.hasItemMeta() && !stack2.hasItemMeta()))
|
||||
return true; // Only one of the items has item meta
|
||||
|
||||
if (stack1.hasItemMeta() && stack2.hasItemMeta()) {
|
||||
ItemMeta itemMeta1 = stack1.getItemMeta();
|
||||
ItemMeta itemMeta2 = stack2.getItemMeta();
|
||||
|
||||
if ((itemMeta1.hasDisplayName() != itemMeta2.hasDisplayName()))
|
||||
return false; // Only one has a display name
|
||||
|
||||
if (!itemMeta1.getDisplayName().equals(itemMeta2.getDisplayName()))
|
||||
return false; // items have different display name
|
||||
|
||||
// This is where the heavy checks are :/
|
||||
|
||||
try {
|
||||
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
|
||||
boolean book1 = itemMeta1 instanceof EnchantmentStorageMeta;
|
||||
boolean book2 = itemMeta2 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) itemMeta1).getStoredEnchants();
|
||||
Map<Enchantment, Integer> ench2 = ((EnchantmentStorageMeta) itemMeta2).getStoredEnchants();
|
||||
if (!ench1.equals(ench2))
|
||||
return false; // Enchants aren't the same.
|
||||
}
|
||||
} catch (ClassNotFoundException ignored) {
|
||||
}
|
||||
|
||||
if (itemMeta1 instanceof Damageable damageable1 && itemMeta2 instanceof Damageable damageable2)
|
||||
if (damageable1.getDamage() != damageable2.getDamage())
|
||||
return false; // Not the same durability
|
||||
|
||||
// We need this check now because mapart stores data in the map NBT
|
||||
if (itemMeta1 instanceof MapMeta mapMeta1 && itemMeta2 instanceof MapMeta mapMeta2) {
|
||||
MapView mapView1 = mapMeta1.getMapView();
|
||||
MapView mapView2 = mapMeta2.getMapView();
|
||||
if (mapView1 == null || mapView2 == null)
|
||||
return false; // at least one is null
|
||||
|
||||
if (mapView1.getId() == mapView2.getId())
|
||||
return true; // ID does not match
|
||||
}
|
||||
|
||||
if (itemMeta1 instanceof TropicalFishBucketMeta tropicalFishBucketMeta1 && itemMeta2 instanceof TropicalFishBucketMeta tropicalFishBucketMeta2) {
|
||||
if (tropicalFishBucketMeta1.getBodyColor() != tropicalFishBucketMeta2.getBodyColor())
|
||||
return false;
|
||||
|
||||
if (tropicalFishBucketMeta1.getPattern() != tropicalFishBucketMeta2.getPattern())
|
||||
return false;
|
||||
|
||||
if (tropicalFishBucketMeta1.getPatternColor() != tropicalFishBucketMeta2.getPatternColor())
|
||||
return false;
|
||||
|
||||
}
|
||||
|
||||
if (itemMeta1 instanceof AxolotlBucketMeta axolotlBucketMeta1 && itemMeta2 instanceof AxolotlBucketMeta axolotlBucketMeta2) {
|
||||
if (axolotlBucketMeta1.getVariant() != axolotlBucketMeta2.getVariant())
|
||||
return false;
|
||||
}
|
||||
|
||||
if (itemMeta1 instanceof BlockStateMeta blockStateMeta1 && itemMeta2 instanceof BlockStateMeta blockStateMeta2) {
|
||||
// extra heavy - Banners, Shulkerboxes, beehive and more?
|
||||
BlockState blockState1 = blockStateMeta1.getBlockState();
|
||||
BlockState blockState2 = blockStateMeta2.getBlockState();
|
||||
|
||||
if (blockState1 instanceof ShulkerBox shulkerBox1 && blockState2 instanceof ShulkerBox shulkerBox2) {
|
||||
if (shulkerBox1.getColor() != shulkerBox2.getColor())
|
||||
return false; // not the same color
|
||||
|
||||
// Do we need all of the above checks inside the shulker?
|
||||
if (Arrays.equals(shulkerBox1.getInventory().getContents(), shulkerBox2.getInventory().getContents()))
|
||||
return true; // same content
|
||||
}
|
||||
}
|
||||
|
||||
// TODO
|
||||
/* if (itemMeta1 instanceof BannerMeta bannerMeta1 && itemMeta2 instanceof BannerMeta bannerMeta2) {
|
||||
if (bannerMeta1.numberOfPatterns() != bannerMeta2.numberOfPatterns())
|
||||
return false;
|
||||
|
||||
if (!bannerMeta1.getPatterns().equals(bannerMeta2.getPatterns()))
|
||||
return false;
|
||||
}
|
||||
|
||||
if (itemMeta1 instanceof BookMeta bookMeta1 && itemMeta2 instanceof BookMeta bookMeta2) {
|
||||
// Todo Books
|
||||
}
|
||||
|
||||
if (itemMeta1 instanceof PotionMeta potionMeta1 && itemMeta2 instanceof PotionMeta potionMeta2) {
|
||||
|
||||
}*/
|
||||
}
|
||||
// if (!stack1.getEnchantments().equals(stack2.getEnchantments()))
|
||||
// return false; // They have the same enchants
|
||||
// if (!stack1.getItemMeta().equals(stack2.getItemMeta()))
|
||||
// return false; // They have the same enchants
|
||||
return Arrays.equals(stack1.serializeAsBytes(), stack2.serializeAsBytes());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of items that can be given to the inventory safely.
|
||||
*
|
||||
|
|
@ -216,7 +55,7 @@ public class ShopUtil {
|
|||
for (ItemStack iStack : contents) {
|
||||
if (iStack == null || iStack.getType() == Material.AIR) {
|
||||
space += item.getMaxStackSize();
|
||||
} else if (matches(item, iStack)) {
|
||||
} else if (ItemMatcher.matches(item, iStack)) {
|
||||
space += item.getMaxStackSize() - iStack.getAmount();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue
Block a user