How to make an instance of a class

Discussion in 'Spigot Plugin Help' started by 2Hex, Jul 18, 2021.

  1. So i dont know why but when i do this
    (new NamespacedKey(Sword.getInstance(), killCount),
    it says
    Cannot resolve constructor 'NamespacedKey(me.hex.sword.Sword, int)'

    Cannot resolve constructor 'NamespacedKey(me.hex.sword.Sword, int)'
    Cannot resolve constructor 'NamespacedKey(me.hex.sword.Sword, int)'
    Please help me!
    this is my code

    Code (Java):

    import me.hex.sword.Sword;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.NamespacedKey;
    import org.bukkit.attribute.Attribute;
    import org.bukkit.attribute.AttributeModifier;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDeathEvent;
    import org.bukkit.inventory.EquipmentSlot;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.persistence.PersistentDataContainer;
    import org.bukkit.persistence.PersistentDataType;

    import java.util.ArrayList;
    import java.util.UUID;

    public class OnKill implements Listener {

        public void OnKill(EntityDeathEvent e){
            LivingEntity entity = e.getEntity();
            if(entity.getKiller() instanceof Player){
                ItemStack DeathSword = new ItemStack(Material.EMERALD, 1);
                ItemMeta meta1 = DeathSword.getItemMeta();
                meta1.setDisplayName(ChatColor.GOLD + "Death Sword");
                ArrayList<String> lore1 = new ArrayList<>();
                lore1.add(ChatColor.DARK_GRAY + "Strength " + ChatColor.RED + "80");
                lore1.add(ChatColor.DARK_GRAY + "Damage: " + ChatColor.RED + "69");

                PersistentDataContainer data = entity.getKiller().getPersistentDataContainer();
                int killCount = data.get(new NamespacedKey(Sword.getInstance()), PersistentDataType.INTEGER);
                AttributeModifier modifier1 = new AttributeModifier(UUID.randomUUID(), "genericattackdamage", killCount/25, AttributeModifier.Operation.ADD_NUMBER, EquipmentSlot.HAND);
                meta1.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, modifier1);

                if (!data.has(new NamespacedKey(Sword.getInstance(), killCount), PersistentDataType.INTEGER)) {
                    data.set(new NamespacedKey((Sword.getInstance()), killCount, PersistentDataType.INTEGER, "0"));

                lore1.add(ChatColor.DARK_GRAY + "Your Kills:" + killCount);
                lore1.add(ChatColor.GOLD.toString() + ChatColor.BOLD + "LEGENDARY SWORD");


    and this is my main
    Code (Java):
    package me.hex.sword;


    public class Sword extends JavaPlugin {

        public static Sword instance;
        public static Sword getInstance() { return instance; }
        public void onEnable() {
            // Plugin startup logic
            instance = this;


  2. I'm not 100% sure on this one but on the docs here,, it says you need an instance of your plugin and a string being the key but instead you have a sword class and two integers
    You can get an instance of your Main class / plugin by doing
    Code (Java):
    private Main plugin;
    public Class(Main plugin){
      this.plugin = plugin;
    I think what you could do is attatch a string to your sword class and then use that instead of the sword class itself
  3. oh ty i figured it out