2014-11-14 12:02:52 +01:00
|
|
|
/*
|
|
|
|
* This file is part of Applied Energistics 2.
|
|
|
|
* Copyright (c) 2013 - 2014, AlgorithmX2, All rights reserved.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Applied Energistics 2 is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
|
|
|
* along with Applied Energistics 2. If not, see <http://www.gnu.org/licenses/lgpl>.
|
|
|
|
*/
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
package appeng.util.item;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import java.lang.ref.WeakReference;
|
|
|
|
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;
|
2015-12-24 02:07:03 +01:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
import appeng.api.AEApi;
|
|
|
|
import appeng.api.features.IItemComparison;
|
|
|
|
import appeng.api.storage.data.IAETagCompound;
|
|
|
|
import appeng.util.Platform;
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
/*
|
|
|
|
* this is used for the shared NBT Cache.
|
|
|
|
*/
|
|
|
|
public class AESharedNBT extends NBTTagCompound implements IAETagCompound
|
|
|
|
{
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
/*
|
|
|
|
* Shared Tag Compound Cache.
|
|
|
|
*/
|
|
|
|
private static final WeakHashMap<SharedSearchObject, WeakReference<SharedSearchObject>> SHARED_TAG_COMPOUND = new WeakHashMap<SharedSearchObject, WeakReference<SharedSearchObject>>();
|
2014-09-29 09:54:34 +02:00
|
|
|
private final Item item;
|
|
|
|
private final int meta;
|
2015-10-08 15:42:42 +02:00
|
|
|
private SharedSearchObject sso;
|
2015-04-03 08:54:31 +02:00
|
|
|
private int hash;
|
2013-12-27 23:59:59 +01:00
|
|
|
private IItemComparison comp;
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
private AESharedNBT( final Item itemID, final int damageValue )
|
2014-01-20 17:41:37 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.item = itemID;
|
|
|
|
this.meta = damageValue;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public AESharedNBT( final int fakeValue )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
this.item = null;
|
|
|
|
this.meta = 0;
|
|
|
|
this.hash = fakeValue;
|
2014-03-22 04:58:03 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
/*
|
|
|
|
* Debug purposes.
|
|
|
|
*/
|
|
|
|
public static int sharedTagLoad()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return SHARED_TAG_COMPOUND.size();
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
/*
|
|
|
|
* Returns an NBT Compound that is used for accelerating comparisons.
|
|
|
|
*/
|
2015-10-08 15:42:42 +02:00
|
|
|
static synchronized NBTTagCompound getSharedTagCompound( final NBTTagCompound tagCompound, final ItemStack s )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
if( tagCompound.hasNoTags() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return null;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final Item item = s.getItem();
|
2015-04-03 08:54:31 +02:00
|
|
|
int meta = -1;
|
|
|
|
if( s.getItem() != null && s.isItemStackDamageable() && s.getHasSubtypes() )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
meta = s.getItemDamage();
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
|
|
|
if( isShared( tagCompound ) )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return tagCompound;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final SharedSearchObject sso = new SharedSearchObject( item, meta, tagCompound );
|
2015-04-03 08:54:31 +02:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final WeakReference<SharedSearchObject> c = SHARED_TAG_COMPOUND.get( sso );
|
2015-04-03 08:54:31 +02:00
|
|
|
if( c != null )
|
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final SharedSearchObject cg = c.get();
|
2015-04-03 08:54:31 +02:00
|
|
|
if( cg != null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2015-10-08 15:42:42 +02:00
|
|
|
return cg.getShared(); // I don't think I really need to check this
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
// as its already certain to exist..
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final AESharedNBT clone = AESharedNBT.createFromCompound( item, meta, tagCompound );
|
2015-10-08 15:42:42 +02:00
|
|
|
sso.setCompound( (NBTTagCompound) sso.getCompound().copy() ); // prevent
|
2015-04-03 08:54:31 +02:00
|
|
|
// modification
|
|
|
|
// of data based
|
|
|
|
// on original
|
|
|
|
// item.
|
2015-10-08 15:42:42 +02:00
|
|
|
sso.setShared( clone );
|
2015-04-03 08:54:31 +02:00
|
|
|
clone.sso = sso;
|
|
|
|
|
|
|
|
SHARED_TAG_COMPOUND.put( sso, new WeakReference<SharedSearchObject>( sso ) );
|
|
|
|
return clone;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* returns true if the compound is part of the shared compound system ( and can thus be compared directly ).
|
|
|
|
*/
|
2015-09-30 14:24:40 +02:00
|
|
|
public static boolean isShared( final NBTTagCompound ta )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
return ta instanceof AESharedNBT;
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
private static AESharedNBT createFromCompound( final Item itemID, final int damageValue, final NBTTagCompound c )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final AESharedNBT x = new AESharedNBT( itemID, damageValue );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2014-02-09 02:34:52 +01:00
|
|
|
// c.getTags()
|
2015-09-30 14:24:40 +02:00
|
|
|
for( final Object o : c.getKeySet() )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-09-30 14:24:40 +02:00
|
|
|
final String name = (String) o;
|
2014-02-09 02:34:52 +01:00
|
|
|
x.setTag( name, c.getTag( name ).copy() );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2016-11-04 09:27:52 +01:00
|
|
|
x.hash = Platform.itemComparisons().createUnorderedNbtHash( c );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
final ItemStack isc = new ItemStack( itemID, 1, damageValue );
|
2013-12-27 23:59:59 +01:00
|
|
|
isc.setTagCompound( c );
|
2014-09-20 22:41:19 +02:00
|
|
|
x.comp = AEApi.instance().registries().specialComparison().getSpecialComparison( isc );
|
2013-12-27 23:59:59 +01:00
|
|
|
|
|
|
|
return x;
|
|
|
|
}
|
|
|
|
|
2015-10-08 15:42:42 +02:00
|
|
|
int getHash()
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
return this.hash;
|
|
|
|
}
|
|
|
|
|
2013-12-27 23:59:59 +01:00
|
|
|
@Override
|
2015-04-03 08:54:31 +02:00
|
|
|
public NBTTagCompound getNBTTagCompoundCopy()
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
return (NBTTagCompound) this.copy();
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public IItemComparison getSpecialComparison()
|
|
|
|
{
|
|
|
|
return this.comp;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2015-09-30 14:24:40 +02:00
|
|
|
public boolean equals( final Object par1Obj )
|
2015-04-03 08:54:31 +02:00
|
|
|
{
|
|
|
|
if( par1Obj instanceof AESharedNBT )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
return this == par1Obj;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
return super.equals( par1Obj );
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public boolean matches( final Item item, final int meta, final int orderlessHash )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return item == this.item && this.meta == meta && this.hash == orderlessHash;
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public boolean comparePreciseWithRegistry( final AESharedNBT tagCompound )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this == tagCompound )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
return true;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.comp != null && tagCompound.comp != null )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.comp.sameAsPrecise( tagCompound.comp );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-30 14:24:40 +02:00
|
|
|
public boolean compareFuzzyWithRegistry( final AESharedNBT tagCompound )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this == tagCompound )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
return true;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2015-04-03 08:54:31 +02:00
|
|
|
if( tagCompound == null )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
return false;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.comp == tagCompound.comp )
|
2015-04-29 02:30:53 +02:00
|
|
|
{
|
2013-12-27 23:59:59 +01:00
|
|
|
return true;
|
2015-04-29 02:30:53 +02:00
|
|
|
}
|
2013-12-27 23:59:59 +01:00
|
|
|
|
2015-04-03 08:54:31 +02:00
|
|
|
if( this.comp != null )
|
2013-12-27 23:59:59 +01:00
|
|
|
{
|
2014-12-29 15:13:47 +01:00
|
|
|
return this.comp.sameAsFuzzy( tagCompound.comp );
|
2013-12-27 23:59:59 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|