mirror of
https://github.com/GTNewHorizons/StorageDrawers.git
synced 2026-11-19 11:59:19 +00:00
Support multiple oredict-compatible items coexisting in drawer system via IInventory and AE2 access.
This commit is contained in:
1 parent
a79fe59264
commit
3efdc56d32
5 files changed
+431
-55
No files matched your search
@@ -0,0 +1,18 @@
|
||||
package com.jaquadro.minecraft.storagedrawers.api.storage;
|
||||
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
public interface ISmartGroup
|
||||
{
|
||||
/**
|
||||
* Gets a lazy enumeration of all slots that will accept at least one item of the given stack, ordered by
|
||||
* insertion preference.
|
||||
*/
|
||||
Iterable<Integer> enumerateDrawersForInsertion (ItemStack stack, boolean strict);
|
||||
|
||||
/**
|
||||
* Gets a lazy enumeration of all slots that will provide at least one item of the given stack, ordered by
|
||||
* extraction preference.
|
||||
*/
|
||||
Iterable<Integer> enumerateDrawersForExtraction (ItemStack stack, boolean strict);
|
||||
}
|
||||
@@ -8,9 +8,13 @@ import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IShroudable;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IVoidable;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute;
|
||||
import com.jaquadro.minecraft.storagedrawers.block.BlockSlave;
|
||||
import com.jaquadro.minecraft.storagedrawers.util.ItemMetaListRegistry;
|
||||
import com.jaquadro.minecraft.storagedrawers.util.ItemMetaRegistry;
|
||||
import com.jaquadro.minecraft.storagedrawers.util.UniqueMetaIdentifier;
|
||||
import net.minecraft.block.Block;
|
||||
import net.minecraft.entity.player.EntityPlayer;
|
||||
import net.minecraft.inventory.ISidedInventory;
|
||||
import net.minecraft.item.Item;
|
||||
import net.minecraft.item.ItemStack;
|
||||
import net.minecraft.nbt.NBTTagCompound;
|
||||
import net.minecraft.network.NetworkManager;
|
||||
@@ -22,13 +26,14 @@ import net.minecraftforge.common.util.ForgeDirection;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
public class TileEntityController extends TileEntity implements IDrawerGroup, IPriorityGroup, ISidedInventory
|
||||
public class TileEntityController extends TileEntity implements IDrawerGroup, IPriorityGroup, ISmartGroup, ISidedInventory
|
||||
{
|
||||
private static final int PRI_VOID = 0;
|
||||
private static final int PRI_LOCKED = 1;
|
||||
private static final int PRI_NORMAL = 2;
|
||||
private static final int PRI_EMPTY = 3;
|
||||
private static final int PRI_DISABLED = 4;
|
||||
private static final int PRI_LOCKED_EMPTY = 4;
|
||||
private static final int PRI_DISABLED = 5;
|
||||
|
||||
private static final int DEPTH_LIMIT = 12;
|
||||
private static final int[] emptySlots = new int[0];
|
||||
@@ -85,7 +90,12 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
|
||||
|
||||
IDrawer drawer = group.getDrawer(drawerSlot);
|
||||
if (drawer.isEmpty()) {
|
||||
return PRI_EMPTY;
|
||||
if ((drawer instanceof ILockable && ((ILockable) drawer).isLocked(LockAttribute.LOCK_EMPTY)) ||
|
||||
(group instanceof ILockable && ((ILockable) group).isLocked(LockAttribute.LOCK_EMPTY))) {
|
||||
return PRI_LOCKED_EMPTY;
|
||||
}
|
||||
else
|
||||
return PRI_EMPTY;
|
||||
}
|
||||
|
||||
if ((drawer instanceof IVoidable && ((IVoidable) drawer).isVoid()) ||
|
||||
@@ -108,6 +118,9 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
|
||||
private List<SlotRecord> invSlotList = new ArrayList<SlotRecord>();
|
||||
private List<SlotRecord> drawerSlotList = new ArrayList<SlotRecord>();
|
||||
|
||||
private ItemMetaListRegistry<SlotRecord> invPrimaryLookup = new ItemMetaListRegistry<SlotRecord>();
|
||||
private ItemMetaListRegistry<SlotRecord> drawerPrimaryLookup = new ItemMetaListRegistry<SlotRecord>();
|
||||
|
||||
private int[] inventorySlots = new int[0];
|
||||
private int[] drawerSlots = new int[0];
|
||||
private int[] autoSides = new int[] { 0, 1, 2, 3, 4, 5 };
|
||||
@@ -261,9 +274,12 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
|
||||
populateNodes(xCoord, yCoord, zCoord);
|
||||
|
||||
flattenLists();
|
||||
inventorySlots = sortSlotRecords(invSlotList, true); //sortInventorySlots();
|
||||
inventorySlots = sortSlotRecords(invSlotList, true);
|
||||
drawerSlots = sortSlotRecords(drawerSlotList, false);
|
||||
|
||||
rebuildPrimaryLookup(invPrimaryLookup, invSlotList, true);
|
||||
rebuildPrimaryLookup(drawerPrimaryLookup, drawerSlotList, false);
|
||||
|
||||
if (preCount != inventorySlots.length && (preCount == 0 || inventorySlots.length == 0)) {
|
||||
if (!worldObj.isRemote)
|
||||
markDirty();
|
||||
@@ -293,6 +309,27 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
|
||||
return slotMap;
|
||||
}
|
||||
|
||||
private void rebuildPrimaryLookup (ItemMetaListRegistry<SlotRecord> lookup, List<SlotRecord> records, boolean invBased) {
|
||||
lookup.clear();
|
||||
|
||||
for (SlotRecord record : records) {
|
||||
IDrawerGroup group = getGroupForCoord(record.coord);
|
||||
if (group == null)
|
||||
continue;
|
||||
|
||||
int drawerSlot = (invBased) ? group.getDrawerInventory().getDrawerSlot(record.slot) : record.slot;
|
||||
if (!group.isDrawerEnabled(drawerSlot))
|
||||
continue;
|
||||
|
||||
IDrawer drawer = group.getDrawer(drawerSlot);
|
||||
if (drawer.isEmpty())
|
||||
continue;
|
||||
|
||||
ItemStack item = drawer.getStoredItemPrototype();
|
||||
lookup.register(item.getItem(), item.getItemDamage(), record);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean containsNullEntries (List<SlotRecord> list) {
|
||||
int nullCount = 0;
|
||||
for (int i = 0, n = list.size(); i < n; i++) {
|
||||
@@ -638,6 +675,20 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
|
||||
if (inventory == null)
|
||||
return false;
|
||||
|
||||
if (slot >= invSlotList.size())
|
||||
return false;
|
||||
|
||||
SlotRecord record = invSlotList.get(slot);
|
||||
List<SlotRecord> primaryRecords = invPrimaryLookup.getEntries(stack.getItem(), stack.getItemDamage());
|
||||
if (primaryRecords != null && !primaryRecords.contains(record)) {
|
||||
for (SlotRecord candidate : primaryRecords) {
|
||||
IDrawerGroup candidateGroup = getGroupForCoord(candidate.coord);
|
||||
IDrawerInventory candidateInventory = candidateGroup.getDrawerInventory();
|
||||
if (candidateInventory.canInsertItem(candidate.slot, stack))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return inventory.canInsertItem(getLocalInvSlot(slot), stack);
|
||||
}
|
||||
|
||||
@@ -728,6 +779,127 @@ public class TileEntityController extends TileEntity implements IDrawerGroup, IP
|
||||
if (inventory == null)
|
||||
return false;
|
||||
|
||||
if (slot >= invSlotList.size())
|
||||
return false;
|
||||
|
||||
SlotRecord record = invSlotList.get(slot);
|
||||
List<SlotRecord> primaryRecords = invPrimaryLookup.getEntries(stack.getItem(), stack.getItemDamage());
|
||||
if (primaryRecords != null && !primaryRecords.contains(record)) {
|
||||
for (SlotRecord candidate : primaryRecords) {
|
||||
IDrawerGroup candidateGroup = getGroupForCoord(candidate.coord);
|
||||
IDrawerInventory candidateInventory = candidateGroup.getDrawerInventory();
|
||||
if (candidateInventory.isItemValidForSlot(candidate.slot, stack))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return inventory.isItemValidForSlot(getLocalInvSlot(slot), stack);
|
||||
}
|
||||
|
||||
private class DrawerStackIterator implements Iterable<Integer>
|
||||
{
|
||||
private ItemStack stack;
|
||||
private boolean strict;
|
||||
private boolean insert;
|
||||
|
||||
public DrawerStackIterator (ItemStack stack, boolean strict, boolean insert) {
|
||||
this.stack = stack;
|
||||
this.strict = strict;
|
||||
this.insert = insert;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<Integer> iterator () {
|
||||
return new Iterator<Integer> ()
|
||||
{
|
||||
List<SlotRecord> primaryRecords = drawerPrimaryLookup.getEntries(stack.getItem(), stack.getItemDamage());
|
||||
Iterator<SlotRecord> iter1;
|
||||
int index2;
|
||||
Integer nextSlot = null;
|
||||
|
||||
@Override
|
||||
public boolean hasNext () {
|
||||
if (nextSlot == null)
|
||||
advance();
|
||||
return nextSlot != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer next () {
|
||||
if (nextSlot == null)
|
||||
advance();
|
||||
|
||||
Integer slot = nextSlot;
|
||||
nextSlot = null;
|
||||
return slot;
|
||||
}
|
||||
|
||||
private void advance () {
|
||||
if (iter1 == null && primaryRecords != null && primaryRecords.size() > 0)
|
||||
iter1 = primaryRecords.iterator();
|
||||
|
||||
if (iter1 != null) {
|
||||
while (iter1.hasNext()) {
|
||||
SlotRecord candidate = iter1.next();
|
||||
IDrawerGroup candidateGroup = getGroupForCoord(candidate.coord);
|
||||
IDrawer drawer = candidateGroup.getDrawer(candidate.slot);
|
||||
|
||||
if (insert) {
|
||||
if (!(drawer.canItemBeStored(stack) && (drawer.isEmpty() || drawer.getRemainingCapacity() > 0)))
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
if (!(drawer.canItemBeExtracted(stack) && drawer.getStoredItemCount() > 0))
|
||||
continue;
|
||||
}
|
||||
|
||||
int slot = drawerSlotList.indexOf(candidate);
|
||||
if (slot > -1) {
|
||||
nextSlot = slot;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (; index2 < drawerSlots.length; index2++) {
|
||||
int slot = drawerSlots[index2];
|
||||
if (!isDrawerEnabled(slot))
|
||||
continue;
|
||||
|
||||
IDrawer drawer = getDrawer(slot);
|
||||
if (strict) {
|
||||
ItemStack proto = drawer.getStoredItemPrototype();
|
||||
if (proto != null && !proto.isItemEqual(stack))
|
||||
continue;
|
||||
}
|
||||
|
||||
if (insert) {
|
||||
if (!(drawer.canItemBeStored(stack) && (drawer.isEmpty() || drawer.getRemainingCapacity() > 0)))
|
||||
continue;
|
||||
}
|
||||
else {
|
||||
if (!(drawer.canItemBeExtracted(stack) && drawer.getStoredItemCount() > 0))
|
||||
continue;
|
||||
}
|
||||
|
||||
SlotRecord record = drawerSlotList.get(slot);
|
||||
if (primaryRecords != null && primaryRecords.contains(record))
|
||||
continue;
|
||||
|
||||
nextSlot = slot;
|
||||
index2++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
public Iterable<Integer> enumerateDrawersForInsertion (ItemStack stack, boolean strict) {
|
||||
return new DrawerStackIterator(stack, strict, true);
|
||||
}
|
||||
|
||||
public Iterable<Integer> enumerateDrawersForExtraction (ItemStack stack, boolean strict) {
|
||||
return new DrawerStackIterator(stack, strict, false);
|
||||
}
|
||||
}
|
||||
@@ -7,15 +7,14 @@ import appeng.api.storage.IMEInventory;
|
||||
import appeng.api.storage.StorageChannel;
|
||||
import appeng.api.storage.data.IAEItemStack;
|
||||
import appeng.api.storage.data.IItemList;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawer;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.IDrawerGroup;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.IFractionalDrawer;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.IPriorityGroup;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.*;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.ILockable;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.IVoidable;
|
||||
import com.jaquadro.minecraft.storagedrawers.api.storage.attribute.LockAttribute;
|
||||
import net.minecraft.item.ItemStack;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
public class DrawerMEInventory implements IMEInventory<IAEItemStack>
|
||||
{
|
||||
final IDrawerGroup group;
|
||||
@@ -26,52 +25,71 @@ public class DrawerMEInventory implements IMEInventory<IAEItemStack>
|
||||
|
||||
@Override
|
||||
public IAEItemStack injectItems (IAEItemStack input, Actionable type, BaseActionSource src) {
|
||||
int[] order = null;
|
||||
if (group instanceof IPriorityGroup)
|
||||
order = ((IPriorityGroup) group).getAccessibleDrawerSlots();
|
||||
|
||||
long itemsLeft = input.getStackSize();
|
||||
for (int i = 0, n = group.getDrawerCount(); i < n; i++) {
|
||||
int slot = (order != null) ? order[i] : i;
|
||||
if (!group.isDrawerEnabled(slot))
|
||||
continue;
|
||||
|
||||
IDrawer drawer = group.getDrawer(slot);
|
||||
ItemStack itemProto = drawer.getStoredItemPrototype();
|
||||
if (itemProto != null) {
|
||||
if (drawer.canItemBeStored(input.getItemStack())) {
|
||||
itemsLeft = injectItemsIntoDrawer(drawer, itemsLeft, type);
|
||||
if (drawer instanceof IVoidable && ((IVoidable) drawer).isVoid())
|
||||
itemsLeft = 0;
|
||||
if (itemsLeft == 0)
|
||||
return null;
|
||||
}
|
||||
if (group instanceof ISmartGroup) {
|
||||
for (int slot : ((ISmartGroup) group).enumerateDrawersForInsertion(input.getItemStack(), false)) {
|
||||
IDrawer drawer = group.getDrawer(slot);
|
||||
ItemStack itemProto = drawer.getStoredItemPrototype();
|
||||
if (itemProto == null)
|
||||
drawer.setStoredItem(input.getItemStack(), 0);
|
||||
|
||||
itemsLeft = injectItemsIntoDrawer(drawer, itemsLeft, type);
|
||||
|
||||
if (drawer instanceof IVoidable && ((IVoidable) drawer).isVoid())
|
||||
itemsLeft = 0;
|
||||
if (itemsLeft == 0)
|
||||
return null;
|
||||
}
|
||||
}
|
||||
else {
|
||||
int[] order = null;
|
||||
if (group instanceof IPriorityGroup)
|
||||
order = ((IPriorityGroup) group).getAccessibleDrawerSlots();
|
||||
|
||||
for (int i = 0, n = group.getDrawerCount(); i < n; i++) {
|
||||
int slot = (order != null) ? order[i] : i;
|
||||
if (!group.isDrawerEnabled(slot))
|
||||
continue;
|
||||
|
||||
if (group instanceof ILockable && ((ILockable) group).isLocked(LockAttribute.LOCK_EMPTY))
|
||||
continue;
|
||||
for (int i = 0, n = group.getDrawerCount(); i < n; i++) {
|
||||
int slot = (order != null) ? order[i] : i;
|
||||
if (!group.isDrawerEnabled(slot))
|
||||
continue;
|
||||
|
||||
IDrawer drawer = group.getDrawer(slot);
|
||||
ItemStack itemProto = drawer.getStoredItemPrototype();
|
||||
IDrawer drawer = group.getDrawer(slot);
|
||||
ItemStack itemProto = drawer.getStoredItemPrototype();
|
||||
if (itemProto != null) {
|
||||
if (drawer.canItemBeStored(input.getItemStack())) {
|
||||
itemsLeft = injectItemsIntoDrawer(drawer, itemsLeft, type);
|
||||
if (drawer instanceof IVoidable && ((IVoidable) drawer).isVoid())
|
||||
itemsLeft = 0;
|
||||
if (itemsLeft == 0)
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemProto == null && drawer instanceof ILockable && ((ILockable) drawer).isLocked(LockAttribute.LOCK_EMPTY))
|
||||
continue;
|
||||
for (int i = 0, n = group.getDrawerCount(); i < n; i++) {
|
||||
int slot = (order != null) ? order[i] : i;
|
||||
if (!group.isDrawerEnabled(slot))
|
||||
continue;
|
||||
|
||||
if (itemProto == null) {
|
||||
itemProto = input.getItemStack();
|
||||
if (drawer.canItemBeStored(itemProto)) {
|
||||
drawer.setStoredItem(itemProto, 0);
|
||||
itemsLeft = injectItemsIntoDrawer(drawer, itemsLeft, type);
|
||||
if (drawer instanceof IVoidable && ((IVoidable) drawer).isVoid())
|
||||
itemsLeft = 0;
|
||||
if (itemsLeft == 0)
|
||||
return null;
|
||||
if (group instanceof ILockable && ((ILockable) group).isLocked(LockAttribute.LOCK_EMPTY))
|
||||
continue;
|
||||
|
||||
IDrawer drawer = group.getDrawer(slot);
|
||||
ItemStack itemProto = drawer.getStoredItemPrototype();
|
||||
|
||||
if (itemProto == null && drawer instanceof ILockable && ((ILockable) drawer).isLocked(LockAttribute.LOCK_EMPTY))
|
||||
continue;
|
||||
|
||||
if (itemProto == null) {
|
||||
itemProto = input.getItemStack();
|
||||
if (drawer.canItemBeStored(itemProto)) {
|
||||
drawer.setStoredItem(itemProto, 0);
|
||||
itemsLeft = injectItemsIntoDrawer(drawer, itemsLeft, type);
|
||||
if (drawer instanceof IVoidable && ((IVoidable) drawer).isVoid())
|
||||
itemsLeft = 0;
|
||||
if (itemsLeft == 0)
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -110,18 +128,13 @@ public class DrawerMEInventory implements IMEInventory<IAEItemStack>
|
||||
|
||||
@Override
|
||||
public IAEItemStack extractItems (IAEItemStack request, Actionable mode, BaseActionSource src) {
|
||||
int[] order = null;
|
||||
if (group instanceof IPriorityGroup)
|
||||
order = ((IPriorityGroup) group).getAccessibleDrawerSlots();
|
||||
|
||||
long itemsLeft = request.getStackSize();
|
||||
for (int i = 0, n = group.getDrawerCount(); i < n; i++) {
|
||||
int slot = (order != null) ? order[i] : i;
|
||||
if (!group.isDrawerEnabled(slot))
|
||||
continue;
|
||||
if (group instanceof ISmartGroup) {
|
||||
for (int slot : ((ISmartGroup) group).enumerateDrawersForExtraction(request.getItemStack(), true)) {
|
||||
if (itemsLeft == 0)
|
||||
break;
|
||||
|
||||
IDrawer drawer = group.getDrawer(slot);
|
||||
if (drawer.canItemBeExtracted(request.getItemStack())) {
|
||||
IDrawer drawer = group.getDrawer(slot);
|
||||
int itemCount = drawer.getStoredItemCount();
|
||||
if (itemsLeft > itemCount) {
|
||||
if (mode == Actionable.MODULATE)
|
||||
@@ -136,6 +149,33 @@ public class DrawerMEInventory implements IMEInventory<IAEItemStack>
|
||||
}
|
||||
}
|
||||
}
|
||||
else {
|
||||
int[] order = null;
|
||||
if (group instanceof IPriorityGroup)
|
||||
order = ((IPriorityGroup) group).getAccessibleDrawerSlots();
|
||||
|
||||
|
||||
for (int i = 0, n = group.getDrawerCount(); i < n; i++) {
|
||||
int slot = (order != null) ? order[i] : i;
|
||||
if (!group.isDrawerEnabled(slot))
|
||||
continue;
|
||||
|
||||
IDrawer drawer = group.getDrawer(slot);
|
||||
if (drawer.canItemBeExtracted(request.getItemStack())) {
|
||||
int itemCount = drawer.getStoredItemCount();
|
||||
if (itemsLeft > itemCount) {
|
||||
if (mode == Actionable.MODULATE)
|
||||
drawer.setStoredItemCount(0);
|
||||
itemsLeft -= itemCount;
|
||||
} else {
|
||||
if (mode == Actionable.MODULATE)
|
||||
drawer.setStoredItemCount(itemCount - (int) itemsLeft);
|
||||
itemsLeft = 0;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (itemsLeft < request.getStackSize()) {
|
||||
ItemStack fulfillment = request.getItemStack().copy();
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
package com.jaquadro.minecraft.storagedrawers.util;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class ItemMetaListRegistry<E>
|
||||
{
|
||||
private ItemMetaRegistry<List<E>> registry;
|
||||
private boolean compactTopLevel;
|
||||
|
||||
public ItemMetaListRegistry () {
|
||||
this(false);
|
||||
}
|
||||
|
||||
public ItemMetaListRegistry (boolean compactTopLevel) {
|
||||
this.registry = new ItemMetaRegistry<List<E>>(compactTopLevel);
|
||||
this.compactTopLevel = compactTopLevel;
|
||||
}
|
||||
|
||||
public void register (Item item, int meta, E entry) {
|
||||
List<E> list = registry.getEntry(item, meta);
|
||||
if (list == null) {
|
||||
list = new ArrayList<E>();
|
||||
registry.register(item, meta, list);
|
||||
}
|
||||
|
||||
list.add(entry);
|
||||
}
|
||||
|
||||
public List<E> getEntries (Item item, int meta) {
|
||||
return registry.getEntry(item, meta);
|
||||
}
|
||||
|
||||
public void remove (Item item, int meta) {
|
||||
registry.remove(item, meta);
|
||||
}
|
||||
|
||||
public void clear (Item item, int meta) {
|
||||
List<E> list = registry.getEntry(item, meta);
|
||||
if (list != null)
|
||||
list.clear();
|
||||
}
|
||||
|
||||
public void clear (Item item) {
|
||||
for (Map.Entry<Integer, List<E>> map : registry.entrySet(item))
|
||||
map.getValue().clear();
|
||||
}
|
||||
|
||||
public void clear () {
|
||||
for (Map.Entry<Item, Map<Integer, List<E>>> map : registry.entrySet()) {
|
||||
for (List<E> list : map.getValue().values())
|
||||
list.clear();
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Map.Entry<Item, Map<Integer, List<E>>>> entrySet () {
|
||||
return registry.entrySet();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
package com.jaquadro.minecraft.storagedrawers.util;
|
||||
|
||||
import net.minecraft.item.Item;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class ItemMetaRegistry<E>
|
||||
{
|
||||
private Map<Item, Map<Integer, E>> registry;
|
||||
private boolean compactTopLevel;
|
||||
|
||||
public ItemMetaRegistry () {
|
||||
registry = new HashMap<Item, Map<Integer, E>>();
|
||||
}
|
||||
|
||||
public ItemMetaRegistry (boolean compactTopLevel) {
|
||||
this();
|
||||
this.compactTopLevel = compactTopLevel;
|
||||
}
|
||||
|
||||
public void register (Item item, int meta, E entry) {
|
||||
Map<Integer, E> metamap = registry.get(item);
|
||||
if (metamap == null) {
|
||||
metamap = new HashMap<Integer, E>();
|
||||
registry.put(item, metamap);
|
||||
}
|
||||
|
||||
metamap.put(meta, entry);
|
||||
}
|
||||
|
||||
public E getEntry (Item item, int meta) {
|
||||
Map<Integer, E> metamap = registry.get(item);
|
||||
if (metamap == null)
|
||||
return null;
|
||||
|
||||
return metamap.get(meta);
|
||||
}
|
||||
|
||||
public void remove (Item item, int meta) {
|
||||
Map<Integer, E> metamap = registry.get(item);
|
||||
if (metamap == null)
|
||||
return;
|
||||
|
||||
metamap.remove(meta);
|
||||
if (compactTopLevel && metamap.isEmpty())
|
||||
registry.remove(item);
|
||||
}
|
||||
|
||||
public void clear (Item item) {
|
||||
Map<Integer, E> metamap = registry.get(item);
|
||||
if (metamap == null)
|
||||
return;
|
||||
|
||||
metamap.clear();
|
||||
if (compactTopLevel)
|
||||
registry.remove(item);
|
||||
}
|
||||
|
||||
public void clear () {
|
||||
if (compactTopLevel)
|
||||
registry.clear();
|
||||
else {
|
||||
for (Item item : registry.keySet())
|
||||
clear(item);
|
||||
}
|
||||
}
|
||||
|
||||
public Set<Map.Entry<Item, Map<Integer, E>>> entrySet () {
|
||||
return registry.entrySet();
|
||||
}
|
||||
|
||||
public Set<Map.Entry<Integer, E>> entrySet (Item item) {
|
||||
Map<Integer, E> metamap = registry.get(item);
|
||||
if (metamap == null) {
|
||||
metamap = new HashMap<Integer, E>();
|
||||
registry.put(item, metamap);
|
||||
}
|
||||
|
||||
return metamap.entrySet();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user