How to store that player data?

Discussion in 'Plugin Development' started by bars96, Dec 9, 2013.

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

    bars96

    What is the best way to store player data like isPlayerPoisoned, isPlayerBleeding, lastDeathTime? This values plugin will get very often.
     
  2. public Map<String, Boolean> posisonedplayers = new HashMap<String, Boolean>();

    posisonedplayers.put(player.getName(), true);

    After that you can save it to the disk.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 5, 2016
    bars96 likes this.
  3. Offline

    samosaara

    Yes, to the saving/loading stuff use this class:
    Code:java
    1. package io.github.samosaara;
    2.  
    3. import java.io.FileInputStream;
    4. import java.io.FileOutputStream;
    5. import java.io.ObjectInputStream;
    6. import java.io.ObjectOutputStream;
    7.  
    8. /** SLAPI = Saving/Loading API
    9. * API for Saving and Loading Objects.
    10. * You can use this API in your projects, but please credit the original author of it.
    11. * @author Tomsik68<[email][email protected][/email]>
    12. */
    13. public class SLAPI
    14. {
    15. public static <T extends Object> void save(T obj,String path) throws Exception
    16. {
    17. oos.writeObject(obj);
    18. oos.flush();
    19. oos.close();
    20. }
    21. @SuppressWarnings("unchecked")
    22. public static <T extends Object> T load(String path) throws Exception
    23. {
    24. T result = (T)ois.readObject();
    25. ois.close();
    26. return result;
    27. }
    28. }

    Credits to the author.
     
    lycano and bars96 like this.
  4. Offline

    lycano

    bars96 depends on your code. If you intend to store more player flags like you listed then i recommend using Player Metadata.

    This data is per plugin instance and already has included such logic like "how to store/get/remove the data" you only need to use those methods provided by bukkit API.

    See http://jd.bukkit.org/rb/doxygen/d5/d74/interfaceorg_1_1bukkit_1_1entity_1_1Player.html and take a look at inherited methods from Metadatable - http://jd.bukkit.org/rb/doxygen/d5/d6f/interfaceorg_1_1bukkit_1_1metadata_1_1Metadatable.html

    Since the Metadatable class is kinda hard to use as and is glued to the player object i would write my own class to reflect such data for easy access and storage.

    (written from mind, might not work)
    Code:java
    1.  
    2. public class PlayerData {
    3. private String pluginName = "my_plugin_name";
    4. private String playerName;
    5.  
    6. public PlayerData(String playerName) {
    7. this.playerName = playerName;
    8. }
    9.  
    10. public void setIsNotBleeding() {
    11. this.setIsBleeding(false);
    12. }
    13.  
    14. public void setIsBleeding() {
    15. this.setIsBleeding(true);
    16. }
    17.  
    18. private void setIsBleeding(Boolean flag){
    19. this.setMetadataValue("isBleeding", flag);
    20. }
    21.  
    22. private MetadataValue getMetadataValue(String metadataKey)
    23. List<MetadataValue> metaData = this.getPlayer().getMetadata(metadataKey)
    24. if (!metaData.size() > 0) // not set
    25. return null;
    26.  
    27. // only use our own metdata, since other plugins could use the same name
    28. // since there is no getMetaDataByPlugin we need to filter it on our own
    29. // I don't like to hardcode the plugin name here but its still better as passing or storing the whole plugin instance in my opinion
    30. for (MetadataValue metaValue : metaData) {
    31. if (metaValue.getOwningPlugin().getName().equals(this.pluginName))
    32. return metaValue;
    33. }
    34.  
    35. // key was set by a foreign plugin (we don't want to use those values)
    36. return null;
    37. }
    38.  
    39. private void setMetadataValue(String metadataKey, Object value) {
    40. this.getPlayer().setMetaData(metadataKey, new FixedMetadataValue Bukkit.getPluginManager().getPlugin(this.pluginName()), value);
    41. }
    42.  
    43. private boolean hasMetaData(String metadataKey) {
    44. return (this.getMetadataValue(metadataKey) != null);
    45. }
    46.  
    47. private Player getPlayer() {
    48. return Bukkit.getPlayerExact(this.playerName);
    49. }
    50. }
    51.  


    I might hear you think "wow, this is a lot of work, why should i use this instead?" well its only hard work because there is no real API implemented yet as its not much used by plugin devs.

    You might also read https://docs.google.com/document/d/1OPU1ITKY1qO6PyEDvx6NpLTfNIAHQLaXdIqIT0dZqGw/

    Source: https://forums.bukkit.org/threads/metadata-q-a-i-wrote-the-metadata-api-ama.68447/

    Take a look at that google doc. Should help explain how the system works. Checkout the github example. It shows another way of using metadata in your plugin.

    Also note: Metadata is not persistent! You have to save them by yourself and load it on plugin enable and after a server reload.
     
    bars96 likes this.
Thread Status:
Not open for further replies.

Share This Page