Refactored AEStacks & ItemList (#4406)

Removed custom VarInt/VarLong solution.
Moved itemstack and fluidstack serialisation to their own NBT tag to avoid interference.
Removed now useless NBT sorting
Added NullItemList
This commit is contained in:
yueh
2020-06-11 20:20:16 +02:00
committed by GitHub
parent c45a15e13b
commit d449ee47c4
10 changed files with 616 additions and 339 deletions
@@ -32,9 +32,6 @@ import appeng.api.config.FuzzyMode;
final class AESharedItemStack implements Comparable<AESharedItemStack>
{
private static final CompoundNBT LOW_TAG = new CompoundNBT();
private static final CompoundNBT HIGH_TAG = new CompoundNBT();
private final ItemStack itemStack;
private final int itemId;
private final int itemDamage;
@@ -48,9 +45,9 @@ final class AESharedItemStack implements Comparable<AESharedItemStack>
this.hashCode = this.makeHashCode();
}
Bounds getBounds( final FuzzyMode fuzzy, final boolean ignoreMeta )
Bounds getBounds( final FuzzyMode fuzzy )
{
return new Bounds( this.itemStack, fuzzy, ignoreMeta );
return new Bounds( this.itemStack, fuzzy );
}
ItemStack getDefinition()
@@ -116,36 +113,9 @@ final class AESharedItemStack implements Comparable<AESharedItemStack>
return damageValue;
}
final int nbt = this.compareNBT( b.getDefinition() );
if( nbt != 0 )
{
return nbt;
}
if( !this.itemStack.areCapsCompatible( b.getDefinition() ) )
{
return System.identityHashCode( this.itemStack ) - System.identityHashCode( b.getDefinition() );
}
return 0;
}
private int compareNBT( final ItemStack b )
{
if( this.itemStack.getTag() == b.getTag() )
{
return 0;
}
if( this.itemStack.getTag() == LOW_TAG || b.getTag() == HIGH_TAG )
{
return -1;
}
if( this.itemStack.getTag() == HIGH_TAG || b.getTag() == LOW_TAG )
{
return 1;
}
return System.identityHashCode( this.itemStack.getTag() ) - System.identityHashCode( b.getTag() );
}
private int makeHashCode()
{
return Objects.hash( this.itemId, this.itemDamage, this.itemStack.hasTag() ? this.itemStack.getTag() : 0 );
@@ -160,20 +130,20 @@ final class AESharedItemStack implements Comparable<AESharedItemStack>
* Bounds enforced by {@link ItemStack#isEmpty()}
*/
private static final int MIN_DAMAGE_VALUE = 0;
private static final int MAX_DAMAGE_VALUE = 65535;
private static final int MAX_DAMAGE_VALUE = Short.MAX_VALUE;
private final AESharedItemStack lower;
private final AESharedItemStack upper;
public Bounds( final ItemStack stack, final FuzzyMode fuzzy, final boolean ignoreMeta )
public Bounds( final ItemStack stack, final FuzzyMode fuzzy )
{
Preconditions.checkState( !stack.isEmpty(), "ItemStack#isEmpty() has to be false" );
Preconditions.checkState( stack.getCount() == 1, "ItemStack#getCount() has to be 1" );
final CompoundNBT tag = stack.hasTag() ? stack.getTag() : null;
this.lower = this.makeLowerBound( stack, tag, fuzzy, ignoreMeta );
this.upper = this.makeUpperBound( stack, tag, fuzzy, ignoreMeta );
this.lower = this.makeLowerBound( stack, tag, fuzzy );
this.upper = this.makeUpperBound( stack, tag, fuzzy );
}
public AESharedItemStack lower()
@@ -186,83 +156,65 @@ final class AESharedItemStack implements Comparable<AESharedItemStack>
return this.upper;
}
private AESharedItemStack makeLowerBound( final ItemStack itemStack, final CompoundNBT tag, final FuzzyMode fuzzy, final boolean ignoreMeta )
private AESharedItemStack makeLowerBound( final ItemStack itemStack, final CompoundNBT tag, final FuzzyMode fuzzy )
{
final ItemStack newDef = itemStack.copy();
if( ignoreMeta )
if( newDef.getItem().isDamageable() )
{
newDef.setDamage( MIN_DAMAGE_VALUE );
newDef.setTag( tag );
}
else
{
if( newDef.getItem().isDamageable() )
if( fuzzy == FuzzyMode.IGNORE_ALL )
{
if( fuzzy == FuzzyMode.IGNORE_ALL )
newDef.setDamage( MIN_DAMAGE_VALUE );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( itemStack.getDamage() == MIN_DAMAGE_VALUE )
{
newDef.setDamage( MIN_DAMAGE_VALUE );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( itemStack.getDamage() == MIN_DAMAGE_VALUE )
{
newDef.setDamage( MIN_DAMAGE_VALUE );
}
else
{
newDef.setDamage( MIN_DAMAGE_VALUE + 1 );
}
}
else
{
final int breakpoint = fuzzy.calculateBreakPoint( itemStack.getMaxDamage() );
final int damage = breakpoint <= itemStack.getDamage() ? breakpoint : 0;
newDef.setDamage( damage );
newDef.setDamage( MIN_DAMAGE_VALUE + 1 );
}
}
newDef.setTag( LOW_TAG );
else
{
final int breakpoint = fuzzy.calculateBreakPoint( itemStack.getMaxDamage() );
final int damage = breakpoint <= itemStack.getDamage() ? breakpoint : 0;
newDef.setDamage( damage );
}
}
return new AESharedItemStack( newDef );
}
private AESharedItemStack makeUpperBound( final ItemStack itemStack, final CompoundNBT tag, final FuzzyMode fuzzy, final boolean ignoreMeta )
private AESharedItemStack makeUpperBound( final ItemStack itemStack, final CompoundNBT tag, final FuzzyMode fuzzy )
{
final ItemStack newDef = itemStack.copy();
if( ignoreMeta )
if( newDef.getItem().isDamageable() )
{
newDef.setDamage( MAX_DAMAGE_VALUE );
newDef.setTag( tag );
}
else
{
if( newDef.getItem().isDamageable() )
if( fuzzy == FuzzyMode.IGNORE_ALL )
{
if( fuzzy == FuzzyMode.IGNORE_ALL )
newDef.setDamage( itemStack.getMaxDamage() + 1 );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( itemStack.getDamage() == MIN_DAMAGE_VALUE )
{
newDef.setDamage( itemStack.getMaxDamage() + 1 );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( itemStack.getDamage() == MIN_DAMAGE_VALUE )
{
newDef.setDamage( MIN_DAMAGE_VALUE );
}
else
{
newDef.setDamage( itemStack.getMaxDamage() + 1 );
}
newDef.setDamage( MIN_DAMAGE_VALUE );
}
else
{
final int breakpoint = fuzzy.calculateBreakPoint( itemStack.getMaxDamage() );
final int damage = itemStack.getDamage() < breakpoint ? breakpoint - 1 : itemStack.getMaxDamage() + 1;
newDef.setDamage( damage );
newDef.setDamage( itemStack.getMaxDamage() + 1 );
}
}
newDef.setTag( HIGH_TAG );
else
{
final int breakpoint = fuzzy.calculateBreakPoint( itemStack.getMaxDamage() );
final int damage = itemStack.getDamage() < breakpoint ? breakpoint - 1 : itemStack.getMaxDamage() + 1;
newDef.setDamage( damage );
}
}
return new AESharedItemStack( newDef );