Solved Way to get PeristentDataContainer from a different class?

Discussion in 'Plugin Development' started by man_in_matrix, Sep 13, 2021.

  1. Offline

    man_in_matrix

    So I'm using a persistent data container to store some values
    And I set the namespaced key with its data type and its value successfully but I'm confused on how to get the container from another class is there a way to do so?

    Original container
    NamespacedKey damage = new NamespacedKey(Main.getPlugin(Main.class), "damage");
    ItemMeta itemMeta = bow.getItemMeta();
    itemMeta.getPersistentDataContainer().set(damage, PersistentDataType.INTEGER, 335);

    How do i get this in a different class?

    EDIT: I tried getting the itemMeta using heldItem.getItemMeta().getPersistentDataContainer().get("damage", PersistentDataType.INTEGER));

    held item is the variable name for the item the player is holding

    but it gives an error
    "The method get(NamespacedKey, PersistentDataType<T,Z>) in the type PersistentDataContainer is not applicable for the arguments (String, PersistentDataType<Integer,Integer>)"
     
  2. Offline

    Kars

  3. Offline

    man_in_matrix

    I dont think this would work because each instance of each item has their own set of variables
    here is my itemmanager.
    Code:
    package me.neo.terminator;
    
    import java.util.Arrays;
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Material;
    import org.bukkit.NamespacedKey;
    import org.bukkit.enchantments.Enchantment;
    import org.bukkit.inventory.ItemFlag;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.Recipe;
    import org.bukkit.inventory.ShapedRecipe;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.persistence.PersistentDataType;
    
    
    
    public class ItemManager {
        @SuppressWarnings("unused")
        private Main plugin;
        public ItemManager(Main plugin) {
            this.plugin = plugin;
        }
      public static ItemStack terminator;
      @SuppressWarnings("unused")
    private StatStorage stats;
      public static void init() {
        Bukkit.addRecipe((Recipe)buildTerminator());
        Bukkit.addRecipe((Recipe)buildHyperion());
      }
     
      public static ShapedRecipe buildTerminator() {
        ItemStack terminator = new ItemStack(Material.BOW);
        terminator.addEnchantment(Enchantment.ARROW_INFINITE, 1);
        ItemMeta terminatorMeta = terminator.getItemMeta();
        terminatorMeta.setDisplayName(ChatColor.GOLD + "Terminator");
       
        terminatorMeta.setLore(Arrays.asList(
                ChatColor.GRAY + "Gear Score: " + ChatColor.LIGHT_PURPLE + "665",
                ChatColor.GRAY + "Item Quality " + ChatColor.LIGHT_PURPLE + "100%",
                ChatColor.GRAY + "Damage: " + ChatColor.RED + "+" + ChatColor.RED + "335",
                ChatColor.GRAY + "Strength: " + ChatColor.RED + "+" + ChatColor.RED + "50",
                ChatColor.GRAY + "Crit Damage: " + ChatColor.RED + "+" + ChatColor.RED + "250" + ChatColor.RED + "%",
                ChatColor.GRAY + "Bonus Attack Speed: " + ChatColor.RED + "+" + ChatColor.RED + "40" + ChatColor.RED + "%",
                ChatColor.GRAY + "",
                ChatColor.GOLD + "Shortbow: Instantly shoots!",
                ChatColor.GRAY + "Shoots " + ChatColor.AQUA + "3 " + ChatColor.GRAY + "arrows at once.",
                ChatColor.GRAY + "Can damage enderman",
                ChatColor.GRAY + "",
                ChatColor.RED + "Divides your " + ChatColor.BLUE + "☣ Crit Chance " + ChatColor.RED + "by 4!",
                ChatColor.GRAY + "",
                ChatColor.GOLD + "Ability: Salvation " + ChatColor.YELLOW + ChatColor.BOLD + "RIGHT CLICK",
                ChatColor.GRAY + "Can be casted after landing " + ChatColor.GOLD + "3 " + ChatColor.GRAY + "hits.",
                ChatColor.GRAY + "Shoot a beam, penetrating up",
                ChatColor.GRAY + "to " + ChatColor.YELLOW + "5 " + ChatColor.GRAY + "foes and dealing " + ChatColor.RED + "2x",
                ChatColor.GRAY + "the damage an arrow would.",
                ChatColor.GRAY + "The beam always crits.",
                ChatColor.DARK_GRAY + "Soulflow Cost " + ChatColor.DARK_AQUA + "1 ⸎",
                ChatColor.DARK_GRAY + "Cooldown: " + ChatColor.GREEN + "2s",
                ChatColor.GRAY + "",
                ChatColor.DARK_GRAY + "This item can be reforged!",
                ChatColor.DARK_RED + "☠" + ChatColor.RED + "Requires " + ChatColor.DARK_PURPLE + "Enderman Slayer 7",
                ChatColor.GOLD + "" + ChatColor.BOLD + "LEGENDARY BOW" ));
       
      
       
        terminatorMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
        NamespacedKey damage = new NamespacedKey(Main.getPlugin(Main.class), "damage");
        NamespacedKey strength = new NamespacedKey(Main.getPlugin(Main.class), "strength");
        NamespacedKey intelligence = new NamespacedKey(Main.getPlugin(Main.class), "intelligence");
        NamespacedKey ferocity = new NamespacedKey(Main.getPlugin(Main.class), "ferocity");
        NamespacedKey cDmg = new NamespacedKey(Main.getPlugin(Main.class), "cDmg");
        NamespacedKey cChance = new NamespacedKey(Main.getPlugin(Main.class), "cChance");
        NamespacedKey attackSpeed = new NamespacedKey(Main.getPlugin(Main.class), "attackSpeed");
       
        ItemMeta itemMeta = terminator.getItemMeta();
        itemMeta.getPersistentDataContainer().set(damage, PersistentDataType.INTEGER, 335);
        itemMeta.getPersistentDataContainer().set(strength, PersistentDataType.INTEGER, 50);
        itemMeta.getPersistentDataContainer().set(intelligence, PersistentDataType.INTEGER, 0);
        itemMeta.getPersistentDataContainer().set(ferocity, PersistentDataType.INTEGER, 0);
        itemMeta.getPersistentDataContainer().set(cDmg, PersistentDataType.INTEGER, 250);
        itemMeta.getPersistentDataContainer().set(cChance, PersistentDataType.INTEGER, 0);
        itemMeta.getPersistentDataContainer().set(attackSpeed, PersistentDataType.INTEGER, 40);
        terminator.setItemMeta(terminatorMeta);
        ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(Main.plugin, "Terminator_Bow"), terminator);
        recipe.shape( " ES", "EBS", " ES" );
        recipe.setIngredient('E', Material.ENDER_PEARL);
        recipe.setIngredient('S', Material.STRING);
        recipe.setIngredient('B', Material.BOW);
       
       
        return recipe;
      }
     
      public static ShapedRecipe buildHyperion() {
        ItemStack hyperion = new ItemStack(Material.IRON_SWORD);
        hyperion.addUnsafeEnchantment(Enchantment.PROTECTION_ENVIRONMENTAL, 1);
        ItemMeta hyperionMeta = hyperion.getItemMeta();
        hyperionMeta.setDisplayName(ChatColor.GOLD + "Hyperion");
        hyperionMeta.setLore(Arrays.asList(
                ChatColor.GRAY + "Gear Score: " + ChatColor.LIGHT_PURPLE + "615",
                ChatColor.GRAY + "Damage: " + ChatColor.RED + "+" + ChatColor.RED + "260",
                ChatColor.GRAY + "Strength: " + ChatColor.RED + "+" + ChatColor.RED + "150",
                ChatColor.GRAY + "",
                ChatColor.GRAY + "Intelligence: " + ChatColor.GREEN + "+" + ChatColor.GREEN + "350",
                ChatColor.GRAY + "Ferocity: " + ChatColor.GREEN + "+" + ChatColor.GREEN + "50",
                ChatColor.GRAY + "",
                ChatColor.GRAY + "Deals +" + ChatColor.GREEN + "50% " + ChatColor.GRAY + "damage to",
                ChatColor.GRAY + "Withers. Grants " + ChatColor.RED + "+1 ❁ Damage",
                ChatColor.GRAY + "and " + ChatColor.GREEN + "+2 " + ChatColor.AQUA + "✎ Intelligence",
                ChatColor.GRAY + "per " + ChatColor.RED + "Catacombs " + ChatColor.GRAY + "level.",
                ChatColor.GRAY + "",
                ChatColor.GRAY + "Your Catacombs level: " + ChatColor.RED + "0",
                ChatColor.GRAY + "",
                ChatColor.YELLOW + "Right-click to use your clas ability!",
                ChatColor.GRAY + "",
                ChatColor.DARK_GRAY + "This item can be reforged!",
                ChatColor.DARK_RED + "❣" + ChatColor.RED + "Requires " + ChatColor.GREEN + "Catacombs Floor VII",
                ChatColor.GREEN + "Completion",
                ChatColor.GOLD + "LEGENDARY DUNGEON SWORD" ));
        hyperionMeta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
        NamespacedKey damage = new NamespacedKey(Main.getPlugin(Main.class), "damage");
        NamespacedKey strength = new NamespacedKey(Main.getPlugin(Main.class), "strength");
        NamespacedKey intelligence = new NamespacedKey(Main.getPlugin(Main.class), "intelligence");
        NamespacedKey ferocity = new NamespacedKey(Main.getPlugin(Main.class), "ferocity");
        NamespacedKey cDmg = new NamespacedKey(Main.getPlugin(Main.class), "cDmg");
        NamespacedKey cChance = new NamespacedKey(Main.getPlugin(Main.class), "cChance");
        NamespacedKey attackSpeed = new NamespacedKey(Main.getPlugin(Main.class), "attackSpeed");
       
        ItemMeta itemMeta = hyperion.getItemMeta();
        itemMeta.getPersistentDataContainer().set(damage, PersistentDataType.INTEGER, 260);
        itemMeta.getPersistentDataContainer().set(strength, PersistentDataType.INTEGER, 150);
        itemMeta.getPersistentDataContainer().set(intelligence, PersistentDataType.INTEGER, 350);
        itemMeta.getPersistentDataContainer().set(ferocity, PersistentDataType.INTEGER, 50);
        itemMeta.getPersistentDataContainer().set(cDmg, PersistentDataType.INTEGER, 0);
        itemMeta.getPersistentDataContainer().set(cChance, PersistentDataType.INTEGER, 0);
        itemMeta.getPersistentDataContainer().set(attackSpeed, PersistentDataType.INTEGER, 0);
        hyperion.setItemMeta(hyperionMeta);
        ShapedRecipe recipe = new ShapedRecipe(new NamespacedKey(Main.plugin, "Hyperion"), hyperion);
        recipe.shape("EEE", "ESE", "EEE");
        recipe.setIngredient('E', Material.ENDER_EYE);
        recipe.setIngredient('S', Material.IRON_SWORD);
        return recipe;
      }
     
    /*
        Base itemmeta stat setter
       
        itemMeta.getPersistentDataContainer().set(damage, PersistentDataType.INTEGER, 335);
        itemMeta.getPersistentDataContainer().set(strength, PersistentDataType.INTEGER, 50);
        itemMeta.getPersistentDataContainer().set(intelligence, PersistentDataType.INTEGER, 0);
        itemMeta.getPersistentDataContainer().set(ferocity, PersistentDataType.INTEGER, 0);
        itemMeta.getPersistentDataContainer().set(cDmg, PersistentDataType.INTEGER, 250);
        itemMeta.getPersistentDataContainer().set(cChance, PersistentDataType.INTEGER, 0);
        itemMeta.getPersistentDataContainer().set(attackSpeed, PersistentDataType.INTEGER, 40);
    */
    
    }
    
    
    and here is my damage class
    Code:
    package me.neo.terminator.Listeners;
    
    import java.util.List;
    
    import org.bukkit.ChatColor;
    import org.bukkit.NamespacedKey;
    import org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.ints.Int2ObjectSortedMaps.SynchronizedSortedMap;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Creeper;
    import org.bukkit.entity.Damageable;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.persistence.PersistentDataContainer;
    import org.bukkit.persistence.PersistentDataType;
    
    import me.neo.terminator.ItemManager;
    import me.neo.terminator.Main;
    import me.neo.terminator.StatStorage;
    import me.neo.terminator.sql.SQLGetter;
    
    @SuppressWarnings("unused")
    public class EntityDamage implements Listener {
       
        private ItemManager stats;
       
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent event) {   
            if (event.getEntity() instanceof LivingEntity) {
            if (event.getDamager() instanceof Player) {
                  Player player = (Player) event.getDamager();
                  Entity entity = event.getEntity();
                  ItemStack heldItem = player.getInventory().getItemInMainHand();
                  ItemMeta meta = heldItem.getItemMeta();
                  if (meta == null)
                    return;
                  List<String> strings = meta.getLore();
                  if (strings == null)
                    return;
                
                
                 try {
                    ((Damageable) entity).damage(heldItem.getItemMeta().getPersistentDataContainer().get("damage", PersistentDataType.INTEGER));
                 } catch (Exception e) {
                     e.printStackTrace();
                 }
          }
        }          
      }       
    }
    
    How do i use the damage and other vars stored in each item in the entityDamage class?
     
  4. Offline

    Dai_Kunai

    From what I can see from the original error and the spigot docs (use these more), you're simply using a String instead of a NamespacedKey.
    https://hub.spigotmc.org/javadocs/s...ey,org.bukkit.persistence.PersistentDataType)

    So you would use this:
    https://hub.spigotmc.org/javadocs/s...t>(org.bukkit.plugin.Plugin,java.lang.String) to make a new NamespacedKey

    or perhaps this:
    https://hub.spigotmc.org/javadocs/s...ng(java.lang.String,org.bukkit.plugin.Plugin)

    I don't really fully understand these fully but I hope it helps; I'm a bit intrigued now.
     
  5. Offline

    man_in_matrix

    ty gonna try those rn
    I also think I might have the issue in that the ItemManager isnt adding the containers in the first place but ill see.


    EDIT: FINALLY FIGURED IT OUT

    Turns out the Itemmanager is not adding the stats in any way
    Code:
    package me.neo.terminator.Listeners;
    
    import java.util.List;
    
    import javax.inject.Inject;
    
    import org.bukkit.ChatColor;
    import org.bukkit.NamespacedKey;
    import org.bukkit.craftbukkit.libs.it.unimi.dsi.fastutil.ints.Int2ObjectSortedMaps.SynchronizedSortedMap;
    import org.bukkit.entity.ArmorStand;
    import org.bukkit.entity.Arrow;
    import org.bukkit.entity.Creeper;
    import org.bukkit.entity.Damageable;
    import org.bukkit.entity.Entity;
    import org.bukkit.entity.LivingEntity;
    import org.bukkit.entity.Player;
    import org.bukkit.entity.Projectile;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.entity.EntityDamageByEntityEvent;
    import org.bukkit.event.entity.ProjectileHitEvent;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.persistence.PersistentDataContainer;
    import org.bukkit.persistence.PersistentDataType;
    
    import me.neo.terminator.ItemManager;
    import me.neo.terminator.Main;
    import me.neo.terminator.StatStorage;
    import me.neo.terminator.sql.SQLGetter;
    
    @SuppressWarnings("unused")
    public class EntityDamage implements Listener {
      
      
      
      
      
        @EventHandler
        public void onDamage(EntityDamageByEntityEvent event) {  
            if (event.getEntity() instanceof LivingEntity) {
            if (event.getDamager() instanceof Player) {
                  Player player = (Player) event.getDamager();
                  Entity entity = event.getEntity();
                  ItemStack heldItem = player.getInventory().getItemInMainHand();
                  ItemMeta meta = heldItem.getItemMeta();
                  if (meta == null)
                    return;
                  List<String> strings = meta.getLore();
                  if (strings == null)
                    return;
               
                
                
                  if (heldItem.getItemMeta().getPersistentDataContainer().get(new NamespacedKey(Main.getPlugin(Main.class), "damage"), PersistentDataType.INTEGER) == null) {
                      System.out.println("No data in container");
                      return;
                    
                  }
                 try {
                    int damage = heldItem.getItemMeta().getPersistentDataContainer().get(new NamespacedKey(Main.getPlugin(Main.class), "damage"), PersistentDataType.INTEGER);
                    // ((Damageable) entity).damage(damage);
                    System.out.println(damage);
                 } catch (NullPointerException e) {
                     e.printStackTrace();
                 }
          }
        }         
      }
    
      
    }
    
      
    
    there is a null check there and in the console, it prints out No data in the container
    so it means I gotta find another way to add the containers.

    marking as solved.

    use heldItem.getItemMeta().getPersistentDataContainer().get(new NamespacedKey(Main.getPlugin(Main.class), "key name"), PersistentDataType.data type here

    This allows you to get the data container for the held item with the key from a different class.

    Held item can be anything its a local var for me. hope this helps.
     
    Last edited: Sep 15, 2021

Share This Page