Skip to content

Commit

Permalink
Fixes #5172: Instead of checking if the itemstack is damageable, we h…
Browse files Browse the repository at this point in the history
…ave to check the item. ItemStacks can be made indestructible in various ways, while the decision to use a fuzzy item list is based on the base-item only. (#5174)
  • Loading branch information
shartte authored Apr 23, 2021
1 parent 1ba232c commit ffd928d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/main/java/appeng/util/item/FuzzyItemVariantList.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public int compare(Object a, Object b) {
* higher number than the upper bound.
*/
static ItemDamageBound makeLowerBound(final ItemStack stack, final FuzzyMode fuzzy) {
Preconditions.checkState(stack.isDamageable(), "ItemStack#isDamageable() has to be true");
Preconditions.checkState(stack.getItem().isDamageable(), "Item#isDamageable() has to be true");

int damage;
if (fuzzy == FuzzyMode.IGNORE_ALL) {
Expand All @@ -163,7 +163,7 @@ static ItemDamageBound makeLowerBound(final ItemStack stack, final FuzzyMode fuz
* lower number than the lower bound. It also is exclusive.
*/
static ItemDamageBound makeUpperBound(final ItemStack stack, final FuzzyMode fuzzy) {
Preconditions.checkState(stack.isDamageable(), "ItemStack#isDamageable() has to be true");
Preconditions.checkState(stack.getItem().isDamageable(), "Item#isDamageable() has to be true");

int damage;
if (fuzzy == FuzzyMode.IGNORE_ALL) {
Expand Down
26 changes: 24 additions & 2 deletions src/test/java/appeng/util/item/FuzzyItemVariantListTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;

import java.util.Arrays;

Expand All @@ -21,17 +22,24 @@ void testOrderForDamagedItems() {
ItemStack undamagedSword = new ItemStack(Items.DIAMOND_SWORD);
AESharedItemStack undamagedStack = new AESharedItemStack(undamagedSword);

// Unbreakable Diamond Sword @ 50% durability
ItemStack unbreakableSword = new ItemStack(Items.DIAMOND_SWORD);
unbreakableSword.setDamage(unbreakableSword.getMaxDamage() / 2);
unbreakableSword.getOrCreateTag().putBoolean("Unbreakable", true);
assertFalse(unbreakableSword.isDamageable());
AESharedItemStack unbreakableStack = new AESharedItemStack(unbreakableSword);

// Unenchanted Diamond Sword @ 0% durability
ItemStack damagedSword = new ItemStack(Items.DIAMOND_SWORD);
damagedSword.setDamage(damagedSword.getMaxDamage());
AESharedItemStack damagedStack = new AESharedItemStack(damagedSword);

// Create a list of stacks and sort by their natural order
AESharedItemStack[] stacks = new AESharedItemStack[] {
damagedStack, undamagedStack
damagedStack, undamagedStack, unbreakableStack
};
Arrays.sort(stacks, FuzzyItemVariantList.COMPARATOR);
assertThat(stacks).containsExactly(damagedStack, undamagedStack);
assertThat(stacks).containsExactly(damagedStack, unbreakableStack, undamagedStack);
}

@Nested
Expand All @@ -51,6 +59,20 @@ void testIgnoreAll() {
assertEquals(-1, bounds.upper.itemDamage);
}

/**
* Unbreakable items are still considered at their current damage value.
*/
@Test
void test99PercentDurabilityWithUnbreakable() {
ItemStack unbreakableStack = damagedStack.copy();
unbreakableStack.getOrCreateTag().putBoolean("Unbreakable", true);
assertFalse(unbreakableStack.isDamageable());

DamageBounds bounds = new DamageBounds(unbreakableStack, FuzzyMode.PERCENT_99);
assertEquals(stack.getMaxDamage(), bounds.lower.itemDamage);
assertEquals(0, bounds.upper.itemDamage);
}

/**
* PERCENT_99 with an undamaged item should select only undamaged items, which translates to a damage range of
* [0, -1).
Expand Down

0 comments on commit ffd928d

Please sign in to comment.