Rework fuzzy comparison for 99 percent threshold (#3367)
* Rework fuzzy comparison for 99 percent threshold The current implementation is quite convoluted and can handle 99% incorrectly when items exceed a certain damage range. * Use metadata explicitly for comparison Some item could use metadata to pack damageable and undamageable items into the same id. Thus could lead to get ItemStack#getItemDamage() to return bogus values for undamageable items.
This commit is contained in:
@@ -167,126 +167,30 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
|
||||
{
|
||||
if( st instanceof IAEItemStack )
|
||||
{
|
||||
final IAEItemStack o = (IAEItemStack) st;
|
||||
final IAEItemStack other = (IAEItemStack) st;
|
||||
|
||||
if( this.sameOre( o ) )
|
||||
if( OreHelper.INSTANCE.sameOre( this, other ) )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
if( o.getItem() == this.getItem() )
|
||||
{
|
||||
if( this.getDefinition().getItem().isDamageable() )
|
||||
{
|
||||
final ItemStack a = this.getDefinition();
|
||||
final ItemStack b = o.getDefinition();
|
||||
final ItemStack itemStack = this.getDefinition();
|
||||
final ItemStack otherStack = other.getDefinition();
|
||||
|
||||
try
|
||||
{
|
||||
if( mode == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if( mode == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
final Item ai = a.getItem();
|
||||
final Item bi = b.getItem();
|
||||
|
||||
return ( ai.getDurabilityForDisplay( a ) < 0.001f ) == ( bi.getDurabilityForDisplay( b ) < 0.001f );
|
||||
}
|
||||
else
|
||||
{
|
||||
final Item ai = a.getItem();
|
||||
final Item bi = b.getItem();
|
||||
|
||||
final float percentDamageOfA = 1.0f - (float) ai.getDurabilityForDisplay( a );
|
||||
final float percentDamageOfB = 1.0f - (float) bi.getDurabilityForDisplay( b );
|
||||
|
||||
return ( percentDamageOfA > mode.breakPoint ) == ( percentDamageOfB > mode.breakPoint );
|
||||
}
|
||||
}
|
||||
catch( final Throwable e )
|
||||
{
|
||||
if( mode == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if( mode == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
return ( a.getItemDamage() > 1 ) == ( b.getItemDamage() > 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
final float percentDamageOfA = (float) a.getItemDamage() / (float) a.getMaxDamage();
|
||||
final float percentDamageOfB = (float) b.getItemDamage() / (float) b.getMaxDamage();
|
||||
|
||||
return ( percentDamageOfA > mode.breakPoint ) == ( percentDamageOfB > mode.breakPoint );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.getItemDamage() == o.getItemDamage();
|
||||
}
|
||||
return this.fuzzyItemStackComparison( itemStack, otherStack, mode );
|
||||
}
|
||||
|
||||
if( st instanceof ItemStack )
|
||||
if( st instanceof IAEItemStack )
|
||||
{
|
||||
final ItemStack o = (ItemStack) st;
|
||||
final ItemStack otherStack = (ItemStack) st;
|
||||
|
||||
OreHelper.INSTANCE.sameOre( this, o );
|
||||
|
||||
if( o.getItem() == this.getItem() )
|
||||
if( OreHelper.INSTANCE.sameOre( this, otherStack ) )
|
||||
{
|
||||
if( this.getDefinition().getItem().isDamageable() )
|
||||
{
|
||||
final ItemStack a = this.getDefinition();
|
||||
|
||||
try
|
||||
{
|
||||
if( mode == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if( mode == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
final Item ai = a.getItem();
|
||||
final Item bi = o.getItem();
|
||||
|
||||
return ( ai.getDurabilityForDisplay( a ) < 0.001f ) == ( bi.getDurabilityForDisplay( o ) < 0.001f );
|
||||
}
|
||||
else
|
||||
{
|
||||
final Item ai = a.getItem();
|
||||
final Item bi = o.getItem();
|
||||
|
||||
final float percentDamageOfA = 1.0f - (float) ai.getDurabilityForDisplay( a );
|
||||
final float percentDamageOfB = 1.0f - (float) bi.getDurabilityForDisplay( o );
|
||||
|
||||
return ( percentDamageOfA > mode.breakPoint ) == ( percentDamageOfB > mode.breakPoint );
|
||||
}
|
||||
}
|
||||
catch( final Throwable e )
|
||||
{
|
||||
if( mode == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if( mode == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
return ( a.getItemDamage() > 1 ) == ( o.getItemDamage() > 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
final float percentDamageOfA = (float) a.getItemDamage() / (float) a.getMaxDamage();
|
||||
final float percentDamageOfB = (float) o.getItemDamage() / (float) o.getMaxDamage();
|
||||
|
||||
return ( percentDamageOfA > mode.breakPoint ) == ( percentDamageOfB > mode.breakPoint );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return this.getItemDamage() == o.getItemDamage();
|
||||
return true;
|
||||
}
|
||||
|
||||
final ItemStack itemStack = this.getDefinition();
|
||||
return this.fuzzyItemStackComparison( itemStack, otherStack, mode );
|
||||
}
|
||||
|
||||
return false;
|
||||
@@ -457,4 +361,33 @@ public final class AEItemStack extends AEStack<IAEItemStack> implements IAEItemS
|
||||
return this.sharedStack;
|
||||
}
|
||||
|
||||
private boolean fuzzyItemStackComparison( ItemStack a, ItemStack b, FuzzyMode mode )
|
||||
{
|
||||
if( a.getItem() == b.getItem() )
|
||||
{
|
||||
if( a.getItem().isDamageable() )
|
||||
{
|
||||
if( mode == FuzzyMode.IGNORE_ALL )
|
||||
{
|
||||
return true;
|
||||
}
|
||||
else if( mode == FuzzyMode.PERCENT_99 )
|
||||
{
|
||||
return ( a.getItemDamage() > 1 ) == ( b.getItemDamage() > 1 );
|
||||
}
|
||||
else
|
||||
{
|
||||
final float percentDamageOfA = (float) a.getItemDamage() / (float) a.getMaxDamage();
|
||||
final float percentDamageOfB = (float) b.getItemDamage() / (float) b.getMaxDamage();
|
||||
|
||||
return ( percentDamageOfA > mode.breakPoint ) == ( percentDamageOfB > mode.breakPoint );
|
||||
}
|
||||
}
|
||||
|
||||
return a.getMetadata() == b.getMetadata();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user