Solved ItemStack Damage

Discussion in 'Plugin Development' started by Zandercross12, Jan 12, 2018.

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

    Zandercross12

    Well, I'm coding a plugin and... I'm making a crafting plugin. Here's some of the code:
    Code:
    ItemStack woodPlanks = new ItemStack(Material.WOOD, 2);
    
    if (player.getInventory().contains(Material.STICK, 1) && player.getInventory().contains(woodPlanks)) {
    
    player.getInventory().removeItem(woodPlanks);
    }
    The problem is, whenever I actually craft the item (or attempt to) it only takes the normal planks. It only takes oak planks. Is there a way that I can make it ignore the data values?
     
  2. Offline

    Zombie_Striker

    @Zandercross12
    Yes, but there is currently no built-in method for it. You will need to create the following system:
    1. Get all the contents of the player's inventory
    2. For loop through it.
    3. If the item is not null, and the type is that of wood, remove it and break; out of the loop.
     
  3. Offline

    Zandercross12

    So I have to do this: ;-;
    Code:
    if (player.getInventory().contains(Material.STICK, 1) && player.getInventory().contains(woodPlanks)) {
            player.getInventory().addItem(craftWoodSword);
            player.getInventory().removeItem(new ItemStack(Material.STICK, 1));
              player.getInventory().removeItem(woodPlanks);
            player.sendMessage(ChatColor.GREEN + "You crafted a Wooden Sword");
    
          }
         
          if (player.getInventory().contains(Material.STICK, 1) && player.getInventory().contains(woodPlanks1)) {
            player.getInventory().addItem(craftWoodSword);
            player.getInventory().removeItem(new ItemStack(Material.STICK, 1));
              player.getInventory().removeItem(woodPlanks1);
            player.sendMessage(ChatColor.GREEN + "You crafted a Wooden Sword");
    
          }
         
          if (player.getInventory().contains(Material.STICK, 1) && player.getInventory().contains(woodPlanks2)) {
            player.getInventory().addItem(craftWoodSword);
            player.getInventory().removeItem(new ItemStack(Material.STICK, 1));
              player.getInventory().removeItem(woodPlanks2);
            player.sendMessage(ChatColor.GREEN + "You crafted a Wooden Sword");
    
          }
         
          if (player.getInventory().contains(Material.STICK, 1) && player.getInventory().contains(woodPlanks3)) {
            player.getInventory().addItem(craftWoodSword);
            player.getInventory().removeItem(new ItemStack(Material.STICK, 1));
              player.getInventory().removeItem(woodPlanks3);
            player.sendMessage(ChatColor.GREEN + "You crafted a Wooden Sword");
    
          }
         
          if (player.getInventory().contains(Material.STICK, 1) && player.getInventory().contains(woodPlanks4)) {
            player.getInventory().addItem(craftWoodSword);
            player.getInventory().removeItem(new ItemStack(Material.STICK, 1));
              player.getInventory().removeItem(woodPlanks4);
            player.sendMessage(ChatColor.GREEN + "You crafted a Wooden Sword");
    
          }
         
          if (player.getInventory().contains(Material.STICK, 1) && player.getInventory().contains(woodPlanks5)) {
            player.getInventory().addItem(craftWoodSword);
            player.getInventory().removeItem(new ItemStack(Material.STICK, 1));
              player.getInventory().removeItem(woodPlanks5);
            player.sendMessage(ChatColor.GREEN + "You crafted a Wooden Sword");
    
          } else {
            player.sendMessage(ChatColor.RED + "[!] You do not have enough materials!");
            player.closeInventory();
          }
     
  4. Offline

    Zombie_Striker

    @Zandercross12
    Not at all. Try this instead:
    Code:
     for(int i = 0; i < player.getInventory().getSize()){
    ItemStack is = player.getInventory().getItem(i);
      if(is!=null&&is.getType()==Material.WOOD){
        if(is.getAmount() > 1)
            is.setAmount(is.getAmount-1);
        else
          is.setType(Material.AIR);
        player.getInventory().setItem(i, is);
         break;
      }
    }
    And all you need to do is replace the "removeitem" line with the above.

    BTW: Instead of doing an ItemStack check for "Inventory#contains", use the Material.WOOD object instead.
     
  5. Offline

    Zandercross12

    I don't know how to put this code in the existing one. I don't really know much about for loops.
     
    Last edited: Jan 13, 2018
  6. First go back to the code that you posted in the first message.
    After that, do as Zombie_Striker said.
     
  7. Offline

    Zandercross12

    I did as he said, but it didn't work. Even with Material.WOOD, it didn't delete the planks. My original code could remove all the planks, but when I have a stack of planks, it didn't work. I'm looking for code that would remove the other types of planks, as well as stacks of them.
     
    Last edited: Jan 30, 2018
  8. Offline

    Zandercross12

    Problem Solved!
    I found my own solution! (it took long enough)
    Here's my code:
    Code:
    for (ItemStack loop : player.getPlayer().getInventory().getContents()) {
                        if (loop != null && loop.getType() == Material.WOOD) {
                            if (loop.getAmount() >= 2) {
                                loop.setAmount(loop.getAmount() - 2);
                                player.sendMessage("You have crafted a " + ChatColor.YELLOW + "Wooden Sword!");
                                inventory.addItem(new ItemStack(Material.WOOD_SWORD));
                                inventory.removeItem(new ItemStack(Material.STICK, 1));
                                break;
                            }
                        }
                    }
     
Thread Status:
Not open for further replies.

Share This Page