onInventoryClick not working

Discussion in 'Plugin Development' started by Gecco1234, Apr 21, 2014.

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

    Gecco1234

    I am trying to make a perk that you can add when you click the feather that is called "Speed 2" it closes the inventory and adds something your yml. But it does not close the inventory nor does it but anything into any player's yml when I click the feather.
    Heres my code:
    Code:java
    1. if (event.getCurrentItem().getItemMeta().getDisplayName().contains("Speed 2")) {
    2. File playerFile = new File(this.getDataFolder() + File.separator + "Users" + File.separator + event.getWhoClicked() + ".yml");
    3. FileConfiguration fc = YamlConfiguration.loadConfiguration(playerFile);
    4. fc.set("GlassWars.Speed2", true);
    5. event.getWhoClicked().closeInventory();
    6. try {
    7. fc.save(playerFile);
    8. } catch (IOException ex) {
    9. ex.printStackTrace();
    10. }
    11.  
    12. }
    13. }
     
  2. Offline

    2MBKindiegames

    It works for me. Here are some things that might be wrong in your code:
    1) Did you place a @EventHandler annotation
    2) Does the class implement @EventHandler
    3) Are you sure this code is reached? (Place some bukkit.broadcastmessage("0")'s)
    4) Is there another event uncalleling it?
    5) Is the item actually names Speed 2
     
  3. Offline

    Gecco1234



    Found problem but don't know how to fix! If I click it in my inventory then it does it all right, but dosnt in that virtual inventory
     
  4. Offline

    2MBKindiegames

    Gecco1234 What do you mean, it has colors?
     
  5. Offline

    Gecco1234

    Look at edited post Ive found problem cause the things happen when I click in my actual invent but not in the virtual inventory
     
  6. Offline

    2MBKindiegames

    Gecco1234 What do you mean, virtual inventory?
     
  7. Offline

    Gecco1234

    A custom inventory made by bukkit that I coded
     
  8. Offline

    2MBKindiegames

    Gecco1234 I'm pretty sure, the CloseInventory should work, does it trow any errors?
     
  9. Offline

    Gecco1234

    It's the whole checkin of it that isn't working not just closeinventory
     
  10. Offline

    2MBKindiegames

    Gecco1234 Maybe try using, startsWith or endsWith instead?
     
  11. Offline

    Gecco1234

    It doesn't matter about that it's about it not working if the Speed 2 is in custom inventory but it works when its in the main inventory
     
  12. Offline

    stink123456

    It might not fix the problem but if you want to do this in a custom inventory, put an if(e.getInventory().equals(inv)){ on the top to save cpu
     
  13. Offline

    Gecco1234

    Ye Ive done that but it wouldnt fix it obviously :/
     
  14. Offline

    Gater12

    Gecco1234
    Then why did your inventory went through if you placed that check?
     
  15. Offline

    Gecco1234

    I removed that for testing purposes when trying to find out what was wrong and that happened in my inventory when I had removed it.
     
  16. Offline

    stink123456

    Gecco1234
    Did you put some testing messages if the onClick works at all in your custom inventory?
     
  17. Offline

    Gecco1234

    Yes
     
  18. Offline

    stink123456

    Well, did they work?
     
  19. Offline

    Gecco1234

    Only when I click it in my actual inventory if I dont check for which inventory
     
  20. Offline

    stink123456

    You could try inv.getItem(inv.getSlot());
     
  21. Offline

    Gecco1234

    How would I use this?
     
  22. Offline

    Zethariel

    Does getWhoClicked return a string? If it doesn't you might have an error with opening/closing/saving the file.
    Also, use debug messages to read the getCurrentItem in both cases.
     
  23. Offline

    Gecco1234

    Its HumanEntity type but that isnt the problem because debug broadcasts don't work aswell.
     
  24. Offline

    Zethariel

    Gecco1234
    File playerFile = new File(this.getDataFolder() + File.separator + "Users" + File.separator + event.getWhoClicked() + ".yml");

    It totally does. You are passing an HumanEntity where a string is expected. I'm surprised you don't get any errors.
     
  25. Offline

    Gecco1234

    I changed it to event.getWhoClicked().getName() but same thing still happens. And getName() is a string

    UPDATE: It saids there is an error on:
    Code:java
    1. if (event.getCurrentItem().getItemMeta().getDisplayName().contains("Speed 2")) {


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

    Seadragon91

    Do you have a check for item meta? Because if the item has no item meta, then an error will be thrown
     
  27. Offline

    Gecco1234

    ..
    Code:
    if (event.getCurrentItem().getItemMeta().getDisplayName().contains("Speed 2")) {
    Its not for if they click anything its for if they just click that
     
  28. Offline

    Seadragon91

    Add that before your line
    Code:
    if (player.getItemInHand().getItemMeta().hasDisplayName())
    { ... }
     
  29. Offline

    Gecco1234

    Not checking itemInHand but I'll try
    Seadragon91 It still only happens in my actual main inventory not the custom bukkit one that I coded
     
  30. Offline

    Gecco1234

Thread Status:
Not open for further replies.

Share This Page