[TUT/LIB] What can you do with Metadata?

Discussion in 'Resources' started by TryB4, Jan 14, 2014.

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

    TryB4

    Hello, I'm TryB4.
    I didn't find one of these on bukkit, so I decided to make one myself.

    I am going to show you a few things you can do with metadata.


    Metadata is data stored on either an entity or a block.

    It stays stored over reloads.

    Let's do an example of how you can use metadata to lock a chest.

    (I will be using my https://gist.github.com/tryb4/8420899 methods for this)


    Code:java
    1. public static boolean isLocked(Chest c)
    2. {
    3. Block b = c.getBlock();
    4. if (b_getTag(b, "locked").equalsIgnoreCase("NO_DATA_RECIEVED"))
    5. {
    6. toggleLock(c);
    7. }
    8. return Boolean.valueOf(b_getTag(b, "locked"));
    9. }
    10.  
    11. public static void toggleLock(Chest c)
    12. {
    13. Block b = c.getBlock();
    14. if (b_getTag(b, "locked").equalsIgnoreCase("NO_DATA_RECIEVED"))
    15. {
    16. b_applyTag(b, "locked", "true", <host>);
    17. }
    18.  
    19. boolean locked = Boolean.valueOf(b_getTag(b, "locked"));
    20. if (locked) {
    21. locked = false;
    22. b_removeTag(b, "locked", <host>);
    23. b_applyTag(b, "locked", "false", <host>);
    24. }else{
    25. locked = true;
    26. b_removeTag(b, "locked", <host>);
    27. b_applyTag(b, "locked", "true", <host>);
    28. }
    29. }


    Let's do another example of how to make personal chests.


    Code:java
    1. public static OfflinePlayer getOwner(Chest c)
    2. {
    3. Block b = c.getBlock();
    4. if (b_getTag(b, "owner").equalsIgnoreCase("NO_DATA_RECIEVED"))
    5. {
    6. b_applyTag(b, "owner", "GLOBAL");
    7. }
    8.  
    9. String owner = "GLOBAL";
    10.  
    11. owner = b_getTag(b, "owner");
    12.  
    13. return Bukkit.getOfflinePlayer(owner);
    14. }
    15.  
    16. public static void setOwner(OfflinePlayer p, Chest c)
    17. {
    18. Block b = c.getBlock();
    19. if (!b_getTag(b, "owner").equalsIgnoreCase("NO_DATA_RECIEVED"))
    20. {
    21. b_applyTag(b, "owner", p.getName(), <host>);
    22. }
    23. else{
    24. b_removeTag(b, "owner", <host>);
    25. b_applyTag(b, "owner", p.getName(), <host>);
    26. }
    27. }
    28.  
    29. public static boolean isOwnerOf(OfflinePlayer p, Chest c)
    30. {
    31. return p.getName().equalsIgnoreCase(getOwner(c).getName()) || getOwner(c).getName().equalsIgnoreCase("GLOBAL");
    32. }
    33.  
    34.  
     
  2. Offline

    Techy4198

    sorry to burst your bubble, but, metadata stays over reloads but NOT restarts.
     
  3. Offline

    TryB4

    Techy4198
    Stays over restarts for me <_<


    EDIT: nvm, I thought I restarted but I actually reloaded.
     
  4. Offline

    Techy4198

    TryB4 as in /stop and then reboot? if so, most people report that it doesn't seem to stay.

    TryB4 You state in the main post that it's a bug. it is actually working exactly how it was designed to work. metadata is not SUPPOSED to be stored over restarts. Idea though. you could write some code which automatically saves and loads metadata to/from file in the onEnable and onDisable methods. bear in mind that you would need to clear all metadata after saving it to file, in onDisable, because it DOES stick around on reloads, and if you don't deal with it properly it may cause duplicate metadata or something weird.

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

    DarkBladee12

    Techy4198 Hmm it didn't always stay after reloads for me... Sometimes it was persistent and sometimes not, maybe that's fixed in the newer versions (I don't really think so because I never saw something related to Metadata in the changelog), but I don't know since it has been a while I've used Metadata in one of my plugins^^ TryB4 Why don't you just use the Metadatable type instead of having to make methods for each class that extends Metadatable? I've made an util class for Metadata a long time ago, maybe it's useful ;)
     
Thread Status:
Not open for further replies.

Share This Page