Riding a mob

Discussion in 'Plugin Development' started by DoggyCode™, Jul 5, 2015.

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

    DoggyCode™

    Hi,

    I was just wondering, does anyone know how to find out if a player is riding a mob or not? I'm blank, and I don't seem to find it anywhere, but I know that it's possible.
     
  2. Entity#getVehicle()
     
  3. Offline

    DoggyCode™

    That didn't really help much..
     
  4. @DoggyCode™
    Player#getVehicle() returns the entity that the player is riding, or null if player is not riding an entity.
     
  5. Offline

    SuperSniper

    Im gonna give you a code example, okay?
    Let's just say that you're doing this in an event.

    Code:
    @EventHandler
    public void f(PlayerInteractEvent e) {
    Player p = e.getPlayer();
    if(p.getVehicle instanceof Horse) {
    p.sendMessage("You are riding a horse while interacting");
    
    got it?
     
  6. Offline

    DoggyCode™

    Yeh, thank you. But I'm not using this in an event. And I'd like you to give me an example code of this. I want to use it in a lore. So, for example, if the player is riding a mob, then I want the lore to be "true", if not, I want the lore to be "false". Here is my code so far.

    Code:
      public Inventory InspectPlayer(Player p){
      //Defining the Player interface above
      //Creating a Player Inventory and setting the name   
      Inventory inv = Bukkit.createInventory(p, 18,"§6Inspecting: §e" + p.getName());
       
      //Making and editing properties of custom item
      //You Can Copy paste this part as much items as you want and set each item to a different slot in the inventory
      ItemStack ridingMob = new ItemStack(Material.getMaterial(417));
      ItemMeta meta = ridingMob.getItemMeta();
      meta.setDisplayName("§6Riding Mob");
      meta.setLore(Arrays.asList("?"));
      ridingMob.setItemMeta(meta);
       
      //Setting the custom item in the inventory
      inv.setItem(0, Main.createItem(Material.getMaterial(382), (int)p.getHealthScale(), (short)0, "§6Health", "§e" + p.getHealthScale() + " §8/ §e20.0"));
      inv.setItem(1, Main.createItem(Material.COOKED_BEEF, (int)p.getFoodLevel(), (short)0, "§6Hunger", "§e" + p.getFoodLevel() + ".0 §8/ §e20.0"));
      inv.setItem(2, Main.createItem(Material.ROTTEN_FLESH, (int)p.getSaturation(), (short)0, "§6Saturation", "§e" + p.getSaturation()));
      inv.setItem(3, ridingMob);
      //Return the filled Inventory to the player that called the function
      return inv;
       }
     
  7. Offline

    SuperSniper

    .

    Code:
    public Inventory InspectPlayer(Player p){
    //Defining the Player interface above
    //Creating a Player Inventory and setting the name
      Inventory inv = Bukkit.createInventory(p, 18,"§6Inspecting: §e" + p.getName());
     
    //Making and editing properties of custom item
    //You Can Copy paste this part as much items as you want and set each item to a different slot in the inventory
      ItemStack ridingMob = new ItemStack(Material.getMaterial(417));
      ItemMeta meta = ridingMob.getItemMeta();
      meta.setDisplayName("§6Riding Mob");
      meta.setLore([URL='http://www.google.com/search?hl=en&q=allinurl%3Aarrays+java.sun.com&btnI=I%27m%20Feeling%20Lucky']Arrays[/URL].asList("?"));
      ridingMob.setItemMeta(meta);
     
    //Setting the custom item in the inventory
    if(p.getVehicle() instanceof null){
      inv.setItem(0, Main.createItem(Material.getMaterial(382), (int)p.getHealthScale(), (short)0, "§6Health", "§e" + p.getHealthScale() + " §8/ §e20.0"));
      inv.setItem(1, Main.createItem(Material.COOKED_BEEF, (int)p.getFoodLevel(), (short)0, "§6Hunger", "§e" + p.getFoodLevel() + ".0 §8/ §e20.0"));
      inv.setItem(2, Main.createItem(Material.ROTTEN_FLESH, (int)p.getSaturation(), (short)0, "§6Saturation", "§e" + p.getSaturation()));
      inv.setItem(3, if they are riding nothing);
    }else if(p.getVehicle() instanceof Horse || p.getVehicle() instanceof Pig) { // I dont know all the ride-able mobs so just add onto this line
      inv.setItem(0, Main.createItem(Material.getMaterial(382), (int)p.getHealthScale(), (short)0, "§6Health", "§e" + p.getHealthScale() + " §8/ §e20.0"));
      inv.setItem(1, Main.createItem(Material.COOKED_BEEF, (int)p.getFoodLevel(), (short)0, "§6Hunger", "§e" + p.getFoodLevel() + ".0 §8/ §e20.0"));
      inv.setItem(2, Main.createItem(Material.ROTTEN_FLESH, (int)p.getSaturation(), (short)0, "§6Saturation", "§e" + p.getSaturation()));
      inv.setItem(3, If they are riding something);
    
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 12, 2016
  8. @SuperSniper
    1. Don't spoonfeed if it's an incredibly easy thing to do
    2. Don't use instanceof null, not even sure if that's possible. Use == null
    @DoggyCode™
    getVehicle() is a method within the Entity class that returns the entity that the entity is riding.
     
  9. Offline

    RoflFrankoc

    @EventHandler
    public void as(PlayerInterractEntityEvent e){
    Player ep = (Player) e.getPlayer;
    Entity es = (Entity) e.getRightClicked;
    if (!(es instanceof Player)){
    if (!es.getPassenger().isEmpty()){
    ep.sendMessage(ChatColor.GREEN+"This entity has a passenger!")
    }
    }
    }
     
  10. Offline

    DoggyCode™

    @SuperSniper , thank you! It worked.
     
Thread Status:
Not open for further replies.

Share This Page