Skip to content

Commit

Permalink
Update durability usage to modern API (Fixes #597)
Browse files Browse the repository at this point in the history
  • Loading branch information
Phoenix616 committed Jun 18, 2024
1 parent bc1f801 commit 07dd3c7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
20 changes: 14 additions & 6 deletions src/main/java/com/Acrobot/Breeze/Utils/MaterialUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,12 @@ public static String getName(ItemStack itemStack, int maxWidth) {
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 = "";
Expand Down Expand Up @@ -386,8 +390,8 @@ public static ItemStack getItem(String itemName) {
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) {
Expand All @@ -414,7 +418,7 @@ public static ItemStack getItem(String itemName) {
* @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()) {
Expand All @@ -429,7 +433,11 @@ public static short getDurability(String itemName) {

data = data.substring(1);

return NumberUtil.isShort(data) ? Short.valueOf(data) : 0;
try {
return Integer.parseInt(data);
} catch (NumberFormatException e) {
return 0;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -35,7 +35,9 @@ public String getMaterialString() {
/**
* 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;
}
Expand Down
10 changes: 9 additions & 1 deletion src/main/java/com/Acrobot/ChestShop/Metadata/ItemDatabase.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
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;
Expand Down Expand Up @@ -140,7 +141,14 @@ public String getItemCode(ItemStack item) {
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);
Expand Down

0 comments on commit 07dd3c7

Please sign in to comment.