Custom Crafting

Discussion in 'Bukkit Help' started by pain____, Oct 23, 2020.

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

    pain____

    (New at coding) So I was creating a plugin for my mc smp and I wanted to create a custom item. I created a custom recipe by checking the location of items on a craft:

    Code:
            checkCraft(new ItemStack(core),e.getInventory(), new HashMap<Integer, ItemStack>(){
                {
                    put(1, new ItemStack(Material.WITHER_SKELETON_SKULL));
                    put(3, new ItemStack(Material.WITHER_SKELETON_SKULL));
                    put(4, new ItemStack(Material.MAGMA_CREAM, 32));
                    put(5, new ItemStack(Material.WITHER_SKELETON_SKULL));
                    put(7, new ItemStack(Material.WITHER_SKELETON_SKULL));
    
                }
            });
    Code:
        public void checkCraft(ItemStack result, CraftingInventory inv, HashMap<Integer, ItemStack> ingredients){
    
    
            ItemStack[] matrix = inv.getMatrix();
            for(int i = 0; i < 9; i++){
                if(ingredients.containsKey(i)){
                    if(matrix[i] == null || !matrix[i].equals(ingredients.get(i)) || matrix[i].getAmount() != ingredients.get(i).getAmount() || !matrix[i].getItemMeta().getDisplayName().equals(ingredients.get(i).getItemMeta().getDisplayName())){
                        return;
                    }
                } else {
                    if(matrix[i] != null){
                        return;
                    }
                }
            }
            inv.setResult(core);
    
        }
    I checked these items by inputting a HashMap with the position in relative to the matrix and the Material itself and if it matches, I would print the result. Then when I cleared the result by using an InventoryClickEvent, it would just disappear completely. I first created a clone of the result in the onItemCraft and checked if the "result" portion was clicked and if so, it would set the contents to be an array, which would store only the clone of the result and nothing else.

    Code:
        @EventHandler
        public void onItemCraft(InventoryClickEvent e) {
    
            ItemStack[] blank = {e.getInventory().getItem(0), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR)};
    
            if(e.getSlot() == 0) {
                e.getInventory().setContents(blank);
            }
    
    
        }
    But I got:



    I think this problem was caused because the result was no longer eligible to be crafted. as the inventory is cleared so you couldn't obtain the item. I tried solving the problem by creating a variable called doesntMatter and on checkCraft, if doesntMatter was true, it would just set the result to the item anyway and after clearing the contents it would set doesntMatter to false like so:

    Code:
        @EventHandler
        public void onItemCraft(InventoryClickEvent e) {
    
            ItemStack[] blank = {e.getInventory().getItem(0), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR)};
    
            if(e.getSlot() == 0) {
                doesntMatter = true;
                e.getInventory().setContents(blank);
                doesntMatter = false;
            }
    
    
        }
    
    
        public void checkCraft(ItemStack result, CraftingInventory inv, HashMap<Integer, ItemStack> ingredients){
    
            if(doesntMatter) {
                inv.setResult(core);
            }
    
            ItemStack[] matrix = inv.getMatrix();
            for(int i = 0; i < 9; i++){
                if(ingredients.containsKey(i)){
                    if(matrix[i] == null || !matrix[i].equals(ingredients.get(i)) || matrix[i].getAmount() != ingredients.get(i).getAmount() || !matrix[i].getItemMeta().getDisplayName().equals(ingredients.get(i).getItemMeta().getDisplayName())){
                        return;
                    }
                } else {
                    if(matrix[i] != null){
                        return;
                    }
                }
            }
            inv.setResult(core);
    
        }
    But something like this happened:



    The code:

    Code:
    @SuppressWarnings("serial")
        @EventHandler
        public void onCraftPrepare(PrepareItemCraftEvent e){
    
    
    
            if(e.getInventory().getMatrix().length < 9){
                return;
            }
          
            checkCraft(new ItemStack(core),e.getInventory(), new HashMap<Integer, ItemStack>(){
                {
                    put(1, new ItemStack(Material.WITHER_SKELETON_SKULL));
                    put(3, new ItemStack(Material.WITHER_SKELETON_SKULL));
                    put(4, new ItemStack(Material.MAGMA_CREAM, 32));
                    put(5, new ItemStack(Material.WITHER_SKELETON_SKULL));
                    put(7, new ItemStack(Material.WITHER_SKELETON_SKULL));
    
                }
            });
    
    
    
        }
    
        @EventHandler
        public void onItemCraft(InventoryClickEvent e) {
    
            ItemStack[] blank = {e.getInventory().getItem(0), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR), new ItemStack(Material.AIR)};
    
            if(e.getSlot() == 0) {
                doesntMatter = true;
                e.getInventory().setContents(blank);
                doesntMatter = false;
            }
    
    
        }
    
    
        public void checkCraft(ItemStack result, CraftingInventory inv, HashMap<Integer, ItemStack> ingredients){
    
            if(doesntMatter) {
                inv.setResult(core);
            }
    
            ItemStack[] matrix = inv.getMatrix();
            for(int i = 0; i < 9; i++){
                if(ingredients.containsKey(i)){
                    if(matrix[i] == null || !matrix[i].equals(ingredients.get(i)) || matrix[i].getAmount() != ingredients.get(i).getAmount() || !matrix[i].getItemMeta().getDisplayName().equals(ingredients.get(i).getItemMeta().getDisplayName())){
                        return;
                    }
                } else {
                    if(matrix[i] != null){
                        return;
                    }
                }
            }
            inv.setResult(core);
    
        }
    Can someone please help me lol

    Edit: im on 1.15.2
     
    Last edited: Oct 23, 2020
Thread Status:
Not open for further replies.

Share This Page