Update durability usage to modern API (Fixes #597)

This commit is contained in:
Phoenix616 2024-06-18 22:49:51 +01:00
parent bc1f80128b
commit 07dd3c7a70
No known key found for this signature in database
GPG Key ID: 40E2321E71738EB0
3 changed files with 26 additions and 8 deletions

View File

@ -259,8 +259,12 @@ public class MaterialUtil {
String itemName = itemStack.getType().toString();
String durability = "";
if (itemStack.getDurability() != 0) {
durability = ":" + itemStack.getDurability();
ItemMeta meta = itemStack.getItemMeta();
if (meta instanceof Damageable) {
Damageable damageable = (Damageable) meta;
if (damageable.hasDamage()) {
durability = ":" + damageable.getDamage();
}
}
String metaData = "";
@ -386,8 +390,8 @@ public class MaterialUtil {
split[i] = split[i].trim();
}
short durability = getDurability(itemName);
MaterialParseEvent parseEvent = new MaterialParseEvent(split[0], durability);
int durability = getDurability(itemName);
MaterialParseEvent parseEvent = new MaterialParseEvent(split[0], (short) durability);
Bukkit.getPluginManager().callEvent(parseEvent);
Material material = parseEvent.getMaterial();
if (material == null) {
@ -414,7 +418,7 @@ public class MaterialUtil {
* @param itemName Item name
* @return Durability found
*/
public static short getDurability(String itemName) {
public static int getDurability(String itemName) {
Matcher m = DURABILITY.matcher(itemName);
if (!m.find()) {
@ -429,7 +433,11 @@ public class MaterialUtil {
data = data.substring(1);
return NumberUtil.isShort(data) ? Short.valueOf(data) : 0;
try {
return Integer.parseInt(data);
} catch (NumberFormatException e) {
return 0;
}
}
/**

View File

@ -11,7 +11,7 @@ public class MaterialParseEvent extends Event {
private final short data;
private Material material = null;
public MaterialParseEvent(String materialString, short data) {
public MaterialParseEvent(String materialString, @Deprecated short data) {
this.materialString = materialString;
this.data = data;
}
@ -35,7 +35,9 @@ public class MaterialParseEvent extends Event {
/**
* Get the data of legacy materials that might result in different flattening materials
* @return The data
* @deprecated Modern materials don't use data values anymore
*/
@Deprecated
public short getData() {
return data;
}

View File

@ -13,6 +13,7 @@ import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.configuration.file.YamlConstructor;
import org.bukkit.configuration.file.YamlRepresenter;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.Damageable;
import org.bukkit.inventory.meta.ItemMeta;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
@ -140,7 +141,14 @@ public class ItemDatabase {
try {
ItemStack clone = new ItemStack(item);
clone.setAmount(1);
clone.setDurability((short) 0);
ItemMeta meta = clone.getItemMeta();
if (meta instanceof Damageable) {
Damageable damageable = (Damageable) clone.getItemMeta();
if (damageable.hasDamage()) {
damageable.setDamage(0);
}
}
String dumped = yaml.dump(clone);
ItemStack loadedItem = yaml.loadAs(dumped, ItemStack.class);