horse plugin help

Discussion in 'Plugin Development' started by chunkaymonkay, Mar 29, 2014.

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

    chunkaymonkay

    hey guys today i was working on a plugin that when u click a saddle it spawns a horse but when i try to click on the saddle nothing happens :( here is my code i hope you can help
    Code:
    public void onClickSaddle(PlayerInteractEvent event)
    {
      if ((event.getItem() != null) && (event.getItem().getType().equals(Material.SADDLE)) &&
        (event.getAction() == Action.RIGHT_CLICK_BLOCK))
        if (event.getPlayer().hasPermission("ph.spawn")) {
          Location spawnLoc = event.getClickedBlock().getRelative(event.getBlockFace()).getLocation();
          Main horse = (Main)spawnLoc.getWorld().spawnEntity(spawnLoc, EntityType.HORSE);
          if (((Entity) horse).isValid()); }
    }
     
  2. Offline

    Wolfey

    Any errors in console?
     
  3. Offline

    chunkaymonkay

    Wolfey no no errors at all
     
  4. Offline

    Wolfey

    chunkaymonkay Well, you are casting the Entity as Main, maybe you should change that to Horse.
     
  5. Offline

    chunkaymonkay

    Wolfey ill try that

    Wolfey still didnt work

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    Wolfey

  7. Offline

    chunkaymonkay

    Code:
    @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
    public void onClickSaddle(PlayerInteractEvent event)
    {
      if ((event.getItem() != null) && (event.getItem().getType().equals(Material.SADDLE)) &&
        (event.getAction() == Action.RIGHT_CLICK_BLOCK))
        if (event.getPlayer().hasPermission("ph.spawn")) {
          Location spawnLoc = event.getClickedBlock().getRelative(event.getBlockFace()).getLocation();
          @SuppressWarnings("rawtypes")
        Horse horse = (Horse)spawnLoc.getWorld().spawnEntity(spawnLoc, EntityType.HORSE);
          if (((Entity) horse).isValid()); }
    }
    Wolfey there

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  8. Offline

    Wolfey

  9. Offline

    chunkaymonkay

    Wolfey yes

    can someone please help me here is all my code
    Code:
    @EventHandler(priority=EventPriority.MONITOR, ignoreCancelled=true)
    public void onClickSaddle(PlayerInteractEvent event)
    {
      if ((event.getItem() != null) && (event.getItem().getType().equals(Material.SADDLE)) &&
        (event.getAction() == Action.RIGHT_CLICK_BLOCK))
        if (event.getPlayer().hasPermission("ph.spawn")) {
          Location spawnLoc = event.getClickedBlock().getRelative(event.getBlockFace()).getLocation();
          @SuppressWarnings("rawtypes")
        Horse horse = (Horse)spawnLoc.getWorld().spawnEntity(spawnLoc, EntityType.HORSE);
          if (((Entity) horse).isValid()); }
    }
     
    @EventHandler
    public void onVehicleExit(VehicleExitEvent event) {
      {
        Player player = (Player)event.getExited();
     
        if (onFile(player)) {
          @SuppressWarnings("unchecked")
        Main h = (Main)event.getVehicle();
          ((Entity) h).remove();
          msgPlayer(player, "Temporary mount removed");
        }
      }
    }
    private boolean onFile(Player player) {
        // TODO Auto-generated method stub
        return false;
    }
     
    private void msgPlayer(Player player, String string) {
        // TODO Auto-generated method stub
       
    }
    }
     
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  10. Offline

    TheMintyMate

    chunkaymonkay
    Your IF statement may need to test for if they are right clicking
    air:
    Code:java
    1.  
    2. if ((event.getItem() != null) && (event.getItem().getType().equals(Material.SADDLE)) &&
    3. (event.getAction() == Action.RIGHT_CLICK_BLOCK) && (event.getAction() == Action.RIGHT_CLICK_AIR))
    4.  
     
  11. Offline

    chunkaymonkay

  12. Offline

    TheMintyMate

    chunkaymonkay
    hm. Nevertheless, keep that change, as a user might just right click the air, and nothing would happen....

    chunkaymonkay
    Would it be possible to see all of the class? The error may not be in the event handlers.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  13. Offline

    Afridge1O1

    chunkaymonkay, technically you are testing if the player right clicks air and right clicks block because you are using &&. I'm not a great coder but shouldn't that be || instead? Don't take my word for it I might be wrong.
     
  14. Offline

    AoH_Ruthless

    chunkaymonkay

    Fix the following:
    1. You don't need a null check if you are checking if the item type is a saddle .. just keep the saddle check.
    2. Make a separate if statement (nested) checking if the action is Right click air or block, as Afridge1O1 pointed out.
    3. Change your event priority to 'highest'. Unless you are making a logging plugin (or something of that kind), do not use Monitor, as it is bad practice.
    TheMintyMate Wolfey
    His code isn't actually doing anything that we need to fix.
    chunkaymonkay
    You are checking if the horse is valid, but not doing anything at all with it. First of all, you have a semi-colon at the end of your if statement and then you aren't doing anything with it.
    Code:java
    1. Horse horse = (Horse) spawnLoc.getWorld().spawnEntity(spawnLoc, EntityType.HORSE);
    2. if (horse.isValid()) { // You don't need to cast entity to it ... therefore remove your rawTypes suppresser.
    3. // CODE
    4. }
     
    Afridge1O1 likes this.
  15. Offline

    Wolfey

    AoH_Ruthless Well it should be spawning a horse. When you call the spawnEntity() method, an entity is suppose to be spawned.
     
Thread Status:
Not open for further replies.

Share This Page