mirror of
https://github.com/Thorfusion/Mekanism-Community-Edition.git
synced 2026-11-25 14:24:35 +00:00
Overhaul InvStack to be more efficient, better designed and thoroughly documented. Also, add in sanity checks to IItemHandler wraps so we can fix any potential issues.
This commit is contained in:
1 parent
8c276b6b1a
commit
2e16622b84
6 files changed
+230
-167
No files matched your search
@@ -1,8 +1,8 @@
|
||||
package mekanism.common.content.transporter;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import mekanism.common.Mekanism;
|
||||
import mekanism.common.util.InventoryUtils;
|
||||
import mekanism.common.util.StackUtils;
|
||||
import net.minecraft.item.ItemStack;
|
||||
@@ -10,78 +10,111 @@ import net.minecraft.tileentity.TileEntity;
|
||||
import net.minecraft.util.EnumFacing;
|
||||
import net.minecraftforge.items.IItemHandler;
|
||||
|
||||
/**
|
||||
* An InvStack attaches to an inventory and tracks a specific homogenous item in a variable amount
|
||||
* of slots. An InvStack can either track the entirety of these slots or portions of their contents.
|
||||
* Utility methods in this class allow for the strategic removal of clustered items from
|
||||
* inventories.
|
||||
*
|
||||
* @author aidancbrady
|
||||
*
|
||||
*/
|
||||
public final class InvStack {
|
||||
|
||||
public TileEntity tileEntity;
|
||||
public ArrayList<ItemStack> itemStacks;
|
||||
public ArrayList<Integer> slotIDs;
|
||||
/** The TileEntity owning the container this InvStack belongs to. */
|
||||
private TileEntity tileEntity;
|
||||
|
||||
/**
|
||||
* A map associating the slot IDs in consideration to the amount of items in those slots we care
|
||||
* about. Note that the associated item counts may not be the full count of items in the actual
|
||||
* inventory slots.
|
||||
*/
|
||||
private Map<Integer, Integer> itemMap = new HashMap<>();
|
||||
|
||||
/** The item type of this InvStack. Will be null until we have an insertion via appendStack. */
|
||||
private HashedItem itemType;
|
||||
|
||||
/** The total amount of items tracked by this InvStack. */
|
||||
private int itemCount;
|
||||
|
||||
/** The side of the inventory we are accessing with this InvStack. */
|
||||
public EnumFacing side;
|
||||
|
||||
public InvStack(TileEntity inv, EnumFacing facing) {
|
||||
tileEntity = inv;
|
||||
itemStacks = new ArrayList<>();
|
||||
slotIDs = new ArrayList<>();
|
||||
side = facing;
|
||||
}
|
||||
|
||||
|
||||
public InvStack(TileEntity inv, int slotID, ItemStack stack, EnumFacing facing) {
|
||||
this(inv, stack, getMap(slotID, stack), facing);
|
||||
}
|
||||
|
||||
public InvStack(TileEntity inv, ItemStack stack, Map<Integer, Integer> idMap, EnumFacing facing) {
|
||||
tileEntity = inv;
|
||||
itemStacks = new ArrayList<>();
|
||||
slotIDs = new ArrayList<>();
|
||||
side = facing;
|
||||
|
||||
for(Map.Entry<Integer, Integer> entry : idMap.entrySet()) {
|
||||
appendStack(entry.getKey(), StackUtils.size(stack, entry.getValue()));
|
||||
}
|
||||
itemMap = idMap;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an ItemStack of the defined type and total item count tracked by this InvStack.
|
||||
*
|
||||
* @return the total ItemStack contents of this InvStack
|
||||
*/
|
||||
public ItemStack getStack() {
|
||||
int size = 0;
|
||||
|
||||
for (ItemStack stack : itemStacks) {
|
||||
size += stack.getCount();
|
||||
if (itemType == null || itemCount == 0) {
|
||||
return ItemStack.EMPTY;
|
||||
}
|
||||
|
||||
if (!itemStacks.isEmpty()) {
|
||||
ItemStack ret = itemStacks.get(0).copy();
|
||||
ret.setCount(size);
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
return ItemStack.EMPTY;
|
||||
return StackUtils.size(itemType.getStack(), itemCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends a slot ID and specified ItemStack count to the tracked map of this InvStack.
|
||||
*
|
||||
* @param id - slot ID of items to track
|
||||
* @param stack - an ItemStack representing the count of items to track in the given slot ID
|
||||
*/
|
||||
public void appendStack(int id, ItemStack stack) {
|
||||
slotIDs.add(id);
|
||||
itemStacks.add(stack);
|
||||
if (itemType == null) {
|
||||
itemType = new HashedItem(stack);
|
||||
}
|
||||
|
||||
itemMap.put(id, stack.getCount());
|
||||
itemCount += stack.getCount();
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a specified amount of items from the parent inventory.
|
||||
*
|
||||
* @param amount - the amount of items to remove
|
||||
*/
|
||||
public void use(int amount) {
|
||||
if (InventoryUtils.isItemHandler(tileEntity, side)) {
|
||||
IItemHandler handler = InventoryUtils.getItemHandler(tileEntity, side);
|
||||
if (!InventoryUtils.isItemHandler(tileEntity, side)) {
|
||||
Mekanism.logger
|
||||
.warn("An InvStack was wrapped around a non-IItemHandler inventory. This should not happen!");
|
||||
return;
|
||||
}
|
||||
|
||||
for (int i = 0; i < slotIDs.size(); i++) {
|
||||
ItemStack stack = itemStacks.get(i);
|
||||
int toUse = Math.min(amount, stack.getCount());
|
||||
handler.extractItem(slotIDs.get(i), toUse, false);
|
||||
amount -= toUse;
|
||||
IItemHandler handler = InventoryUtils.getItemHandler(tileEntity, side);
|
||||
|
||||
if (amount == 0) {
|
||||
return;
|
||||
}
|
||||
for (Map.Entry<Integer, Integer> entry : itemMap.entrySet()) {
|
||||
int toUse = Math.min(amount, entry.getValue());
|
||||
handler.extractItem(entry.getKey(), toUse, false);
|
||||
amount -= toUse;
|
||||
|
||||
if (amount == 0) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all the items being tracked by this InvStack.
|
||||
*/
|
||||
public void use() {
|
||||
use(getStack().getCount());
|
||||
}
|
||||
|
||||
|
||||
private static Map<Integer, Integer> getMap(int slotID, ItemStack stack) {
|
||||
Map<Integer, Integer> map = new HashMap<>();
|
||||
map.put(slotID, stack.getCount());
|
||||
|
||||
Reference in new issue
Block a user