How to get an NBT attribute using this lib?

Discussion in 'Plugin Development' started by westjet, May 29, 2015.

Thread Status:
Not open for further replies.
  1. Offline

    westjet

    I'm trying to get an NBT attribute from an ItemStack, but whatever I tried using using this library has resulted in an exception.

    I'm using this: http://bukkit.org/threads/library-edit-or-create-nbt-tags-with-a-compact-class-no-obc-nms.178464/

    I have the "Attributes" class and the "NbtFactory" class.

    I want to get the attribute "generic.attackDamage" on a player's item in hand.

    Code:
    @EventHandler
        public void interactDamage(PlayerInteractEvent e) {
            if(e.getAction() == Action.LEFT_CLICK_AIR || e.getAction() == Action.LEFT_CLICK_BLOCK) {
                if(condition1) {
                    Utils.echo("Called");
                    Player p = e.getPlayer();
                    NbtCompound other = NbtFactory.fromItemTag(NbtFactory.getCraftItemStack(p.getItemInHand()));
                    Utils.echo((int) other.getPath("generic.attackDamage") + ""); //Utils.echo is basically Bukkit.getLogger().info(), but just faster for me to type because I do a lot of debugging.
                 
                    
                }
            }
        }
    I would like to try to avoid using NMS for such a small thing like this. How do I accomplish this?
     
  2. Offline

    teej107

    post please. You should also try contacting the creator of that resource.
     
  3. Offline

    westjet

  4. Offline

    teej107

    Yes, but I'm not the creator.
     
  5. Offline

    westjet

    He was last seen a week ago. I'm hoping someone has used that library and knows how to work it OR someone has another idea on how to get an attribute from an itemstack.

    @Comphenix

    EDIT:

    As much as I hated writing it, this is what I'm using now until I get a better method:
    Code:
    public static double itemToDamageValue(ItemStack i, Player damager) {
            Material m = i.getType();
           
            double damage = 1D;
            if (m == Material.DIAMOND_SWORD) {
                damage = 8;
            }
            if (m == Material.DIAMOND_AXE) {
                damage = 7;
            }
            if (m == Material.DIAMOND_PICKAXE) {
                damage = 6;
            }
            if (m == Material.DIAMOND_SPADE) {
                damage = 5;
            }
            if (m == Material.IRON_SWORD) {
                damage = 7;
            }
            if (m == Material.IRON_AXE) {
                damage = 6;
            }
            if (m == Material.IRON_PICKAXE) {
                damage = 5;
            }
            if (m == Material.IRON_SPADE) {
                damage = 4;
            }
            if (m == Material.STONE_SWORD) {
                damage = 6;
            }
            if (m == Material.STONE_AXE) {
                damage = 5;
            }
            if (m == Material.STONE_PICKAXE) {
                damage = 4;
            }
            if (m == Material.STONE_SPADE) {
                damage = 3;
            }
            if (m == Material.GOLD_SWORD || m == Material.WOOD_SWORD) {
                damage = 5;
            }
            if (m == Material.GOLD_AXE || m == Material.WOOD_AXE) {
                damage = 4;
            }
            if (m == Material.GOLD_PICKAXE || m == Material.WOOD_PICKAXE) {
                damage = 3;
            }
            if (m == Material.GOLD_SPADE || m == Material.WOOD_PICKAXE) {
                damage = 2;
            }
            //Basic Critical Hit Detection
            if(damager.getWorld().getBlockAt(damager.getLocation().subtract(0,1,0)).getType() == Material.AIR) {
                damage = damage * 1.5;
            }
    
            return damage;
        }
        
     
    Last edited: May 29, 2015
Thread Status:
Not open for further replies.

Share This Page