Skip to content

Instantly share code, notes, and snippets.

@habnabit
Created November 14, 2015 17:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save habnabit/1e6ee2884553cfcd008f to your computer and use it in GitHub Desktop.
Save habnabit/1e6ee2884553cfcd008f to your computer and use it in GitHub Desktop.
bibliocraft/mekanism compatibility
//
// Decompiled by Procyon v0.5.29
//
package jds.bibliocraft;
import net.minecraft.nbt.NBTBase;
import java.util.List;
import java.util.Collections;
import net.minecraft.util.StatCollector;
import jds.bibliocraft.helpers.InventoryListItem;
import jds.bibliocraft.helpers.SortedListItem;
import jds.bibliocraft.helpers.InventorySet;
import net.minecraft.world.World;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import java.util.ArrayList;
import net.minecraft.tileentity.TileEntity;
import powercrystals.minefactoryreloaded.api.IDeepStorageUnit;
public class BiblioSortingHelper
{
public static ArrayList<ItemStack> getStackForBuiltinLabel(final TileEntity tile) {
final IInventory inv = (IInventory)tile;
final ArrayList<ItemStack> chestOfStuff = new ArrayList();
for (int i = 0; i < inv.func_70302_i_(); ++i) {
final ItemStack thing = inv.func_70301_a(i);
if (thing != null) {
boolean alreadyInList = false;
for (int n = 0; n < chestOfStuff.size(); ++n) {
final ItemStack oldThing = chestOfStuff.get(n);
if (oldThing != null && oldThing.func_77969_a(thing) && ItemStack.func_77970_a(thing, oldThing)) {
final ItemStack itemStack = oldThing;
itemStack.field_77994_a += thing.field_77994_a;
final int extras = getExtraStackQuanityFromSpecialInventories(tile, oldThing.field_77994_a, i);
if (extras != -1) {
oldThing.field_77994_a = extras;
chestOfStuff.set(n, oldThing);
}
alreadyInList = true;
break;
}
}
if (!alreadyInList) {
final ItemStack newThing = thing.func_77946_l();
final int extras2 = getExtraStackQuanityFromSpecialInventories(tile, newThing.field_77994_a, i);
if (extras2 != -1) {
newThing.field_77994_a = extras2;
chestOfStuff.add(newThing);
}
}
}
}
return (ArrayList<ItemStack>)chestOfStuff;
}
public static int getExtraStackQuanityFromSpecialInventories(final TileEntity tile, int oldValue, final int slotNum) {
if (tile != null) {
if (tile instanceof IDeepStorageUnit) {
ItemStack stack = ((IDeepStorageUnit)tile).getStoredItemType();
if (stack != null) {
return stack.field_77994_a;
}
}
final NBTTagCompound tags = new NBTTagCompound();
tile.func_145841_b(tags);
if (tags != null) {
if (tags.func_74764_b("id") && tags.func_74779_i("id").contentEquals("TileEntityBarrel") && tags.func_74764_b("storage")) {
final NBTTagCompound storage = tags.func_74775_l("storage");
if (storage != null && storage.func_74764_b("amount")) {
return storage.func_74762_e("amount");
}
}
if (tags.func_74764_b("id") && tags.func_74779_i("id").contentEquals("factoryDeepStorageUnit") && tags.func_74764_b("storedQuantity")) {
return tags.func_74762_e("storedQuantity");
}
if (tags.func_74764_b("id") && tags.func_74779_i("id").contentEquals("tilequantumdsu") && tags.func_74764_b("storedQuantity")) {
final int quan = tags.func_74762_e("storedQuantity");
if (slotNum == 0) {
return oldValue;
}
if (hasSecondSlotItem(tags)) {
--oldValue;
}
if (slotNum == 2) {
return -1;
}
return oldValue + quan;
}
else {
if (tags.func_74764_b("id") && tags.func_74779_i("id").contentEquals("StorageDrawers:tileDrawersStandard")) {
final NBTTagList tagList = tags.func_150295_c("Slots", 10);
if (tagList != null) {
final int tagCount = tagList.func_74745_c();
final NBTTagCompound thing = tagList.func_150305_b(slotNum - tagCount);
if (thing != null && thing.func_74764_b("Count")) {
return thing.func_74762_e("Count");
}
}
}
if (tags.func_74764_b("id") && tags.func_74779_i("id").contentEquals("thermalexpansion.Cache") && tags.func_74764_b("Item")) {
final NBTTagCompound thing2 = tags.func_74775_l("Item");
if (thing2 != null && thing2.func_74764_b("Count")) {
return oldValue + thing2.func_74762_e("Count");
}
}
if (tags.func_74764_b("id") && tags.func_74779_i("id").contentEquals("StorageDrawers:tileDrawersComp")) {
final NBTTagList tagList = tags.func_150295_c("Slots", 10);
if (tagList != null && tags.func_74764_b("Count")) {
final NBTTagCompound slot1 = tagList.func_150305_b(0);
final int count = tags.func_74762_e("Count");
if (slot1 != null && slotNum == 3) {
final int mod = tags.func_74762_e("Conv0");
final int measuredQuantity = count / mod;
return measuredQuantity;
}
return -1;
}
}
}
}
}
return oldValue;
}
public static boolean hasSecondSlotItem(final NBTTagCompound tags) {
if (tags.func_74764_b("Items")) {
final NBTTagList list = tags.func_150295_c("Items", 10);
if (list != null) {
for (int i = 0; i < list.func_74745_c(); ++i) {
final NBTTagCompound comp = list.func_150305_b(i);
if (comp.func_74764_b("Slot") && comp.func_74765_d("Slot") == 2) {
return true;
}
}
}
}
return false;
}
public static ItemStack getLargestStackInList(final ArrayList<ItemStack> list) {
ItemStack bestStack = null;
for (int i = 0; i < list.size(); ++i) {
final ItemStack stack = list.get(i);
if (stack != null) {
if (bestStack != null) {
if (stack.field_77994_a > bestStack.field_77994_a) {
bestStack = stack;
}
}
else {
bestStack = stack;
}
}
}
return bestStack;
}
public static ArrayList<InventorySet> getUnsortedInventoryListFromStockroomCatalog(final ItemStack catalog, final World world) {
final ArrayList<InventorySet> unsortedList = new ArrayList<InventorySet>();
final NBTTagCompound tags = catalog.func_77978_p();
if (tags != null) {
final NBTTagList invList = tags.func_150295_c("inventoryList", 10);
if (invList != null && invList.func_74745_c() > 0) {
for (int i = 0; i < invList.func_74745_c(); ++i) {
final NBTTagCompound invTag = invList.func_150305_b(i);
if (invTag != null && invTag.func_74764_b("x") && invTag.func_74764_b("y") && invTag.func_74764_b("z")) {
final int x = invTag.func_74762_e("x");
final int y = invTag.func_74762_e("y");
final int z = invTag.func_74762_e("z");
final String name = invTag.func_74779_i("name");
ArrayList<ItemStack> inventoryStacks = new ArrayList<ItemStack>();
final TileEntity tile = world.func_147438_o(x, y, z);
if (tile != null && tile instanceof IInventory) {
inventoryStacks = getStackForBuiltinLabel(tile);
}
if (inventoryStacks.size() > 0) {
unsortedList.add(new InventorySet(name, x, y, z, inventoryStacks));
}
}
}
}
}
return unsortedList;
}
public static ArrayList<SortedListItem> buildUnsortedItemList(final ArrayList<InventorySet> inventoryset) {
final ArrayList<SortedListItem> itemList = new ArrayList<SortedListItem>();
for (int i = 0; i < inventoryset.size(); ++i) {
final InventorySet invSet = inventoryset.get(i);
if (invSet != null) {
final ArrayList<ItemStack> inv = (ArrayList<ItemStack>)invSet.inventoryList;
for (int j = 0; j < inv.size(); ++j) {
final ItemStack stack = inv.get(j);
if (stack != null) {
final InventoryListItem newInvListItem = new InventoryListItem(stack.func_77977_a(), stack.field_77994_a, invSet.inventoryName, invSet.tileX, invSet.tileY, invSet.tileZ);
boolean listHasItem = false;
for (int k = 0; k < itemList.size(); ++k) {
final SortedListItem item = itemList.get(k);
if (item.itemName.contentEquals(stack.func_77977_a())) {
ArrayList<InventoryListItem> listOfInventories = item.inventoryList;
boolean alreadyHasCurrentInventory = false;
for (int n = 0; n < listOfInventories.size(); ++n) {
final InventoryListItem invListItem = listOfInventories.get(n);
if (invListItem.inventoryName.contentEquals(invSet.inventoryName) && invListItem.tileX == invSet.tileX && invListItem.tileY == invSet.tileY && invListItem.tileZ == invSet.tileZ) {
final InventoryListItem inventoryListItem = invListItem;
inventoryListItem.itemQuantity += stack.field_77994_a;
alreadyHasCurrentInventory = true;
listOfInventories.set(n, invListItem);
break;
}
}
if (!alreadyHasCurrentInventory) {
listOfInventories.add(newInvListItem);
}
listOfInventories = sortListOfInventories(listOfInventories);
item.inventoryList = listOfInventories;
final SortedListItem sortedListItem = item;
sortedListItem.itemQuantity += stack.field_77994_a;
listHasItem = true;
break;
}
}
if (!listHasItem) {
final ArrayList<InventoryListItem> listOfInventories2 = new ArrayList<InventoryListItem>();
listOfInventories2.add(newInvListItem);
itemList.add(new SortedListItem(stack.func_77977_a(), stack.field_77994_a, listOfInventories2));
}
}
}
}
}
return itemList;
}
public static ArrayList<InventoryListItem> sortListOfInventories(final ArrayList<InventoryListItem> list) {
final ArrayList<InventoryListItem> sortedList = new ArrayList<InventoryListItem>();
final ArrayList<InventoryListItem> tempList = (ArrayList<InventoryListItem>)list.clone();
for (int i = 0; i < list.size(); ++i) {
final int highestQuanIndex = getHighestQuantityInventoryFromList(tempList);
if (highestQuanIndex != -1) {
sortedList.add(tempList.get(highestQuanIndex));
tempList.remove(highestQuanIndex);
}
}
return sortedList;
}
public static int getHighestQuantityInventoryFromList(final ArrayList<InventoryListItem> list) {
int quantity = 0;
int index = -1;
for (int i = 0; i < list.size(); ++i) {
final InventoryListItem item = list.get(i);
if (item.itemQuantity > quantity) {
quantity = item.itemQuantity;
index = i;
}
}
return index;
}
public static ArrayList<SortedListItem> getUnsortedList(final ItemStack catalog, final World world) {
return buildUnsortedItemList(getUnsortedInventoryListFromStockroomCatalog(catalog, world));
}
public static ArrayList<SortedListItem> getSortedListByQuantity(final ArrayList<SortedListItem> list) {
final ArrayList<SortedListItem> sortedList = new ArrayList<SortedListItem>();
final ArrayList<SortedListItem> tempList = (ArrayList<SortedListItem>)list.clone();
for (int i = 0; i < list.size(); ++i) {
final int highestQuanIndex = getHighestQuantityItemFromList(tempList);
if (highestQuanIndex != -1) {
sortedList.add(tempList.get(highestQuanIndex));
tempList.remove(highestQuanIndex);
}
}
return sortedList;
}
public static int getHighestQuantityItemFromList(final ArrayList<SortedListItem> list) {
int quantity = 0;
int index = -1;
for (int i = 0; i < list.size(); ++i) {
final SortedListItem item = list.get(i);
if (item.itemQuantity > quantity) {
quantity = item.itemQuantity;
index = i;
}
}
return index;
}
public static ArrayList<SortedListItem> getSortedListByAlphabet(final ArrayList<SortedListItem> list) {
final ArrayList<SortedListItem> sortedList = new ArrayList<SortedListItem>();
final List<String> localizedNameList = new ArrayList<String>();
for (int i = 0; i < list.size(); ++i) {
final SortedListItem item = list.get(i);
if (item != null) {
localizedNameList.add(StatCollector.func_74838_a(item.itemName + ".name"));
}
}
Collections.sort(localizedNameList);
for (int i = 0; i < localizedNameList.size(); ++i) {
final String localName = localizedNameList.get(i);
for (int j = 0; j < list.size(); ++j) {
final SortedListItem item2 = list.get(j);
if (localName.contentEquals(StatCollector.func_74838_a(item2.itemName + ".name"))) {
sortedList.add(item2);
break;
}
}
}
return sortedList;
}
public static NBTTagList convertArrayListToNBTTagList(final ArrayList<SortedListItem> list) {
final NBTTagList tagList = new NBTTagList();
for (int i = 0; i < list.size(); ++i) {
final SortedListItem item = list.get(i);
final NBTTagCompound subTag = new NBTTagCompound();
final NBTTagList invTagList = new NBTTagList();
subTag.func_74778_a("itemName", item.itemName);
subTag.func_74768_a("itemQuantity", item.itemQuantity);
final ArrayList<InventoryListItem> invList = item.inventoryList;
for (int j = 0; j < invList.size(); ++j) {
final NBTTagCompound invTag = new NBTTagCompound();
final InventoryListItem invItem = invList.get(j);
invTag.func_74778_a("itemName", invItem.itemName);
invTag.func_74778_a("inventoryName", invItem.inventoryName);
invTag.func_74768_a("itemQuantity", invItem.itemQuantity);
invTag.func_74768_a("tileX", invItem.tileX);
invTag.func_74768_a("tileY", invItem.tileY);
invTag.func_74768_a("tileZ", invItem.tileZ);
invTagList.func_74742_a((NBTBase)invTag);
}
subTag.func_74782_a("inventories", (NBTBase)invTagList);
tagList.func_74742_a((NBTBase)subTag);
}
return tagList;
}
public static NBTTagCompound getFullyLoadedSortedListsInNBTTags(final ItemStack catalog, final World world) {
final NBTTagCompound tags = new NBTTagCompound();
final ArrayList<SortedListItem> unsortedList = getUnsortedList(catalog, world);
final ArrayList<SortedListItem> alphaList = getSortedListByAlphabet(unsortedList);
final ArrayList<SortedListItem> quantaList = getSortedListByQuantity(unsortedList);
tags.func_74782_a("alphaList", (NBTBase)convertArrayListToNBTTagList(alphaList));
tags.func_74782_a("quantaList", (NBTBase)convertArrayListToNBTTagList(quantaList));
return tags;
}
public static ArrayList<SortedListItem> convertNBTTagListToArrayList(final NBTTagList tags) {
final ArrayList<SortedListItem> list = new ArrayList<SortedListItem>();
for (int i = 0; i < tags.func_74745_c(); ++i) {
final NBTTagCompound subTag = tags.func_150305_b(i);
final NBTTagList invTags = subTag.func_150295_c("inventories", 10);
final ArrayList<InventoryListItem> invList = new ArrayList<InventoryListItem>();
for (int j = 0; j < invTags.func_74745_c(); ++j) {
final NBTTagCompound inv = invTags.func_150305_b(j);
invList.add(new InventoryListItem(inv.func_74779_i("itemName"), inv.func_74762_e("itemQuantity"), inv.func_74779_i("inventoryName"), inv.func_74762_e("tileX"), inv.func_74762_e("tileY"), inv.func_74762_e("tileZ")));
}
final SortedListItem item = new SortedListItem(subTag.func_74779_i("itemName"), subTag.func_74762_e("itemQuantity"), invList);
list.add(item);
}
return list;
}
public static void listTester(final ArrayList<SortedListItem> list) {
for (int i = 0; i < list.size(); ++i) {
final SortedListItem item = list.get(i);
if (item != null) {
System.out.println("item name: " + StatCollector.func_74838_a(item.itemName + ".name") + " Quantity: " + item.itemQuantity + " Inside x inventories: " + item.inventoryList.size());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment