Opening GUI Without Compass in hand

Discussion in 'Plugin Development' started by DaBoltLegend, Jan 27, 2014.

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

    DaBoltLegend

    My plugin is opening a GUI whenever I right click WITHOUT a compass that I set it to. Here's the code for that bit:

    I would like to make it so that people can't open the GUI when they right click anything, they need to click the compass in order to open the GUI. Please help.

    Code:java
    1.  
    2. @EventHandler
    3. public void openInventory(PlayerInteractEvent e) {
    4. if((e.getAction().equals(Action.RIGHT_CLICK_AIR)) &&
    5. (e.useItemInHand().equals(Material.COMPASS))) {
    6. e.getPlayer().openInventory(this.inv);
    7. }
    8.  
    9. I've also tried using e.getMaterial(). instead of e.useItemInHand(). but there was no difference.
    10.  
    11. EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
  2. Offline

    sebasju1234

    You have to use:
    Code:java
    1. event.getPlayer().getItemInHand().getType().equals(Material.COMPASS)

    Instead of:
    Code:java
    1. event.getPlayer().getItemInHand().equals(Material.COMPASS)
     
  3. Offline

    DaBoltLegend

    sebasju1234
    Still opens GUI whenever I right click anything.
     
  4. Offline

    AoH_Ruthless

    DaBoltLegend
    The event action operand requires '==' instead of .equals(), as does checking a material type.
    As sebasju1234 pointed out, you need to use:

    Code:java
    1. event.getPlayer().getItemInHand().getType() == Material.COMPASS
     
  5. Offline

    sebasju1234

    Hhm.. for some reason it always works what I've just posted haha. :p
     
  6. Offline

    AoH_Ruthless

    sebasju1234
    Does it? For me it works either way but from time to time using .equals() on material results in errors in the console log (At least, I believe that caused the issue). I will have to look into it, then.
     
  7. Offline

    DaBoltLegend

    AoH_Ruthless
    Still opens GUI whenever I right click anything.
    The whole code is this:
    Code:java
    1.  
    2. public class KitGui implements Listener {
    3.  
    4. private Inventory inv;
    5. private ItemStack pvp, arc, tan;
    6. ArrayList<String> kits = new ArrayList<String>();
    7.  
    8. public KitGui(Plugin p) {
    9. inv = Bukkit.getServer().createInventory(null, 9, "Choose your Kit!");
    10.  
    11. pvp = createItem(Material.DIAMOND_SWORD, ChatColor.BLUE + "PvP");
    12. arc = createItem(Material.BOW, ChatColor.BLUE + "Archer");
    13. tan = createItem(Material.DIAMOND_CHESTPLATE, ChatColor.BLUE + "Tank");
    14.  
    15. inv.setItem(2, pvp);
    16. inv.setItem(4, arc);
    17. inv.setItem(6, tan);
    18.  
    19. Bukkit.getServer().getPluginManager().registerEvents(this, p);
    20. }
    21.  
    22. @EventHandler
    23. public void playerDeath(PlayerDeathEvent e) {
    24. kits.remove(e.getEntity().getName());
    25. }
    26.  
    27. public void show(Player p) {
    28. p.openInventory(inv);
    29. }
    30.  
    31. @EventHandler
    32. public void onJoinEvent(PlayerJoinEvent e) {
    33. e.getPlayer().getInventory().addItem(new ItemStack (Material.COMPASS));
    34. }
    35.  
    36. @EventHandler
    37. public void onRespawnEvent(PlayerRespawnEvent e) {
    38. e.getPlayer().getInventory().addItem(new ItemStack (Material.COMPASS));
    39. }
    40.  
    41. @EventHandler
    42. public void openInventory(PlayerInteractEvent e) {
    43. if((e.getAction().equals(Action.RIGHT_CLICK_AIR)) &&
    44. (e.getPlayer().getItemInHand().getType() == Material.COMPASS)) {
    45. e.getPlayer().openInventory(this.inv);
    46. }
    47. }
    48.  
    49. private ItemStack createItem(Material mc, String name) {
    50. ItemStack i = new ItemStack(mc);
    51. ItemMeta im = i.getItemMeta();
    52. im.setDisplayName(name);
    53. im.setLore(Arrays.asList(ChatColor.DARK_RED + "Set your kit", ChatColor.DARK_RED + "to " + name));
    54. i.setItemMeta(im);
    55. return i;
    56. }
     
  8. Offline

    AoH_Ruthless

    DaBoltLegend
    Make sure you restarted your server and have no other plugins on your server (might be conflicting issues). Also, try recompiling the plugin. You also still have an incorrect operand usage when checking the event action.
     
  9. Offline

    DaBoltLegend

    AoH_Ruthless
    I just tried it without any plugins, still opens GUI with anything in hand.
    Where is the incorrect operand usage?
     
  10. Offline

    tardreted

    You could try naming the compass and using e.getItemInHand.getDisplayName.equalsIgnoreCase("Special Compass");
     
  11. Offline

    DoctorDark

    Do you have any other interact events that log right clicking?

    if (e.getAction() == Action.RIGHT_CLICK_BLOCK) instead of if (e.getAction().equals
     
  12. Offline

    DaBoltLegend

    DoctorDark
    No, that's the only interact event that logs right clicking.
     
  13. Offline

    DoctorDark

    DaBoltLegend

    I edited my post, did you apply that and see if it worked?
     
  14. Offline

    DaBoltLegend

    DoctorDark
    I applied that, and the GUI still opens with any item in hand.

    Anybody?

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

    calebbfmv

    if(event.getAction() == Acrtion.ACTION){
    if(event.getPlayer().getItemInHand().getType() == Material.COMPASS){
     
  16. Offline

    DaBoltLegend

    calebbfmv
    You mean Action.RIGHT_CLICK_BLOCK?

    YOU'RE ARE A LIFE-SAVER! THANK YOU!
     
  17. Offline

    calebbfmv

Thread Status:
Not open for further replies.

Share This Page