Solved I can't change inventory called on InventoryClickEvent

Discussion in 'Plugin Development' started by PDKnight, Jul 12, 2015.

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

    PDKnight

    Hey, Bukkit, I need your help again :( I have this simple code:
    Code:java
    1. @EventHandler
    2. public void CGinvClick(InventoryClickEvent event) {
    3. Player p = (Player) event.getWhoClicked();
    4. if (event.getInventory().getType() == InventoryType.CHEST) {
    5. ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
    6. book.setAmount(1);
    7. book.setItemMeta(CGmodifyEmptyBook(book, "my chest").getItemMeta());
    8. event.getInventory().setItem(0,book);
    9. p.sendMessage("Bah!");
    10. }
    11. }

    The code works almost fine, plugin prints me "Bah!" message, but it doesn't change the inventory like I want to. Here's the video where I'm showing you, how bugged is it: http://rbg.wz.cz/mc.avi

    Is there anyone who knows how to fix this problem?
     
  2. Offline

    sander_blaadjes

    @PDKnight I think you need to call something like p.updateInventory();
     
    Jiheyr NV. likes this.
  3. Offline

    PDKnight

    @sander_blaadjes I know, but event.getInventory().update() or event.updateInventory() doesn't exist :( I tried your p.updateInventory(), but it didn't help me :(
     
  4. Offline

    sander_blaadjes

    @PDKnight What are you trying to do? Because I think you should be using something other than InventoryClickEvent.

    InventoryClickEvent Is good if you are trying to create some kind of GUI like in most minigames, where players can't take the item out of the inventory / chest.
     
  5. Offline

    PDKnight

    @sander_blaadjes I'm just trying to set item to the first slot in chest right when player grabs it (so then player has item in cursor and it's in the first slot, too), nothing else. I didn't show you the complete code, because I just want to solve this problem :)
     
  6. Offline

    _Error

    Seems strange, The way I would do it is.
    Code:
    ItemStack book = new ItemStack(Material.WRITTEN_BOOK, 1);
    book.setItemMeta(CGmodifyEmptyBook(book, "my chest").getItemMeta());
    event.getInventory().addItem(book);
    If you tried that and it does not work can you show us the CGmodifyEmptyBook class
    and p.getInventory.setItem(stuff goes here) You are getting the players inventory and setting it to an item? I think you're supposed to use .addItem(stuff goes here)
    This is my 100th msg
     
  7. Offline

    PDKnight

    @_Error
    CGmodifyEmptyBook is a function:
    Code:java
    1. private ItemStack CGmodifyEmptyBook(ItemStack book, String chest_name) {
    2. BookMeta bm = (BookMeta) book.getItemMeta();
    3. List<String> found = getConfig().getStringList("found." + chest_name);
    4. String book_log_str = "";
    5. for (Object o : found)
    6. book_log_str += (found.indexOf(o) + 1) + ". " + o + "\n";
    7. bm.setAuthor(ChatColor.AQUA + "CGeo_");
    8. bm.setTitle(ChatColor.AQUA + "GeoChest Log");
    9. bm.addPage(parseColors(getConfig().getString("log_who_found"))
    10. + "\u00a70\n\n" + book_log_str);
    11. book.setItemMeta(bm);
    12.  
    13. return book;
    14. }


    EDIT: Anyway, this function works great, it gives me a book, the problem isn't caused by it :)
     
    _Error likes this.
  8. Offline

    Doomfighter

    Maybe you have to at first get the Chests Inventory, then Change it and at last set the Chests Inventory to the changed one. I dont know if that works, but you can try
     
  9. Offline

    sander_blaadjes

    @PDKnight This is not perfect and can deffinitly be improved a lot, but this should help you.

    Code:java
    1.  
    2. @SuppressWarnings("deprecation")
    3. @EventHandler
    4. public void CGinvClick(InventoryClickEvent event) {
    5. final int slot = 0;
    6. Player p = (Player) event.getWhoClicked();
    7. p.sendMessage(event.getInventory().getName());
    8. if (event.getRawSlot() < event.getInventory().getSize() && event.getRawSlot() == slot) {
    9. event.setCancelled(true);
    10. ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
    11. book.setAmount(1);
    12. event.getInventory().clear();
    13. event.getInventory().setItem(slot,book);
    14. p.updateInventory();
    15. p.setItemOnCursor(book);
    16.  
    17.  
    18. }
    19. }
     
    Last edited: Jul 12, 2015
  10. Offline

    PDKnight

    @Doomfighter I tried it, but without any effect.

    @sander_blaadjes Okay, I'll try it when I arrive home, but I'll repair it first :)
    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void CGinvClick(InventoryClickEvent event) {
    4. Player p = (Player) event.getWhoClicked();
    5. InventoryType it = event.getInventory().getType();
    6. if (it == InventoryType.CHEST
    7. && event.getRawSlot() < event.getInventory().getSize()
    8. && event.getRawSlot() == 0) {
    9. event.setCancelled(true); // <-- why do you cancel it?
    10. ItemStack book = new ItemStack(Material.WRITTEN_BOOK,1);
    11. book.setItemMeta(CGmodifyEmptyBook(book, "my chest").getItemMeta());
    12. event.getInventory().setItem(0,book);
    13. p.updateInventory();
    14. p.setItemOnCursor(book);
    15. }
    16. }


    EDIT: @sander_blaadjes, MANY THANKS! You don't know, but you propably saved me several days!

    SOLVED
     
    Last edited: Jul 12, 2015
  11. Offline

    sander_blaadjes

    @PDKnight Sorry, I also had a version with comments:

    Same code (might be more clear to see) http://pastebin.com/BabF6Gha

    Code:java
    1. @SuppressWarnings("deprecation")
    2. @EventHandler
    3. public void CGinvClick(InventoryClickEvent event) {
    4. final int slot = 0; //The first slot of the chest
    5. Player p = (Player) event.getWhoClicked();
    6. p.sendMessage(event.getInventory().getName());
    7. if (event.getRawSlot() < event.getInventory().getSize() && //This makes sure the player clicked in the chest, and not the player's inventory.
    8. event.getRawSlot() == slot) // This makes sure it only gives the player the item in his hand if he clicks the item. (Could be replaced with event.getItem() == book)
    9. {
    10. event.setCancelled(true); //If you don't cancel it, it will do this odd thing you showed in your video, where it creates ghost books.
    11. ItemStack book = new ItemStack(Material.WRITTEN_BOOK);
    12. book.setAmount(1);
    13. event.getInventory().clear(); //Clearing it, in case the player placed the book in the chest.
    14. event.getInventory().setItem(slot,book); //Because we cancelled the event, we have to manually give the player the item on his cursor.
    15. p.updateInventory();
    16. p.setItemOnCursor(book); //Can't explain, remove it to see the difference :p
    17.  
    18.  
    19. }
    20. }
     
Thread Status:
Not open for further replies.

Share This Page