Grabbing items from all inv

Discussion in 'Plugin Development' started by drillzy, May 15, 2017.

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

    drillzy

    Hello guys! i need some help here. So what i want to do is when i do an event with playerinteractevent it will remove or get the amount i want to get or remove from the whole inv. So say i have 64 and i want to remove 65 that will grab one more from the inventory and not just the hand. Because what i have right now, it is getting the amount from the item IN HAND and not the inventory. Please help i am lost at this part and im making a big plugin for my server that involves itemstacking and events and stuff. thanks :)
     
  2. Offline

    Zombie_Striker

    @drillzy
    1. On player interact event.
    2. Create an int that is equal to the amount of items you want to remove
    3. For loop through all the contents of the inventory
    4. For each item, check if it is equal to the item you want to remove.
    5. If so, check the amount and see if the amount is less than the int from #2.
    6. If that is the case, subtract the amount from #2, remove the item, and continue.
    7. Else, if the amount is greater than or equal to the int from #2
    8. Get the itemstack's instance, subtract the int from the amount, and reset the item in the inventory and break.
    Of course, this assumes the player has all the items. If you want to make sure the player has the right amount of items, for loop through the items and find the amount the player has before removing the items.
     
  3. Offline

    drillzy

    @Zombie_Striker would this be it?

    Code:
    public void onInteract(PlayerInteractEvent event){
    
      Inventory inv = event.getPlayer().getInventory();
    
        Player p = event.getPlayer();
    
    
    if(p.hasPermission("rage.use.lvlone")){
    
      if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    
     
    
      for(ItemStack is : inv ){
    
     
    
        if(is.getType() == Material.INK_SACK && (int) is.getDurability() == 5){
    
       
    
       
    
          if(is.getAmount() >= 120){
    
         
    
            p.removePotionEffect(PotionEffectType.SPEED);
    
            p.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
    
            p.removePotionEffect(PotionEffectType.DAMAGE_RESISTANCE);
    
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 2400, 0));
    
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2400, 2));
    
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 2400, 1));
    
            event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("RageActivated")));
    
            is.setAmount(is.getAmount() - 120); // Sets the ammount by getting the total ammount - 20
    
                event.getPlayer().getInventory().setItemInHand(is); //Sets the new ammount to the item held
    
                event.getPlayer().updateInventory(); //updates inventory with the new ammount
    
         
    
      } else {
    
            event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("NotEnoughEcoLevel 1")));
    
      }
    
       
    
       
    
        }
    
        break;
    
      }
    
      } else {
    
    
      }
    
    
    }
    
    
    
    }
    
     
  4. Offline

    Zombie_Striker

    @drillzy
    No:
    1. inv is not an array of itemstacks, so that for loop will not work. But even then, that s not what you wanr (Read #4)
    2. You did not add the int that is require to actually check the amount of items
    3. The items amount will never be over 120, as the limit for a single itemstack is 64.
    4. Setting the item in the hand will only set the item in the player's hand. If you want to check the rest of the inventory, you will need to use the Inventory#setItem method. Also note that if you do this, it would be better to do the for loop using the slot index instead of just looping through the itemstack instances.
     
  5. Offline

    drillzy

    @Zombie_Striker
    i have absolutely ZERO ideas on what to do with 2 and 4. I am so noobish at coding. Help meh XD

    EDIT:
    @Zombie_Striker

    ok this is what i have come up with so far. the i++ is a dead code and have no idea why. help plz :)

    Code:
    public void onInteract(PlayerInteractEvent event){
    
        Player p = event.getPlayer();
    
      
    
    if(p.hasPermission("rage.use.lvlone")){
    
      if(event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK){
    
    
    
        for(int i = 0; i < p.getInventory().getSize(); i++){
    
    
    
          ItemStack itm = p.getInventory().getItem(i);
    
        if(itm != null && itm.getType() == Material.INK_SACK && (int) itm.getDurability() == 5){
    
      
    
        
    
        
    
            p.removePotionEffect(PotionEffectType.SPEED);
    
            p.removePotionEffect(PotionEffectType.INCREASE_DAMAGE);
    
            p.removePotionEffect(PotionEffectType.DAMAGE_RESISTANCE);
    
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 2400, 0));
    
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 2400, 2));
    
            event.getPlayer().addPotionEffect(new PotionEffect(PotionEffectType.DAMAGE_RESISTANCE, 2400, 1));
    
            event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("RageActivated")));
    
            int amt = itm.getAmount() - getConfig().getInt("RageCostLevel 1");
    
      
    
          itm.setAmount(amt);
    
          p.getInventory().setItem(i, amt > 0 ? itm : null);
    
          p.updateInventory();
    
        
    
      } else {
    
            event.getPlayer().sendMessage(ChatColor.translateAlternateColorCodes('&', getConfig().getString("NotEnoughEcoLevel 1")));
    
      }
    
        break;
    
        }
    
      }
    
      } else {
    
    
      }
    
    }
    
     
    Last edited: May 15, 2017
  6. Offline

    Caderape2

    @drillzy

    - I would suggest to create a list of itemstack you want to remove, if it's over 1 stack.
    - Loops the list of itemstack
    - Then use the methods inventory.containsAtLeast(itemstack, itemstack.getAmount()) and inventory.removeItem(itemstack)
     
  7. Offline

    drillzy

    @Caderape2
    Whats up with this code tho? isn't it good enough? if it isnt then explain to me how to make that list of item stack and all that good stuff.
     
Thread Status:
Not open for further replies.

Share This Page