Solved What i am checking wrong

Discussion in 'Plugin Development' started by xelatercero, Apr 4, 2017.

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

    xelatercero

    Hello, i have a code where basically i have a map with the materials and amount to craft a recipe, then this code checks if in a chest there are those items , the problem is for example i want to craft a dropper and i need cobblestone x 7, bow x 1, 1 x redstone , the problem is that the dropper is crafted, i mean there is not a bow inside the chest so why the dispenser is crafted if the chest only have wood redstone iron, but not a bow.

    Sorry for my expression, thanks.


    PHP:
    Block chestBlock player.getWorld().getBlockAt(chestLoc);
                                if(
    chestBlock.getType() == Material.CHEST) {
                                    
    Chest ch = (ChestchestBlock.getState();
                                    
    boolean allItems false;
                                    for(
    Entry<MaterialIntegeringredients ing.entrySet()) {
                                        
    Material mat ingredients.getKey(); 
                                        
    int num ingredients.getValue();
                                        
    ItemStack generalItems = new ItemStack(mat); //ignore this i dont use it
                                        
    generalItems.setAmount(num); // ignore this
                                      
                                        
    ItemStack[] chestItems ch.getInventory().getContents();
                                        for(
    ItemStack chitem chestItems) {
                                            if(
    chitem != null && chitem.getType() == mat) {
                                                if(
    chitem.getAmount() >= num) {
                                                    
    allItems true;
                                                    continue;
                                                } else {
                                                  
                                                    
    allItems false;
                                                    break;
                                                }
                                            }
                                        }
                                      
                                      
                                      
                                      
                                  
                                    }
                                  
                                    if(
    allItems == true) {
                                        for(
    Entry<MaterialIntegeringredients ing.entrySet()) {
                                            
    Material mat ingredients.getKey();
                                            
    int num ingredients.getValue();
                                            
    ItemStack generalItems = new ItemStack(mat);
                                            
    generalItems.setAmount(num);
                                            
    System.out.println(generalItems);
                                            
    ch.getInventory().removeItem(generalItems);
                                          
                                        }
                                      
                                      
                                      
                                      
                                        
    int recipes 0;
                                        
    ItemStack resultnull;
                                        for(
    Recipe r Bukkit.getRecipesFor(clickedItem)) {
                                          
                                            
    result r.getResult();
                                            break;
                                        }
                                        if(
    result != null) {
                                            
    player.getWorld().dropItemNaturally(player.getLocation(), result);
                                            
    allItems false;
                                        }
                                  
                                    } else {
                                        
    player.sendMessage("no items");
                                    }
     
  2. Your code sets allItems to true for the next ingredient even if it has been set to false by a previous ingredient, because you only break from the current loop (chest).

    You need to break from the ingredient loop aswell: after the first loop check if allItems is false, then break
     
    Last edited: Apr 5, 2017
    xelatercero likes this.
  3. Offline

    xelatercero

    @FisheyLP

    Edit: ok nvm i understand it , thank you
     
    Last edited: Apr 4, 2017
Thread Status:
Not open for further replies.

Share This Page