#PDC and events with custom items

1 messages · Page 1 of 1 (latest)

visual nest
#

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.

#
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

livid kayak
#

Why are you storing the durability in the pdc

#

Just store a unique key that identifies it as your weakness sword

high kindle
#

what hes doing is fine though

#

its gonna work?

visual nest
#
@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

livid kayak
#

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

visual nest
#

and how can I do it? Should I create the axe's pdc tag inside the createSupremeAxe method?

livid kayak
#

Yes

visual nest
#
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?

livid kayak
#

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

visual nest
#
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);
    }
}
livid kayak
#

Looks good to me

visual nest
#

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

livid kayak
#

You should

#

But you don’t have to

visual nest
#

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

livid kayak
#

“An error” is not helpful

visual nest
#

"cannot assign a value to final variable 'supremeAxeNK'"

livid kayak
#

Why are you making the axe final

#

I thought this was about the key

visual nest
#

I'm making the NamespacedKey final

livid kayak
#

Assign it when you declare it

high kindle
#

@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

visual nest
#
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

visual nest
#
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?

high kindle
#

why tf r u doing this

#

just move ur god damn constructor