diff --git a/pom.xml b/pom.xml index 36e9f78..e436fc5 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ org.spigotmc spigot-api - 1.16.1-R0.1-SNAPSHOT + 1.16.5-R0.1-SNAPSHOT provided diff --git a/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceListener.java b/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceListener.java index aeafa38..720b458 100644 --- a/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceListener.java +++ b/src/main/java/com/github/timkalkus/autoreplace/AutoReplaceListener.java @@ -205,7 +205,9 @@ public void run() { rt.replace(); return; } - if (!event.getItem().getEnchantments().isEmpty()) { + // item has enchantment and is no elytra or is elytra with 1 durability left + if (!event.getItem().getEnchantments().isEmpty() && !event.getItem().getType().equals(Material.ELYTRA) || + event.getItem().getType().equals(Material.ELYTRA) && event.getItem().getType().getMaxDurability() - ((Damageable) Objects.requireNonNull(event.getItem().getItemMeta())).getDamage() == 1) { ReplaceHelper rt = new ReplaceHelper(event.getPlayer(), event.getItem(), itemSlot); rt.swapTool(); } diff --git a/src/test/java/com/github/timkalkus/autoreplace/AutoReplaceTest.java b/src/test/java/com/github/timkalkus/autoreplace/AutoReplaceTest.java index 3f46781..00b69ac 100644 --- a/src/test/java/com/github/timkalkus/autoreplace/AutoReplaceTest.java +++ b/src/test/java/com/github/timkalkus/autoreplace/AutoReplaceTest.java @@ -129,6 +129,56 @@ public void saveEnchantedTool() { assertTrue(player.getInventory().contains(item), "Item was not found in inventory"); } + /** + * Checks that non-enchanted elytras will be replaced with 1 durability left + */ + @Test + public void replaceUnenchantedElytra() { + // create player and add one damaged and one undamaged item + Player player = server.addPlayer(); + ItemStack item0 = getDamageable(Material.ELYTRA, 3, false); + ItemStack item1 = getDamageable(Material.ELYTRA, -1, false); + player.getInventory().setItem(38, item0); + item0 = player.getInventory().getItem(38); + player.getInventory().setItem(0, item1); + // execute event + executeItemDamageEvent(player, item0); + // check if elytra is kept with 2 durability left + assertEquals(2, getRestDurability(player.getInventory().getItem(38)), "elytra should not have been moved, but was"); + assertEquals(2, Arrays.stream(player.getInventory().getContents()).filter(Objects::nonNull). + filter(items -> !AutoReplaceListener.isNullOrAir(items)).count(), "amount of items in inventory does not match expectations"); + executeItemDamageEvent(player, player.getInventory().getItem(38)); + // check if elytra gets replaced with 1 durability left + assertTrue(getRestDurability(player.getInventory().getItem(38)) > 5, "elytra should have been moved, but wasn't"); + assertEquals(2, Arrays.stream(player.getInventory().getContents()).filter(Objects::nonNull). + filter(items -> !AutoReplaceListener.isNullOrAir(items)).count(), "amount of items in inventory does not match expectations"); + } + + /** + * Checks that enchanted elytras will be replaced with 1 durability left + */ + @Test + public void replaceEnchantedElytra() { + // create player and add one damaged and one undamaged item + Player player = server.addPlayer(); + ItemStack item0 = getDamageable(Material.ELYTRA, 3, true); + ItemStack item1 = getDamageable(Material.ELYTRA, -1, true); + player.getInventory().setItem(38, item0); + item0 = player.getInventory().getItem(38); + player.getInventory().setItem(0, item1); + // execute event + executeItemDamageEvent(player, item0); + // check if elytra is kept with 2 durability left + assertEquals(2, getRestDurability(player.getInventory().getItem(38)), "elytra should not have been moved, but was"); + assertEquals(2, Arrays.stream(player.getInventory().getContents()).filter(Objects::nonNull). + filter(items -> !AutoReplaceListener.isNullOrAir(items)).count(), "amount of items in inventory does not match expectations"); + executeItemDamageEvent(player, player.getInventory().getItem(38)); + // check if elytra gets replaced with 1 durability left + assertTrue(getRestDurability(player.getInventory().getItem(38)) > 5, "elytra should have been moved, but wasn't"); + assertEquals(2, Arrays.stream(player.getInventory().getContents()).filter(Objects::nonNull). + filter(items -> !AutoReplaceListener.isNullOrAir(items)).count(), "amount of items in inventory does not match expectations"); + } + @Test public void refillStack() { // create player and add single item