Update itemMatcher

This commit is contained in:
Len 2024-04-20 21:44:42 +02:00
parent 55c92d6f20
commit da2bee447d
3 changed files with 37 additions and 21 deletions

View File

@ -41,10 +41,10 @@ tasks {
file.parentFile.mkdirs() file.parentFile.mkdirs()
} }
if (!file.exists()) { 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) serverJar(file)
minecraftVersion("1.20.2") minecraftVersion("1.20.4")
} }
jar { jar {
@ -71,7 +71,7 @@ publishing {
} }
dependencies { 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 isChanging = true
} }
compileOnly("com.github.milkbowl:VaultAPI:1.7") { compileOnly("com.github.milkbowl:VaultAPI:1.7") {

View File

@ -22,6 +22,7 @@ public class InventoryUtils {
*/ */
public static int countItems(Inventory inv, ItemStack item) { public static int countItems(Inventory inv, ItemStack item) {
int items = 0; int items = 0;
for (ItemStack iStack : inv.getContents()) { for (ItemStack iStack : inv.getContents()) {
if (iStack == null) if (iStack == null)
continue; continue;
@ -38,8 +39,10 @@ public class InventoryUtils {
public static int removeItem(Inventory inventory, ItemStack itemStack) { public static int removeItem(Inventory inventory, ItemStack itemStack) {
if (inventory == null) if (inventory == null)
return itemStack.getAmount(); return itemStack.getAmount();
if (itemStack == null || itemStack.getAmount() <= 0) if (itemStack == null || itemStack.getAmount() <= 0)
return 0; return 0;
ItemStack[] contents = inventory.getContents(); ItemStack[] contents = inventory.getContents();
int amount = itemStack.getAmount(); int amount = itemStack.getAmount();
for (ItemStack stack : contents) { for (ItemStack stack : contents) {
@ -69,8 +72,10 @@ public class InventoryUtils {
public static int addItem(Inventory inventory, ItemStack itemStack) { public static int addItem(Inventory inventory, ItemStack itemStack) {
if (inventory == null) if (inventory == null)
return itemStack.getAmount(); return itemStack.getAmount();
if (itemStack.getAmount() <= 0) if (itemStack.getAmount() <= 0)
return 0; return 0;
ArrayList<ItemStack> itemStacksAdding = new ArrayList<>(); ArrayList<ItemStack> itemStacksAdding = new ArrayList<>();
//break up the itemstack into multiple ItemStacks with correct stack size //break up the itemstack into multiple ItemStacks with correct stack size
@ -81,6 +86,7 @@ public class InventoryUtils {
is.setAmount(is.getMaxStackSize()); is.setAmount(is.getMaxStackSize());
itemStacksAdding.add(is); itemStacksAdding.add(is);
} }
ItemStack is = itemStack.clone(); ItemStack is = itemStack.clone();
is.setAmount(partialStack); is.setAmount(partialStack);
if (partialStack > 0) if (partialStack > 0)
@ -100,6 +106,7 @@ public class InventoryUtils {
public static boolean hasRoom(Inventory inventory, ItemStack itemStack) { public static boolean hasRoom(Inventory inventory, ItemStack itemStack) {
if (inventory == null) if (inventory == null)
return false; return false;
if (itemStack.getAmount() <= 0) if (itemStack.getAmount() <= 0)
return true; return true;

View File

@ -3,6 +3,7 @@ package com.alttd.playershops.utils;
import com.alttd.playershops.gui.GuiIcon; import com.alttd.playershops.gui.GuiIcon;
import com.alttd.playershops.shop.PlayerShop; import com.alttd.playershops.shop.PlayerShop;
import com.alttd.playershops.shop.ShopTransaction; import com.alttd.playershops.shop.ShopTransaction;
import lombok.extern.java.Log;
import net.kyori.adventure.text.Component; import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.format.NamedTextColor; import net.kyori.adventure.text.format.NamedTextColor;
import net.kyori.adventure.text.minimessage.MiniMessage; import net.kyori.adventure.text.minimessage.MiniMessage;
@ -51,65 +52,74 @@ public class ShopUtil {
public static boolean matches(ItemStack stack1, ItemStack stack2) { public static boolean matches(ItemStack stack1, ItemStack stack2) {
if (stack1 == stack2) if (stack1 == stack2)
return true; // Referring to the same thing, or both are null. return true; // Referring to the same thing, or both are null.
if (stack1 == null || stack2 == null) if (stack1 == null || stack2 == null)
return false; // One of them is null (Can't be both, see above) return false; // One of them is null (Can't be both, see above)
if (stack1.getType() != stack2.getType()) if (stack1.getType() != stack2.getType())
return false; // Not the same material 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()) { if (stack1.hasItemMeta() && stack2.hasItemMeta()) {
ItemMeta itemMeta1 = stack1.getItemMeta(); ItemMeta itemMeta1 = stack1.getItemMeta();
ItemMeta itemMeta2 = stack2.getItemMeta(); ItemMeta itemMeta2 = stack2.getItemMeta();
if ((itemMeta1.hasDisplayName() != itemMeta2.hasDisplayName())) { if ((itemMeta1.hasDisplayName() != itemMeta2.hasDisplayName()))
return false; // Only one has a display name 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 return false; // items have different display name
}
// This is where the heavy checks are :/ // 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()) if (damageable1.getDamage() != damageable2.getDamage())
return false; // Not the same durability return false; // Not the same durability
}
// We need this check now because mapart stores data in the map NBT // We need this check now because mapart stores data in the map NBT
if (itemMeta1 instanceof MapMeta mapMeta1 && itemMeta2 instanceof MapMeta mapMeta2) { if (itemMeta1 instanceof MapMeta mapMeta1 && itemMeta2 instanceof MapMeta mapMeta2) {
MapView mapView1 = mapMeta1.getMapView(); MapView mapView1 = mapMeta1.getMapView();
MapView mapView2 = mapMeta2.getMapView(); MapView mapView2 = mapMeta2.getMapView();
if (mapView1 == null || mapView2 == null) if (mapView1 == null || mapView2 == null)
return false; // at least one is null return false; // at least one is null
if (mapView1.getId() != mapView2.getId()) if (mapView1.getId() != mapView2.getId())
return false; // ID does not match return false; // ID does not match
} }
if (itemMeta1 instanceof TropicalFishBucketMeta tropicalFishBucketMeta1 && itemMeta2 instanceof TropicalFishBucketMeta tropicalFishBucketMeta2) { if (itemMeta1 instanceof TropicalFishBucketMeta tropicalFishBucketMeta1 && itemMeta2 instanceof TropicalFishBucketMeta tropicalFishBucketMeta2) {
if (tropicalFishBucketMeta1.getBodyColor() != tropicalFishBucketMeta2.getBodyColor()) if (tropicalFishBucketMeta1.getBodyColor() != tropicalFishBucketMeta2.getBodyColor())
return false; return false;
if (tropicalFishBucketMeta1.getPattern() != tropicalFishBucketMeta2.getPattern()) if (tropicalFishBucketMeta1.getPattern() != tropicalFishBucketMeta2.getPattern())
return false; return false;
if (tropicalFishBucketMeta1.getPatternColor() != tropicalFishBucketMeta2.getPatternColor()) if (tropicalFishBucketMeta1.getPatternColor() != tropicalFishBucketMeta2.getPatternColor())
return false; return false;
} }
if (itemMeta1 instanceof AxolotlBucketMeta axolotlBucketMeta1 && itemMeta2 instanceof AxolotlBucketMeta axolotlBucketMeta2) { if (itemMeta1 instanceof AxolotlBucketMeta axolotlBucketMeta1 && itemMeta2 instanceof AxolotlBucketMeta axolotlBucketMeta2) {
if (axolotlBucketMeta1.getVariant() != axolotlBucketMeta2.getVariant()) if (axolotlBucketMeta1.getVariant() != axolotlBucketMeta2.getVariant())
return false; return false;
} }
if (itemMeta1 instanceof BlockStateMeta blockStateMeta1 && itemMeta2 instanceof BlockStateMeta blockStateMeta2) { if (itemMeta1 instanceof BlockStateMeta blockStateMeta1 && itemMeta2 instanceof BlockStateMeta blockStateMeta2) {
// extra heavy - Banners, Shulkerboxes, beehive and more? // extra heavy - Banners, Shulkerboxes, beehive and more?
BlockState blockState1 = blockStateMeta1.getBlockState(); BlockState blockState1 = blockStateMeta1.getBlockState();
BlockState blockState2 = blockStateMeta2.getBlockState(); BlockState blockState2 = blockStateMeta2.getBlockState();
if (blockState1 instanceof ShulkerBox shulkerBox1 && blockState2 instanceof ShulkerBox shulkerBox2) { 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 return false; // not the same color
}
// Do we need all of the above checks inside the shulker? // Do we need all of the above checks inside the shulker?
if (!Arrays.equals(shulkerBox1.getInventory().getContents(), shulkerBox2.getInventory().getContents())) { if (Arrays.equals(shulkerBox1.getInventory().getContents(), shulkerBox2.getInventory().getContents()))
return false; // not the same content return true; // same content
}
} }
} }
try { try {
Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta"); Class.forName("org.bukkit.inventory.meta.EnchantmentStorageMeta");
boolean book1 = itemMeta1 instanceof EnchantmentStorageMeta; boolean book1 = itemMeta1 instanceof EnchantmentStorageMeta;
@ -125,12 +135,11 @@ public class ShopUtil {
} catch (ClassNotFoundException ignored) { } catch (ClassNotFoundException ignored) {
} }
} }
if (!stack1.getEnchantments().equals(stack2.getEnchantments())) // if (!stack1.getEnchantments().equals(stack2.getEnchantments()))
return false; // They have the same enchants // return false; // They have the same enchants
// if (!stack1.getItemMeta().equals(stack2.getItemMeta())) // if (!stack1.getItemMeta().equals(stack2.getItemMeta()))
// return false; // They have the same enchants // return false; // They have the same enchants
return Arrays.equals(stack1.serializeAsBytes(), stack2.serializeAsBytes());
return true;
} }
/** /**