Feature request for Bukkit

Discussion in 'Bukkit Discussion' started by Yogoda, Jan 4, 2011.

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

    Yogoda

    I did read some of the post and I don't know if something like this is planned to be added in Bukkit, but one of the things I would like to see the most is this :

    Being able to attach custom data to players

    Every time I create a plugin for hmod I need to add a player list to keep per player informations, then every time a callback is called the plugin need to look in a list/hastable to find the corresponding player. If you have 20 plugins doing this it take memory for nothing, and it is hard to share player properties between the plugins.

    I request something like this :
    • setProperty(Player player, String name, Object o)
    • Object getProperty(Player player, String name)
    It only requires one pointer to a HashMap per player, if it is not used, it does take almost 0 memory and do not impact the server performances at all.

    If it is used, then a HashMap is created for the player. All plugins will be able to access the player properties set by other plugins.

    For example, a currency plugin like iConomy could do a : setProperty(player, "cashAccount", new Integer(balance)).

    All other plugins could easily access the player cash account by using : (Integer)getProperty(player, "cashAccount")

    For MoveCraft for example, I could get the player controlled craft inside a callback like this : (Craft)getProperty(player, "movecraft")

    This is for players, it could also be done for mobs, items and so on, but my request is only for players.

    That would be an awesome feature, is this something planned ?
     
  2. Offline

    Dinnerbone Bukkit Team Member

    I know how much of an issue this is and I've tried to think long and hard to an easy way to do it but I can't.

    Example scenario
    Imagine you have two plugins, a social plugin and an economy plugin. The social plugin stores a players join-date, last-seen and last-message. The economy plugin stores a players balance and their preferred currency. This is 3 fields and 2 fields respectively, totaling 5 custom fields.

    The economy plugin has a nice /money command, which checks your current balance. The social plugin has a /whois command, telling you when a player first joined the server.

    Storing per-plugin
    When a player types /money, it looks up in a hashmap containing (current player count) keys to find an object holding the required information.
    When a player types /whois, it looks up in a hashmap containing (current player count) keys to find an object holding the required information.

    Storing on the player
    When a player types /money, it looks up in a hashmap containing 5 keys to find the required information.
    When a player types /whois, it looks up in a hashmap containing 5 keys to find the required information.



    This means, basically, there's a big line of where the performance hit will be per-server, and which one would be the better choice. If you have more players than plugins, then storing on the player is best. If you have more plugins than players, then storing per-plugin is best.

    I really can't think of a nice middleground here. The per-player is nice but it would need a lot of sanity checking to make sure another plugin hasn't tainted your own stuff, and we'd rather you call economy.setBalance(player, x) than directly go to its datasource to set the value, to allow the plugin a little control over what happens when it's set (maybe send events to other plugins to check how much they should really receive).
     
  3. Offline

    Yogoda

    Tell me if I am wrong, I don't know exactly how a Hashmap works in Java, but the time needed to find an information into a Hashtable is not impacted by the number of elements.

    The algorithm is :

    1 : convert the key to a hashcode
    2 : take the element at the [hashcode] position
    3 : if there is several elements at this location, then do a real comparison of the key

    So if you have 1 or 10000 elements into the HashMap, the time to find something should be more or less the same
     
  4. Offline

    root

    So just write a plugin for the plugin :)

    Really, it should be up to the plugin developers to write the public methods to call their own data. It's a bit rude just to go rummaging around in another program's stuff. Besides, you don't know where it's been!
     
  5. Offline

    Raphfrk

    Right, though that assumes minimum collisions.

    What could make a bigger difference is the type of key.

    Hashcode generation occurs in 2 steps, first a 32 bit hashcode is generated and then that is converted into a smaller code (probably by getting the remainder when dividing by size of the hash table.

    The formula for a String is:

    s[n-1]*1 + s[n-2]*31 + s[n-3]*31^2 + .... + s[0]*31^(n-1)

    s[n] = the string characters

    This takes longer for longer strings.

    Integers don't need to be converted at all. The hashcode is just its value.

    You could also have a public/private option with the fields. Variables with the private field set wouldn't be accessible by other plugins.

    PS
    I think this would be a good idea. There is no point in all plugins doing their own data management.

    However, it would be good if there was a plugin field too.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 8, 2016
Thread Status:
Not open for further replies.

Share This Page