Cache ItemMeta instead of creating a new ItemMeta for each potion check

This commit is contained in:
MrPowerGamerBR 2024-08-25 18:02:37 -03:00
parent 969b901615
commit 595c43e34e
2 changed files with 11 additions and 3 deletions

View File

@ -9,6 +9,7 @@ import org.bukkit.Color;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.inventory.meta.PotionMeta;
import org.bukkit.potion.PotionEffect; import org.bukkit.potion.PotionEffect;
import org.bukkit.potion.PotionEffectType; import org.bukkit.potion.PotionEffectType;
@ -334,9 +335,10 @@ public class PotionConfig extends LegacyConfigLoader {
* @return AlchemyPotion that corresponds to the given ItemStack. * @return AlchemyPotion that corresponds to the given ItemStack.
*/ */
public AlchemyPotion getPotion(ItemStack item) { public AlchemyPotion getPotion(ItemStack item) {
ItemMeta itemMeta = item.getItemMeta();
final List<AlchemyPotion> potionList = alchemyPotions.values() final List<AlchemyPotion> potionList = alchemyPotions.values()
.stream() .stream()
.filter(potion -> potion.isSimilarPotion(item)) .filter(potion -> potion.isSimilarPotion(item, itemMeta))
.toList(); .toList();
if(potionList.size() > 1) { if(potionList.size() > 1) {
mcMMO.p.getLogger().severe("Multiple potions defined in config have matched this potion, for mcMMO to behave" + mcMMO.p.getLogger().severe("Multiple potions defined in config have matched this potion, for mcMMO to behave" +

View File

@ -4,6 +4,7 @@ import com.gmail.nossr50.mcMMO;
import com.gmail.nossr50.util.PotionUtil; import com.gmail.nossr50.util.PotionUtil;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import org.bukkit.inventory.meta.PotionMeta; import org.bukkit.inventory.meta.PotionMeta;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
@ -49,13 +50,18 @@ public class AlchemyPotion {
} }
public boolean isSimilarPotion(@NotNull ItemStack otherPotion) { public boolean isSimilarPotion(@NotNull ItemStack otherPotion) {
return isSimilarPotion(otherPotion, otherPotion.getItemMeta());
}
public boolean isSimilarPotion(@NotNull ItemStack otherPotion, @Nullable ItemMeta otherMeta) {
requireNonNull(otherPotion, "otherPotion cannot be null"); requireNonNull(otherPotion, "otherPotion cannot be null");
if (otherPotion.getType() != potionItemStack.getType()) { if (otherPotion.getType() != potionItemStack.getType()) {
return false; return false;
} }
// no potion meta, no match // no potion meta, no match
if (!otherPotion.hasItemMeta()) { if (otherMeta == null) {
return false; return false;
} }
@ -63,7 +69,7 @@ public class AlchemyPotion {
* Compare custom effects on both potions. * Compare custom effects on both potions.
*/ */
final PotionMeta otherPotionMeta = (PotionMeta) otherPotion.getItemMeta(); final PotionMeta otherPotionMeta = (PotionMeta) otherMeta;
// compare custom effects on both potions, this has to be done in two traversals // compare custom effects on both potions, this has to be done in two traversals
// comparing thisPotionMeta -> otherPotionMeta and otherPotionMeta -> thisPotionMeta // comparing thisPotionMeta -> otherPotionMeta and otherPotionMeta -> thisPotionMeta
if (hasDifferingCustomEffects(getAlchemyPotionMeta(), otherPotionMeta) if (hasDifferingCustomEffects(getAlchemyPotionMeta(), otherPotionMeta)