How can I take a player inv and drop it at a different location

Discussion in 'Plugin Development' started by keeper317, Aug 30, 2014.

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

    keeper317

    I am trying to take a player's inv when they die and drop it at a specific location predetermined in the config. Also I only want to drop wool blocks otherwise just delete the item.
     
  2. Offline

    Skionz

    iterate through the players inventory when they die, drop the item, cancel the event.
     
  3. Offline

    keeper317

    that will only drop the item where the player dies and you cannot cancel the on death event.
     
  4. Offline

    Gater12

    keeper317
    Loop through the contents (getContents return ItemStack[]).

    Perform your checks, logic...

    Drop item at your desired location.

    ????

    Profit.
     
  5. Offline

    keeper317

    how do you drop an item or better an inv at a specific location
     
  6. Offline

    Gater12

  7. Offline

    keeper317

    This is what i have
    Code:java
    1. @EventHandler
    2. public void onDeath(PlayerDeathEvent e)
    3. {
    4. Player p = (Player) e.getPlayer();
    5. int dropsize = e.getDrops().size();
    6. ItemStack[] stack = (ItemStack[]) e.getDrops().toArray();
    7. for(int i = 0; i < dropsize; i++)
    8. {
    9. if(stack[i].getType() != Material.WOOL)
    10. e.getDrops().remove(stack[i]);
    11. else if(stack[i].getType() == Material.WOOL)
    12. p.getWorld().dropItem(Main.field[2], stack[i]);
    13. }
    14. }[/i][/i][/i][/i]

    this did not work. it says it cannot pass event
     
  8. Offline

    Gater12

    keeper317
    A ConcurrentModificationException has occurred. You cannot modify the List while it is looping through the values.

    To solve this, use an Iterator to iterate through values and removing them when neccessary.

    OR

    Have a separate List or Set that stores ItemStack

    You can loop through the List of ItemStack
    see if it is the ItemStack you want to drop
    Add the drop to the List or Set previously created.
    After looping through the drops, loop through the List or Set
    Drop each item
    Then clear the drops.
     
  9. Offline

    keeper317

    So like what i did already by taking the inv and setting to an array and setting that to an itemstack array. Then going through the array and dropping the wool otherwise removing it.
     
  10. Offline

    ampayne2

    He's not even using a foreach, and even if he was he's still looping through a copy of the list and removing the items from the original list. My guess is the error is from Main.field[2] - can't know for sure unless you actually post the error though.
     
  11. Offline

    keeper317

    The code is on my desktop which currently does not connect to the network. For what ever reason it does not like my school's network. As soon as i can get to the file and copy it over to my laptop i will post it.
     
  12. Offline

    keeper317

    @EventHandler
    public void onDeath(PlayerDeathEvent e)
    {
    Player p = ((Player) e).getPlayer();

    Main.kills.put(p.getKiller().getName(), Misc.getKills(p) + 1);
    ScoreBoardHandler.updateBoard();

    int dropsize = e.getDrops().size();
    ItemStack[] stack = (ItemStack[]) e.getDrops().toArray();
    for(int i = 0; i < dropsize; i++)
    {
    if(stack.getType() == Material.WOOL)
    p.getWorld().dropItem(Main.field[2], stack);
    }
    p.getInventory().clear();
    }
     
  13. Offline

    keeper317

  14. Offline

    keeper317

  15. Offline

    joeygallegos

    keeper317 This should do what you want. Just change the location.
    Code:java
    1. public static function dropItems(Player player) {
    2. Inventory inv = player.getInventory();
    3. for (ItemStack item : inv.getContents()) {
    4. if (item != null) {
    5. player.getWorld().dropItemNaturally(player.getLocation(), item.clone());
    6. }
    7. }
    8. inv.clear();
    9. }
     
Thread Status:
Not open for further replies.

Share This Page