PrepareItemCraftEvent duplication

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

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

    FoxinatorDev

    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

    davidclue

    Share code...
     
  3. Offline

    FoxinatorDev

    Whoops...
    Code:
     @EventHandler
        public void onCraft(PrepareItemCraftEvent event) {
            CraftingInventory inventory = event.getInventory();
            ItemStack[] matrix = inventory.getMatrix();
    
            // Item Checking
            if(Arrays.stream(matrix).anyMatch(item -> {
                CustomItem customItem = getItem(item);
    
                if(customItem == null) return false;
                return customItem.getName().equals("color_material_gradient_pattern");
            })) return;
    
            if(Arrays.stream(matrix).noneMatch(item -> {
                CustomItem customItem = getItem(item);
    
                if(customItem == null) return false;
                return customItem.getName().equals("color_material");
            })) return;
    
            if(Arrays.stream(event.getInventory().getMatrix()).filter(itemStack -> 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")
                    .build();
    
            ItemMeta meta = item.getItemMeta();
    
            meta.getPersistentDataContainer().set(ItemColorMaterial.colorKey, PersistentDataType.INTEGER_ARRAY, new int[]{ColorUtils.asRGB(color)});
    
            item.setItemMeta(meta);
            inventory.setResult(item);
        }
     
  4. Offline

    Kars

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

    FoxinatorDev

    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?
     
Thread Status:
Not open for further replies.

Share This Page