Fixes a bug in multi-item salvage
All checks were successful
EpicKnarvik97/Blacksmith/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2023-01-14 17:46:29 +01:00
parent 7cc2aef9d4
commit bbb93bb0eb
2 changed files with 49 additions and 3 deletions

View File

@ -72,12 +72,28 @@ public final class SalvageHelper {
}
//Make sure to give salvage for all items if a stack > 1 is provided
List<ItemStack> salvage = new ArrayList<>();
List<ItemStack> allSalvage = new ArrayList<>();
for (int i = 0; i < salvagedItem.getAmount(); i++) {
salvage.addAll(getSalvage(ingredients, salvagedItem));
allSalvage.addAll(getSalvage(copyItems(ingredients), salvagedItem));
}
return combineStacks(salvage);
return combineStacks(allSalvage);
}
/**
* Copies a list of items
*
* <p>Note: This does not copy any metadata. It only copies the item type and the amount.</p>
*
* @param itemsToCopy <p>The items to make a copy of</p>
* @return <p>A copy of the given items</p>
*/
private static List<ItemStack> copyItems(List<ItemStack> itemsToCopy) {
List<ItemStack> copies = new ArrayList<>(itemsToCopy.size());
for (ItemStack itemStack : itemsToCopy) {
copies.add(new ItemStack(itemStack.getType(), itemStack.getAmount()));
}
return copies;
}
/**

View File

@ -54,6 +54,16 @@ public class SalvageHelperTest {
assertEquals(expectedSalvage, new HashSet<>(SalvageHelper.getSalvage(server, itemToSalvage, null)));
}
@Test
public void getSeveralFullSalvageTest() {
Set<ItemStack> expectedSalvage = new HashSet<>();
expectedSalvage.add(new ItemStack(Material.DIAMOND, 21));
expectedSalvage.add(new ItemStack(Material.STICK, 14));
ItemStack itemToSalvage = new ItemStack(Material.DIAMOND_PICKAXE, 7);
//Note: Conversion to sets makes sure the order doesn't matter
assertEquals(expectedSalvage, new HashSet<>(SalvageHelper.getSalvage(server, itemToSalvage, null)));
}
@Test
public void getNonFullSalvageTest() {
List<ItemStack> expectedSalvage = new ArrayList<>();
@ -73,4 +83,24 @@ public class SalvageHelperTest {
assertNotEquals(expectedSalvage, salvage);
}
@Test
public void ignoredSalvageTest() {
List<ItemStack> expectedSalvage = new ArrayList<>();
expectedSalvage.add(new ItemStack(Material.DIAMOND, 2));
ItemStack itemToSalvage = new ItemStack(Material.DIAMOND_PICKAXE, 1);
List<Material> ignoredSalvage = new ArrayList<>();
ignoredSalvage.add(Material.STICK);
ItemMeta meta = itemToSalvage.getItemMeta();
Damageable damageable = (Damageable) meta;
if (damageable != null) {
damageable.setDamage(100);
}
itemToSalvage.setItemMeta(meta);
List<ItemStack> salvage = SalvageHelper.getSalvage(server, itemToSalvage, ignoredSalvage);
//Assert that some items are given
assertNotEquals(salvage, new ArrayList<>());
//Assert that a damaged diamond pickaxe with sticks ignored returns 2 diamonds a salvage
assertEquals(expectedSalvage, salvage);
}
}