Getting the owner of a ocelot

Discussion in 'Plugin Development' started by edragy, Jul 14, 2012.

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

    edragy

    Here is the code I am using, I am having trouble with String playerName = ((OfflinePlayer)ocelot.getOwner()).getName(); how do I fix it?
    Code:java
    1. @EventHandler
    2. public void OnCatPunch(EntityDamageByEntityEvent event)
    3. {
    4. EntityType entType = event.getDamager().getType();
    5. if (entType == EntityType.PLAYER) {
    6. entType = event.getEntity().getType();
    7. if (entType == EntityType.OCELOT) {
    8. boolean isTamed = false;
    9. Ocelot ocelot = (Ocelot)event.getEntity();
    10. isTamed = ocelot.isTamed();
    11. if (isTamed)
    12. {
    13. Player player = (Player)event.getDamager();
    14.  
    15. boolean noDamage = player.hasPermission("work");
    16. if (noDamage) {
    17. event.setCancelled(true);
    18. }
    19.  
    20. String playerName = ((OfflinePlayer)ocelot.getOwner()).getName();
    21. player.sendMessage("That Cat Belongs to: " + playerName);
    22. }
    23. }
    24. }
    25. }
     
  2. Just do ocelot.getOwner().getName().

    If you want the OfflinePlayer object do: Bukkit.getOfflinePlayer(ocelot.getOwner().getName());
     
  3. Offline

    edragy

    ok but how do I make it so it only works when you hit them with a certain item, like a fish?
     
  4. If the event doesn't have one... use player.getItemInHand() but be sure to check it against null before you call .getType() on it.
     
  5. Offline

    edragy

    can I have some code to do that please?
     
  6. Offline

    EnvisionRed

    if (player.getItemInHand() == null){
    return;
    }else{
    Material type = player.getItemInHand().getType()
    }
     
  7. Offline

    edragy

    so where do I put that code?
     
  8. Offline

    EnvisionRed

    In your onCatPunch method
     
  9. Oh, you posted the "else if" after "public class" thing... you should really use the requests forum instead because you don't understand a bit of what code you have.

    Also, feeding code to you will get you more errors and more headaches... so either learn (read on basic Java) or request the built plugin (requests forum).
     
    ferrybig likes this.
  10. Offline

    edragy

    Please can someone please just edit the code and make it so it only happens when the cat is punched with a fish?
     
  11. Offline

    Jnorr44

    edragy
    I think this deserves a like XD
    Code:
          @EventHandler
          public void OnCatPunch(EntityDamageByEntityEvent event)
          {
            EntityType entType = event.getDamager().getType();
            if (entType == EntityType.PLAYER) {
              entType = event.getEntity().getType();
              if (entType == EntityType.OCELOT) {
                boolean isTamed = false;
                Ocelot ocelot = (Ocelot)event.getEntity();
                isTamed = ocelot.isTamed();
                if (isTamed)
                {
                  Player player = (Player)event.getDamager();
                  boolean noDamage = player.hasPermission("work");
                  if (noDamage) {
                    event.setCancelled(true);
                  }
                  if (player.getItemInHand() != null && player.getItemInHand().getType() == Material.<your material>){
                    String playerName = ((OfflinePlayer)ocelot.getOwner()).getName();
                    player.sendMessage("That Cat Belongs to: " + playerName);
                  }
                }
              }
            }
          }
     
    Kodfod likes this.
  12. I think is is faster if you do
    Code:java
    1.  
    2. Ocelot ocelot = (Ocelot)event.getEntity();
    3. boolean isTamed = ocelot.isTamed();
    instead of
    Code:java
    1. boolean isTamed = false;
    2. Ocelot ocelot = (Ocelot)event.getEntity();
    3. isTamed = ocelot.isTamed();
    and it is also saving you some lines(but no line savings at the example)
     
  13. Even faster if you don't store isTamed() because it's used only once.
     
  14. Offline

    edragy

    OK thanks but how do I make it so it makes them sit when my code is called, because while it cancels the damage event, it still makes them get up and run around if they are sitting
     
  15. ocelot.setSitting(boolean)...
    If you would've just looked at the method list you would've found it.
     
Thread Status:
Not open for further replies.

Share This Page