2022-11-22 15:26:15 +01:00
|
|
|
package com.pahimar.ee3.handler;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
|
|
|
import com.pahimar.ee3.util.ItemStackUtils;
|
|
|
|
import com.pahimar.ee3.util.LogHelper;
|
|
|
|
import net.minecraft.item.Item;
|
|
|
|
import net.minecraft.item.ItemStack;
|
2023-01-03 17:47:36 +01:00
|
|
|
import org.apache.logging.log4j.Level;
|
2022-11-22 15:26:15 +01:00
|
|
|
|
2023-01-03 17:47:36 +01:00
|
|
|
public class EquivalencyHandler {
|
2022-11-22 15:26:15 +01:00
|
|
|
private static final EquivalencyHandler instance;
|
|
|
|
private static ArrayList<ArrayList<ItemStack>> equivalencyList;
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public static EquivalencyHandler instance() {
|
|
|
|
return EquivalencyHandler.instance;
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public ArrayList<ArrayList<ItemStack>> getAllLists() {
|
|
|
|
return EquivalencyHandler.equivalencyList;
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public void addObjects(final Object obj1, final Object obj2) {
|
|
|
|
final ItemStack stack1 = ItemStackUtils.convertObjectToItemStack(obj1);
|
|
|
|
final ItemStack stack2 = ItemStackUtils.convertObjectToItemStack(obj2);
|
|
|
|
ArrayList<ItemStack> currentList = new ArrayList();
|
|
|
|
final Integer stack1Index = this.getIndexInList(stack1);
|
|
|
|
final Integer stack2Index = this.getIndexInList(stack2);
|
|
|
|
if (stack1Index != null && stack2Index != null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (stack1Index != null && stack2Index == null) {
|
|
|
|
currentList = EquivalencyHandler.equivalencyList.get(stack1Index);
|
|
|
|
currentList.add(stack2);
|
|
|
|
EquivalencyHandler.equivalencyList.set(stack1Index, currentList);
|
2023-01-03 17:47:36 +01:00
|
|
|
} else if (stack1Index == null && stack2Index != null) {
|
2022-11-22 15:26:15 +01:00
|
|
|
currentList = EquivalencyHandler.equivalencyList.get(stack2Index);
|
|
|
|
currentList.add(stack1);
|
|
|
|
EquivalencyHandler.equivalencyList.set(stack2Index, currentList);
|
2023-01-03 17:47:36 +01:00
|
|
|
} else if (stack1Index == null && stack2Index == null) {
|
2022-11-22 15:26:15 +01:00
|
|
|
currentList.add(stack1);
|
|
|
|
currentList.add(stack2);
|
|
|
|
EquivalencyHandler.equivalencyList.add(currentList);
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public void addObjects(final Object... objList) {
|
|
|
|
if (objList.length < 2) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
for (int i = 0; i < objList.length - 1; ++i) {
|
|
|
|
this.addObjects(objList[i], objList[i + 1]);
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public Integer getIndexInList(final Object obj) {
|
|
|
|
final ItemStack checkStack = ItemStackUtils.convertObjectToItemStack(obj);
|
|
|
|
for (int i = 0; i < EquivalencyHandler.equivalencyList.size(); ++i) {
|
2023-01-03 17:47:36 +01:00
|
|
|
final ArrayList<ItemStack> currentList
|
|
|
|
= EquivalencyHandler.equivalencyList.get(i);
|
2022-11-22 15:26:15 +01:00
|
|
|
for (final ItemStack currentStack : currentList) {
|
|
|
|
if (ItemStack.areItemStacksEqual(checkStack, currentStack)) {
|
|
|
|
return new Integer(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public Integer getIndexInList(final Item id, final int meta) {
|
|
|
|
for (int i = 0; i < EquivalencyHandler.equivalencyList.size(); ++i) {
|
2023-01-03 17:47:36 +01:00
|
|
|
final ArrayList<ItemStack> currentList
|
|
|
|
= EquivalencyHandler.equivalencyList.get(i);
|
2022-11-22 15:26:15 +01:00
|
|
|
for (final ItemStack currentStack : currentList) {
|
2023-01-03 17:47:36 +01:00
|
|
|
if (id == currentStack.getItem()
|
|
|
|
&& meta == currentStack.getItemDamage()) {
|
2022-11-22 15:26:15 +01:00
|
|
|
return new Integer(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public ArrayList<ItemStack> getEquivalencyList(final Object obj) {
|
|
|
|
final ItemStack checkStack = ItemStackUtils.convertObjectToItemStack(obj);
|
|
|
|
if (checkStack == null) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
for (final ArrayList<ItemStack> list : EquivalencyHandler.equivalencyList) {
|
|
|
|
for (final ItemStack currentStack : list) {
|
|
|
|
if (ItemStack.areItemStacksEqual(checkStack, currentStack)) {
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public ArrayList<ItemStack> getEquivalencyList(final Item id, final int meta) {
|
|
|
|
for (final ArrayList<ItemStack> list : EquivalencyHandler.equivalencyList) {
|
|
|
|
for (final ItemStack currentStack : list) {
|
2023-01-03 17:47:36 +01:00
|
|
|
if (id == currentStack.getItem()
|
|
|
|
&& meta == currentStack.getItemDamage()) {
|
2022-11-22 15:26:15 +01:00
|
|
|
return list;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public ItemStack getNextInList(final Object obj) {
|
|
|
|
final ItemStack checkStack = ItemStackUtils.convertObjectToItemStack(obj);
|
|
|
|
if (checkStack != null) {
|
|
|
|
return this.getNextInList(checkStack.getItem(), checkStack.getItemDamage());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public ItemStack getNextInList(final Item id, final int meta) {
|
|
|
|
final ArrayList<ItemStack> list = this.getEquivalencyList(id, meta);
|
|
|
|
ItemStack returnStack = null;
|
|
|
|
int i = 0;
|
|
|
|
if (list != null) {
|
|
|
|
if (list.size() == 1) {
|
|
|
|
return list.get(i);
|
|
|
|
}
|
|
|
|
while (i < list.size()) {
|
|
|
|
final ItemStack currentStack = list.get(i);
|
2023-01-03 17:47:36 +01:00
|
|
|
if (id == currentStack.getItem()
|
|
|
|
&& meta == currentStack.getItemDamage()) {
|
2022-11-22 15:26:15 +01:00
|
|
|
returnStack = list.get((i + 1) % list.size());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnStack;
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public ItemStack getPrevInList(final Object obj) {
|
|
|
|
final ItemStack checkStack = ItemStackUtils.convertObjectToItemStack(obj);
|
|
|
|
if (checkStack != null) {
|
|
|
|
return this.getPrevInList(checkStack.getItem(), checkStack.getItemDamage());
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public ItemStack getPrevInList(final Item id, final int meta) {
|
|
|
|
final ArrayList<ItemStack> list = this.getEquivalencyList(id, meta);
|
|
|
|
ItemStack returnStack = null;
|
|
|
|
int i = 0;
|
|
|
|
if (list != null) {
|
|
|
|
if (list.size() == 1) {
|
|
|
|
return list.get(i);
|
|
|
|
}
|
|
|
|
while (i < list.size()) {
|
|
|
|
final ItemStack currentStack = list.get(i);
|
2023-01-03 17:47:36 +01:00
|
|
|
if (id == currentStack.getItem()
|
|
|
|
&& meta == currentStack.getItemDamage()) {
|
2022-11-22 15:26:15 +01:00
|
|
|
final int index = (i - 1 + list.size()) % list.size();
|
|
|
|
returnStack = list.get(index);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return returnStack;
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public boolean areEquivalent(final Object obj1, final Object obj2) {
|
2023-01-03 17:47:36 +01:00
|
|
|
return this.getEquivalencyList(obj1) != null
|
|
|
|
&& this.getEquivalencyList(obj2) != null
|
|
|
|
&& ((ItemStackUtils.convertObjectToItemStack(obj1).getItem()
|
|
|
|
== ItemStackUtils.convertObjectToItemStack(obj2).getItem()
|
|
|
|
&& ItemStackUtils.convertObjectToItemStack(obj1).getItemDamage()
|
|
|
|
== ItemStackUtils.convertObjectToItemStack(obj2).getItemDamage())
|
|
|
|
|| this.getEquivalencyList(obj1).equals(this.getEquivalencyList(obj2)));
|
2022-11-22 15:26:15 +01:00
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public boolean areWorldEquivalent(final Object obj1, final Object obj2) {
|
|
|
|
final ItemStack first = ItemStackUtils.convertObjectToItemStack(obj1);
|
|
|
|
if (first == null) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
final ItemStack second = ItemStackUtils.convertObjectToItemStack(obj2);
|
2023-01-03 17:47:36 +01:00
|
|
|
return second != null
|
|
|
|
&& (this.getEquivalencyList(first.getItem(), first.getItemDamage()) != null
|
|
|
|
&& this.getEquivalencyList(second.getItem(), second.getItemDamage())
|
|
|
|
!= null)
|
|
|
|
&& ((first.getItem() == second.getItem()
|
|
|
|
&& first.getItemDamage() == second.getItemDamage())
|
|
|
|
|| this.getEquivalencyList(first.getItem(), first.getItemDamage())
|
|
|
|
.equals(this.getEquivalencyList(
|
|
|
|
second.getItem(), second.getItemDamage()
|
|
|
|
)));
|
2022-11-22 15:26:15 +01:00
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
public void debug() {
|
|
|
|
int i = 0;
|
|
|
|
for (final ArrayList list : EquivalencyHandler.equivalencyList) {
|
|
|
|
LogHelper.log(Level.INFO, "equivalencyList[" + i + "]: " + list.toString());
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
2023-01-03 17:47:36 +01:00
|
|
|
|
2022-11-22 15:26:15 +01:00
|
|
|
static {
|
|
|
|
instance = new EquivalencyHandler();
|
|
|
|
EquivalencyHandler.equivalencyList = new ArrayList();
|
|
|
|
}
|
|
|
|
}
|