Hi guys. Basically I'm trying to create an axe that causes weakness on hit. I created a class for the item creation and added an event for this axe. The problem is that someone told me that my event would break once the axe's durability decreases and that I should create a PDC for my custom item. As it is my first time dealing with PDC, I'm kinda struggling to get it done.
#PDC and events with custom items
1 messages · Page 1 of 1 (latest)
public class SupremeAxe {
private short maxDur;
private int lostDur;
private int totalDur;
private final NamespacedKey supreme_axe_nk;
public static ItemStack supremeAxe;
public static void initialize(){
createSupremeAxe();
}
private static void createSupremeAxe(){
ItemStack item = new ItemStack(Material.NETHERITE_AXE);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("§a§lSupreme Axe");
List<String> lore = new ArrayList<>();
lore.add(0,"§fThe most powerful axe");
lore.add(1,"§ffor the strongest warrior!");
meta.setLore(lore);
meta.addEnchant(Enchantment.DAMAGE_ALL, 7, true);
meta.addEnchant(Enchantment.FIRE_ASPECT, 4, true);
meta.addEnchant(Enchantment.MENDING, 2, true);
item.setItemMeta(meta);
}
public SupremeAxe(Plugin myPlugin){
this.supreme_axe_nk = new NamespacedKey(myPlugin,"supreme_axe_durability");
}
public void supremeAxeDurability(ItemStack itemStack){
if(!(itemStack.equals(supremeAxe))){
return;
}
ItemMeta itemMeta = itemStack.getItemMeta();
this.maxDur = itemStack.getType().getMaxDurability();
if(!(itemMeta instanceof Damageable)){
return;
}
this.lostDur = ((Damageable) itemMeta).getDamage();
this.totalDur = (int)maxDur - lostDur;
itemMeta.getPersistentDataContainer().set(supreme_axe_nk, PersistentDataType.INTEGER, totalDur);
itemStack.setItemMeta(itemMeta);
}
}
this is the class where i create the item and its pdc to store the durability
Why are you storing the durability in the pdc
Just store a unique key that identifies it as your weakness sword
to check the durability in the event handler
@EventHandler
public void onHitBySupremeAxe(EntityDamageByEntityEvent e){
if(e.getDamager() instanceof Player){
Player damager = (Player)e.getDamager();
if(damager.getInventory().getItemInMainHand().equals(SupremeAxe.supremeAxe)){
if(e.getEntity() instanceof LivingEntity) {
FluPotion.FLU_POTION_EFFECT.apply((LivingEntity)e.getEntity());
}
}
}
}
this is the event handler
I was said that it would break and that I had to check the durability
No
It was said that it would break when the durability decreased
Add a pdc tag to your axe and compare that rather than comparing itemstacks
and how can I do it? Should I create the axe's pdc tag inside the createSupremeAxe method?
Yes
private static void createSupremeAxe(){
ItemStack item = new ItemStack(Material.NETHERITE_AXE);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("§a§lSupreme Axe");
List<String> lore = new ArrayList<>();
lore.add(0,"§fThe most powerful axe");
lore.add(1,"§ffor the strongest warrior!");
meta.setLore(lore);
meta.addEnchant(Enchantment.DAMAGE_ALL, 7, true);
meta.addEnchant(Enchantment.FIRE_ASPECT, 4, true);
meta.addEnchant(Enchantment.MENDING, 2, true);
NamespacedKey supreme_axe_nk = new NamespacedKey(TestPlugin.getInstance(),"supreme_axe_durability");
if (!(meta instanceof Damageable)){
return;
}
meta.getPersistentDataContainer().set(supreme_axe_nk, PersistentDataType.INTEGER, (((Damageable) meta).getDamage()));
item.setItemMeta(meta);
}
did i do it right?
Stop storing the durability
Just store a string to identify your item
Or an int, or whatever you want to identify it
Then check for that tag in whatever event you want, and apply your effects
public class SupremeAxe {
public static ItemStack supremeAxe;
public static void initialize(){
createSupremeAxe();
}
private static void createSupremeAxe(){
ItemStack item = new ItemStack(Material.NETHERITE_AXE);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("§a§lSupreme Axe");
List<String> lore = new ArrayList<>();
lore.add(0,"§fThe most powerful axe");
lore.add(1,"§ffor the strongest warrior!");
meta.setLore(lore);
meta.addEnchant(Enchantment.DAMAGE_ALL, 7, true);
meta.addEnchant(Enchantment.FIRE_ASPECT, 4, true);
meta.addEnchant(Enchantment.MENDING, 2, true);
NamespacedKey supreme_axe_nk = new NamespacedKey(TestPlugin.getInstance(),"supreme_axe");
meta.getPersistentDataContainer().set(supreme_axe_nk, PersistentDataType.STRING, "supreme_axe_tag");
item.setItemMeta(meta);
}
}
Looks good to me
the only doubt I still have is if I created the NamespacedKey correctly
cause someone said i had to declare it as a private and final variable
And how would I do that? I tried to but I get an error telling me that I can't a non static field inside a static method, but when a declare the object as static I still get an error
Sorry if I'm bothering you, I'm just a bit noob at this
“An error” is not helpful
"cannot assign a value to final variable 'supremeAxeNK'"
I'm making the NamespacedKey final
Assign it when you declare it
@visual nest
its since
u initialized the namespacedkey after u used it
which isnt how it works
(its since ur constructor is down there for some reason lmao)
the constructor is before everything except the fields
public class SupremeAxe {
private static final NamespacedKey supreme_axe_key;
public static ItemStack supremeAxe;
public static void initialize(){
createSupremeAxe();
}
private static void createSupremeAxe(){
ItemStack item = new ItemStack(Material.NETHERITE_AXE);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("§a§lSupreme Axe");
List<String> lore = new ArrayList<>();
lore.add(0,"§fThe most powerful axe");
lore.add(1,"§ffor the strongest warrior!");
meta.setLore(lore);
meta.addEnchant(Enchantment.DAMAGE_ALL, 7, true);
meta.addEnchant(Enchantment.FIRE_ASPECT, 4, true);
meta.addEnchant(Enchantment.MENDING, 2, true);
supreme_axe_key = new NamespacedKey(TestPlugin.getInstance(),"supreme_axe");
meta.getPersistentDataContainer().set(supreme_axe_key, PersistentDataType.STRING, "supreme_axe_tag");
item.setItemMeta(meta);
}
}
this is my class rn
i didn't initialized the namespacedkey after i used it, i just declared it
and then i initialized it inside the createSupremeAxe() method
public class SupremeAxe {
private static final NamespacedKey supreme_axe_id = new NamespacedKey(TestPlugin.getInstance(),"supreme_axe_id");
public static ItemStack supremeAxe;
public static void initialize(){
createSupremeAxe();
}
private static void createSupremeAxe(){
ItemStack item = new ItemStack(Material.NETHERITE_AXE);
ItemMeta meta = item.getItemMeta();
meta.setDisplayName("§a§lSupreme Axe");
List<String> lore = new ArrayList<>();
lore.add(0,"§fThe most powerful axe");
lore.add(1,"§ffor the strongest warrior!");
meta.setLore(lore);
meta.addEnchant(Enchantment.DAMAGE_ALL, 7, true);
meta.addEnchant(Enchantment.FIRE_ASPECT, 4, true);
meta.addEnchant(Enchantment.MENDING, 2, true);
meta.getPersistentDataContainer().set(supreme_axe_id, PersistentDataType.STRING, "supreme_axe_tag");
item.setItemMeta(meta);
}
}
would it work now?