Make inventories not close until an item is clicked

Discussion in 'Plugin Development' started by jojodmo, Jun 22, 2014.

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

    jojodmo

    How could I make it so inventories will not be able to be closed by the player until and item in the inventory is clicked? (I don't want them pressing esc to get out of the inventory before they select a kit)? Here's what I'm doing right now:

    Code:java
    1. public static void openGUI(Player p){
    2. cantClose.add(p.getName());
    3.  
    4. Inventory inv = Bukkit.createInventory(null, 18, ChatColor.BOLD + "Choose a kit");
    5.  
    6. ItemStack scout = PodzolAPI.setColoredLore(PodzolAPI.newStack(Material.FEATHER, "Scout"), ChatColor.GRAY, PodzolAPI.stringToList("Scout does not take fall damage, and has", "full leather armor, a stone sword, and Speed"));
    7. ItemStack archer = PodzolAPI.setColoredLore(PodzolAPI.newStack(Material.BOW, "Archer"), ChatColor.GRAY, PodzolAPI.stringToList("Archer takes less damage from arrows, and has", "full leather armor, a wood sword, and an infinity bow"));
    8. ItemStack engineer = PodzolAPI.setColoredLore(PodzolAPI.newStack(Material.WEB, "Engineer"), ChatColor.GRAY, PodzolAPI.stringToList("Engineer takes less damage from fire, and has", "a gold helmet, an iron chestplate, and leather leggings & boots. ", "They have 32 wood planks, and 32 glass, and they also get", "a Stone Axe with knockback, and Faster mining"));
    9. ItemStack tank = PodzolAPI.setColoredLore(PodzolAPI.newStack(Material.IRON_CHESTPLATE, "Tank"), ChatColor.GRAY, PodzolAPI.stringToList("Tank takes less damage from explosions, and has", "full iron armor, and an iron sword with knockback.", "yet, they move slowly"));
    10.  
    11. inv.setItem(1, scout); inv.setItem(3, archer); inv.setItem(5, engineer); inv.setItem(7, tank);
    12.  
    13. p.openInventory(inv);
    14. }
    15.  
    16. @EventHandler
    17. public void inventoryClick(InventoryClickEvent e){
    18. if(e.getInventory().getName().equals(ChatColor.BOLD + "Choose a kit") && e.getCurrentItem() != null && e.getCurrentItem().hasItemMeta() && e.getWhoClicked() != null && e.getWhoClicked() instanceof Player){
    19. final String s = e.getCurrentItem().getItemMeta().getDisplayName();
    20. final User u = new User((Player) e.getWhoClicked());
    21.  
    22. cantClose.remove(u.getPlayer().getName());
    23.  
    24. e.setCancelled(true);
    25. Main.that.getServer().getScheduler().scheduleSyncDelayedTask(Main.that, new Runnable(){
    26. public void run(){
    27. u.getPlayer().closeInventory();
    28. u.giveKit(Kit.getKit(ChatColor.stripColor(s)));
    29. }
    30. },2L);
    31. }
    32. }
    33.  
    34. @EventHandler
    35. public void inventoryClose(InventoryCloseEvent e){
    36. if(e.getPlayer() instanceof Player && e.getInventory().getName().equals(ChatColor.BOLD + "Choose a kit")){
    37. final Player p = (Player) e.getPlayer();
    38. Main.that.getServer().getScheduler().scheduleSyncDelayedTask(Main.that, new Runnable(){
    39. public void run(){
    40. if(cantClose.contains(p.getName())){
    41. openGUI(p);
    42. }
    43. }
    44. },2L);
    45. }
    46. }


    And it's working fine, just once you select the item, it doesn't seem to be removing you from the array, because then inventory won't close. When a player presses the escape key, it doesn't close the inventory, but it doesn't close it when the player selects an item, either.

    any ideas?
     
  2. Use a Map and the PlayerMoveEvent, if the Player is in the map cancel the PlayerMoveEvent and open the Inventory again.
    If the player choose a kit, remove the player from the map:)
     
  3. Offline

    jojodmo


    That's kind of what I did but using a List... I'm not using PlayerMoveEvent though, I would rather have it re-open when they close it if that's possible... My problem is that the player isn't being removed from the list I made
     
  4. Offline

    mazentheamazin

    Kingzuck
    PlayerMoveEvent is a terrible way to get at this, as its called whenever a player moves their cursor or moves; all this will do is use memory where is not required.
     
  5. Which Event do you choose for the Problem ?
    Sorry for my bad english^^
     
  6. Offline

    mazentheamazin

    No worries, I don't think there is any certain event that should be used to check. Its the fact that the player is not being removed from the list.

    jojodmo
    Use debug statements to figure out who is in the list before and then after the player has been 'removed'
     
Thread Status:
Not open for further replies.

Share This Page