Skip to content

Commit

Permalink
Rework fuzzy comparison for 99 percent threshold
Browse files Browse the repository at this point in the history
The current implementation is quite convoluted and can handle 99%
incorrectly when items exceed a certain damage range.
  • Loading branch information
yueh committed Jan 30, 2018
1 parent b1472c5 commit 27bfce1
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 163 deletions.
67 changes: 12 additions & 55 deletions src/main/java/appeng/util/helpers/ItemComparisonHelper.java
Expand Up @@ -95,55 +95,23 @@ public boolean isFuzzyEqualItem( final ItemStack a, final ItemStack b, final Fuz
return false;
}

/*
* if ( a.itemID != 0 && b.itemID != 0 && a.isItemStackDamageable() && ! a.getHasSubtypes() && a.itemID ==
* b.itemID ) { return (a.getItemDamage() > 0) == (b.getItemDamage() > 0); }
*/

// test damageable items..
if( a.getItem() != Items.AIR && b.getItem() != Items.AIR && a.getItem().isDamageable() && a.getItem() == b.getItem() )
if( a.getItem().isDamageable() && a.getItem() == b.getItem() )
{
try
if( mode == FuzzyMode.IGNORE_ALL )
{
return true;
}
else if( mode == FuzzyMode.PERCENT_99 )
{
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 ) > 1 ) == ( bi.getDurabilityForDisplay( b ) > 1 );
}
else
{
final Item ai = a.getItem();
final Item bi = b.getItem();

final float percentDamagedOfA = 1.0f - (float) ai.getDurabilityForDisplay( a );
final float percentDamagedOfB = 1.0f - (float) bi.getDurabilityForDisplay( b );

return ( percentDamagedOfA > mode.breakPoint ) == ( percentDamagedOfB > mode.breakPoint );
}
return ( a.getItemDamage() > 1 ) == ( b.getItemDamage() > 1 );
}
catch( final Throwable e )
else
{
if( mode == FuzzyMode.IGNORE_ALL )
{
return true;
}
else if( mode == FuzzyMode.PERCENT_99 )
{
return ( a.getItemDamage() > 1 ) == ( b.getItemDamage() > 1 );
}
else
{
final float percentDamagedOfA = (float) a.getItemDamage() / (float) a.getMaxDamage();
final float percentDamagedOfB = (float) b.getItemDamage() / (float) b.getMaxDamage();

return ( percentDamagedOfA > mode.breakPoint ) == ( percentDamagedOfB > mode.breakPoint );
}
final float percentDamagedOfA = (float) a.getItemDamage() / (float) a.getMaxDamage();
final float percentDamagedOfB = (float) b.getItemDamage() / (float) b.getMaxDamage();

return ( percentDamagedOfA > mode.breakPoint ) == ( percentDamagedOfB > mode.breakPoint );
}
}

Expand All @@ -155,17 +123,6 @@ else if( mode == FuzzyMode.PERCENT_99 )
return true;
}

/*
* // test ore dictionary.. int OreID = getOreID( a ); if ( OreID != -1 ) return OreID == getOreID( b );
* if ( Mode != FuzzyMode.IGNORE_ALL ) { if ( a.hasTagCompound() && !isShared( a.getTagCompound() ) ) { a =
* Platform.getSharedItemStack( AEItemStack.create( a ) ); }
* if ( b.hasTagCompound() && !isShared( b.getTagCompound() ) ) { b = Platform.getSharedItemStack(
* AEItemStack.create( b ) ); }
* // test regular items with damage values and what not... if ( isShared( a.getTagCompound() ) && isShared(
* b.getTagCompound() ) && a.itemID == b.itemID ) { return ((AppEngSharedNBTTagCompound)
* a.getTagCompound()).compareFuzzyWithRegistry( (AppEngSharedNBTTagCompound) b.getTagCompound() ); } }
*/

return a.isItemEqual( b );
}

Expand Down
152 changes: 44 additions & 108 deletions src/main/java/appeng/util/item/AEItemStack.java
Expand Up @@ -26,8 +26,11 @@
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

import com.google.common.base.Preconditions;

import io.netty.buffer.ByteBuf;

import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
Expand Down Expand Up @@ -167,126 +170,30 @@ public boolean fuzzyComparison( final Object st, final FuzzyMode mode )
{
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();

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 );
}
}
}
final ItemStack itemStack = this.getDefinition();
final ItemStack otherStack = other.getDefinition();

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;
Expand Down Expand Up @@ -457,4 +364,33 @@ AESharedItemStack getSharedStack()
return this.sharedStack;
}

private boolean fuzzyItemStackComparison( ItemStack a, ItemStack b, FuzzyMode mode )
{
if( a.getItem() == b.getItem() )
{
if( this.getDefinition().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 this.getItemDamage() == b.getItemDamage();
}

return false;
}

}

0 comments on commit 27bfce1

Please sign in to comment.