Save NBTTag UUIDLeast and UUIDMost only when crafting an item

Discussion in 'Plugin Development' started by yologamer384, Sep 18, 2018.

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

    yologamer384

    This is my onEnable with "CustomRecipe.customrecipes();" (this class is below) that load all the recipes when the plugin load. Is there a way to add NBTTag UUIDLeast and UUIDMost for attributes modifies in a yml file only when a player craft a recipe? I know that one of them (or both) are stored in the crafted item that gives to it a specific characteristic to an item (like base damage or armor point).
    Why when I check if the item type for "armor" attribute is a gold piece of armor it gives me:
    Code:
    org.bukkit.plugin.AuthorNagException: No legacy enum constant for GOLDEN_HELMET. Did you forget to define api-version: 1.13 in your plugin.yml?
    (with all the pieces). Same for ITEM_SKULL or any skull
    Code:
    @Override
    public void onEnable() {
        new UUIDDatabaseFile(this);
        RecipesFile.createRecipeFile();
        CustomRecipe.customrecipes();
        RegisterEvents();
        instance = this;
    }
    CustomRecipes Class (open)

    Code:
    public static void customrecipes() {
            for (String identifier : RecipesFile.getRecipe().getConfigurationSection("recipes").getKeys(false)) {
                Random r = new Random();
                int UUIDLeast = r.nextInt(999999999);
                int UUIDMost = r.nextInt(999999999);
                Integer amount = RecipesFile.getRecipe().getInt("recipes." + identifier + ".Amount");
                Short damage = (short) RecipesFile.getRecipe().getInt("recipes." + identifier + ".Data");
                List<String> enchant = RecipesFile.getRecipe().getStringList("recipes." + identifier + ".Enchants");
                String color = RecipesFile.getRecipe().getString("recipes." + identifier + ".Color");
                String[] attributes = RecipesFile.getRecipe().getString("recipes." + identifier + ".Attributes").split(",");
                String[] enchants = new String[enchant.size()];
                enchant.toArray(enchants);
                ItemStack stack = new ItemStack(
                        Material.valueOf(
                                RecipesFile.getRecipe().getString("recipes." + identifier + ".Material").toUpperCase()),
                        amount, damage);
                //if (stack.getType() == Material.PLAYER_HEAD) {
                //    SkullMeta skullMeta = (SkullMeta) stack.getItemMeta();
                //    skullMeta.setOwner(RecipesFile.getRecipe().getString("recipes." + identifier + ".Owner", ""));
                //    String name = Main.color(RecipesFile.getRecipe().getString("recipes." + identifier + ".Name"));
                //    if (!name.isEmpty()) {
                //        skullMeta.setDisplayName(name);
                //    }
                //    stack.setItemMeta(skullMeta);
                //}
                ItemMeta stackMeta = stack.getItemMeta();
                List<String> lores = RecipesFile.getRecipe().getStringList("recipes." + identifier + ".Lores");
                List<String> lore = new ArrayList<String>();
                for (int i = 0; i < lores.size(); i++) {
                    lore.add(Main.color(lores.get(i)));
                    stackMeta.setLore(lore);
                }
                String name = Main.color(RecipesFile.getRecipe().getString("recipes." + identifier + ".Name"));
                if (!name.isEmpty()) {
                    stackMeta.setDisplayName(name);
                }
                String itemflag = RecipesFile.getRecipe().getString("recipes." + identifier + ".ItemFlags");
                stack.setItemMeta(stackMeta);
                if (itemflag.equalsIgnoreCase("unbreakable")) {
                    stackMeta.setUnbreakable(true);
                    stackMeta.addItemFlags(ItemFlag.HIDE_UNBREAKABLE);
                }
                if (stack.getType().equals(Material.LEATHER_CHESTPLATE) || stack.getType().equals(Material.LEATHER_BOOTS) || stack.getType().equals(Material.LEATHER_HELMET) || stack.getType().equals(Material.LEATHER_LEGGINGS) && color != null) {
                    colorizeArmor(stack, color);
                }
                net.minecraft.server.v1_13_R2.ItemStack nmsStack = CraftItemStack.asNMSCopy(stack);
                NBTTagCompound compound = (nmsStack.hasTag()) ? nmsStack.getTag() : new NBTTagCompound();
                NBTTagList modifiers = new NBTTagList();
                if (attributes[0].equals("attack")) {
                    if (!UUIDDatabaseFile.getUUIDLeast(UUIDLeast).equals(UUIDLeast)) {
                        NBTTagCompound attackdamage = new NBTTagCompound();
                        attackdamage.set("AttributeName", new NBTTagString("generic.attackDamage"));
                        attackdamage.set("Name", new NBTTagString("generic.attackDamage"));
                        attackdamage.set("Amount", new NBTTagInt(Integer.valueOf(attributes[1])));
                        attackdamage.set("Operation", new NBTTagInt(0));
                        attackdamage.set("UUIDLeast", new NBTTagInt(UUIDLeast));
                        attackdamage.set("UUIDMost", new NBTTagInt(UUIDMost));
                        attackdamage.set("Slot", new NBTTagString("mainhand"));
                        modifiers.add(attackdamage);
                        compound.set("AttributeModifiers", modifiers);
                        nmsStack.setTag(compound);
                        stack = CraftItemStack.asBukkitCopy(nmsStack);
                        UUIDDatabaseFile.setUUIDLeast(UUIDLeast);
                        UUIDDatabaseFile.setUUIDMost(UUIDMost);
                    }
                }
                if (attributes[0].equals("armor")) {
                    if (stack.getType() == Material.LEATHER_HELMET ||
                            stack.getType() == Material.CHAINMAIL_HELMET ||
                            stack.getType() == Material.IRON_HELMET ||
                            stack.getType() == Material.DIAMOND_HELMET) {
                        NBTTagCompound armor = new NBTTagCompound();
                        armor.set("AttributeName", new NBTTagString("generic.armor"));
                        armor.set("Name", new NBTTagString("generic.armor"));
                        armor.set("Amount", new NBTTagInt(Integer.parseInt(attributes[1])));
                        armor.set("Operation", new NBTTagInt(0));
                        armor.set("UUIDLeast", new NBTTagInt(UUIDLeast));
                        armor.set("UUIDMost", new NBTTagInt(UUIDMost));
                        armor.set("Slot", new NBTTagString("helm"));
                        modifiers.add(armor);
                        compound.set("AttributeModifiers", modifiers);
                        nmsStack.setTag(compound);
                        stack = CraftItemStack.asBukkitCopy(nmsStack);
                        UUIDDatabaseFile.setUUIDLeast(UUIDLeast);
                        UUIDDatabaseFile.setUUIDMost(UUIDMost);
                    }
                    if (stack.getType() == Material.LEATHER_CHESTPLATE ||
                            stack.getType() == Material.CHAINMAIL_CHESTPLATE ||
                            stack.getType() == Material.IRON_CHESTPLATE ||
                            stack.getType() == Material.DIAMOND_CHESTPLATE) {
                        NBTTagCompound armor = new NBTTagCompound();
                        armor.set("AttributeName", new NBTTagString("generic.armor"));
                        armor.set("Name", new NBTTagString("generic.armor"));
                        armor.set("Amount", new NBTTagInt(Integer.parseInt(attributes[1])));
                        armor.set("Operation", new NBTTagInt(0));
                        armor.set("UUIDLeast", new NBTTagInt(UUIDLeast));
                        armor.set("UUIDMost", new NBTTagInt(UUIDMost));
                        armor.set("Slot", new NBTTagString("chest"));
                        modifiers.add(armor);
                        compound.set("AttributeModifiers", modifiers);
                        nmsStack.setTag(compound);
                        stack = CraftItemStack.asBukkitCopy(nmsStack);
                        UUIDDatabaseFile.setUUIDLeast(UUIDLeast);
                        UUIDDatabaseFile.setUUIDMost(UUIDMost);
    
                    }
                    if (stack.getType() == Material.LEATHER_LEGGINGS ||
                            stack.getType() == Material.CHAINMAIL_LEGGINGS ||
                            stack.getType() == Material.IRON_LEGGINGS ||
                            stack.getType() == Material.DIAMOND_LEGGINGS) {
                        NBTTagCompound armor = new NBTTagCompound();
                        armor.set("AttributeName", new NBTTagString("generic.armor"));
                        armor.set("Name", new NBTTagString("generic.armor"));
                        armor.set("Amount", new NBTTagInt(Integer.parseInt(attributes[1])));
                        armor.set("Operation", new NBTTagInt(0));
                        armor.set("UUIDLeast", new NBTTagInt(UUIDLeast));
                        armor.set("UUIDMost", new NBTTagInt(UUIDMost));
                        armor.set("Slot", new NBTTagString("legs"));
                        modifiers.add(armor);
                        compound.set("AttributeModifiers", modifiers);
                        nmsStack.setTag(compound);
                        stack = CraftItemStack.asBukkitCopy(nmsStack);
                        UUIDDatabaseFile.setUUIDLeast(UUIDLeast);
                        UUIDDatabaseFile.setUUIDMost(UUIDMost);
    
                    }
                    if (stack.getType() == Material.LEATHER_BOOTS ||
                            stack.getType() == Material.CHAINMAIL_BOOTS ||
                            stack.getType() == Material.IRON_BOOTS ||
                            stack.getType() == Material.DIAMOND_BOOTS) {
                        NBTTagCompound armor = new NBTTagCompound();
                        armor.set("AttributeName", new NBTTagString("generic.armor"));
                        armor.set("Name", new NBTTagString("generic.armor"));
                        armor.set("Amount", new NBTTagInt(Integer.parseInt(attributes[1])));
                        armor.set("Operation", new NBTTagInt(0));
                        armor.set("UUIDLeast", new NBTTagInt(UUIDLeast));
                        armor.set("UUIDMost", new NBTTagInt(UUIDMost));
                        armor.set("Slot", new NBTTagString("feet"));
                        modifiers.add(armor);
                        compound.set("AttributeModifiers", modifiers);
                        nmsStack.setTag(compound);
                        stack = CraftItemStack.asBukkitCopy(nmsStack);
                        UUIDDatabaseFile.setUUIDLeast(UUIDLeast);
                        UUIDDatabaseFile.setUUIDMost(UUIDMost);
                    }
                }
                if (enchants.length > 0) {
                    for (int i = 0; i < enchants.length; i++) {
                        try {
                            if (Enchantment.getByName(enchants[i].toUpperCase().split(",")[0]) != null) {
                                stack.addUnsafeEnchantment(
                                        Enchantment.getByName(enchants[i].toUpperCase().split(",")[0]),
                                        Integer.parseInt(enchants[i].split(",")[1]));
                            }
                        } finally {
                            if (EnchantsAlias.getAlias(enchants[i].split(",")[0]) != null) {
                                stack.addUnsafeEnchantment(
                                        EnchantsAlias.getAlias(enchants[i].split(",")[0]),
                                        Integer.parseInt(enchants[i].split(",")[1]));
                            }
                        }
                    }
                }
            }
        }
    
        private static ItemStack colorizeArmor(ItemStack is, String color) {
            org.bukkit.inventory.meta.LeatherArmorMeta lam = (org.bukkit.inventory.meta.LeatherArmorMeta) is.getItemMeta();
            lam.setColor(org.bukkit.Color.fromRGB(Integer.valueOf(color.split(" ")[0]), Integer.valueOf(color.split(" ")[1]), Integer.valueOf(color.split(" ")[2])));
            is.setItemMeta(lam);
            return is;
        }
     
  2. It looks like you are getting an error because the names of the items in minecraft are not always equal to the names of the bukkit enum constants in Material.
    If you would like other people to help you, it would be very helpful to tell us the line number the exception mentioned and to show the line numbers of your java file.
     
Thread Status:
Not open for further replies.

Share This Page