Getting owner of pig in event

Discussion in 'Plugin Development' started by Plugers11, Jul 2, 2014.

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

    Plugers11

    Hi
    How to get owner in EntityDeathEvent and send message when pig die ?

    MY HM :

    public HashMap<String, Entity> bum = new HashMap<String, Entity>();
     
  2. Offline

    tommyhoogstra

    Code:java
    1. @EventHandler
    2. public void entDeath(EntityDeathEvent e){
    3. if(!(e.getEntity() instanceof LivingEntity)) return;
    4.  
    5. Tameable ent = (Tameable) e.getEntity();
    6. Player p = (Player) ent.getOwner();
    7. p.sendMessage("Your pet just died!");
    8. }
    9. }


    Untested, you would then check if instanceof pig Tamable ent
     
  3. Offline

    Plugers11

    tommyhoogstra
    But ...
    I want to get from hashmap no ,,owner" like wolf... owner like String in hm
     
  4. Are you storing the player's name in a hashmap?
     
  5. Offline

    Plugers11

    Yes [i know hashmaps, but i don't know how check player from entity]
     
  6. Offline

    tommyhoogstra

    Why do you need to do that, if its already built in with bukkit?
     
  7. Offline

    Plugers11

    tommyhoogstra
    Beacuse i want to create flying pigs ...
    And if my pig die then send message ?
     
  8. Offline

    tommyhoogstra

    Oh, I see now.
    No idea. lol.
     
  9. Offline

    Plugers11

    I think i solved

    HM : public HashMap<Entity, String> bum = new HashMap<Entity, String>();

    adding :
    bum.put(pig,p.getName());

    Getting player:

    if(bum.containsValue(pig)){
    Bukkit.getPlayer(bum.get(pig)).sendMessage("");
    }

    Ok solved ;D
    Thx for lib : http://forums.bukkit.org/threads/tut-creating-a-chest-lock-addon-for-a-minigame.217481/


    Method to get string from entity:

    Code:
    @SuppressWarnings("unchecked")
        public static <K, V> K getKeyAtValue(HashMap<String, Entity> bum2, Pig pig) {
            if(bum2 instanceof BiMap) {
            return ((BiMap<K, V>) bum2).inverse().get(pig);
            }
            for(Entry<String, Entity> entry : bum2.entrySet()) {
            if(entry.getValue().equals(pig)) {
            return (K) entry.getKey();
            }
            }
            return null;
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
Thread Status:
Not open for further replies.

Share This Page