package appeng.util.item; import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Iterator; import java.util.LinkedList; import java.util.List; import java.util.TreeMap; import net.minecraftforge.oredict.OreDictionary; import appeng.api.config.FuzzyMode; import appeng.api.storage.data.IAEFluidStack; import appeng.api.storage.data.IAEItemStack; import appeng.api.storage.data.IAEStack; import appeng.api.storage.data.IItemList; public final class ItemList implements IItemList { private final TreeMap records = new TreeMap(); private final Class clz; // private int currentPriority = Integer.MIN_VALUE; int iteration = Integer.MIN_VALUE; public Throwable stacktrace; public ItemList(Class cla) { clz = cla; } private boolean checkStackType(StackType st) { if ( st == null ) return true; if ( !clz.isInstance( st ) ) throw new RuntimeException( "WRONG TYPE - got " + st.getClass().getName() + " expected " + clz.getName() ); return false; } @Override synchronized public void add(StackType option) { if ( checkStackType( option ) ) return; StackType st = records.get( option ); if ( st != null ) { // st.setPriority( currentPriority ); st.add( option ); return; } StackType opt = (StackType) option.copy(); // opt.setPriority( currentPriority ); records.put( opt, opt ); } @Override synchronized public void addStorage(StackType option) // adds a stack as // stored. { if ( checkStackType( option ) ) return; StackType st = records.get( option ); if ( st != null ) { // st.setPriority( currentPriority ); st.incStackSize( option.getStackSize() ); return; } StackType opt = (StackType) option.copy(); // opt.setPriority( currentPriority ); records.put( opt, opt ); } @Override synchronized public void addCrafting(StackType option) // adds a stack as // craftable. { if ( checkStackType( option ) ) return; StackType st = records.get( option ); if ( st != null ) { // st.setPriority( currentPriority ); st.setCraftable( true ); return; } StackType opt = (StackType) option.copy(); // opt.setPriority( currentPriority ); opt.setStackSize( 0 ); opt.setCraftable( true ); records.put( opt, opt ); } @Override synchronized public void addRequestable(StackType option) // adds a stack // as // requestable. { if ( checkStackType( option ) ) return; StackType st = records.get( option ); if ( st != null ) { // st.setPriority( currentPriority ); ((IAEItemStack) st).setCountRequestable( ((IAEItemStack) st).getCountRequestable() + ((IAEItemStack) option).getCountRequestable() ); return; } StackType opt = (StackType) option.copy(); // opt.setPriority( currentPriority ); opt.setStackSize( 0 ); opt.setCraftable( false ); opt.setCountRequestable( opt.getCountRequestable() ); records.put( opt, opt ); } @Override synchronized public StackType getFirstItem() { Iterator i = this.iterator(); while (i.hasNext()) return i.next(); return null; } @Override synchronized public void resetStatus() { for (StackType i : this) i.reset(); } /* * synchronized public void clean() { Iterator i = iterator(); while (i.hasNext()) { StackType AEI = * i.next(); if ( !AEI.isMeaningful() ) i.remove(); } } */ @Override synchronized public Iterator iterator() { return new MeanfulIterator( records.values().iterator() ); } @Override synchronized public StackType findPrecise(StackType i) { if ( checkStackType( i ) ) return null; StackType is = records.get( i ); if ( is != null ) { return is; } return null; } @Override synchronized public int size() { return records.values().size(); } @Override public boolean isEmpty() { return !iterator().hasNext(); } public Collection findFuzzyDamage(AEItemStack filter, FuzzyMode fuzzy, boolean ignoreMeta) { StackType low = (StackType) filter.getLow( fuzzy, ignoreMeta ); StackType high = (StackType) filter.getHigh( fuzzy, ignoreMeta ); return records.subMap( low, true, high, true ).descendingMap().values(); } @Override public Collection findFuzzy(StackType filter, FuzzyMode fuzzy) { if ( checkStackType( filter ) ) return new ArrayList(); if ( filter instanceof IAEFluidStack ) return filter.equals( this ) ? (List) Arrays.asList( new IAEFluidStack[] { (IAEFluidStack) filter } ) : (List) Arrays .asList( new IAEFluidStack[] {} ); AEItemStack ais = (AEItemStack) filter; if ( ais.isOre() ) { OreRefrence or = ais.def.isOre; if ( or.getAEEquivilients().size() == 1 ) { IAEItemStack is = or.getAEEquivilients().get( 0 ); return findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE ); } else { Collection output = new LinkedList(); for (IAEItemStack is : or.getAEEquivilients()) output.addAll( findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE ) ); return output; } } return findFuzzyDamage( ais, fuzzy, false ); } }