2013-12-27 23:59:59 +01:00
|
|
|
package appeng.util.item;
|
|
|
|
|
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
import java.util.Iterator;
|
|
|
|
import java.util.WeakHashMap;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
import net.minecraft.item.Item;
|
2013-12-27 23:59:59 +01:00
|
|
|
import net.minecraft.item.ItemStack;
|
|
|
|
import net.minecraft.nbt.NBTTagCompound;
|
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.features.IItemComparison;
|
|
|
|
import appeng.api.storage.data.IAETagCompound;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* this is used for the shared NBT Cache.
|
|
|
|
*/
|
|
|
|
public class AESharedNBT extends NBTTagCompound implements IAETagCompound
|
|
|
|
{
|
|
|
|
|
2014-03-22 04:58:03 +01:00
|
|
|
private Item item;
|
2014-02-09 02:34:52 +01:00
|
|
|
private int meta, hash;
|
2013-12-27 23:59:59 +01:00
|
|
|
public SharedSearchObject sso;
|
|
|
|
private IItemComparison comp;
|
|
|
|
|
2014-01-20 17:41:37 +01:00
|
|
|
public int getHash()
|
|
|
|
{
|
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public IItemComparison getSpecialComparison()
|
|
|
|
{
|
|
|
|
return comp;
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
private AESharedNBT(Item itemID, int damageValue) {
|
|
|
|
super();
|
2014-03-22 04:58:03 +01:00
|
|
|
item = itemID;
|
2013-12-27 23:59:59 +01:00
|
|
|
meta = damageValue;
|
|
|
|
}
|
|
|
|
|
2014-03-22 04:58:03 +01:00
|
|
|
public AESharedNBT(int fakeValue) {
|
|
|
|
super();
|
|
|
|
item = null;
|
|
|
|
meta = 0;
|
|
|
|
hash = fakeValue;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
|
|
|
public NBTTagCompound getNBTTagCompoundCopy()
|
|
|
|
{
|
|
|
|
return (NBTTagCompound) copy();
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
public static AESharedNBT createFromCompound(Item itemID, int damageValue, NBTTagCompound c)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
AESharedNBT x = new AESharedNBT( itemID, damageValue );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
// c.getTags()
|
|
|
|
Iterator var2 = c.func_150296_c().iterator();
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
while (var2.hasNext())
|
|
|
|
{
|
2014-02-09 02:34:52 +01:00
|
|
|
String name = (String) var2.next();
|
|
|
|
x.setTag( name, c.getTag( name ).copy() );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
x.hash = Platform.NBTOrderlessHash( c );
|
|
|
|
|
|
|
|
ItemStack isc = new ItemStack( itemID, 1, damageValue );
|
|
|
|
isc.setTagCompound( c );
|
|
|
|
x.comp = AEApi.instance().registries().specialComparson().getSpecialComparion( isc );
|
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object par1Obj)
|
|
|
|
{
|
|
|
|
if ( par1Obj instanceof AESharedNBT )
|
|
|
|
return this == par1Obj;
|
|
|
|
return super.equals( par1Obj );
|
|
|
|
}
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
public boolean matches(Item itemid2, int meta2, int orderlessHash)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-03-22 04:58:03 +01:00
|
|
|
return itemid2 == item && meta == meta2 && hash == orderlessHash;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public boolean comparePreciseWithRegistry(AESharedNBT tagCompound)
|
|
|
|
{
|
|
|
|
if ( this == tagCompound )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ( comp != null && tagCompound.comp != null )
|
|
|
|
{
|
|
|
|
return comp.sameAsPrecise( tagCompound.comp );
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean compareFuzzyWithRegistry(AESharedNBT tagCompound)
|
|
|
|
{
|
|
|
|
if ( this == tagCompound )
|
|
|
|
return true;
|
|
|
|
if ( tagCompound == null )
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if ( comp == tagCompound.comp )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if ( comp != null )
|
|
|
|
{
|
|
|
|
return comp.sameAsFuzzy( tagCompound.comp );
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Shared Tag Compound Cache.
|
|
|
|
*/
|
|
|
|
private static WeakHashMap<SharedSearchObject, WeakReference<SharedSearchObject>> sharedTagCompounts = new WeakHashMap();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Debug purposes.
|
|
|
|
*/
|
|
|
|
public static int sharedTagLoad()
|
|
|
|
{
|
|
|
|
return sharedTagCompounts.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* returns true if the compound is part of the shared compound system ( and can thus be compared directly ).
|
|
|
|
*/
|
|
|
|
public static boolean isShared(NBTTagCompound ta)
|
|
|
|
{
|
|
|
|
return ta instanceof AESharedNBT;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns an NBT Compound that is used for accelerating comparisons.
|
|
|
|
*/
|
2014-01-01 10:03:00 +01:00
|
|
|
synchronized public static NBTTagCompound getSharedTagCompound(NBTTagCompound tagCompound, ItemStack s)
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
|
|
|
if ( tagCompound.hasNoTags() )
|
|
|
|
return null;
|
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
Item itemid = s.getItem();
|
2013-12-27 23:59:59 +01:00
|
|
|
int meta = -1;
|
2014-02-09 02:34:52 +01:00
|
|
|
if ( s.getItem() != null && s.isItemStackDamageable() && s.getHasSubtypes() )
|
2013-12-27 23:59:59 +01:00
|
|
|
meta = s.getItemDamage();
|
|
|
|
|
|
|
|
if ( isShared( tagCompound ) )
|
|
|
|
return tagCompound;
|
|
|
|
|
|
|
|
SharedSearchObject sso = new SharedSearchObject( itemid, meta, tagCompound );
|
|
|
|
|
|
|
|
WeakReference<SharedSearchObject> c = sharedTagCompounts.get( sso );
|
|
|
|
if ( c != null )
|
|
|
|
{
|
|
|
|
SharedSearchObject cg = c.get();
|
|
|
|
if ( cg != null )
|
|
|
|
return cg.shared; // I don't think I really need to check this
|
|
|
|
// as its already certain to exist..
|
|
|
|
}
|
|
|
|
|
|
|
|
AESharedNBT clone = AESharedNBT.createFromCompound( itemid, meta, tagCompound );
|
|
|
|
sso.compound = (NBTTagCompound) sso.compound.copy(); // prevent
|
|
|
|
// modification
|
|
|
|
// of data based
|
|
|
|
// on original
|
|
|
|
// item.
|
|
|
|
sso.shared = clone;
|
|
|
|
clone.sso = sso;
|
|
|
|
|
|
|
|
sharedTagCompounts.put( sso, new WeakReference<SharedSearchObject>( sso ) );
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|