Applied-Energistics-2-tiler.../src/main/java/appeng/util/item/AESharedNBT.java

219 lines
5.1 KiB
Java
Raw Normal View History

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>.
*/
package appeng.util.item;
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;
2014-02-09 02:34:52 +01:00
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
2015-12-24 02:07:03 +01:00
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
{
/*
* Shared Tag Compound Cache.
*/
private static final WeakHashMap<SharedSearchObject, WeakReference<SharedSearchObject>> SHARED_TAG_COMPOUND = new WeakHashMap<SharedSearchObject, WeakReference<SharedSearchObject>>();
private final Item item;
private final int meta;
private SharedSearchObject sso;
private int hash;
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;
}
2015-09-30 14:24:40 +02:00
public AESharedNBT( final int fakeValue )
{
2014-12-29 15:13:47 +01:00
this.item = null;
this.meta = 0;
this.hash = fakeValue;
}
/*
* Debug purposes.
*/
public static int sharedTagLoad()
{
return SHARED_TAG_COMPOUND.size();
}
/*
* Returns an NBT Compound that is used for accelerating comparisons.
*/
static synchronized NBTTagCompound getSharedTagCompound( final NBTTagCompound tagCompound, final ItemStack s )
{
if( tagCompound.hasNoTags() )
2015-04-29 02:30:53 +02:00
{
return null;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final Item item = s.getItem();
int meta = -1;
if( s.getItem() != null && s.isItemStackDamageable() && s.getHasSubtypes() )
2015-04-29 02:30:53 +02:00
{
meta = s.getItemDamage();
2015-04-29 02:30:53 +02:00
}
if( isShared( tagCompound ) )
2015-04-29 02:30:53 +02:00
{
return tagCompound;
2015-04-29 02:30:53 +02:00
}
2015-09-30 14:24:40 +02:00
final SharedSearchObject sso = new SharedSearchObject( item, meta, tagCompound );
2015-09-30 14:24:40 +02:00
final WeakReference<SharedSearchObject> c = SHARED_TAG_COMPOUND.get( sso );
if( c != null )
{
2015-09-30 14:24:40 +02:00
final SharedSearchObject cg = c.get();
if( cg != null )
2015-04-29 02:30:53 +02:00
{
return cg.getShared(); // I don't think I really need to check this
2015-04-29 02:30:53 +02:00
}
// as its already certain to exist..
}
2015-09-30 14:24:40 +02:00
final AESharedNBT clone = AESharedNBT.createFromCompound( item, meta, tagCompound );
sso.setCompound( (NBTTagCompound) sso.getCompound().copy() ); // prevent
// modification
// of data based
// on original
// item.
sso.setShared( clone );
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 )
{
return ta instanceof AESharedNBT;
}
private static AESharedNBT createFromCompound( final Item itemID, final int damageValue, final NBTTagCompound c )
{
2015-09-30 14:24:40 +02:00
final AESharedNBT x = new AESharedNBT( itemID, damageValue );
2014-02-09 02:34:52 +01:00
// c.getTags()
2015-09-30 14:24:40 +02:00
for( final Object o : c.getKeySet() )
{
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() );
}
x.hash = Platform.itemComparisons().createUnorderedNbtHash( c );
2015-09-30 14:24:40 +02:00
final ItemStack isc = new ItemStack( itemID, 1, damageValue );
isc.setTagCompound( c );
2014-09-20 22:41:19 +02:00
x.comp = AEApi.instance().registries().specialComparison().getSpecialComparison( isc );
return x;
}
int getHash()
{
return this.hash;
}
@Override
public NBTTagCompound getNBTTagCompoundCopy()
{
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 )
{
if( par1Obj instanceof AESharedNBT )
2015-04-29 02:30:53 +02:00
{
return this == par1Obj;
2015-04-29 02:30:53 +02: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 )
{
2014-12-29 15:13:47 +01:00
return item == this.item && this.meta == meta && this.hash == orderlessHash;
}
2015-09-30 14:24:40 +02:00
public boolean comparePreciseWithRegistry( final AESharedNBT tagCompound )
{
if( this == tagCompound )
2015-04-29 02:30:53 +02:00
{
return true;
2015-04-29 02:30:53 +02:00
}
if( this.comp != null && tagCompound.comp != null )
{
2014-12-29 15:13:47 +01:00
return this.comp.sameAsPrecise( tagCompound.comp );
}
return false;
}
2015-09-30 14:24:40 +02:00
public boolean compareFuzzyWithRegistry( final AESharedNBT tagCompound )
{
if( this == tagCompound )
2015-04-29 02:30:53 +02:00
{
return true;
2015-04-29 02:30:53 +02:00
}
if( tagCompound == null )
2015-04-29 02:30:53 +02:00
{
return false;
2015-04-29 02:30:53 +02:00
}
if( this.comp == tagCompound.comp )
2015-04-29 02:30:53 +02:00
{
return true;
2015-04-29 02:30:53 +02:00
}
if( this.comp != null )
{
2014-12-29 15:13:47 +01:00
return this.comp.sameAsFuzzy( tagCompound.comp );
}
return false;
}
}