2021-03-03 01:56:19 +01:00
|
|
|
package de.ellpeck.prettypipes.misc;
|
|
|
|
|
2021-12-02 12:31:04 +01:00
|
|
|
import net.minecraft.resources.ResourceLocation;
|
|
|
|
import net.minecraft.world.item.ItemStack;
|
2021-03-03 01:56:19 +01:00
|
|
|
|
|
|
|
import java.util.function.BiFunction;
|
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
|
|
|
public class ItemEquality {
|
|
|
|
|
2021-12-02 12:31:04 +01:00
|
|
|
public static final ItemEquality DAMAGE = new ItemEquality((stack, filter) -> stack.getDamageValue() == filter.getDamageValue(), false, Type.DAMAGE);
|
2021-12-28 12:04:53 +01:00
|
|
|
public static final ItemEquality NBT = new ItemEquality(ItemStack::tagMatches, false, Type.NBT);
|
2021-03-03 01:56:19 +01:00
|
|
|
public static final ItemEquality MOD = new ItemEquality((stack, filter) -> stack.getItem().getCreatorModId(stack).equals(filter.getItem().getCreatorModId(filter)), true, Type.MOD);
|
|
|
|
|
|
|
|
public final Type type;
|
|
|
|
private final BiFunction<ItemStack, ItemStack, Boolean> filter;
|
|
|
|
private final boolean ignoreItemEquality;
|
|
|
|
|
|
|
|
ItemEquality(BiFunction<ItemStack, ItemStack, Boolean> filter, boolean ignoreItemEquality, Type type) {
|
|
|
|
this.filter = filter;
|
|
|
|
this.ignoreItemEquality = ignoreItemEquality;
|
|
|
|
this.type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static ItemEquality tag(ResourceLocation tag) {
|
|
|
|
return new ItemEquality((stack, filter) -> stack.getItem().getTags().contains(tag), true, Type.TAG);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static boolean compareItems(ItemStack stack, ItemStack filter, ItemEquality... types) {
|
2021-12-02 12:31:04 +01:00
|
|
|
var equal = ItemStack.isSameIgnoreDurability(stack, filter);
|
2021-03-03 01:56:19 +01:00
|
|
|
if (types.length <= 0)
|
|
|
|
return equal;
|
2021-12-02 12:31:04 +01:00
|
|
|
for (var type : types) {
|
2021-03-03 01:56:19 +01:00
|
|
|
if (!type.ignoreItemEquality && !equal)
|
|
|
|
return false;
|
|
|
|
if (!type.filter.apply(stack, filter))
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum Type {
|
|
|
|
DAMAGE(() -> ItemEquality.DAMAGE),
|
|
|
|
NBT(() -> ItemEquality.NBT),
|
|
|
|
MOD(() -> ItemEquality.MOD),
|
|
|
|
TAG(null);
|
|
|
|
|
|
|
|
private final Supplier<ItemEquality> defaultInstance;
|
|
|
|
|
|
|
|
Type(Supplier<ItemEquality> defaultInstance) {
|
|
|
|
this.defaultInstance = defaultInstance;
|
|
|
|
}
|
|
|
|
|
|
|
|
public ItemEquality getDefaultInstance() {
|
|
|
|
return this.defaultInstance.get();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|