Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve ItemMeta comparisons #5374

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 17 additions & 29 deletions src/main/java/ch/njol/skript/aliases/ItemData.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,13 @@
import ch.njol.yggdrasil.YggdrasilSerializable.YggdrasilExtendedSerializable;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.OfflinePlayer;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.inventory.ItemFactory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.inventory.meta.SkullMeta;
import org.bukkit.potion.PotionData;
import org.eclipse.jdt.annotation.Nullable;

import java.io.IOException;
Expand All @@ -50,6 +46,7 @@
import java.nio.file.Paths;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;

Expand Down Expand Up @@ -371,8 +368,7 @@ private boolean hasFlag(int flag) {

/**
* Compares {@link ItemMeta}s for {@link #matchAlias(ItemData)}.
* Note that this does NOT compare everything; only the most
* important bits.
* Note that this method assumes the metas are coming from similar types.
* @param first Meta of this item.
* @param second Meta of given item.
* @return Match quality of metas.
Expand Down Expand Up @@ -417,30 +413,22 @@ private static MatchQuality compareItemMetas(ItemMeta first, ItemMeta second) {
if (!newQuality.isBetter(quality))
quality = newQuality;
}

// Potion data
if (second instanceof PotionMeta) {
if (!(first instanceof PotionMeta)) {
return MatchQuality.DIFFERENT; // Second is a potion, first is clearly not
}
// Compare potion type, including extended and level 2 attributes
PotionData ourPotion = ((PotionMeta) first).getBasePotionData();
PotionData theirPotion = ((PotionMeta) second).getBasePotionData();
return !Objects.equals(ourPotion, theirPotion) ? MatchQuality.SAME_MATERIAL : quality;
}

// Skull owner
if (second instanceof SkullMeta) {
if (!(first instanceof SkullMeta)) {
return MatchQuality.DIFFERENT; // Second is a skull, first is clearly not
}
// Compare skull owners
OfflinePlayer ourOwner = ((SkullMeta) first).getOwningPlayer();
OfflinePlayer theirOwner = ((SkullMeta) second).getOwningPlayer();
return !Objects.equals(ourOwner, theirOwner) ? MatchQuality.SAME_MATERIAL : quality;
}

return quality;
// awful but we have to make these values the same so that they don't matter for comparison
second = second.clone(); // don't actually change it

second.setDisplayName(ourName); // set our name
second.setLore(ourLore); // set our lore
for (Enchantment theirEnchant : theirEnchants.keySet()) // remove their enchants
second.removeEnchant(theirEnchant);
for (Entry<Enchantment, Integer> ourEnchant : ourEnchants.entrySet()) // add our enchants
second.addEnchant(ourEnchant.getKey(), ourEnchant.getValue(), true);
for (ItemFlag theirFlag : theirFlags) // remove their flags
second.removeItemFlags(theirFlag);
for (ItemFlag ourFlag : ourFlags) // add our flags
second.addItemFlags(ourFlag);

return first.equals(second) ? quality : MatchQuality.SAME_MATERIAL;
}

/**
Expand Down