2013-12-27 23:59:59 +01:00
|
|
|
package appeng.util.item;
|
|
|
|
|
2014-02-21 21:36:40 +01:00
|
|
|
import java.util.ArrayList;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.Collection;
|
|
|
|
import java.util.Iterator;
|
2014-02-13 06:24:07 +01:00
|
|
|
import java.util.LinkedList;
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.util.List;
|
|
|
|
import java.util.TreeMap;
|
|
|
|
|
2014-02-13 06:24:07 +01:00
|
|
|
import net.minecraftforge.oredict.OreDictionary;
|
2013-12-27 23:59:59 +01:00
|
|
|
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<StackType extends IAEStack> implements IItemList<StackType>
|
|
|
|
{
|
|
|
|
|
|
|
|
private final TreeMap<StackType, StackType> records = new TreeMap();
|
2014-02-21 21:36:40 +01:00
|
|
|
private final Class<? extends IAEStack> clz;
|
2014-05-22 04:08:13 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
// private int currentPriority = Integer.MIN_VALUE;
|
|
|
|
|
|
|
|
int iteration = Integer.MIN_VALUE;
|
|
|
|
public Throwable stacktrace;
|
2014-05-22 04:08:13 +02:00
|
|
|
|
|
|
|
public ItemList(Class<? extends IAEStack> cla) {
|
2014-02-21 21:36:40 +01:00
|
|
|
clz = cla;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-05-22 04:08:13 +02:00
|
|
|
private boolean checkStackType(StackType st)
|
2014-02-21 21:36:40 +01:00
|
|
|
{
|
2014-05-22 04:08:13 +02:00
|
|
|
if ( st == null )
|
2014-02-21 21:36:40 +01:00
|
|
|
return true;
|
2014-05-22 04:08:13 +02:00
|
|
|
|
|
|
|
if ( !clz.isInstance( st ) )
|
|
|
|
throw new RuntimeException( "WRONG TYPE - got " + st.getClass().getName() + " expected " + clz.getName() );
|
|
|
|
|
2014-02-21 21:36:40 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-05-22 04:08:13 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
synchronized public void add(StackType option)
|
|
|
|
{
|
2014-02-21 21:36:40 +01:00
|
|
|
if ( checkStackType( option ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
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.
|
|
|
|
{
|
2014-02-21 21:36:40 +01:00
|
|
|
if ( checkStackType( option ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
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.
|
|
|
|
{
|
2014-02-21 21:36:40 +01:00
|
|
|
if ( checkStackType( option ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
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.
|
|
|
|
{
|
2014-02-21 21:36:40 +01:00
|
|
|
if ( checkStackType( option ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
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<StackType> 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<StackType> i = iterator(); while (i.hasNext()) { StackType AEI =
|
|
|
|
* i.next(); if ( !AEI.isMeaninful() ) i.remove(); } }
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Override
|
|
|
|
synchronized public Iterator iterator()
|
|
|
|
{
|
|
|
|
return new MeanfulIterator( records.values().iterator() );
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
synchronized public StackType findPrecise(StackType i)
|
|
|
|
{
|
2014-02-21 21:36:40 +01:00
|
|
|
if ( checkStackType( i ) )
|
2013-12-27 23:59:59 +01:00
|
|
|
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 records.isEmpty();
|
|
|
|
}
|
|
|
|
|
2014-02-13 06:24:07 +01:00
|
|
|
public Collection<StackType> findFuzzyDamage(AEItemStack filter, FuzzyMode fuzzy, boolean ignoreMeta)
|
|
|
|
{
|
|
|
|
StackType low = (StackType) filter.getLow( fuzzy, ignoreMeta );
|
|
|
|
StackType high = (StackType) filter.getHigh( fuzzy, ignoreMeta );
|
2014-05-22 04:08:13 +02:00
|
|
|
return records.subMap( low, true, high, true ).values();
|
2014-02-13 06:24:07 +01:00
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public Collection<StackType> findFuzzy(StackType filter, FuzzyMode fuzzy)
|
|
|
|
{
|
2014-02-21 21:36:40 +01:00
|
|
|
if ( checkStackType( filter ) )
|
|
|
|
return new ArrayList();
|
2014-05-22 04:08:13 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
if ( filter instanceof IAEFluidStack )
|
|
|
|
return filter.equals( this ) ? (List<StackType>) Arrays.asList( new IAEFluidStack[] { (IAEFluidStack) filter } ) : (List<StackType>) Arrays
|
|
|
|
.asList( new IAEFluidStack[] {} );
|
|
|
|
|
|
|
|
AEItemStack ais = (AEItemStack) filter;
|
2014-02-13 06:24:07 +01:00
|
|
|
if ( ais.isOre() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-13 06:24:07 +01:00
|
|
|
OreRefrence or = ais.def.isOre;
|
|
|
|
if ( or.aeotherOptions.size() == 1 )
|
|
|
|
{
|
|
|
|
IAEItemStack is = or.aeotherOptions.get( 0 );
|
|
|
|
return findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
Collection<StackType> output = new LinkedList();
|
|
|
|
|
|
|
|
for (IAEItemStack is : or.aeotherOptions)
|
|
|
|
output.addAll( findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE ) );
|
|
|
|
|
|
|
|
return output;
|
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2014-02-13 06:24:07 +01:00
|
|
|
return findFuzzyDamage( ais, fuzzy, false );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
}
|