PrepareItemCraftEvent duplication

Discussion in 'Plugin Development' started by FoxinatorDev, May 17, 2021.

  1. Offline


    I'm trying to make a special custom recipe similar to dying leather armor using PrepareItemCraftEvent but I've come across a duplication issue. Whenever I craft, the items in the matrix duplicate if there is an extra amount of that item. From what I've tested, the item doubles in amount and subtracts by 2. I know I could work around this by dividing by 2 and adding 2 to each item if the amount is greater than 1 but I would want to know if there is a better way to fix this. All help is appreciated and if you need any other information, I'll be glad to give.

    I'm using 1.16.5 btw
    Last edited: May 17, 2021
  2. Offline


    Share code...
  3. Offline


        public void onCraft(PrepareItemCraftEvent event) {
            CraftingInventory inventory = event.getInventory();
            ItemStack[] matrix = inventory.getMatrix();
            // Item Checking
            if( -> {
                CustomItem customItem = getItem(item);
                if(customItem == null) return false;
                return customItem.getName().equals("color_material_gradient_pattern");
            })) return;
            if( -> {
                CustomItem customItem = getItem(item);
                if(customItem == null) return false;
                return customItem.getName().equals("color_material");
            })) return;
            if( -> itemStack != null).count() <= 1) return;
            org.bukkit.Color color = null;
            // Combining colors
            for(ItemStack item : event.getInventory().getMatrix()) {
                CustomItem customItem = getItem(item);
                if(customItem == null) continue;
                if(!customItem.getName().equals("color_material")) continue;
                ItemMeta meta = item.getItemMeta();
                int[] rgb = meta.getPersistentDataContainer().get(ItemColorMaterial.colorKey, PersistentDataType.INTEGER_ARRAY);
                if(rgb == null) return;
                if(rgb.length > 1) return;
                int c = rgb[0];
                Color col = new Color(c);
                if(color == null) {
                    color = org.bukkit.Color.fromRGB(col.getRed(), col.getGreen(), col.getBlue());
                } else {
                    color = color.mixColors(org.bukkit.Color.fromRGB(col.getRed(), col.getGreen(), col.getBlue()));
            // Creating result
            if(color == null) return;
            ItemStack item = new ItemStackBuilder(new ItemColorMaterial().getItemStack())
                    .setDisplayName(ChatColor.of(ColorUtils.toColor(color)) + "Color Material")
            ItemMeta meta = item.getItemMeta();
            meta.getPersistentDataContainer().set(ItemColorMaterial.colorKey, PersistentDataType.INTEGER_ARRAY, new int[]{ColorUtils.asRGB(color)});
  4. Offline


    I had the same problem a while back and have yet to solve it. I recon this is why custom recipes exist.
  5. Offline


    Yeah I checked threads related to this issue and there aren't any solved :(

    For now, I have a work around and a question. I have made a recipe that needs 2 name tags, which is the same material "Color Material" item is. When the player puts in 2 name tags in the crafting table, it checks if both items are "Color Material". If so, it will combine the colors and set the result, and if not, it removes the result. I have yet to find any problem with this but my question is is there a way to check recipes based off PersistentData? I've only found out how to create recipes with exact ItemStacks or with materials. And my other question is is there any other way to create recipes similar to crafting dyed leather armor?

Share This Page