Skip to content

Commit

Permalink
Use enums instead of strings for item IDs
Browse files Browse the repository at this point in the history
  • Loading branch information
EsotericEnderman committed Aug 8, 2024
1 parent c5d38a4 commit ca6bb91
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,18 @@ public abstract class AbstractCustomItem implements Listener {

protected final TemplatePaperPlugin plugin;

protected final String itemId;
protected final CustomItem itemId;
protected final Material material;

private final NamespacedKey itemIdKey;

public AbstractCustomItem(TemplatePaperPlugin plugin, String itemId, Material material) {
public AbstractCustomItem(TemplatePaperPlugin plugin, CustomItem itemId, Material material) {
this.plugin = plugin;

this.itemId = itemId;
this.material = material;

itemIdKey = new NamespacedKey(plugin, itemId);
itemIdKey = new NamespacedKey(plugin, itemId.name());

Bukkit.getPluginManager().registerEvents(this, plugin);
}
Expand All @@ -34,7 +34,7 @@ public AbstractCustomItem(TemplatePaperPlugin plugin, String itemId, Material ma

public ItemStack getCustomItem(Player player) {
ItemStack item = new ItemStack(material);
item.editMeta((meta) -> meta.getPersistentDataContainer().set(itemIdKey, PersistentDataType.STRING, itemId));
item.editMeta((meta) -> meta.getPersistentDataContainer().set(itemIdKey, PersistentDataType.STRING, itemId.name()));
return generateCustomItem(item, player);
}

Expand All @@ -47,6 +47,15 @@ public boolean isItem(ItemStack itemStack) {
return false;
}

return itemId.equals(itemStack.getItemMeta().getPersistentDataContainer().get(itemIdKey, PersistentDataType.STRING));
String dataContainerItemIdValue = itemStack.getItemMeta().getPersistentDataContainer().get(itemIdKey, PersistentDataType.STRING);
if (dataContainerItemIdValue == null) {
return false;
}

try {
return itemId == CustomItem.valueOf(dataContainerItemIdValue);
} catch (IllegalArgumentException exception) {
return false;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package net.slqmy.template_paper_plugin.custom_item;

public enum CustomItem {

}

0 comments on commit ca6bb91

Please sign in to comment.