Fixes #3768: Damageable items with nbt and without damage could crash. (#3867)

This commit is contained in:
yueh
2018-12-23 22:42:09 +01:00
committed by GitHub
parent a847a01d8a
commit 32a4fcab77
2 changed files with 98 additions and 55 deletions
@@ -46,6 +46,11 @@ final class AESharedItemStack implements Comparable<AESharedItemStack>
this.itemDamage = itemStack.getItemDamage();
}
Bounds getBounds( final FuzzyMode fuzzy, final boolean ignoreMeta )
{
return new Bounds( this.itemStack, fuzzy, ignoreMeta );
}
ItemStack getDefinition()
{
return this.itemStack;
@@ -139,83 +144,122 @@ final class AESharedItemStack implements Comparable<AESharedItemStack>
return System.identityHashCode( this.itemStack.getTagCompound() ) - System.identityHashCode( b.getTagCompound() );
}
public AESharedItemStack getLowerBound( final FuzzyMode fuzzy, final boolean ignoreMeta )
/**
* Creates the lower and upper bounds for a specific shared itemstack.
*/
public static final class Bounds
{
Preconditions.checkState( this.itemStack.getCount() == 1, "ItemStack#getCount() has to be 1" );
final ItemStack newDef = this.itemStack.copy();
/**
* Bounds enforced by {@link ItemStack#isEmpty()}
*/
private static final int MIN_DAMAGE_VALUE = 0;
private static final int MAX_DAMAGE_VALUE = 65535;
if( ignoreMeta )
private final AESharedItemStack lower;
private final AESharedItemStack upper;
public Bounds( final ItemStack stack, final FuzzyMode fuzzy, final boolean ignoreMeta )
{
newDef.setItemDamage( 0 );
Preconditions.checkState( stack.isEmpty(), "ItemStack#isEmpty() has to be false" );
Preconditions.checkState( stack.getCount() == 1, "ItemStack#getCount() has to be 1" );
final NBTTagCompound tag = stack.hasTagCompound() ? stack.getTagCompound() : null;
this.lower = this.makeLowerBound( stack, tag, fuzzy, ignoreMeta );
this.upper = this.makeUpperBound( stack, tag, fuzzy, ignoreMeta );
}
else
public AESharedItemStack lower()
{
if( newDef.getItem().isDamageable() )
return this.lower;
}
public AESharedItemStack upper()
{
return this.upper;
}
private AESharedItemStack makeLowerBound( final ItemStack itemStack, final NBTTagCompound tag, final FuzzyMode fuzzy, final boolean ignoreMeta )
{
final ItemStack newDef = itemStack.copy();
if( ignoreMeta )
{
if( fuzzy == FuzzyMode.IGNORE_ALL )
newDef.setItemDamage( MIN_DAMAGE_VALUE );
newDef.setTagCompound( tag );
}
else
{
if( newDef.getItem().isDamageable() )
{
newDef.setItemDamage( 0 );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( this.itemStack.getItemDamage() == 0 )
if( fuzzy == FuzzyMode.IGNORE_ALL )
{
newDef.setItemDamage( 0 );
newDef.setItemDamage( MIN_DAMAGE_VALUE );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( itemStack.getItemDamage() == MIN_DAMAGE_VALUE )
{
newDef.setItemDamage( MIN_DAMAGE_VALUE );
}
else
{
newDef.setItemDamage( MIN_DAMAGE_VALUE + 1 );
}
}
else
{
newDef.setItemDamage( 1 );
final int breakpoint = fuzzy.calculateBreakPoint( itemStack.getMaxDamage() );
final int damage = breakpoint <= itemStack.getItemDamage() ? breakpoint : 0;
newDef.setItemDamage( damage );
}
}
else
{
final int breakpoint = fuzzy.calculateBreakPoint( this.itemStack.getMaxDamage() );
newDef.setItemDamage( breakpoint <= this.itemDamage ? breakpoint : 0 );
}
newDef.setTagCompound( LOW_TAG );
}
newDef.setTagCompound( LOW_TAG );
return new AESharedItemStack( newDef );
}
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 )
private AESharedItemStack makeUpperBound( final ItemStack itemStack, final NBTTagCompound tag, final FuzzyMode fuzzy, final boolean ignoreMeta )
{
newDef.setItemDamage( Integer.MAX_VALUE );
}
else
{
if( newDef.getItem().isDamageable() )
final ItemStack newDef = itemStack.copy();
if( ignoreMeta )
{
if( fuzzy == FuzzyMode.IGNORE_ALL )
newDef.setItemDamage( MAX_DAMAGE_VALUE );
newDef.setTagCompound( tag );
}
else
{
if( newDef.getItem().isDamageable() )
{
newDef.setItemDamage( this.itemStack.getMaxDamage() + 1 );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( this.itemStack.getItemDamage() == 0 )
if( fuzzy == FuzzyMode.IGNORE_ALL )
{
newDef.setItemDamage( 0 );
newDef.setItemDamage( itemStack.getMaxDamage() + 1 );
}
else if( fuzzy == FuzzyMode.PERCENT_99 )
{
if( itemStack.getItemDamage() == MIN_DAMAGE_VALUE )
{
newDef.setItemDamage( MIN_DAMAGE_VALUE );
}
else
{
newDef.setItemDamage( itemStack.getMaxDamage() + 1 );
}
}
else
{
newDef.setItemDamage( this.itemStack.getMaxDamage() + 1 );
final int breakpoint = fuzzy.calculateBreakPoint( itemStack.getMaxDamage() );
final int damage = itemStack.getItemDamage() < breakpoint ? breakpoint - 1 : itemStack.getMaxDamage() + 1;
newDef.setItemDamage( damage );
}
}
else
{
final int breakpoint = fuzzy.calculateBreakPoint( this.itemStack.getMaxDamage() );
newDef.setItemDamage( this.itemDamage < breakpoint ? breakpoint - 1 : this.itemStack.getMaxDamage() + 1 );
}
newDef.setTagCompound( HIGH_TAG );
}
newDef.setTagCompound( HIGH_TAG );
return new AESharedItemStack( newDef );
}
return new AESharedItemStack( newDef );
}
}
+4 -5
View File
@@ -31,6 +31,7 @@ import net.minecraftforge.oredict.OreDictionary;
import appeng.api.config.FuzzyMode;
import appeng.api.storage.data.IAEItemStack;
import appeng.api.storage.data.IItemList;
import appeng.util.item.AESharedItemStack.Bounds;
public final class ItemList implements IItemList<IAEItemStack>
@@ -99,8 +100,7 @@ public final class ItemList implements IItemList<IAEItemStack>
return output;
}
} )
.orElse( this.findFuzzyDamage( ais, fuzzy, false ) );
} ).orElse( this.findFuzzyDamage( ais, fuzzy, false ) );
}
@Override
@@ -222,9 +222,8 @@ public final class ItemList implements IItemList<IAEItemStack>
private Collection<IAEItemStack> findFuzzyDamage( final IAEItemStack filter, final FuzzyMode fuzzy, final boolean ignoreMeta )
{
final AEItemStack itemStack = (AEItemStack) filter;
final AESharedItemStack low = itemStack.getSharedStack().getLowerBound( fuzzy, ignoreMeta );
final AESharedItemStack high = itemStack.getSharedStack().getUpperBound( fuzzy, ignoreMeta );
final Bounds bounds = itemStack.getSharedStack().getBounds( fuzzy, ignoreMeta );
return this.records.subMap( low, true, high, true ).descendingMap().values();
return this.records.subMap( bounds.lower(), true, bounds.upper(), true ).descendingMap().values();
}
}