Solved InventoryClickEvent...Advanced

Discussion in 'Plugin Development' started by ASHninja1997, Aug 18, 2013.

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

    ASHninja1997

    This is the inventory that is being created...It is basically an Item Menu.
    .
    Code:java
    1. Public Inventory createInventoryPERK(){
    2. pot = Bukkit.createInventory(null, 9, ChatColor.BLUE + "Perks"); // (owner, size (int), name)
    3.  
    4. //Add Items
    5.  
    6. pot.setItem(0, BACK);
    7. pot.setItem(1, REGAN);
    8. pot.setItem(2, SPEED);
    9. pot.setItem(3, STRENGTH);
    10. pot.setItem(4, INVISIBILTY);
    11. return pot;
    12. }
    13.  

    Basically I am trying to get the player that opens that inventory
    I need to get the player that opens the inventory because I need that to determine if the player bought the item or not.
    Here is how it tells them if they bought the item
    Code:java
    1. List<String> ls = new ArrayList<String>();
    2. ls.add(ChatColor.GRAY + "----------");
    3. if(((Permissible) p).hasPermission("Snipecraft.Regen")){
    4. ls.add(ChatColor.GREEN + "Purchased");
    5. }else {
    6. ls.add(ChatColor.GOLD + "Cost:");
    7. ls.add(ChatColor.GREEN + "50 Emeralds");}


    Here is how they see the item in the inventory that they opened up
    Code:java
    1. ItemStack REGAN = cname(new ItemStack(Material.PUMPKIN_PIE), ChatColor.LIGHT_PURPLE + "Gives you Meds", ls);
    2. ItemMeta REGANMeta = REGAN.getItemMeta();
    3. REGAN.setItemMeta(REGANMeta);

    This is what stores the player the item they selected
    This section of code is not part of the Inventory.
    It is instead a click listener. So when a player clicks on the item Pumkin_Pie this is executed.
    Code:java
    1.  
    2. @EventHandler
    3. public void click(InventoryClickEvent event){
    4. if (event.getInventory.getSize() == 9) {
    5. event.setCancelled(true);
    6. if(event.getCurrentItem.getType() == Material.PUMKIN_PIE){
    7. if (!p.hasPermission("Snipecraft.Speed")){((CommandSender) p).sendMessage(ChatColor.BLUE + "You have to buy this Perk!");return;}
    8. ((Player) p).playSound(p.getLocation(), Sound.FIRE_IGNITE, 1f, 1f);
    9. List<String> ls = new ArrayList<String>();
    10. ls.add(ChatColor.BLUE + "Click to activate");
    11. ItemStack pk = cname(new ItemStack(Material.DIAMOND_BOOTS), ChatColor.LIGHT_PURPLE + "Speed", ls);
    12. perk.put(p.getName(), pk);
    13. ((CommandSender) p).sendMessage(ChatColor.BLUE + "Perk:" + ChatColor.LIGHT_PURPLE + " Speed");}}}
    14. return;}}

    This gives them the item they selected
    Code:
    ItemStack retrieve5 = perk.get(po.getName());
    po.getInventory().addItem(retrieve5);
    I gave you this long detailed list of code so you know exactly what is going on code wise.
    Again the problem is that I am trying to get the player who is viewing the inventory......so it gets their
    stats. Stats are determined by permissions, as you see above in the ArrayList example. I will take any question you ask...Thanks for reading this very long help message.
     
  2. Offline

    xTrollxDudex

    ASHninja1997
    event.getView().getPlayer(), cast to Player because it returns a HumanEntity I believe
     
  3. Offline

    ASHninja1997

    So, I can set
    Code:java
    1. Player p = (Player) event.getView().getPlayer();


    xTrollxDudex
    This Is what I put so far...haven't tested it yet.
    Code:java
    1. public Inventory createInventoryPERK(){
    2. pot = Bukkit.createInventory(null, 9, ChatColor.BLUE + "Perks"); // (owner, size (int), name)
    3. InventoryClickEvent event2;
    4. InventoryClickEvent event = event2;
    5. Player p = (Player) event.getView().getPlayer();
    6. //rest of Inventory
    7. }
    8.  

    Good thing: No error in code :)

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

    xTrollxDudex

    ASHninja1997
    It should be inside the InventoryClickEvent handler
     
  5. Offline

    ASHninja1997

    xTrollxDudex
    well....That's my problem
    I want it to when a player's mouse hovers over the item...it tells them, through permissions, if they have Purchased the item or not. That why I need to get the player viewing the Inventory.
    If you don't understand what I am saying...Hypixel's server, QuakeCraft, will serve a fine example for that. In his Inventory GUI...It tells you if you have unlocked that item or not and what you need to do to get it...My Inventory GUI is very similar. Ask if you need more Description. I will be glad to give it.
     
  6. Offline

    xTrollxDudex

    ASHninja1997
    That would mean you need to edit the inventory before it is opened.
     
    ASHninja1997 likes this.
  7. Offline

    ASHninja1997

    xTrollxDudex
    Humm...I think I know what your saying. Let me try something out then get back to you.

    xTrollxDudex
    Ok I think I got it to almost work....There are no errors in the code but when I try to open it there is a NPE in the console.
    Ok so instead of opening a separate method I made a variable that equals a inventory...I basically did what you told me and I put the code that creates the inventory inside the InventoryClickEvent.
    Here is the code that I changed
    Code:java
    1. @EventHandler
    2. public void click(InventoryClickEvent event) {
    3. if (event.getInventory().getSize() == 9) {
    4. event.setCancelled(true);
    5. if (event.getCurrentItem().getType() == Material.BEACON){
    6. HumanEntity p = event.getWhoClicked();
    7. p.closeInventory();
    8. ((Player) p).playSound(p.getLocation(), Sound.BAT_TAKEOFF, 1f, 1f);
    9. p.openInventory(pot);
    10. pot = Bukkit.createInventory(null, 9, ChatColor.BLUE + "Perks"); // (owner, size (int), name)
    11. List<String> ls = new ArrayList<String>();
    12. ls.add(ChatColor.GRAY + "----------");
    13. if(p.hasPermission("Snipecraft.Regen")){
    14. ls.add(ChatColor.GREEN + "Purchased");
    15. }else {
    16. ls.add(ChatColor.GOLD + "Cost:");
    17. ls.add(ChatColor.GREEN + "50 Emeralds");}
    18.  
    19. List<String> ls2 = new ArrayList<String>();
    20. ls2.add(ChatColor.GRAY + "----------");
    21. if(p.hasPermission("Snipecraft.Speed")){
    22. ls2.add(ChatColor.GREEN + "Purchased");
    23. }else {
    24. ls2.add(ChatColor.GOLD + "Cost:");
    25. ls2.add(ChatColor.GREEN + "50 Emeralds");}
    26.  
    27. List<String> ls3 = new ArrayList<String>();
    28. ls3.add(ChatColor.GRAY + "----------");
    29. if(p.hasPermission("Snipecraft.Strength")){
    30. ls3.add(ChatColor.GREEN + "Purchased");
    31. }else {
    32. ls3.add(ChatColor.GOLD + "Cost:");
    33. ls3.add(ChatColor.GREEN + "50 Emeralds");}
    34.  
    35. List<String> ls4 = new ArrayList<String>();
    36. ls4.add(ChatColor.GRAY + "----------");
    37. if(p.hasPermission("Snipecraft.Invisibility")){
    38. ls4.add(ChatColor.GREEN + "Purchased");
    39. }else {
    40. ls4.add(ChatColor.GOLD + "Cost:");
    41. ls4.add(ChatColor.GREEN + "50 Emeralds");}
    42.  
    43. ItemStack BACK = new ItemStack(Material.ENDER_PEARL, 1);
    44. ItemMeta BACKMeta = BACK.getItemMeta();
    45. BACKMeta.setDisplayName(ChatColor.LIGHT_PURPLE + "Back");
    46. BACK.setItemMeta(BACKMeta);
    47.  
    48. ItemStack REGAN = cname(new ItemStack(Material.PUMPKIN_PIE), ChatColor.LIGHT_PURPLE + "Gives you Meds", ls);
    49. ItemMeta REGANMeta = REGAN.getItemMeta();
    50. REGAN.setItemMeta(REGANMeta);
    51.  
    52. ItemStack SPEED = cname(new ItemStack(Material.DIAMOND_BOOTS), ChatColor.LIGHT_PURPLE + "Increase your Speed", ls2);
    53. ItemMeta SPEEDMeta = SPEED.getItemMeta();
    54. SPEED.setItemMeta(SPEEDMeta);
    55.  
    56. ItemStack STRENGTH = cname(new ItemStack(Material.GOLD_CHESTPLATE), ChatColor.LIGHT_PURPLE + "Increase your Strength", ls3);
    57. ItemMeta STRENGTHMeta = STRENGTH.getItemMeta();
    58. STRENGTH.setItemMeta(STRENGTHMeta);
    59.  
    60. ItemStack INVISIBILTY = cname(new ItemStack(Material.GLASS), ChatColor.LIGHT_PURPLE + "Gives you Invisibility", ls4);
    61. ItemMeta INVISIBILTYMeta = INVISIBILTY.getItemMeta();
    62. INVISIBILTY.setItemMeta(INVISIBILTYMeta);
    63.  
    64. pot.setItem(0, BACK);
    65. pot.setItem(1, REGAN);
    66. pot.setItem(2, SPEED);
    67. pot.setItem(3, STRENGTH);
    68. pot.setItem(4, INVISIBILTY);
    69. return;
    70. }


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

    xTrollxDudex

  9. Offline

    ASHninja1997

  10. Offline

    Atakyn

    Whats on line 128 of snipecraft.java?
     
  11. Offline

    xTrollxDudex

    ASHninja1997
    Switch lines 9 and 10 around. The null for createInventory should be (Player) p
     
  12. Offline

    ASHninja1997

    xTrollxDudex
    What do you mean line 9 and 10..those are imports.

    Atakyn
    Code:java
    1. p.openInventory(pri);


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

    Atakyn

    I think he meant switch these:
    1. p.openInventory(pot);
    2. pot = Bukkit.createInventory(null, 9, ChatColor.BLUE + "Perks"); // (owner, size (int), name)

    p.openInventory(pot);
    pot = Bukkit.createInventory(null, 9, ChatColor.BLUE + "Perks"); // (owner, size (int), name)

    These two, you need to define pot before you can open it

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

    ASHninja1997

    xTrollxDudex Atakyn
    I think I solved it...let me test it out

    xTrollxDudex Atakyn
    IT WORKS!!!!!!!!!!!!!!!!! xTrollxDudex You are the best!!!!!
    Atakyn Thank you so much for the understanding!!!

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

Share This Page