Solved Locking horses per player

Discussion in 'Plugin Development' started by yupie_123, May 24, 2014.

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


    So what I was trying to do is if a player is on a horse, get that horse and change the horse's metadata to the players' name. Then whenever someone tries to get on the horse, I check if the meta data is theirs, if it isn't theirs they get kicked off. But I am having trouble with setting metadata and getting the value.

    1. Horse horse = (Horse)p.getVehicle();
    3. horse.setMetadata("Locked", new FixedMetadataValue(plugin, p.getName()));

    This is my code for "Locking" a horse. I'm not very sure about how MetaData works so I might be doing something completely wrong.

    But when I try to get the meta data it returns a list of meta data. I'm not sure how I would get the value: p.getName() so I can check if the players' name equals the horses' meta data value.

    Any help would be appreciated :)
  2. yupie_123
    How about giving the horse a custom name instead of metadata? I've never actually messed with metadata either so I can't help you on that, but I've used custom names on entities before on a spawnandfollow plugin.


    Something like
    1. @EventHandler
    2. public void onEnter(VehicleEnterEvent event){
    4. if (event.getVehicle() instanceof Horse){
    5. if (event.getEntered() instanceof Player){
    7. Horse horse = (Horse)event.getVehicle();
    8. Player player = (Player)event.getEntered();
    9. horse.setCustomName(player.getName()+"'s horse");
    10. }
    11. }
    12. }

    Then you can setup a HashMap<String, String> and store player names with horse names (if you want the player to only have 1 horse) or setup a HashMap<String, List<String>> (if you want each player to have multiple horses). That's up to you really.
  3. Offline


    RAFA_GATO_FOFO Ok I'll try that, it's the .setCustomName method right? And to get it the getCustomName method.
  4. Offline


    Well I believe you have to cast it to a living entity... So if horse instanceof livingentity, liv.setDISPLAYname(string's horse);
    I'm up to such a plugin :p
  5. mine-care
    If it's a Horse, it's obviously already a living entity, so that's kinda useless.
  6. Offline


  7. fireblast709
    He can disable using nametags on Horses. But reading about Metadata will surely be helpful.
  8. Offline


  9. Offline


    Horse implements Tameable. It has "setOwner" and "getOwner" methods built in.
  10. Offline


    Garris0n Yes, thank you, this is much easier, but I'll definately look at MetData aswell, as I think I might need it in the future!
  11. Offline


    Note that MetaData is not persistent between server reloads/restarts.
  12. Offline


    Save it in a config.
Thread Status:
Not open for further replies.

Share This Page