Skip to content

Commit

Permalink
Improve equal item listing.
Browse files Browse the repository at this point in the history
Change when items should be grouped.
Instead of relaying strictly from ignoreMetaData set, now try to group equal elements without durability check, and use set only if that fails.
  • Loading branch information
BONNe committed Mar 22, 2022
1 parent e96e2c7 commit 36ad61b
Showing 1 changed file with 28 additions and 6 deletions.
34 changes: 28 additions & 6 deletions src/main/java/world/bentobox/challenges/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,12 @@
import java.util.Locale;
import java.util.Set;

import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.Statistic;
import org.bukkit.World;
import org.bukkit.*;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.EntityType;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.*;
import org.bukkit.potion.PotionData;
import org.bukkit.potion.PotionEffectType;
import org.bukkit.potion.PotionType;
import org.eclipse.jdt.annotation.Nullable;

Expand All @@ -31,6 +27,31 @@
*/
public class Utils
{
/**
* This method checks if 2 given item stacks are similar without durability check.
* @param input First item.
* @param stack Second item.
* @return {@code true} if items are equal, {@code false} otherwise.
*/
public static boolean isSimilarNoDurability(@Nullable ItemStack input, @Nullable ItemStack stack)
{
if (stack == null || input == null)
{
return false;
}
else if (stack == input)
{
return true;
}
else
{
return input.getType() == stack.getType() &&
input.hasItemMeta() == stack.hasItemMeta() &&
(!input.hasItemMeta() || Bukkit.getItemFactory().equals(input.getItemMeta(), stack.getItemMeta()));
}
}


/**
* This method groups input items in single itemstack with correct amount and returns it.
* Allows to remove duplicate items from list.
Expand All @@ -55,7 +76,8 @@ public static List<ItemStack> groupEqualItems(List<ItemStack> requiredItems, Set
ItemStack required = returnItems.get(i);

// Merge items which meta can be ignored or is similar to item in required list.
if (ignoreMetaData.contains(item.getType()) && item.getType().equals(required.getType()))
if (Utils.isSimilarNoDurability(required, item) ||
ignoreMetaData.contains(item.getType()) && item.getType().equals(required.getType()))
{
required.setAmount(required.getAmount() + item.getAmount());
isUnique = false;
Expand Down

0 comments on commit 36ad61b

Please sign in to comment.