Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.20-R0.1-SNAPSHOT</version>
<version>1.21.4-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>me.clip</groupId>
<artifactId>placeholderapi</artifactId>
<version>2.11.3</version>
<version>2.11.6</version>
<scope>provided</scope>
</dependency>
</dependencies>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package com.extendedclip.papi.expansion.player;

import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderAPIPlugin;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.PlayerInventory;
import org.bukkit.inventory.meta.Damageable;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
Expand Down Expand Up @@ -199,7 +199,23 @@ public static ItemStack itemInHand(Player p) {
}

public static int durability(ItemStack item) {
return item != null ? item.getType().getMaxDurability() - item.getDurability() : 0;
if (item == null) {
return 0;
}

if (!VersionHelper.IS_1_21_OR_NEWER) {
return item.getType().getMaxDurability() - item.getDurability();
}

final Damageable meta = (Damageable) item.getItemMeta();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't it unsafe to do such a cast here without checking if it actually is of type Damagable?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, All ItemMeta implements Damageable https://hub.spigotmc.org/stash/projects/SPIGOT/repos/craftbukkit/browse/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java#172

Spigot is also doing the same in ItemStack#getDurability
image

if (meta == null) {
return 0;
}

if (meta.hasMaxDamage()) {
return meta.getMaxDamage() - meta.getDamage();
}
return item.getType().getMaxDurability() - meta.getDamage();
}

public static int getEmptySlots(Player p) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ public final class VersionHelper {
*/
public static final boolean IS_1_20_4_OR_NEWER = VERSION >= 1_20_4;

/**
* @see org.bukkit.inventory.meta.Damageable#getMaxDamage()
*/
public static final boolean IS_1_21_OR_NEWER = VERSION >= 1_21_0;

private VersionHelper() { }

/**
Expand Down