1
\$\begingroup\$

I am making a Bukkit plugin that takes the player to a "FunZone" and allows them to do just about anything. I don't want data transferred between the two worlds. So far I have got most everything stopped. Here's the problem code:

            Integer normInvSize = player.getInventory().getSize();
            Integer i = 0;
            for (i=0; i<normInvSize; i++) {
                ItemStack item = player.getInventory().getItem(i);
                pConfig.set(player.getWorld().getName()+"Inv."+i.toString()+".amount", item.getAmount());
                Short durab = item.getDurability();
                pConfig.set(player.getWorld().getName()+"Inv."+i.toString()+".durabillity", durab.intValue());
                pConfig.set(player.getWorld().getName()+"Inv."+i.toString()+".type", item.getTypeId());
                player.getInventory().removeItem(new ItemStack(item.getType(), item.getAmount()));
            }

I have everything set up how I want it but I get stray items following me between the worlds. The line

player.getInventory().removeItem(new ItemStack(item.getType(), item.getAmount()));

does not remove all of the inventory. I want some code that will remove all inventory items (I've handled armor already). I already have a way to restore the inventory later, but my code currently won't remove everything. For example I can end up transfering some stone tools from the "FunZone" to the normal world, which I don't want.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Did you try to delete the item directly with player.getInventory().removeItem(item) ? \$\endgroup\$
    – Fox32
    Commented Jan 17, 2012 at 18:35

1 Answer 1

0
\$\begingroup\$

You can't clear a list while you're iterating over it, at least not in this way. Let me show you this, we'll assume a list with 5 items:

  1. List: 5, Index: 0
  2. List: 4, Index: 1
  3. List: 3, Index: 2
  4. Break condition already met. 2 Items remaining.

What you want to do is clear the whole inventory after you've iterated over it.

// Cache the inventory instead of a number you'll only use once.
PlayerInventory inventory = player.getInventory();

// Cache the world name
String worldName = player.getWorld().getName();

// Use a foreach instead. Looks better.
for (ItemStack stack : inventory.getContents()) {
    // More cache, String concetanations are expensive!
    String start = worldName + "Inv." + Integer.toString(i);

    pConfig.set(start + ".amount", stack.getAmount());
    pConfig.set(start + ".durability", stack.getDurability().intValue()); // Please be aware that I fixed a typo in durability
    pConfig.set(start + ".type", stack.getTypeId());
}

// And now clear the inventory
inventory.clear();

Also, I don't know what pConfig is, but the structure smells broken if you need save stuff like this. You use an Object-Oriented language, so if you need to split strings to get something, you're doing something wrong. You might want to rewrite that...thing.

For further infos please go to Stack Overflow, f.e.: Efficient Equivalent to Removing while Iterating a Collection

\$\endgroup\$
5
  • \$\begingroup\$ Thank you very much for the help pConfig is my player config file is looks like this: File saveFile = new File(this.getDataFolder() + File.separator + "players", player.getName() + ".yml"); FileConfiguration pConfig = YamlConfiguration.loadConfiguration(saveFile); \$\endgroup\$
    – HcgRandon
    Commented Jan 19, 2012 at 16:42
  • \$\begingroup\$ Also i am very new to java i have been working on it for maybe a couple of weeks so im sure there are plenty better ways of doing things then how i do it. Long story short i am storing the players loc and inv from the world they came. And then when they leave fz(funzone) save inv and loc there. Then teleport and restore. As i said i have this all laid out already teleport inv save and fetch but removeing was not working.And Thanks to this suggestion i can now ride the unused slots (b4 i was saving all inv even blank slots). Then i need to work on armor and exp \$\endgroup\$
    – HcgRandon
    Commented Jan 19, 2012 at 16:59
  • \$\begingroup\$ Um i have a prob the ItemStack stack : inventory.getItems() does not work \$\endgroup\$
    – HcgRandon
    Commented Jan 20, 2012 at 17:28
  • \$\begingroup\$ @HcgRandon: Typo on my end. \$\endgroup\$
    – Bobby
    Commented Jan 21, 2012 at 13:59
  • \$\begingroup\$ yeah i got it all fixed \$\endgroup\$
    – HcgRandon
    Commented Jan 23, 2012 at 13:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .