2015-04-05 15:57:46 +02:00
|
|
|
package com.pahimar.ee3.reference;
|
|
|
|
|
2023-01-03 17:47:36 +01:00
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Comparator;
|
|
|
|
import java.util.Set;
|
|
|
|
|
2016-05-18 19:53:13 +02:00
|
|
|
import com.pahimar.ee3.api.exchange.EnergyValueRegistryProxy;
|
2016-05-20 03:10:55 +02:00
|
|
|
import com.pahimar.ee3.exchange.WrappedStack;
|
2015-05-05 04:36:39 +02:00
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraftforge.oredict.OreDictionary;
|
2015-04-05 15:57:46 +02:00
|
|
|
|
2016-05-20 03:10:55 +02:00
|
|
|
public class Comparators {
|
2023-01-03 17:47:36 +01:00
|
|
|
public static final Comparator<Collection<ItemStack>> ITEM_STACK_COLLECTION_COMPARATOR
|
|
|
|
= new Comparator<Collection<ItemStack>>() {
|
|
|
|
@Override
|
|
|
|
public int compare(Collection<ItemStack> o1, Collection<ItemStack> o2) {
|
|
|
|
if (o1 != null && o2 != null) {
|
|
|
|
if (o1.size() == o2.size()) {
|
|
|
|
if (o1.containsAll(o2)) {
|
|
|
|
if (o2.containsAll(o1)) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return o1.size() - o2.size();
|
|
|
|
}
|
|
|
|
} else if (o1 != null) {
|
|
|
|
return -1;
|
|
|
|
} else if (o2 != null) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2015-05-05 04:36:39 +02:00
|
|
|
|
2023-01-03 17:47:36 +01:00
|
|
|
public static final Comparator<Set<WrappedStack>> WRAPPED_STACK_SET_COMPARATOR
|
|
|
|
= new Comparator<Set<WrappedStack>>() {
|
|
|
|
@Override
|
|
|
|
public int compare(
|
|
|
|
Set<WrappedStack> collection1, Set<WrappedStack> collection2
|
|
|
|
) {
|
|
|
|
if (collection1 != null && collection2 != null) {
|
|
|
|
if (collection1.size() == collection2.size()) {
|
|
|
|
if (collection1.containsAll(collection2)) {
|
|
|
|
if (collection2.containsAll(collection1)) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return collection1.size() - collection2.size();
|
|
|
|
}
|
|
|
|
} else if (collection1 != null) {
|
|
|
|
return -1;
|
|
|
|
} else if (collection2 != null) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-05-27 22:03:14 +02:00
|
|
|
|
2023-01-03 17:47:36 +01:00
|
|
|
public static final Comparator<String> STRING_COMPARATOR
|
|
|
|
= String::compareToIgnoreCase;
|
2016-05-27 22:03:14 +02:00
|
|
|
|
2023-01-03 17:47:36 +01:00
|
|
|
public static final Comparator<ItemStack> ID_COMPARATOR
|
|
|
|
= new Comparator<ItemStack>() {
|
|
|
|
@Override
|
|
|
|
public int compare(ItemStack itemStack1, ItemStack itemStack2) {
|
|
|
|
if (itemStack1 != null && itemStack2 != null) {
|
|
|
|
if (itemStack1.getItem() != null && itemStack2.getItem() != null) {
|
|
|
|
// Sort on id
|
|
|
|
if (Item.getIdFromItem(itemStack1.getItem())
|
|
|
|
- Item.getIdFromItem(itemStack2.getItem())
|
|
|
|
== 0) {
|
|
|
|
// Sort on item
|
|
|
|
if (itemStack1.getItem() == itemStack2.getItem()) {
|
|
|
|
// Then sort on meta
|
|
|
|
if ((itemStack1.getItemDamage()
|
|
|
|
== itemStack2.getItemDamage())
|
|
|
|
|| itemStack1.getItemDamage()
|
|
|
|
== OreDictionary.WILDCARD_VALUE
|
|
|
|
|| itemStack2.getItemDamage()
|
|
|
|
== OreDictionary.WILDCARD_VALUE) {
|
|
|
|
// Then sort on NBT
|
|
|
|
if (itemStack1.hasTagCompound()
|
|
|
|
&& itemStack2.hasTagCompound()) {
|
|
|
|
// Then sort on stack size
|
|
|
|
if (ItemStack.areItemStackTagsEqual(
|
|
|
|
itemStack1, itemStack2
|
|
|
|
)) {
|
|
|
|
return (
|
|
|
|
itemStack1.stackSize
|
|
|
|
- itemStack2.stackSize
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
itemStack1.getTagCompound().hashCode()
|
|
|
|
- itemStack2.getTagCompound().hashCode()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (!(itemStack1.hasTagCompound()) && itemStack2.hasTagCompound()) {
|
|
|
|
return -1;
|
|
|
|
} else if (itemStack1.hasTagCompound() && !(itemStack2.hasTagCompound())) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
itemStack1.stackSize - itemStack2.stackSize
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return (
|
|
|
|
itemStack1.getItemDamage()
|
|
|
|
- itemStack2.getItemDamage()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return itemStack1.getItem()
|
|
|
|
.getUnlocalizedName(itemStack1)
|
|
|
|
.compareToIgnoreCase(
|
|
|
|
itemStack2.getItem().getUnlocalizedName(
|
|
|
|
itemStack2
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
return Item.getIdFromItem(itemStack1.getItem())
|
|
|
|
- Item.getIdFromItem(itemStack2.getItem());
|
|
|
|
}
|
|
|
|
} else if (itemStack1.getItem() != null) {
|
|
|
|
return -1;
|
|
|
|
} else if (itemStack2.getItem() != null) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (itemStack1 != null) {
|
|
|
|
return -1;
|
|
|
|
} else if (itemStack2 != null) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-05-14 04:28:10 +02:00
|
|
|
|
2023-01-03 17:47:36 +01:00
|
|
|
public static final Comparator<ItemStack> DISPLAY_NAME_COMPARATOR
|
|
|
|
= new Comparator<ItemStack>() {
|
|
|
|
@Override
|
|
|
|
public int compare(ItemStack itemStack1, ItemStack itemStack2) {
|
|
|
|
if (itemStack1 != null && itemStack2 != null) {
|
|
|
|
if (itemStack1.getDisplayName().equalsIgnoreCase(
|
|
|
|
itemStack2.getDisplayName()
|
|
|
|
)) {
|
|
|
|
return ID_COMPARATOR.compare(itemStack1, itemStack2);
|
|
|
|
} else {
|
|
|
|
return itemStack1.getDisplayName().compareToIgnoreCase(
|
|
|
|
itemStack2.getDisplayName()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} else if (itemStack1 != null) {
|
|
|
|
return -1;
|
|
|
|
} else if (itemStack2 != null) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2016-05-20 03:10:55 +02:00
|
|
|
|
2023-01-03 17:47:36 +01:00
|
|
|
public static final Comparator<ItemStack>
|
|
|
|
ENERGY_VALUE_ITEM_STACK_COMPARATOR = new Comparator<ItemStack>() {
|
|
|
|
@Override
|
|
|
|
public int compare(ItemStack itemStack1, ItemStack itemStack2) {
|
|
|
|
if (itemStack1 != null && itemStack2 != null) {
|
|
|
|
if (EnergyValueRegistryProxy.hasEnergyValue(itemStack1)
|
|
|
|
&& EnergyValueRegistryProxy.hasEnergyValue(itemStack2)) {
|
|
|
|
return Float.compare(
|
|
|
|
EnergyValueRegistryProxy.getEnergyValue(itemStack1)
|
|
|
|
.getValue(),
|
|
|
|
EnergyValueRegistryProxy.getEnergyValue(itemStack2).getValue()
|
|
|
|
);
|
2016-05-26 18:02:37 +02:00
|
|
|
} else {
|
2023-01-03 17:47:36 +01:00
|
|
|
return ID_COMPARATOR.compare(itemStack1, itemStack2);
|
2015-05-05 04:36:39 +02:00
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
} else if (itemStack1 != null) {
|
2016-05-26 18:02:37 +02:00
|
|
|
return -1;
|
2023-01-03 17:47:36 +01:00
|
|
|
} else if (itemStack2 != null) {
|
2016-05-26 18:02:37 +02:00
|
|
|
return 1;
|
2023-01-03 17:47:36 +01:00
|
|
|
} else {
|
2016-05-26 18:02:37 +02:00
|
|
|
return 0;
|
2015-05-05 04:36:39 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
};
|
2015-04-05 15:57:46 +02:00
|
|
|
}
|