Skip to content

Commit

Permalink
better elytra handling
Browse files Browse the repository at this point in the history
  • Loading branch information
timkalkus committed May 1, 2021
1 parent abe5eee commit f8cfd9e
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.16.1-R0.1-SNAPSHOT</version>
<version>1.16.5-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency><!-- ### this is a custom local build ### -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f8cfd9e

Please sign in to comment.