Multiple Horses bug help please

Discussion in 'Plugin Development' started by ijakgamez, Apr 15, 2014.

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

    ijakgamez

    I am getting so far with creating my own plugin and the plugin is to allow people to either choose with the saddle a white horse and when they dismount they get the item back or use the gold_record(music disk "13") which would give them a gray horse and when they dismount they get that back.

    The error is when they use the gold_record and then dismount off the horse it gives them the saddle instead which then they can use to get the white horse instead please help.


    The code is in a pastebin link:
    http://pastebin.com/UP3urqQW
     
  2. Offline

    Hsflaxz

    Use these event listeners ...Sorry about the formatting.
    Code:java
    1. @EventHandler
    2. public void onPlayerInteractEvent(PlayerInteractEvent e) {
    3. Player p = e.getPlayer();
    4.  
    5. //check for the item that was used.
    6. if (e.getAction().equals(Action.RIGHT_CLICK_AIR) || e.getAction().equals(Action.RIGHT_CLICK_BLOCK)) {
    7. if (p.getItemInHand() == new ItemStack(Material.SADDLE)) {
    8. if (p.hasPermission("horse.use")) {
    9. Horse h = (Horse) p.getWorld().spawnEntity(p.getLocation(), EntityType.HORSE);
    10. h.setOwner(p);
    11. h.getInventory().setSaddle(new ItemStack(Material.SADDLE, 1));
    12. h.setCustomName(ChatColor.BOLD + "" + ChatColor.GOLD + p.getName() + "'s Horse.");
    13. h.setCustomNameVisible(true);
    14.  
    15. p.getInventory().remove(Material.SADDLE);
    16. }
    17. }
    18. else if (p.getItemInHand() == new ItemStack(Material.GOLD_RECORD)) {
    19. if (p.hasPermission("grayhorse.use")) {
    20. Horse h = (Horse) p.getWorld().spawnEntity(p.getLocation(), EntityType.HORSE);
    21. h.setOwner(p);
    22. h.getInventory().setSaddle(new ItemStack(Material.SADDLE, 1));
    23. h.setCustomName(ChatColor.BOLD + "" + ChatColor.GOLD + p.getName() + "'s Horse.");
    24. h.setCustomNameVisible(true);
    25. h.setColor(Color.GRAY);
    26.  
    27. p.getInventory().remove(Material.GOLD_RECORD);
    28. }
    29. }
    30. }
    31. }
    32.  
    33. @EventHandler
    34. public void removemount(VehicleExitEvent e){
    35. Player p = (Player) e.getExited();
    36. if (e.getVehicle() instanceof Horse) {
    37. Horse h = (Horse) e.getVehicle();
    38. if (h.getColor() == Color.WHITE) {
    39. if (p.hasPermission("horse.use")) {
    40. h.remove();
    41. p.getInventory().addItem(new ItemStack(Material.SADDLE));
    42. }
    43. }
    44. else if (h.getColor() == Color.GRAY) {
    45. if (p.hasPermission("grayhorse.use")) {
    46. h.remove();
    47. p.getInventory().addItem(new ItemStack(Material.GOLD_RECORD));
    48. }
    49. }
    50. }
    51. }
     
  3. Offline

    zDylann

    Well first off, you do not have to have to separate VehicleExitEvent into two events like that, you can have both blocks of code in 1. Also when working with permissions you should understand that when you are op you have EVERY permission. So make sure you are not opped while testing. If permissions wasn't your issue, you do not have the @Eventhandler tag on your second VehicleExitEvent.
     
  4. Offline

    ijakgamez

    Even while using this code it still gives me the saddle instead of the gold_record when i dismount the gray horse so i don't know



    I have took that into mind but the code Hsflxz gave me isn't working as it still gives me the wrong item on dismount on the gray horse.
     
  5. Offline

    zDylann

    ijakgamez I have just copied the exact code that he gave you and tested it and it works perfectly fine. This 100% an issue on your end with permissions.

    Also a quick tip, add this after you have set all of the horse preferences:
    Code:java
    1. h.setPassenger(p);
    2.  

    It will automatically put you on the horse.(If that's what you would like of course.)
     
  6. Offline

    ijakgamez


    May I ask how you have your permissions setup so o can see where I am going wrong exactly
     
  7. Offline

    zDylann

    What permissions plugin do you use? I literally just created two groups with each permission node on them.
     
  8. Offline

    BillyGalbreath

    You need to store the information somewhere that tells what item they used to set the color. Using permissions is not the best idea because someone users may have multiple permissions. When multiple permissions happen the first one they have in the code will run no matter what and thats that.

    1) User clicks horse with item.
    2) Store info somewhere (global/static vars, yml, etc)
    3) Perform the actions
    4) User gets off horse
    5) Check stored data
    6) Act accordingly

    Ignore this. I misread the code due to lack of indentions.
     
  9. Offline

    Hsflaxz

    Shouldn't matter about permissions, the code is based off of the Color variable from the horse... If you have the permission for that Color, it'll drop the corresponding item.... By the way, ehen you register your permissions in plugin.yml, you can set default: op so you can take out the if (p.isOP()) checks
     
  10. Offline

    BillyGalbreath

    So sorry, misread the code due to lack of indentions.

    I placed the code into Eclipse and got an error about the == operand on the color checks. I replaced with .equals() and the error went away. Might want to give that a try..

    Code:java
    1.  
    2. if (h.getColor().equals(Color.WHITE)) {
    3.  
     
  11. Offline

    ijakgamez

    i am using group manager

    I tried only having two groups but that never worked I could not spawn them in at all when right clicking so I don't know what I have done

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page