From da2bee447d2070192d39fa84e9500b622b942b3b Mon Sep 17 00:00:00 2001 From: Len <40720638+destro174@users.noreply.github.com> Date: Sat, 20 Apr 2024 21:44:42 +0200 Subject: [PATCH] Update itemMatcher --- build.gradle.kts | 6 +-- .../playershops/utils/InventoryUtils.java | 7 +++ .../com/alttd/playershops/utils/ShopUtil.java | 45 +++++++++++-------- 3 files changed, 37 insertions(+), 21 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 4294c3f..b4dbda8 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -41,10 +41,10 @@ tasks { file.parentFile.mkdirs() } if (!file.exists()) { - download("https://repo.destro.xyz/private/com/alttd/Galaxy-Server/Galaxy-paperclip-1.20.1-R0.1-SNAPSHOT-reobf.jar", fileName) + download("https://repo.destro.xyz/private/com/alttd/Galaxy-Server/Galaxy-paperclip-1.20.4-R0.1-SNAPSHOT-reobf.jar", fileName) } serverJar(file) - minecraftVersion("1.20.2") + minecraftVersion("1.20.4") } jar { @@ -71,7 +71,7 @@ publishing { } dependencies { - compileOnly("com.alttd:Galaxy-API:1.20.2-R0.1-SNAPSHOT") { + compileOnly("com.alttd:Galaxy-API:1.20.4-R0.1-SNAPSHOT") { isChanging = true } compileOnly("com.github.milkbowl:VaultAPI:1.7") { diff --git a/src/main/java/com/alttd/playershops/utils/InventoryUtils.java b/src/main/java/com/alttd/playershops/utils/InventoryUtils.java index 106fdcf..95b4c0e 100644 --- a/src/main/java/com/alttd/playershops/utils/InventoryUtils.java +++ b/src/main/java/com/alttd/playershops/utils/InventoryUtils.java @@ -22,6 +22,7 @@ public class InventoryUtils { */ public static int countItems(Inventory inv, ItemStack item) { int items = 0; + for (ItemStack iStack : inv.getContents()) { if (iStack == null) continue; @@ -38,8 +39,10 @@ public class InventoryUtils { public static int removeItem(Inventory inventory, ItemStack itemStack) { if (inventory == null) return itemStack.getAmount(); + if (itemStack == null || itemStack.getAmount() <= 0) return 0; + ItemStack[] contents = inventory.getContents(); int amount = itemStack.getAmount(); for (ItemStack stack : contents) { @@ -69,8 +72,10 @@ public class InventoryUtils { public static int addItem(Inventory inventory, ItemStack itemStack) { if (inventory == null) return itemStack.getAmount(); + if (itemStack.getAmount() <= 0) return 0; + ArrayList itemStacksAdding = new ArrayList<>(); //break up the itemstack into multiple ItemStacks with correct stack size @@ -81,6 +86,7 @@ public class InventoryUtils { is.setAmount(is.getMaxStackSize()); itemStacksAdding.add(is); } + ItemStack is = itemStack.clone(); is.setAmount(partialStack); if (partialStack > 0) @@ -100,6 +106,7 @@ public class InventoryUtils { public static boolean hasRoom(Inventory inventory, ItemStack itemStack) { if (inventory == null) return false; + if (itemStack.getAmount() <= 0) return true; diff --git a/src/main/java/com/alttd/playershops/utils/ShopUtil.java b/src/main/java/com/alttd/playershops/utils/ShopUtil.java index 94c3e14..f83a061 100644 --- a/src/main/java/com/alttd/playershops/utils/ShopUtil.java +++ b/src/main/java/com/alttd/playershops/utils/ShopUtil.java @@ -3,6 +3,7 @@ package com.alttd.playershops.utils; import com.alttd.playershops.gui.GuiIcon; import com.alttd.playershops.shop.PlayerShop; import com.alttd.playershops.shop.ShopTransaction; +import lombok.extern.java.Log; import net.kyori.adventure.text.Component; import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.minimessage.MiniMessage; @@ -51,65 +52,74 @@ public class ShopUtil { 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.hasItemMeta() && stack2.hasItemMeta())) { - return false; // Only one of the items has item meta - } + + 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())) { + if ((itemMeta1.hasDisplayName() != itemMeta2.hasDisplayName())) return false; // Only one has a display name - } - if (!itemMeta1.getDisplayName().equals(itemMeta2.getDisplayName())) { + + if (!itemMeta1.getDisplayName().equals(itemMeta2.getDisplayName())) return false; // items have different display name - } // This is where the heavy checks are :/ - if (itemMeta1 instanceof Damageable damageable1 && itemMeta2 instanceof Damageable damageable2) { + 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 false; // 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()) { + 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 false; // not the same content - } + if (Arrays.equals(shulkerBox1.getInventory().getContents(), shulkerBox2.getInventory().getContents())) + return true; // same content } } + try { Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta"); boolean book1 = itemMeta1 instanceof EnchantmentStorageMeta; @@ -125,12 +135,11 @@ public class ShopUtil { } catch (ClassNotFoundException ignored) { } } - if (!stack1.getEnchantments().equals(stack2.getEnchantments())) - return false; // They have the same enchants +// 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 true; + return Arrays.equals(stack1.serializeAsBytes(), stack2.serializeAsBytes()); } /**