Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question
1  
Did you try to delete the item directly with player.getInventory().removeItem(item) ? –  Fox32 Jan 17 '12 at 18:35
add comment

1 Answer

up vote 0 down vote accepted

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

share|improve this answer
    
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); –  HcgRandon Jan 19 '12 at 16:42
    
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 –  HcgRandon Jan 19 '12 at 16:59
    
Um i have a prob the ItemStack stack : inventory.getItems() does not work –  HcgRandon Jan 20 '12 at 17:28
    
@HcgRandon: Typo on my end. –  Bobby Jan 21 '12 at 13:59
    
yeah i got it all fixed –  HcgRandon Jan 23 '12 at 13:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.