Rework AEItemStack (#3091)

* Use itemstack as itemdef
* HIGH_TAG/LOW_TAG should be compared both directions
* Remove getTagCompound
* Make Itemlist implementation independent
* Cache item id for performance reasons
* Add preconditions to saveguard against external meddling
* Chache itemDamage
* Remove IAEStackSearchKey for now, rename getDisplayStack
This commit is contained in:
fscan
2017-09-30 17:18:30 +02:00
committed by yueh
parent 2ed7a5598a
commit 1e15b23506
75 changed files with 559 additions and 1512 deletions
+1 -1
View File
@@ -97,7 +97,7 @@ public class ItemSorters
return CONFIG_BASED_SORT_BY_NAME.compare( o1, o2 );
}
final int cmp = api.compareItems( o1.getItemStack(), o2.getItemStack() );
final int cmp = api.compareItems( o1.asItemStackRepresentation(), o2.asItemStackRepresentation() );
if( getDirection() == SortDir.ASCENDING )
{
+12 -45
View File
@@ -103,7 +103,6 @@ import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IAETagCompound;
import appeng.api.storage.data.IItemList;
import appeng.api.util.AEColor;
import appeng.api.util.AEPartLocation;
@@ -123,7 +122,6 @@ import appeng.me.helpers.AENetworkProxy;
import appeng.util.helpers.ItemComparisonHelper;
import appeng.util.helpers.P2PHelper;
import appeng.util.item.AEItemStack;
import appeng.util.item.AESharedNBT;
import appeng.util.prioritylist.IPartitionList;
@@ -571,11 +569,11 @@ public class Platform
}
@SideOnly( Side.CLIENT )
public static List getTooltip( final Object o )
public static List<String> getTooltip( final Object o )
{
if( o == null )
{
return new ArrayList();
return new ArrayList<>();
}
ItemStack itemStack = ItemStack.EMPTY;
@@ -590,7 +588,7 @@ public class Platform
}
else
{
return new ArrayList();
return new ArrayList<>();
}
try
@@ -601,7 +599,7 @@ public class Platform
}
catch( final Exception errB )
{
return new ArrayList();
return new ArrayList<>();
}
}
@@ -661,32 +659,6 @@ public class Platform
}
}
public static boolean hasSpecialComparison( final IAEItemStack willAdd )
{
if( willAdd == null )
{
return false;
}
final IAETagCompound tag = willAdd.getTagCompound();
if( tag != null && tag.getSpecialComparison() != null )
{
return true;
}
return false;
}
public static boolean hasSpecialComparison( final ItemStack willAdd )
{
if( AESharedNBT.isShared( willAdd.getTagCompound() ) )
{
if( ( (IAETagCompound) willAdd.getTagCompound() ).getSpecialComparison() != null )
{
return true;
}
}
return false;
}
public static boolean isWrench( final EntityPlayer player, final ItemStack eq, final BlockPos pos )
{
if( !eq.isEmpty() )
@@ -1475,7 +1447,7 @@ public class Platform
final IAEItemStack ae_ext = src.extractItems( ae_req, realForFake, mySrc );
if( ae_ext != null )
{
final ItemStack extracted = ae_ext.getItemStack();
final ItemStack extracted = ae_ext.createItemStack();
if( !extracted.isEmpty() )
{
energySrc.extractAEPower( 1, realForFake, PowerMultiplier.CONFIG );
@@ -1484,21 +1456,21 @@ public class Platform
}
}
final boolean checkFuzzy = ae_req.isOre() || providedTemplate.getItemDamage() == OreDictionary.WILDCARD_VALUE || providedTemplate
final boolean checkFuzzy = ae_req.getOre().isPresent() || providedTemplate.getItemDamage() == OreDictionary.WILDCARD_VALUE || providedTemplate
.hasTagCompound() || providedTemplate.isItemStackDamageable();
if( items != null && checkFuzzy )
{
for( final IAEItemStack x : items )
{
final ItemStack sh = x.getItemStack();
if( ( Platform.itemComparisons().isEqualItemType( providedTemplate,
sh ) || ae_req.sameOre( x ) ) && !Platform.itemComparisons().isEqualItem( sh, output ) )
final ItemStack sh = x.getDefinition();
if( ( Platform.itemComparisons().isEqualItemType( providedTemplate, sh ) || ae_req.sameOre( x ) ) && !ItemStack.areItemsEqual( sh,
output ) )
{ // Platform.isSameItemType( sh, providedTemplate )
final ItemStack cp = Platform.cloneItemStack( sh );
final ItemStack cp = sh.copy();
cp.setCount( 1 );
ci.setInventorySlotContents( slot, cp );
if( r.matches( ci, w ) && Platform.itemComparisons().isEqualItem( r.getCraftingResult( ci ), output ) )
if( r.matches( ci, w ) && ItemStack.areItemsEqual( r.getCraftingResult( ci ), output ) )
{
final IAEItemStack ax = x.copy();
ax.setStackSize( 1 );
@@ -1508,7 +1480,7 @@ public class Platform
if( ex != null )
{
energySrc.extractAEPower( 1, realForFake, PowerMultiplier.CONFIG );
return ex.getItemStack();
return ex.createItemStack();
}
}
}
@@ -1520,11 +1492,6 @@ public class Platform
return ItemStack.EMPTY;
}
public static ItemStack cloneItemStack( final ItemStack a )
{
return a.copy();
}
public static ItemStack getContainerItem( final ItemStack stackInSlot )
{
if( stackInSlot == null )
@@ -19,26 +19,15 @@
package appeng.util.helpers;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import javax.annotation.Nonnull;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTPrimitive;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.nbt.NBTTagString;
import net.minecraftforge.oredict.OreDictionary;
import appeng.api.config.FuzzyMode;
import appeng.core.AELog;
import appeng.util.item.AESharedNBT;
import appeng.util.item.OreHelper;
import appeng.util.item.OreReference;
@@ -50,8 +39,6 @@ import appeng.util.item.OreReference;
public class ItemComparisonHelper
{
private static Field tagList;
/**
* Compare the two {@link ItemStack}s based on the same {@link Item} and damage value.
*
@@ -62,7 +49,7 @@ public class ItemComparisonHelper
*
* @return true, if both are equal.
*/
public boolean isEqualItemType( final ItemStack that, final ItemStack other )
public boolean isEqualItemType( @Nonnull final ItemStack that, @Nonnull final ItemStack other )
{
if( !that.isEmpty() && !other.isEmpty() && that.getItem() == other.getItem() )
{
@@ -75,29 +62,6 @@ public class ItemComparisonHelper
return false;
}
/**
* A wrapper around {@link ItemStack#isItemEqual(ItemStack)}.
*
* The benefit is to compare two null item stacks, without any additional null checks.
*
* Ignores NBT.
*
* @return true, if both are equal.
*/
public boolean isEqualItem( @Nonnull final ItemStack left, @Nonnull final ItemStack right )
{
return this.isItemEqual( left, right );
}
/**
* A slightly different method from ItemStack.java to skip the isEmpty() check. This allows you to check for
* identical empty spots..
*/
public boolean isItemEqual( ItemStack left, ItemStack right )
{
return left.getItem() == right.getItem() && left.getItemDamage() == right.getItemDamage();
}
/**
* Compares two {@link ItemStack} and their NBT tag for equality.
*
@@ -108,7 +72,7 @@ public class ItemComparisonHelper
*/
public boolean isSameItem( @Nonnull final ItemStack is, @Nonnull final ItemStack filter )
{
return this.isEqualItem( is, filter ) && this.hasSameNbtTag( is, filter );
return ItemStack.areItemsEqual( is, filter ) && this.isNbtTagEqual( is.getTagCompound(), filter.getTagCompound() );
}
/**
@@ -183,8 +147,8 @@ public class ItemComparisonHelper
}
}
final OreReference aOR = OreHelper.INSTANCE.isOre( a );
final OreReference bOR = OreHelper.INSTANCE.isOre( b );
final OreReference aOR = OreHelper.INSTANCE.getOre( a ).orElse( null );
final OreReference bOR = OreHelper.INSTANCE.getOre( b ).orElse( null );
if( OreHelper.INSTANCE.sameOre( aOR, bOR ) )
{
@@ -212,243 +176,14 @@ public class ItemComparisonHelper
*/
public boolean isNbtTagEqual( final NBTBase left, final NBTBase right )
{
// same type?
final byte id = left.getId();
if( id == right.getId() )
if( left == right )
{
switch( id )
{
case 10:
{
final NBTTagCompound ctA = (NBTTagCompound) left;
final NBTTagCompound ctB = (NBTTagCompound) right;
final Set<String> cA = ctA.getKeySet();
final Set<String> cB = ctB.getKeySet();
if( cA.size() != cB.size() )
{
return false;
}
for( final String name : cA )
{
final NBTBase tag = ctA.getTag( name );
final NBTBase aTag = ctB.getTag( name );
if( aTag == null )
{
return false;
}
if( !this.isNbtTagEqual( tag, aTag ) )
{
return false;
}
}
return true;
}
case 9: // ) // A instanceof NBTTagList )
{
final NBTTagList lA = (NBTTagList) left;
final NBTTagList lB = (NBTTagList) right;
if( lA.tagCount() != lB.tagCount() )
{
return false;
}
final List<NBTBase> tag = this.tagList( lA );
final List<NBTBase> aTag = this.tagList( lB );
if( tag.size() != aTag.size() )
{
return false;
}
for( int x = 0; x < tag.size(); x++ )
{
if( aTag.get( x ) == null )
{
return false;
}
if( !this.isNbtTagEqual( tag.get( x ), aTag.get( x ) ) )
{
return false;
}
}
return true;
}
case 1: // NBTTagByte
return ( (NBTPrimitive) left ).getByte() == ( (NBTPrimitive) right ).getByte();
case 4: // NBTTagLong
return ( (NBTPrimitive) left ).getLong() == ( (NBTPrimitive) right ).getLong();
case 8: // NBTTagString
return ( (NBTTagString) left ).getString().equals( ( (NBTTagString) right ).getString() );
case 6: // NBTTagDouble
return ( (NBTPrimitive) left ).getDouble() == ( (NBTPrimitive) right ).getDouble();
case 5: // NBTTagFloat
return ( (NBTPrimitive) left ).getFloat() == ( (NBTPrimitive) right ).getFloat();
case 3: // NBTTagInt
return ( (NBTPrimitive) left ).getInt() == ( (NBTPrimitive) right ).getInt();
default:
return left.equals( right );
}
return true;
}
if( left != null )
{
return left.equals( right );
}
return false;
}
/**
* Unordered hash of NBT Data, used to work thought huge piles fast, but ignores the order just in case MC
* decided to change it... WHICH IS BAD...
*/
public int createUnorderedNbtHash( final NBTBase nbt )
{
// same type?
int hash = 0;
final byte id = nbt.getId();
hash += id;
switch( id )
{
case 10:
{
final NBTTagCompound ctA = (NBTTagCompound) nbt;
final Set<String> cA = ctA.getKeySet();
for( final String name : cA )
{
hash += name.hashCode() ^ this.createUnorderedNbtHash( ctA.getTag( name ) );
}
return hash;
}
case 9: // ) // A instanceof NBTTagList )
{
final NBTTagList lA = (NBTTagList) nbt;
hash += 9 * lA.tagCount();
final List<NBTBase> l = this.tagList( lA );
for( int x = 0; x < l.size(); x++ )
{
hash += ( (Integer) x ).hashCode() ^ this.createUnorderedNbtHash( l.get( x ) );
}
return hash;
}
case 1: // NBTTagByte
return hash + ( (NBTPrimitive) nbt ).getByte();
case 4: // NBTTagLong
return hash + (int) ( (NBTPrimitive) nbt ).getLong();
case 8: // NBTTagString
return hash + ( (NBTTagString) nbt ).getString().hashCode();
case 6: // NBTTagDouble
return hash + (int) ( (NBTPrimitive) nbt ).getDouble();
case 5: // NBTTagFloat
return hash + (int) ( (NBTPrimitive) nbt ).getFloat();
case 3: // NBTTagInt
return hash + ( (NBTPrimitive) nbt ).getInt();
default:
return hash;
}
}
/**
* Lots of silliness to try and account for weird tag related junk, basically requires that two tags have at least
* something in their tags before it wastes its time comparing them.
*/
private boolean hasSameNbtTag( final ItemStack a, final ItemStack b )
{
if( a.isEmpty() && b.isEmpty() )
{
return true;
}
if( a.isEmpty() || b.isEmpty() )
{
return false;
}
if( a == b )
{
return true;
}
final NBTTagCompound ta = a.getTagCompound();
final NBTTagCompound tb = b.getTagCompound();
if( ta == tb )
{
return true;
}
if( ( ta == null && tb == null ) || ( ta != null && ta.hasNoTags() && tb == null ) || ( tb != null && tb
.hasNoTags() && ta == null ) || ( ta != null && ta.hasNoTags() && tb != null && tb.hasNoTags() ) )
{
return true;
}
if( ( ta == null && tb != null ) || ( ta != null && tb == null ) )
{
return false;
}
// if both tags are shared this is easy...
if( AESharedNBT.isShared( ta ) && AESharedNBT.isShared( tb ) )
{
return ta == tb;
}
return this.isNbtTagEqual( ta, tb );
}
private List<NBTBase> tagList( final NBTTagList lB )
{
if( tagList == null )
{
try
{
tagList = lB.getClass().getDeclaredField( "tagList" );
}
catch( final Throwable t )
{
try
{
tagList = lB.getClass().getDeclaredField( "field_74747_a" );
}
catch( final Throwable z )
{
AELog.debug( t );
AELog.debug( z );
}
}
}
try
{
tagList.setAccessible( true );
return (List<NBTBase>) tagList.get( lB );
}
catch( final Throwable t )
{
AELog.debug( t );
}
return new ArrayList<>();
}
}
@@ -190,7 +190,7 @@ public class AdaptorList extends InventoryAdaptor
for( final ItemStack is : this.i )
{
if( Platform.itemComparisons().isEqualItem( is, left ) )
if( ItemStack.areItemsEqual( is, left ) )
{
is.grow( left.getCount() );
return ItemStack.EMPTY;
@@ -99,7 +99,7 @@ public class IMEAdaptor extends InventoryAdaptor
if( out != null )
{
return out.getItemStack();
return out.createItemStack();
}
return ItemStack.EMPTY;
@@ -139,7 +139,7 @@ public class IMEAdaptor extends InventoryAdaptor
out = this.target.extractItems( req, type, this.src );
if( out != null )
{
return out.getItemStack();
return out.createItemStack();
}
}
}
@@ -166,7 +166,7 @@ public class IMEAdaptor extends InventoryAdaptor
final IAEItemStack out = this.target.injectItems( in, Actionable.MODULATE, this.src );
if( out != null )
{
return out.getItemStack();
return out.createItemStack();
}
}
return ItemStack.EMPTY;
@@ -181,7 +181,7 @@ public class IMEAdaptor extends InventoryAdaptor
final IAEItemStack out = this.target.injectItems( in, Actionable.SIMULATE, this.src );
if( out != null )
{
return out.getItemStack();
return out.createItemStack();
}
}
return ItemStack.EMPTY;
+1 -1
View File
@@ -37,7 +37,7 @@ public class ItemSlot
public ItemStack getItemStack()
{
return this.itemStack
.isEmpty() ? ( this.aeItemStack == null ? ItemStack.EMPTY : ( this.itemStack = this.aeItemStack.getItemStack() ) ) : this.itemStack;
.isEmpty() ? ( this.aeItemStack == null ? ItemStack.EMPTY : ( this.itemStack = this.aeItemStack.createItemStack() ) ) : this.itemStack;
}
public void setItemStack( final ItemStack is )
@@ -35,12 +35,12 @@ import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraftforge.fluids.Fluid;
import net.minecraftforge.fluids.FluidStack;
import net.minecraftforge.fluids.FluidUtil;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEFluidStack;
import appeng.api.storage.data.IAEStack;
import appeng.api.storage.data.IAETagCompound;
import appeng.util.Platform;
@@ -49,7 +49,7 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
private final int myHash;
private final Fluid fluid;
private IAETagCompound tagCompound;
private NBTTagCompound tagCompound;
private AEFluidStack( final AEFluidStack is )
{
@@ -262,12 +262,6 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
return dup;
}
@Override
public IAETagCompound getTagCompound()
{
return this.tagCompound;
}
@Override
public boolean isItem()
{
@@ -330,11 +324,6 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
return false;
}
if( AESharedNBT.isShared( tb ) )
{
return ta == tb;
}
return Platform.itemComparisons().isNbtTagEqual( ta, tb );
}
}
@@ -359,7 +348,7 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
final FluidStack is = new FluidStack( this.fluid, (int) Math.min( Integer.MAX_VALUE, this.getStackSize() ) );
if( this.tagCompound != null )
{
is.tag = this.tagCompound.getNBTTagCompoundCopy();
is.tag = this.tagCompound.copy();
}
return is;
@@ -372,22 +361,24 @@ public final class AEFluidStack extends AEStack<IAEFluidStack> implements IAEFlu
}
@Override
void writeIdentity( final ByteBuf i ) throws IOException
public ItemStack asItemStackRepresentation()
{
// TODO: fluids, how do they even work?
return FluidUtil.getFilledBucket( getFluidStack() );
}
@Override
protected void writeToStream( final ByteBuf i ) throws IOException
{
final byte[] name = this.fluid.getName().getBytes( "UTF-8" );
i.writeByte( (byte) name.length );
i.writeBytes( name );
}
@Override
void readNBT( final ByteBuf i ) throws IOException
{
if( this.hasTagCompound() )
{
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final DataOutputStream data = new DataOutputStream( bytes );
CompressedStreamTools.write( (NBTTagCompound) this.tagCompound, data );
CompressedStreamTools.write( this.tagCompound, data );
final byte[] tagBytes = bytes.toByteArray();
final int size = tagBytes.length;
@@ -1,223 +0,0 @@
/*
* 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.util.List;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import appeng.util.Platform;
public class AEItemDef
{
static final AESharedNBT LOW_TAG = new AESharedNBT( Integer.MIN_VALUE );
static final AESharedNBT HIGH_TAG = new AESharedNBT( Integer.MAX_VALUE );
private final int itemID;
private final Item item;
private int myHash;
private int def;
private int damageValue;
private int displayDamage;
private int maxDamage;
private AESharedNBT tagCompound;
@SideOnly( Side.CLIENT )
private String displayName;
@SideOnly( Side.CLIENT )
private List tooltip;
@SideOnly( Side.CLIENT )
private ResourceLocation uniqueID;
private OreReference isOre;
public AEItemDef( final Item it )
{
this.item = it;
this.itemID = Item.getIdFromItem( it );
}
AEItemDef copy()
{
final AEItemDef t = new AEItemDef( this.getItem() );
t.def = this.def;
t.setDamageValue( this.getDamageValue() );
t.setDisplayDamage( this.getDisplayDamage() );
t.setMaxDamage( this.getMaxDamage() );
t.setTagCompound( this.getTagCompound() );
t.setIsOre( this.getIsOre() );
return t;
}
@Override
public boolean equals( final Object obj )
{
if( obj == null )
{
return false;
}
if( this.getClass() != obj.getClass() )
{
return false;
}
final AEItemDef other = (AEItemDef) obj;
return other.getDamageValue() == this.getDamageValue() && other.getItem() == this.getItem() && this.getTagCompound() == other.getTagCompound();
}
boolean isItem( final ItemStack otherStack )
{
// hackery!
final int dmg = this.getDamageValueHack( otherStack );
if( this.getItem() == otherStack.getItem() && dmg == this.getDamageValue() )
{
if( ( this.getTagCompound() != null ) == otherStack.hasTagCompound() )
{
return true;
}
if( this.getTagCompound() != null && otherStack.hasTagCompound() )
{
return Platform.itemComparisons().isNbtTagEqual( this.getTagCompound(), otherStack.getTagCompound() );
}
return true;
}
return false;
}
int getDamageValueHack( final ItemStack is )
{
return Items.BLAZE_ROD.getDamage( is );
}
void reHash()
{
this.def = this.getItemID() << Platform.DEF_OFFSET | this.getDamageValue();
this.myHash = this.def ^ ( this.getTagCompound() == null ? 0 : System.identityHashCode( this.getTagCompound() ) );
}
AESharedNBT getTagCompound()
{
return this.tagCompound;
}
void setTagCompound( final AESharedNBT tagCompound )
{
this.tagCompound = tagCompound;
}
int getDamageValue()
{
return this.damageValue;
}
int setDamageValue( final int damageValue )
{
this.damageValue = damageValue;
return damageValue;
}
Item getItem()
{
return this.item;
}
int getDisplayDamage()
{
return this.displayDamage;
}
void setDisplayDamage( final int displayDamage )
{
this.displayDamage = displayDamage;
}
String getDisplayName()
{
return this.displayName;
}
void setDisplayName( final String displayName )
{
this.displayName = displayName;
}
List getTooltip()
{
return this.tooltip;
}
List setTooltip( final List tooltip )
{
this.tooltip = tooltip;
return tooltip;
}
ResourceLocation getUniqueID()
{
return this.uniqueID;
}
ResourceLocation setUniqueID( final ResourceLocation uniqueID )
{
this.uniqueID = uniqueID;
return uniqueID;
}
OreReference getIsOre()
{
return this.isOre;
}
void setIsOre( final OreReference isOre )
{
this.isOre = isOre;
}
int getItemID()
{
return this.itemID;
}
int getMaxDamage()
{
return this.maxDamage;
}
void setMaxDamage( final int maxDamage )
{
this.maxDamage = maxDamage;
}
/**
* TODO: Check if replaceable by hashCode();
*/
int getMyHash()
{
return this.myHash;
}
}
+66 -380
View File
@@ -19,102 +19,57 @@
package appeng.util.item;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.security.InvalidParameterException;
import java.util.List;
import java.util.Optional;
import javax.annotation.Nullable;
import io.netty.buffer.ByteBuf;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.CompressedStreamTools;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.ResourceLocation;
import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.ItemHandlerHelper;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.StorageChannel;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IAETagCompound;
import appeng.util.Platform;
public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemStack, Comparable<AEItemStack>
public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemStack
{
private AESharedItemStack sharedStack;
private Optional<OreReference> oreReference;
private AEItemDef def;
@SideOnly( Side.CLIENT )
private String displayName;
@SideOnly( Side.CLIENT )
private List<String> tooltip;
@SideOnly( Side.CLIENT )
private ResourceLocation uniqueID;
private AEItemStack( final AEItemStack is )
{
this.setDefinition( is.getDefinition() );
this.setStackSize( is.getStackSize() );
this.setCraftable( is.isCraftable() );
this.setCountRequestable( is.getCountRequestable() );
this.sharedStack = is.sharedStack;
this.oreReference = is.oreReference;
}
private AEItemStack( final ItemStack is )
private AEItemStack( final AESharedItemStack is, long size )
{
if( is.isEmpty() )
{
throw new InvalidParameterException( "null is not a valid ItemStack for AEItemStack." );
}
final Item item = is.getItem();
if( item == Items.AIR )
{
throw new InvalidParameterException( "Contained item is null, thus not a valid ItemStack for AEItemStack." );
}
this.setDefinition( new AEItemDef( item ) );
if( this.getDefinition().getItem() == Items.AIR )
{
throw new InvalidParameterException( "This ItemStack is bad, it has a null item." );
}
/*
* Prevent an Item from changing the damage value on me... Either, this or a core mod.
*/
/*
* Super hackery.
* is.itemID = appeng.api.Materials.matQuartz.itemID; damageValue = is.getItemDamage(); is.itemID = itemID;
*/
/*
* Kinda hackery
*/
this.getDefinition().setDamageValue( this.def.getDamageValueHack( is ) );
if( !is.getItem().isDamageable() )
{
this.getDefinition().setDisplayDamage( Integer.MAX_VALUE );
}
else
{
this.getDefinition().setDisplayDamage( (int) ( is.getItem().getDurabilityForDisplay( is ) * Integer.MAX_VALUE ) );
}
this.getDefinition().setMaxDamage( is.getMaxDamage() );
final NBTTagCompound tagCompound = is.getTagCompound();
if( tagCompound != null )
{
this.getDefinition().setTagCompound( (AESharedNBT) AESharedNBT.getSharedTagCompound( tagCompound, is ) );
}
this.setStackSize( is.getCount() );
this.sharedStack = is;
this.setStackSize( size );
this.setCraftable( false );
this.setCountRequestable( 0 );
this.getDefinition().reHash();
this.getDefinition().setIsOre( OreHelper.INSTANCE.isOre( is ) );
this.oreReference = OreHelper.INSTANCE.getOre( is.getDefinition() );
}
public static IAEItemStack loadItemStackFromNBT( final NBTTagCompound i )
@@ -131,7 +86,6 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
}
final AEItemStack item = AEItemStack.create( itemstack );
// item.priority = i.getInteger( "Priority" );
item.setStackSize( i.getLong( "Cnt" ) );
item.setCountRequestable( i.getLong( "Req" ) );
item.setCraftable( i.getBoolean( "Craft" ) );
@@ -146,7 +100,7 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
return null;
}
return new AEItemStack( stack );
return new AEItemStack( AEItemStackRegistry.getRegisteredStack( stack ), stack.getCount() );
}
public static IAEItemStack loadItemStackFromPacket( final ByteBuf data ) throws IOException
@@ -156,41 +110,17 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
final byte stackType = (byte) ( ( mask & 0x0C ) >> 2 );
final byte countReqType = (byte) ( ( mask & 0x30 ) >> 4 );
final boolean isCraftable = ( mask & 0x40 ) > 0;
final boolean hasTagCompound = ( mask & 0x80 ) > 0;
// don't send this...
final NBTTagCompound d = new NBTTagCompound();
// For some insane reason, Vanilla can only parse numeric item ids if they are strings
short itemNumericId = data.readShort();
d.setString( "id", String.valueOf( itemNumericId ) );
d.setShort( "Damage", data.readShort() );
d.setByte( "Count", (byte) 1 ); // 1 Because vanilla'll freak out otherwise
if( hasTagCompound )
{
final int len = data.readInt();
final byte[] bd = new byte[len];
data.readBytes( bd );
final ByteArrayInputStream di = new ByteArrayInputStream( bd );
d.setTag( "tag", CompressedStreamTools.read( new DataInputStream( di ) ) );
}
// long priority = getPacketValue( PriorityType, data );
final ItemStack itemstack = ByteBufUtils.readItemStack( data );
final long stackSize = getPacketValue( stackType, data );
final long countRequestable = getPacketValue( countReqType, data );
final ItemStack itemstack = new ItemStack( d );
if( itemstack.isEmpty() )
{
return null;
}
final AEItemStack item = AEItemStack.create( itemstack );
// item.priority = (int) priority;
item.setStackSize( stackSize );
item.setCountRequestable( countRequestable );
item.setCraftable( isCraftable );
@@ -205,9 +135,6 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
return;
}
// if ( priority < ((AEItemStack) option).priority )
// priority = ((AEItemStack) option).priority;
this.incStackSize( option.getStackSize() );
this.setCountRequestable( this.getCountRequestable() + option.getCountRequestable() );
this.setCraftable( this.isCraftable() || option.isCraftable() );
@@ -216,57 +143,10 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
@Override
public void writeToNBT( final NBTTagCompound i )
{
/*
* Mojang Fucked this over ; GC Optimization - Ugly Yes, but it saves a lot in the memory department.
*/
/*
* NBTBase id = i.getTag( "id" ); NBTBase Count = i.getTag( "Count" ); NBTBase Cnt = i.getTag( "Cnt" ); NBTBase
* Req = i.getTag( "Req" ); NBTBase Craft = i.getTag( "Craft" ); NBTBase Damage = i.getTag( "Damage" );
*/
/*
* if ( id != null && id instanceof NBTTagShort ) ((NBTTagShort) id).data = (short) this.def.item.itemID; else
*/
// i.setShort( "id", (short) Item.REGISTRY.getIDForObject( this.getDefinition().getItem() ) );
ResourceLocation resourcelocation = Item.REGISTRY.getNameForObject( this.getItem() );
i.setString( "id", resourcelocation == null ? "minecraft:air" : resourcelocation.toString() );
/*
* if ( Count != null && Count instanceof NBTTagByte ) ((NBTTagByte) Count).data = (byte) 1; else
*/
i.setByte( "Count", (byte) 1 );
/*
* if ( Cnt != null && Cnt instanceof NBTTagLong ) ((NBTTagLong) Cnt).data = this.stackSize; else
*/
this.getDefinition().writeToNBT( i );
i.setLong( "Cnt", this.getStackSize() );
/*
* if ( Req != null && Req instanceof NBTTagLong ) ((NBTTagLong) Req).data = this.stackSize; else
*/
i.setLong( "Req", this.getCountRequestable() );
/*
* if ( Craft != null && Craft instanceof NBTTagByte ) ((NBTTagByte) Craft).data = (byte) (this.isCraftable() ?
* 1 : 0); else
*/
i.setBoolean( "Craft", this.isCraftable() );
/*
* if ( Damage != null && Damage instanceof NBTTagShort ) ((NBTTagShort) Damage).data = (short)
* this.def.damageValue; else
*/
i.setShort( "Damage", (short) this.getDefinition().getDamageValue() );
if( this.getDefinition().getTagCompound() != null )
{
i.setTag( "tag", this.getDefinition().getTagCompound() );
}
else
{
i.removeTag( "tag" );
}
}
@Override
@@ -285,8 +165,8 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
{
if( this.getDefinition().getItem().isDamageable() )
{
final ItemStack a = this.getItemStack();
final ItemStack b = o.getItemStack();
final ItemStack a = this.getDefinition();
final ItemStack b = o.getDefinition();
try
{
@@ -346,7 +226,7 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
{
if( this.getDefinition().getItem().isDamageable() )
{
final ItemStack a = this.getItemStack();
final ItemStack a = this.getDefinition();
try
{
@@ -405,20 +285,6 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
return new AEItemStack( this );
}
@Override
public IAEItemStack empty()
{
final IAEItemStack dup = this.copy();
dup.reset();
return dup;
}
@Override
public IAETagCompound getTagCompound()
{
return this.getDefinition().getTagCompound();
}
@Override
public boolean isItem()
{
@@ -438,16 +304,9 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
}
@Override
public ItemStack getItemStack()
public ItemStack createItemStack()
{
final ItemStack is = new ItemStack( this.getDefinition().getItem(), (int) Math.min( Integer.MAX_VALUE, this.getStackSize() ), this.getDefinition()
.getDamageValue() );
if( this.getDefinition().getTagCompound() != null )
{
is.setTagCompound( this.getDefinition().getTagCompound().getNBTTagCompoundCopy() );
}
return is;
return ItemHandlerHelper.copyStackWithSize( this.getDefinition(), (int) Math.min( Integer.MAX_VALUE, this.getStackSize() ) );
}
@Override
@@ -459,7 +318,7 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
@Override
public int getItemDamage()
{
return this.getDefinition().getDamageValue();
return this.sharedStack.getItemDamage();
}
@Override
@@ -476,7 +335,7 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
return false;
}
return this.getDefinition().equals( ( (AEItemStack) otherStack ).getDefinition() );
return this.sharedStack == ( (AEItemStack) otherStack ).sharedStack;
}
@Override
@@ -486,14 +345,19 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
{
return false;
}
int oldSize = otherStack.getCount();
return this.getDefinition().isItem( otherStack );
otherStack.setCount( 1 );
boolean ret = ItemStack.areItemStacksEqual( this.getDefinition(), otherStack );
otherStack.setCount( oldSize );
return ret;
}
@Override
public int hashCode()
{
return this.getDefinition().getMyHash();
return this.sharedStack.hashCode();
}
@Override
@@ -501,40 +365,11 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
{
if( ia instanceof AEItemStack )
{
return ( (AEItemStack) ia ).getDefinition().equals( this.def );// && def.tagCompound == ((AEItemStack)
// ia).def.tagCompound;
return this.isSameType( (AEItemStack) ia );
}
else if( ia instanceof ItemStack )
{
final ItemStack is = (ItemStack) ia;
if( is.getItem() == this.getDefinition().getItem() && is.getItemDamage() == this.getDefinition().getDamageValue() )
{
final NBTTagCompound ta = this.getDefinition().getTagCompound();
final NBTTagCompound tb = is.getTagCompound();
if( ta == tb )
{
return true;
}
if( ( ta == null && tb == null ) || ( ta != null && ta.hasNoTags() && tb == null ) || ( tb != null && tb
.hasNoTags() && ta == null ) || ( ta != null && ta.hasNoTags() && tb != null && tb.hasNoTags() ) )
{
return true;
}
if( ( ta == null && tb != null ) || ( ta != null && tb == null ) )
{
return false;
}
if( AESharedNBT.isShared( tb ) )
{
return ta == tb;
}
return Platform.itemComparisons().isNbtTagEqual( ta, tb );
}
return this.isSameType( (ItemStack) ia );
}
return false;
}
@@ -542,226 +377,77 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
@Override
public String toString()
{
return this.getItemStack().toString();
}
@Override
public int compareTo( final AEItemStack b )
{
final int id = this.getDefinition().getItemID() - b.getDefinition().getItemID();
if( id != 0 )
{
return id;
}
final int damageValue = this.getDefinition().getDamageValue() - b.getDefinition().getDamageValue();
if( damageValue != 0 )
{
return damageValue;
}
final int displayDamage = this.getDefinition().getDisplayDamage() - b.getDefinition().getDisplayDamage();
if( displayDamage != 0 )
{
return displayDamage;
}
return ( this.getDefinition().getTagCompound() == b.getDefinition().getTagCompound() ) ? 0 : this.compareNBT( b.getDefinition() );
}
private int compareNBT( final AEItemDef b )
{
final int nbt = this.compare( ( this.getDefinition().getTagCompound() == null ? 0 : this.getDefinition().getTagCompound().getHash() ),
( b.getTagCompound() == null ? 0 : b.getTagCompound().getHash() ) );
if( nbt == 0 )
{
return this.compare( System.identityHashCode( this.getDefinition().getTagCompound() ), System.identityHashCode( b.getTagCompound() ) );
}
return nbt;
}
private int compare( final int l, final int m )
{
return l < m ? -1 : ( l > m ? 1 : 0 );
return this.getDefinition().toString();
}
@SideOnly( Side.CLIENT )
public List getToolTip()
public List<String> getToolTip()
{
if( this.getDefinition().getTooltip() == null )
if( this.tooltip == null )
{
final ItemStack is = this.getItemStack();
is.setCount( 1 );
this.getDefinition().setTooltip( Platform.getTooltip( is ) );
this.tooltip = Platform.getTooltip( this.asItemStackRepresentation() );
}
return this.getDefinition().getTooltip();
return this.tooltip;
}
@SideOnly( Side.CLIENT )
public String getDisplayName()
{
if( this.getDefinition().getDisplayName() == null )
if( this.displayName == null )
{
final ItemStack is = this.getItemStack();
is.setCount( 1 );
this.getDefinition().setDisplayName( Platform.getItemDisplayName( is ) );
this.displayName = Platform.getItemDisplayName( this.asItemStackRepresentation() );
}
return this.getDefinition().getDisplayName();
return this.displayName;
}
@SideOnly( Side.CLIENT )
public String getModID()
{
if( this.getDefinition().getUniqueID() != null )
if( this.uniqueID == null )
{
return this.getModName( this.getDefinition().getUniqueID() );
this.uniqueID = Item.REGISTRY.getNameForObject( this.getDefinition().getItem() );
}
return this.getModName( this.getDefinition().setUniqueID( Item.REGISTRY.getNameForObject( this.getDefinition().getItem() ) ) );
}
private String getModName( final ResourceLocation uniqueIdentifier )
{
if( uniqueIdentifier == null )
if( this.uniqueID == null )
{
return "** Null";
}
return uniqueIdentifier.getResourceDomain() == null ? "** Null" : uniqueIdentifier.getResourceDomain();
return this.uniqueID.getResourceDomain() == null ? "** Null" : this.uniqueID.getResourceDomain();
}
IAEItemStack getLow( final FuzzyMode fuzzy, final boolean ignoreMeta )
public Optional<OreReference> getOre()
{
final AEItemStack bottom = new AEItemStack( this );
final AEItemDef newDef = bottom.setDefinition( bottom.getDefinition().copy() );
if( ignoreMeta )
{
newDef.setDisplayDamage( newDef.setDamageValue( 0 ) );
newDef.reHash();
return bottom;
}
if( newDef.getItem().isDamageable() )
{
if( fuzzy == FuzzyMode.IGNORE_ALL )
{
newDef.setDisplayDamage( 0 );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( this.getDefinition().getDamageValue() == 0 )
{
newDef.setDisplayDamage( 0 );
}
else
{
newDef.setDisplayDamage( 1 );
}
}
else
{
final int breakpoint = fuzzy.calculateBreakPoint( this.getDefinition().getMaxDamage() );
newDef.setDisplayDamage( breakpoint <= this.getDefinition().getDisplayDamage() ? breakpoint : 0 );
}
newDef.setDamageValue( newDef.getDisplayDamage() );
}
newDef.setTagCompound( AEItemDef.LOW_TAG );
newDef.reHash();
return bottom;
}
IAEItemStack getHigh( final FuzzyMode fuzzy, final boolean ignoreMeta )
{
final AEItemStack top = new AEItemStack( this );
final AEItemDef newDef = top.setDefinition( top.getDefinition().copy() );
if( ignoreMeta )
{
newDef.setDisplayDamage( newDef.setDamageValue( Integer.MAX_VALUE ) );
newDef.reHash();
return top;
}
if( newDef.getItem().isDamageable() )
{
if( fuzzy == FuzzyMode.IGNORE_ALL )
{
newDef.setDisplayDamage( this.getDefinition().getMaxDamage() + 1 );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( this.getDefinition().getDamageValue() == 0 )
{
newDef.setDisplayDamage( 0 );
}
else
{
newDef.setDisplayDamage( this.getDefinition().getMaxDamage() + 1 );
}
}
else
{
final int breakpoint = fuzzy.calculateBreakPoint( this.getDefinition().getMaxDamage() );
newDef.setDisplayDamage( this.getDefinition().getDisplayDamage() < breakpoint ? breakpoint - 1 : this.getDefinition().getMaxDamage() + 1 );
}
newDef.setDamageValue( newDef.getDisplayDamage() );
}
newDef.setTagCompound( AEItemDef.HIGH_TAG );
newDef.reHash();
return top;
}
public boolean isOre()
{
return this.getDefinition().getIsOre() != null;
return this.oreReference;
}
@Override
void writeIdentity( final ByteBuf i ) throws IOException
protected void writeToStream( final ByteBuf data ) throws IOException
{
i.writeShort( Item.REGISTRY.getIDForObject( this.getDefinition().getItem() ) );
i.writeShort( this.getItemDamage() );
}
@Override
void readNBT( final ByteBuf i ) throws IOException
{
if( this.hasTagCompound() )
{
final ByteArrayOutputStream bytes = new ByteArrayOutputStream();
final DataOutputStream data = new DataOutputStream( bytes );
CompressedStreamTools.write( (NBTTagCompound) this.getTagCompound(), data );
final byte[] tagBytes = bytes.toByteArray();
final int size = tagBytes.length;
i.writeInt( size );
i.writeBytes( tagBytes );
}
ByteBufUtils.writeItemStack( data, this.getDefinition() );
}
@Override
public boolean hasTagCompound()
{
return this.getDefinition().getTagCompound() != null;
return this.getDefinition().hasTagCompound();
}
AEItemDef getDefinition()
@Override
public ItemStack asItemStackRepresentation()
{
return this.def;
return getDefinition().copy();
}
private AEItemDef setDefinition( final AEItemDef def )
@Override
public ItemStack getDefinition()
{
this.def = def;
return def;
return this.sharedStack.getDefinition();
}
AESharedItemStack getSharedStack()
{
return this.sharedStack;
}
}
@@ -0,0 +1,71 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2017 AlgorithmX2
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package appeng.util.item;
import java.lang.ref.WeakReference;
import java.util.WeakHashMap;
import javax.annotation.Nonnull;
import net.minecraft.item.ItemStack;
public final class AEItemStackRegistry
{
private static final WeakHashMap<AESharedItemStack, WeakReference<AESharedItemStack>> REGISTRY = new WeakHashMap<>();
private AEItemStackRegistry()
{
}
static synchronized AESharedItemStack getRegisteredStack( final @Nonnull ItemStack itemStack )
{
if( itemStack.isEmpty() )
{
throw new IllegalArgumentException( "stack cannot be empty" );
}
int oldStackSize = itemStack.getCount();
itemStack.setCount( 1 );
AESharedItemStack search = new AESharedItemStack( itemStack );
WeakReference<AESharedItemStack> weak = REGISTRY.get( search );
AESharedItemStack ret = null;
if( weak != null )
{
ret = weak.get();
}
if( ret == null )
{
ret = new AESharedItemStack( itemStack.copy() );
REGISTRY.put( ret, new WeakReference<>( ret ) );
}
itemStack.setCount( oldStackSize );
return ret;
}
}
@@ -0,0 +1,216 @@
/*
* 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.util.Objects;
import com.google.common.base.Preconditions;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import appeng.api.config.FuzzyMode;
import appeng.util.Platform;
final class AESharedItemStack implements Comparable<AESharedItemStack>
{
private static final NBTTagCompound LOW_TAG = new NBTTagCompound();
private static final NBTTagCompound HIGH_TAG = new NBTTagCompound();
private final ItemStack itemStack;
private final int itemId;
private final int itemDamage;
public AESharedItemStack( final ItemStack itemStack )
{
this.itemStack = itemStack;
this.itemId = Item.getIdFromItem( itemStack.getItem() );
this.itemDamage = itemStack.getItemDamage();
}
ItemStack getDefinition()
{
return this.itemStack;
}
int getItemDamage()
{
return this.itemDamage;
}
@Override
public int hashCode()
{
return Objects.hash( itemStack.getItem(), this.itemDamage );
}
@Override
public boolean equals( final Object obj )
{
if( obj instanceof AESharedItemStack )
{
final AESharedItemStack other = (AESharedItemStack) obj;
Preconditions.checkState( this.itemStack.getCount() == 1, "ItemStack#getCount() has to be 1" );
Preconditions.checkArgument( other.getDefinition().getCount() == 1, "ItemStack#getCount() has to be 1" );
if( this.itemStack == other.itemStack )
{
return true;
}
return ItemStack.areItemStacksEqual( this.itemStack, other.itemStack );
}
return false;
}
@Override
public int compareTo( final AESharedItemStack b )
{
Preconditions.checkState( this.itemStack.getCount() == 1, "ItemStack#getCount() has to be 1" );
Preconditions.checkArgument( b.getDefinition().getCount() == 1, "ItemStack#getCount() has to be 1" );
if( this.itemStack == b.getDefinition() )
{
return 0;
}
final int id = this.itemId - b.itemId;
if( id != 0 )
{
return id;
}
final int damageValue = this.itemDamage - b.itemDamage;
if( damageValue != 0 )
{
return damageValue;
}
return this.compareNBT( b.getDefinition() );
}
private int compareNBT( final ItemStack b )
{
if( Platform.itemComparisons().isNbtTagEqual( this.itemStack.getTagCompound(), b.getTagCompound() ) )
{
return 0;
}
if( this.itemStack.getTagCompound() == LOW_TAG || b.getTagCompound() == HIGH_TAG )
{
return -1;
}
if( this.itemStack.getTagCompound() == HIGH_TAG || b.getTagCompound() == LOW_TAG )
{
return 1;
}
final int nbt = ( this.itemStack.hasTagCompound() ? this.itemStack.getTagCompound().hashCode() : 0 ) - ( b.hasTagCompound() ? b.getTagCompound()
.hashCode() : 0 );
if( nbt != 0 )
{
return nbt;
}
return System.identityHashCode( this.itemStack.getTagCompound() ) - System.identityHashCode( b.getTagCompound() );
}
public AESharedItemStack getLowerBound( final FuzzyMode fuzzy, final boolean ignoreMeta )
{
Preconditions.checkState( this.itemStack.getCount() == 1, "ItemStack#getCount() has to be 1" );
final ItemStack newDef = this.itemStack.copy();
if( ignoreMeta )
{
newDef.setItemDamage( 0 );
}
else
{
if( newDef.getItem().isDamageable() )
{
if( fuzzy == FuzzyMode.IGNORE_ALL )
{
newDef.setItemDamage( 0 );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( this.itemStack.getItemDamage() == 0 )
{
newDef.setItemDamage( 0 );
}
else
{
newDef.setItemDamage( 0 );
}
}
else
{
final int breakpoint = fuzzy.calculateBreakPoint( this.itemStack.getMaxDamage() );
newDef.setItemDamage( breakpoint <= this.itemDamage ? breakpoint : 0 );
}
}
newDef.setTagCompound( LOW_TAG );
}
return new AESharedItemStack( newDef );
}
public AESharedItemStack getUpperBound( final FuzzyMode fuzzy, final boolean ignoreMeta )
{
Preconditions.checkState( this.itemStack.getCount() == 1, "ItemStack#getCount() has to be 1" );
final ItemStack newDef = this.itemStack.copy();
if( ignoreMeta )
{
newDef.setItemDamage( Integer.MAX_VALUE );
}
else
{
if( newDef.getItem().isDamageable() )
{
if( fuzzy == FuzzyMode.IGNORE_ALL )
{
newDef.setItemDamage( this.itemStack.getMaxDamage() + 1 );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( this.itemStack.getItemDamage() == 0 )
{
newDef.setItemDamage( 0 );
}
else
{
newDef.setItemDamage( this.itemStack.getMaxDamage() + 1 );
}
}
else
{
final int breakpoint = fuzzy.calculateBreakPoint( this.itemStack.getMaxDamage() );
newDef.setItemDamage( this.itemDamage < breakpoint ? breakpoint - 1 : this.itemStack.getMaxDamage() + 1 );
}
}
newDef.setTagCompound( HIGH_TAG );
}
return new AESharedItemStack( newDef );
}
}
@@ -1,219 +0,0 @@
/*
* 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;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
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
{
/*
* Shared Tag Compound Cache.
*/
private static final WeakHashMap<SharedSearchObject, WeakReference<SharedSearchObject>> SHARED_TAG_COMPOUND = new WeakHashMap<>();
private final Item item;
private final int meta;
private SharedSearchObject sso;
private int hash;
private IItemComparison comp;
private AESharedNBT( final Item itemID, final int damageValue )
{
this.item = itemID;
this.meta = damageValue;
}
public AESharedNBT( final int fakeValue )
{
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() )
{
return null;
}
final Item item = s.getItem();
int meta = -1;
if( s.getItem() != Items.AIR && s.isItemStackDamageable() && s.getHasSubtypes() )
{
meta = s.getItemDamage();
}
if( isShared( tagCompound ) )
{
return tagCompound;
}
final SharedSearchObject sso = new SharedSearchObject( item, meta, tagCompound );
final WeakReference<SharedSearchObject> c = SHARED_TAG_COMPOUND.get( sso );
if( c != null )
{
final SharedSearchObject cg = c.get();
if( cg != null )
{
return cg.getShared(); // I don't think I really need to check this
}
// as its already certain to exist..
}
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<>( sso ) );
return clone;
}
/*
* returns true if the compound is part of the shared compound system ( and can thus be compared directly ).
*/
public static boolean isShared( final NBTTagCompound ta )
{
return ta instanceof AESharedNBT;
}
private static AESharedNBT createFromCompound( final Item itemID, final int damageValue, final NBTTagCompound c )
{
final AESharedNBT x = new AESharedNBT( itemID, damageValue );
// c.getTags()
for( final Object o : c.getKeySet() )
{
final String name = (String) o;
x.setTag( name, c.getTag( name ).copy() );
}
x.hash = Platform.itemComparisons().createUnorderedNbtHash( c );
final ItemStack isc = new ItemStack( itemID, 1, damageValue );
isc.setTagCompound( c );
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
public boolean equals( final Object par1Obj )
{
if( par1Obj instanceof AESharedNBT )
{
return this == par1Obj;
}
return super.equals( par1Obj );
}
public boolean matches( final Item item, final int meta, final int orderlessHash )
{
return item == this.item && this.meta == meta && this.hash == orderlessHash;
}
public boolean comparePreciseWithRegistry( final AESharedNBT tagCompound )
{
if( this == tagCompound )
{
return true;
}
if( this.comp != null && tagCompound.comp != null )
{
return this.comp.sameAsPrecise( tagCompound.comp );
}
return false;
}
public boolean compareFuzzyWithRegistry( final AESharedNBT tagCompound )
{
if( this == tagCompound )
{
return true;
}
if( tagCompound == null )
{
return false;
}
if( this.comp == tagCompound.comp )
{
return true;
}
if( this.comp != null )
{
return this.comp.sameAsFuzzy( tagCompound.comp );
}
return false;
}
}
+12 -9
View File
@@ -26,7 +26,7 @@ import io.netty.buffer.ByteBuf;
import appeng.api.storage.data.IAEStack;
public abstract class AEStack<StackType extends IAEStack> implements IAEStack<StackType>
public abstract class AEStack<StackType extends IAEStack<StackType>> implements IAEStack<StackType>
{
private boolean isCraftable;
@@ -106,6 +106,14 @@ public abstract class AEStack<StackType extends IAEStack> implements IAEStack<St
return (StackType) this;
}
@Override
public StackType empty()
{
final StackType dup = this.copy();
dup.reset();
return dup;
}
@Override
public boolean isMeaningful()
{
@@ -144,15 +152,14 @@ public abstract class AEStack<StackType extends IAEStack> implements IAEStack<St
i.writeByte( mask );
this.writeIdentity( i );
this.writeToStream( i );
this.readNBT( i );
// putPacketValue( i, priority );
this.putPacketValue( i, this.stackSize );
this.putPacketValue( i, this.countRequestable );
}
protected abstract void writeToStream( final ByteBuf data ) throws IOException;
private byte getType( final long num )
{
if( num <= 255 )
@@ -175,10 +182,6 @@ public abstract class AEStack<StackType extends IAEStack> implements IAEStack<St
abstract boolean hasTagCompound();
abstract void writeIdentity( ByteBuf i ) throws IOException;
abstract void readNBT( ByteBuf i ) throws IOException;
private void putPacketValue( final ByteBuf tag, final long num )
{
if( num <= 255 )
+16 -18
View File
@@ -36,7 +36,7 @@ import appeng.api.storage.data.IItemList;
public final class ItemList implements IItemList<IAEItemStack>
{
private final NavigableMap<IAEItemStack, IAEItemStack> records = new ConcurrentSkipListMap<>();
private final NavigableMap<AESharedItemStack, IAEItemStack> records = new ConcurrentSkipListMap<>();
@Override
public void add( final IAEItemStack option )
@@ -46,7 +46,7 @@ public final class ItemList implements IItemList<IAEItemStack>
return;
}
final IAEItemStack st = this.records.get( option );
final IAEItemStack st = this.records.get( ( (AEItemStack) option ).getSharedStack() );
if( st != null )
{
@@ -67,7 +67,7 @@ public final class ItemList implements IItemList<IAEItemStack>
return null;
}
return this.records.get( itemStack );
return this.records.get( ( (AEItemStack) itemStack ).getSharedStack() );
}
@Override
@@ -80,15 +80,13 @@ public final class ItemList implements IItemList<IAEItemStack>
final AEItemStack ais = (AEItemStack) filter;
if( ais.isOre() )
return ais.getOre().map( or ->
{
final OreReference or = ais.getDefinition().getIsOre();
if( or.getAEEquivalents().size() == 1 )
{
final IAEItemStack is = or.getAEEquivalents().get( 0 );
return this.findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE );
return this.findFuzzyDamage( is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE );
}
else
{
@@ -96,14 +94,13 @@ public final class ItemList implements IItemList<IAEItemStack>
for( final IAEItemStack is : or.getAEEquivalents() )
{
output.addAll( this.findFuzzyDamage( (AEItemStack) is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE ) );
output.addAll( this.findFuzzyDamage( is, fuzzy, is.getItemDamage() == OreDictionary.WILDCARD_VALUE ) );
}
return output;
}
}
return this.findFuzzyDamage( ais, fuzzy, false );
} )
.orElse( this.findFuzzyDamage( ais, fuzzy, false ) );
}
@Override
@@ -120,7 +117,7 @@ public final class ItemList implements IItemList<IAEItemStack>
return;
}
final IAEItemStack st = this.records.get( option );
final IAEItemStack st = this.records.get( ( (AEItemStack) option ).getSharedStack() );
if( st != null )
{
@@ -146,7 +143,7 @@ public final class ItemList implements IItemList<IAEItemStack>
return;
}
final IAEItemStack st = this.records.get( option );
final IAEItemStack st = this.records.get( ( (AEItemStack) option ).getSharedStack() );
if( st != null )
{
@@ -169,7 +166,7 @@ public final class ItemList implements IItemList<IAEItemStack>
return;
}
final IAEItemStack st = this.records.get( option );
final IAEItemStack st = this.records.get( ( (AEItemStack) option ).getSharedStack() );
if( st != null )
{
@@ -219,13 +216,14 @@ public final class ItemList implements IItemList<IAEItemStack>
private IAEItemStack putItemRecord( final IAEItemStack itemStack )
{
return this.records.put( itemStack, itemStack );
return this.records.put( ( (AEItemStack) itemStack ).getSharedStack(), itemStack );
}
private Collection<IAEItemStack> findFuzzyDamage( final AEItemStack filter, final FuzzyMode fuzzy, final boolean ignoreMeta )
private Collection<IAEItemStack> findFuzzyDamage( final IAEItemStack filter, final FuzzyMode fuzzy, final boolean ignoreMeta )
{
final IAEItemStack low = filter.getLow( fuzzy, ignoreMeta );
final IAEItemStack high = filter.getHigh( fuzzy, ignoreMeta );
final AEItemStack itemStack = (AEItemStack) filter;
final AESharedItemStack low = itemStack.getSharedStack().getLowerBound( fuzzy, ignoreMeta );
final AESharedItemStack high = itemStack.getSharedStack().getUpperBound( fuzzy, ignoreMeta );
return this.records.subMap( low, true, high, true ).descendingMap().values();
}
+15 -17
View File
@@ -24,6 +24,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import com.google.common.cache.CacheBuilder;
@@ -63,7 +64,7 @@ public class OreHelper
*
* @return true if an ore entry exists, false otherwise
*/
public OreReference isOre( final ItemStack itemStack )
public Optional<OreReference> getOre( final ItemStack itemStack )
{
final ItemRef ir = new ItemRef( itemStack );
@@ -109,13 +110,13 @@ public class OreHelper
}
}
return this.references.get( ir );
return Optional.ofNullable( this.references.get( ir ) );
}
boolean sameOre( final AEItemStack aeItemStack, final IAEItemStack is )
{
final OreReference a = aeItemStack.getDefinition().getIsOre();
final OreReference b = aeItemStack.getDefinition().getIsOre();
final OreReference a = aeItemStack.getOre().orElse( null );
final OreReference b = aeItemStack.getOre().orElse( null );
return this.sameOre( a, b );
}
@@ -146,24 +147,21 @@ public class OreHelper
boolean sameOre( final AEItemStack aeItemStack, final ItemStack o )
{
final OreReference a = aeItemStack.getDefinition().getIsOre();
if( a == null )
return aeItemStack.getOre().map( a ->
{
return false;
}
for( final String oreName : a.getEquivalents() )
{
for( final ItemStack oreItem : this.oreDictCache.getUnchecked( oreName ) )
for( final String oreName : a.getEquivalents() )
{
if( OreDictionary.itemMatches( oreItem, o, false ) )
for( final ItemStack oreItem : this.oreDictCache.getUnchecked( oreName ) )
{
return true;
if( OreDictionary.itemMatches( oreItem, o, false ) )
{
return true;
}
}
}
}
return false;
return false;
} )
.orElse( false );
}
List<ItemStack> getCachedOres( final String oreName )
@@ -1,87 +0,0 @@
/*
* 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 net.minecraft.item.Item;
import net.minecraft.nbt.NBTTagCompound;
import appeng.util.Platform;
public class SharedSearchObject
{
private final int def;
private final int hash;
private AESharedNBT shared;
private NBTTagCompound compound;
public SharedSearchObject( final Item itemID, final int damageValue, final NBTTagCompound tagCompound )
{
this.def = ( damageValue << Platform.DEF_OFFSET ) | Item.REGISTRY.getIDForObject( itemID );
this.hash = Platform.itemComparisons().createUnorderedNbtHash( tagCompound );
this.setCompound( tagCompound );
}
@Override
public int hashCode()
{
return this.def ^ this.hash;
}
@Override
public boolean equals( final Object obj )
{
if( obj == null )
{
return false;
}
if( this.getClass() != obj.getClass() )
{
return false;
}
final SharedSearchObject other = (SharedSearchObject) obj;
if( this.def == other.def && this.hash == other.hash )
{
return Platform.itemComparisons().isNbtTagEqual( this.getCompound(), other.getCompound() );
}
return false;
}
AESharedNBT getShared()
{
return this.shared;
}
void setShared( final AESharedNBT shared )
{
this.shared = shared;
}
NBTTagCompound getCompound()
{
return this.compound;
}
void setCompound( final NBTTagCompound compound )
{
this.compound = compound;
}
}