How to handle all cases of a specific item leaving the player's inventory?

Discussion in 'Plugin Development' started by Philipp15b, May 21, 2013.

Thread Status:
Not open for further replies.
  1. Hi,
    In a plugin I want to use an item to display a message via the description to the user, but only as long as it is in the player's inventory. The user would enter a command and the item would be renamed, but when it leaves the player's inventory (through death or dropping the item) the description should be reset to default.

    Now I have two problems: First, for which events do I have to listen? I got PlayerDropItemEvent and EntityDeathEvent so far. The second problem is how to keep the item in memory? With Bukkit's somewhat broken API I always have the fear of creating memory leaks through chunk/world unloads and stuff.

    Thanks for some help.
     
  2. Offline

    Pink__Slime

    Philipp15b
    Use a hashmap, possibly HashMap<String, String>
    First string would be your player's name then the second would be the description (It depends on what you need to save)

    Then just save that to a config file on disable.

    That's just my 2 cents.
     
  3. Offline

    xize

    I'm not sure but can getItemMetaData() and setItemMetaData() not do this?, if I'm correct it can save names and enchants but I'm not fully sure ive never worked with it though.
     
  4. It's not the problem that I don't know how to save players or edit the item description/name, but that I don't know in which ways an item can leave the player's inventory.
     
  5. Offline

    xize

    I think the best way is saving it to a tempory file and delete it when its done, perhaps it sounds strange.

    I guess a HashMap isn't that good solution because it will override key entrys at some point with diffrent type drops.
    a ArrayList is more likely but it's not safe enough against memory leaks, when a player use something like a drop hack with 10 diffrent items your server may can get in some problems.

    I think its best to safe it to a file like a tmp file, in my opinion unless you find a way to to prevent duplicates which is close to memory leaks, or overrides if you have the intention to still save it to a HashMap or ArrayList, then make a method for the saved file and use it into your methods.
     
Thread Status:
Not open for further replies.

Share This Page