Solved Using Metadata

Discussion in 'Plugin Development' started by caelum19, Mar 13, 2013.

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

    caelum19

    Hello all, I'm making a grenade plugin and I need to use Metadata to set and read Metadata,
    but setMetadata()'s second arguement has to be a MetadataValue, how would I save a String into a metadataValue and then extract it at later point?

    example:
    Code:
    public void  ProjectileHitEvent(org.bukkit.event.entity.ProjectileHitEvent evt)
    {
       evt.getEntity().setMetadata("ExplosiveType", "Anti-grav"); //Problem here
      String type = String.valueOf(evt.getEntity().getMetadata("ExplosiveType")
        getLogger.info(type);
    }
    Should return : "Anti-grav", but it doesn't.
     
  2. Offline

    CoderCloud

    You have to create a own MetadatValue class.


    The class could look like this one that i used in my Plugin.
    In this class you would have to use the value() method to get the value because i were to lazy to set up all the other methods correctly.

    You can set a metadata with setMetadata("NAME", new MyMetadata(YOURPLUGIN, "VALUE"))

    And get the metadata with value() and cast it to a String
    Code:java
    1.  
    2. package me.leo.sorcery;
    3.  
    4. import org.bukkit.metadata.MetadataValue;
    5. import org.bukkit.plugin.Plugin;
    6.  
    7. public class MyMetadata implements MetadataValue{
    8.  
    9. Object value;
    10. Plugin p;
    11.  
    12. public MyMetadata(Plugin p, Object o) {
    13. this.p = p;
    14. this.value = o;
    15. }
    16.  
    17. @Override
    18. public boolean equals(Object obj) {
    19. return value.equals(obj);
    20. }
    21.  
    22. public void set(Object o) {
    23. this.value = o;
    24. }
    25.  
    26. @Override
    27. public boolean asBoolean() {
    28. }
    29.  
    30. @Override
    31. public byte asByte() {
    32. }
    33.  
    34. @Override
    35. public double asDouble() {
    36. }
    37.  
    38. @Override
    39. public float asFloat() {
    40. }
    41.  
    42. @Override
    43. public int asInt() {
    44. }
    45.  
    46. @Override
    47. public long asLong() {
    48. }
    49.  
    50. @Override
    51. public short asShort() {
    52. }
    53.  
    54. @Override
    55. public String asString() {
    56. }
    57.  
    58. @Override
    59. public Plugin getOwningPlugin() {
    60. return p;
    61. }
    62.  
    63. @Override
    64. public void invalidate() {
    65. return;
    66.  
    67. }
    68.  
    69. @Override
    70. public Object value() {
    71. return value;
    72. }
    73.  
    74. }
     
    yewtree8 likes this.
  3. caelum19
    Metadata is useful when you want to share that data with other plugins... if you don't, it's faster to just use a HashMap.
    You can use entity's ID (or entity's unique ID if you want to save the hashmap) as key and whatever value you want, then you just retrieve it.
    It would be more complicated to retrieve a metadata value, here's how it's done: http://wiki.bukkit.org/Plugin_Tutorial#Metadata
     
  4. Offline

    caelum19

    CoderCloud

    Tried that and it works, thank you very much.

    AlexLeporiday

    Oh right I understand now, thanks.


    Digi

    Ohh okay then, I'll use hashtags, but it's useful to know how to use Metadata anyway :p
     
Thread Status:
Not open for further replies.

Share This Page