Custom Inventory Broken?

Discussion in 'Plugin Development' started by PolarCraft, Mar 17, 2014.

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

    PolarCraft

    Okay so people can drag out items and keep them. Also If the item someone took is in their inventory it will not be in the menu. Like you type /admin and the item is not their. Is their any way to block it?

    Code:java
    1. package net.jc.minecraft;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. //import org.bukkit.DyeColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.inventory.InventoryClickEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. //import org.bukkit.material.Wool;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public final class Main extends JavaPlugin implements Listener {
    20. public static Inventory main;
    21. public static Inventory pvpChange;
    22. public static Inventory timeChange;
    23. public static Inventory weatherChange;
    24. public ItemStack dayItem, nightItem;
    25. public ItemStack onItem, offItem;
    26. public ItemStack sunItem, rainItem;
    27. public static CommandSender day;
    28.  
    29.  
    30. public void onEnable() {
    31. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    32. }
    33.  
    34. public Main() {
    35. main = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    36. main.setItem(2, new ItemStack(Material.WATCH, 1));
    37. //3
    38. main.setItem(4, new ItemStack(Material.DIAMOND_SWORD));
    39. //5
    40. main.setItem(6, new ItemStack(Material.BLAZE_ROD, 1));
    41.  
    42. ItemMeta time = main.getItem(2).getItemMeta();
    43. //3
    44. ItemMeta pvp = main.getItem(4).getItemMeta();
    45. //5
    46. ItemMeta weather = main.getItem(6).getItemMeta();
    47.  
    48. pvp.setDisplayName(ChatColor.DARK_RED + "Change Pvp");
    49. time.setDisplayName(ChatColor.DARK_RED + "Change Time");
    50. weather.setDisplayName(ChatColor.DARK_RED + "Change Weather");
    51. main.getItem(2).setItemMeta(time);
    52. main.getItem(4).setItemMeta(pvp);
    53. main.getItem(6).setItemMeta(weather);
    54. }
    55. /**timeChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    56.   dayItem = createItem(DyeColor.LIME, ChatColor.DARK_RED + "Day");
    57.   nightItem = createItem(DyeColor.RED, ChatColor.DARK_RED + "Night");
    58.   timeChange.setItem(2, dayItem);
    59.   timeChange.setItem(6, nightItem);
    60.  
    61.   weatherChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    62.   sunItem = createItem(DyeColor.ORANGE, ChatColor.DARK_RED + "Sun");
    63.   rainItem = createItem(DyeColor.PURPLE, ChatColor.DARK_RED + "Rain");
    64.   weatherChange.setItem(2, sunItem);
    65.   weatherChange.setItem(6, rainItem);
    66.  
    67.   pvpChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    68.   onItem = createItem(DyeColor.LIME, ChatColor.GREEN + "On");
    69.   offItem = createItem(DyeColor.RED, ChatColor.DARK_RED + "Off");
    70.   pvpChange.setItem(2, onItem);
    71.   pvpChange.setItem(6, offItem);
    72.  
    73.   }
    74.  
    75.   private ItemStack createItem(DyeColor dc, String name) {
    76.   ItemStack i = new Wool(dc).toItemStack(1);
    77.   ItemMeta im = i.getItemMeta();
    78.   im.setDisplayName(name);
    79.   i.setItemMeta(im);
    80.   return i;
    81.   } **/
    82.  
    83. @EventHandler
    84. public void onInventoryClick(InventoryClickEvent e) {
    85. Player p = (Player) e.getWhoClicked();
    86. if(!e.getInventory().getName().equalsIgnoreCase(main.getName())) return;
    87. if(e.getCurrentItem().getItemMeta() == null) return;
    88. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Pvp")) {
    89. e.setCancelled(true);
    90. p.chat("/pvpset");
    91. e.getWhoClicked().closeInventory();
    92. } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Time")) {
    93. e.setCancelled(true);
    94. p.chat("/timeset");
    95. e.getWhoClicked().closeInventory();
    96. } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Weather")) {
    97. e.setCancelled(true);
    98. p.chat("/weatherset");
    99. e.getWhoClicked().closeInventory();
    100. }
    101. }
    102.  
    103. /**@EventHandler
    104.   public void onPlayerJoin(PlayerJoinEvent e) {
    105.   Player p = e.getPlayer();
    106.   if(p.hasPermission("at")) {
    107.   ItemStack r = new ItemStack (Material.RED_ROSE);
    108.   p.getInventory().addItem(r);
    109.   }
    110.   }**/
    111.  
    112. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    113. if(cmd.getName().equalsIgnoreCase("admin")) {
    114. Player p = (Player) sender;
    115. p.openInventory(main);
    116. }
    117. return true;
    118. }
    119. }
     
  2. Offline

    97WaterPolo

    PolarCraft
    On player click event, set the inventory set the close, then run the code.
     
  3. Offline

    PolarCraft

  4. Offline

    97WaterPolo

    PolarCraft
    http://jd.bukkit.org/dev/doxygen/d9...inventory_1_1InventoryClickEvent.html#details

    It can not be called at the same time, so you would want to use this. This is how I would do this, so modify it to your hearts content! I haven't tested it so would be awesome if you told me whether this would work or not.

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent e) {
    3. final Player p = (Player) e.getWhoClicked();
    4. if(!e.getInventory().getName().equalsIgnoreCase(main.getName())) return;
    5. if(e.getCurrentItem().getItemMeta() == null) return;
    6. if (e.getInventory().equals(main.getName())) //Want to add this to make sure these ONLY happen if its in the inventory you created. Could create some NPE if you modify inventories in different ways for future plugins.
    7. {
    8. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Pvp")) {
    9. // Bukkit.dispatchCommand(player, "pvpset"); This is an alternative to the p.chat. This will work just as fine, first variable is who its sent from, second string is command. Your choice on what to use.
    10. p.chat("/pvpset");
    11. } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Time")) {
    12. p.chat("/timeset");
    13. } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Weather")) {
    14. p.chat("/weatherset");
    15. }
    16. //A delayed task so it runs this a second after it clicks. Make sure its INSIDE your new inventory, so it only applies to it.
    17. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(MAIN_CLASS, new Runnable()
    18. {
    19. @Override
    20. public void run()
    21. {
    22. p.closeInventory();
    23. }
    24. }, 2L); //2 tick delay, you can do 1 tick, but IMO if the sevrer lags or you have to much running it might skip a tick.
    25. }
    26. }
     
  5. Offline

    PolarCraft

  6. Offline

    97WaterPolo

    PolarCraft
    Would be a reference to the class that extends your JavaPlugin. If the event is in the class that extends java plugin, then put "this" there, if not use

    Code:
        public [MAIN CLASS] plugin;//The variable to refer to the Main
        public [CURRENT CLASS]([MAIN CLASS] plugin){//The constructor
            this.plugin = plugin;
        }    
    Then put "plugin" in the scheduler.
     
  7. Offline

    Wolfey

    PolarCraft
    Code:java
    1. Bukkit.getPluginManager().getPlugin("pluginName")
     
  8. Offline

    PolarCraft

    97waterpolo Now the plugin wont load :(

    Code:java
    1. package net.jc.minecraft;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. //import org.bukkit.DyeColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.inventory.InventoryClickEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. //import org.bukkit.material.Wool;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public final class Main extends JavaPlugin implements Listener {
    20.  
    21. public Main plugin;//The variable to refer to the Main
    22. public Main(Main plugin){//The constructor
    23. this.plugin = plugin;
    24. }
    25.  
    26. public static Inventory main;
    27.  
    28. //public static Inventory pvpChange;
    29. //public static Inventory timeChange;
    30. //public static Inventory weatherChange;
    31. //public ItemStack dayItem, nightItem;
    32. //public ItemStack onItem, offItem;
    33. //public ItemStack sunItem, rainItem;
    34. //public static CommandSender day;
    35.  
    36.  
    37. public void onEnable() {
    38. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    39. }
    40.  
    41. public Main() {
    42. main = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    43. main.setItem(2, new ItemStack(Material.WATCH, 1));
    44. //3
    45. main.setItem(4, new ItemStack(Material.DIAMOND_SWORD));
    46. //5
    47. main.setItem(6, new ItemStack(Material.BLAZE_ROD, 1));
    48.  
    49. ItemMeta time = main.getItem(2).getItemMeta();
    50. //3
    51. ItemMeta pvp = main.getItem(4).getItemMeta();
    52. //5
    53. ItemMeta weather = main.getItem(6).getItemMeta();
    54.  
    55. pvp.setDisplayName(ChatColor.DARK_RED + "Change Pvp");
    56. time.setDisplayName(ChatColor.DARK_RED + "Change Time");
    57. weather.setDisplayName(ChatColor.DARK_RED + "Change Weather");
    58. main.getItem(2).setItemMeta(time);
    59. main.getItem(4).setItemMeta(pvp);
    60. main.getItem(6).setItemMeta(weather);
    61. }
    62. /**timeChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    63.   dayItem = createItem(DyeColor.LIME, ChatColor.DARK_RED + "Day");
    64.   nightItem = createItem(DyeColor.RED, ChatColor.DARK_RED + "Night");
    65.   timeChange.setItem(2, dayItem);
    66.   timeChange.setItem(6, nightItem);
    67.  
    68.   weatherChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    69.   sunItem = createItem(DyeColor.ORANGE, ChatColor.DARK_RED + "Sun");
    70.   rainItem = createItem(DyeColor.PURPLE, ChatColor.DARK_RED + "Rain");
    71.   weatherChange.setItem(2, sunItem);
    72.   weatherChange.setItem(6, rainItem);
    73.  
    74.   pvpChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    75.   onItem = createItem(DyeColor.LIME, ChatColor.GREEN + "On");
    76.   offItem = createItem(DyeColor.RED, ChatColor.DARK_RED + "Off");
    77.   pvpChange.setItem(2, onItem);
    78.   pvpChange.setItem(6, offItem);
    79.  
    80.   }
    81.  
    82.   private ItemStack createItem(DyeColor dc, String name) {
    83.   ItemStack i = new Wool(dc).toItemStack(1);
    84.   ItemMeta im = i.getItemMeta();
    85.   im.setDisplayName(name);
    86.   i.setItemMeta(im);
    87.   return i;
    88.   } **/
    89.  
    90. @EventHandler
    91. public void onInventoryClick(InventoryClickEvent e) {
    92. final Player p = (Player) e.getWhoClicked();
    93. if(!e.getInventory().getName().equalsIgnoreCase(main.getName())) return;
    94. if(e.getCurrentItem().getItemMeta() == null) return;
    95. if (e.getInventory().equals(main.getName())) //Want to add this to make sure these ONLY happen if its in the inventory you created. Could create some NPE if you modify inventories in different ways for future plugins.
    96. {
    97. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Pvp")) {
    98. // Bukkit.dispatchCommand(player, "pvpset"); This is an alternative to the p.chat. This will work just as fine, first variable is who its sent from, second string is command. Your choice on what to use.
    99. p.chat("time set night");
    100. } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Time")) {
    101. p.chat("/time set day");
    102. } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Weather")) {
    103. p.chat("/time set night");
    104. }
    105. //A delayed task so it runs this a second after it clicks. Make sure its INSIDE your new inventory, so it only applies to it.
    106. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new Runnable()
    107. {
    108. @Override
    109. public void run()
    110. {
    111. p.closeInventory();
    112. }
    113. }, 2L); //2 tick delay, you can do 1 tick, but IMO if the sevrer lags or you have to much running it might skip a tick.
    114. }
    115. }
    116.  
    117. /**@EventHandler
    118.   public void onPlayerJoin(PlayerJoinEvent e) {
    119.   Player p = e.getPlayer();
    120.   if(p.hasPermission("at")) {
    121.   ItemStack r = new ItemStack (Material.RED_ROSE);
    122.   p.getInventory().addItem(r);
    123.   }
    124.   }**/
    125.  
    126. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    127. if(cmd.getName().equalsIgnoreCase("admin")) {
    128. Player p = (Player) sender;
    129. p.openInventory(main);
    130. }
    131. return true;
    132. }
    133. }


    Code:java
    1. name: AdminGui
    2. version: 1.0
    3. main: net.jc.minecraft.Main
    4. commands:
    5. admin:
    6. description: Opens controls.
     
  9. Offline

    97WaterPolo

    PolarCraft
    Small problem, nothing big! :p I said a message above that if this listener is IN the class that extends your JavaPlugin, then just put "this" for the scheduler. Checks out in my IDE, haven't compiled it yet to test it. Hopefully it works!

    Code:java
    1. package net.jc.minecraft;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. //import org.bukkit.DyeColor;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.entity.Player;
    10. import org.bukkit.event.EventHandler;
    11. import org.bukkit.event.Listener;
    12. import org.bukkit.event.inventory.InventoryClickEvent;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.meta.ItemMeta;
    16. //import org.bukkit.material.Wool;
    17. import org.bukkit.plugin.java.JavaPlugin;
    18.  
    19. public final class Main extends JavaPlugin implements Listener {
    20.  
    21. public static Inventory main;
    22.  
    23. //public static Inventory pvpChange;
    24. //public static Inventory timeChange;
    25. //public static Inventory weatherChange;
    26. //public ItemStack dayItem, nightItem;
    27. //public ItemStack onItem, offItem;
    28. //public ItemStack sunItem, rainItem;
    29. //public static CommandSender day;
    30.  
    31.  
    32. public void onEnable() {
    33. Bukkit.getServer().getPluginManager().registerEvents(this, this);
    34. }
    35.  
    36. public Main() {
    37. main = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    38. main.setItem(2, new ItemStack(Material.WATCH, 1));
    39. //3
    40. main.setItem(4, new ItemStack(Material.DIAMOND_SWORD));
    41. //5
    42. main.setItem(6, new ItemStack(Material.BLAZE_ROD, 1));
    43.  
    44. ItemMeta time = main.getItem(2).getItemMeta();
    45. //3
    46. ItemMeta pvp = main.getItem(4).getItemMeta();
    47. //5
    48. ItemMeta weather = main.getItem(6).getItemMeta();
    49.  
    50. pvp.setDisplayName(ChatColor.DARK_RED + "Change Pvp");
    51. time.setDisplayName(ChatColor.DARK_RED + "Change Time");
    52. weather.setDisplayName(ChatColor.DARK_RED + "Change Weather");
    53. main.getItem(2).setItemMeta(time);
    54. main.getItem(4).setItemMeta(pvp);
    55. main.getItem(6).setItemMeta(weather);
    56. }
    57. /**timeChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    58.   dayItem = createItem(DyeColor.LIME, ChatColor.DARK_RED + "Day");
    59.   nightItem = createItem(DyeColor.RED, ChatColor.DARK_RED + "Night");
    60.   timeChange.setItem(2, dayItem);
    61.   timeChange.setItem(6, nightItem);
    62.  
    63.   weatherChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    64.   sunItem = createItem(DyeColor.ORANGE, ChatColor.DARK_RED + "Sun");
    65.   rainItem = createItem(DyeColor.PURPLE, ChatColor.DARK_RED + "Rain");
    66.   weatherChange.setItem(2, sunItem);
    67.   weatherChange.setItem(6, rainItem);
    68.  
    69.   pvpChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    70.   onItem = createItem(DyeColor.LIME, ChatColor.GREEN + "On");
    71.   offItem = createItem(DyeColor.RED, ChatColor.DARK_RED + "Off");
    72.   pvpChange.setItem(2, onItem);
    73.   pvpChange.setItem(6, offItem);
    74.  
    75.   }
    76.  
    77.   private ItemStack createItem(DyeColor dc, String name) {
    78.   ItemStack i = new Wool(dc).toItemStack(1);
    79.   ItemMeta im = i.getItemMeta();
    80.   im.setDisplayName(name);
    81.   i.setItemMeta(im);
    82.   return i;
    83.   } **/
    84.  
    85. @EventHandler
    86. public void onInventoryClick(InventoryClickEvent e) {
    87. final Player p = (Player) e.getWhoClicked();
    88. if(!e.getInventory().getName().equalsIgnoreCase(main.getName())) return;
    89. if(e.getCurrentItem().getItemMeta() == null) return;
    90. if (e.getInventory().equals(main.getName())) //Want to add this to make sure these ONLY happen if its in the inventory you created. Could create some NPE if you modify inventories in different ways for future plugins.
    91. {
    92. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Pvp")) {
    93. // Bukkit.dispatchCommand(player, "pvpset"); This is an alternative to the p.chat. This will work just as fine, first variable is who its sent from, second string is command. Your choice on what to use.
    94. p.chat("time set night");
    95. } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Time")) {
    96. p.chat("/time set day");
    97. } else if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Weather")) {
    98. p.chat("/time set night");
    99. }
    100. //A delayed task so it runs this a second after it clicks. Make sure its INSIDE your new inventory, so it only applies to it.
    101. Bukkit.getServer().getScheduler().scheduleSyncDelayedTask(this, new Runnable()
    102. {
    103. @Override
    104. public void run()
    105. {
    106. p.closeInventory();
    107. }
    108. }, 2L); //2 tick delay, you can do 1 tick, but IMO if the sevrer lags or you have to much running it might skip a tick.
    109. }
    110. }
    111.  
    112. /**@EventHandler
    113.   public void onPlayerJoin(PlayerJoinEvent e) {
    114.   Player p = e.getPlayer();
    115.   if(p.hasPermission("at")) {
    116.   ItemStack r = new ItemStack (Material.RED_ROSE);
    117.   p.getInventory().addItem(r);
    118.   }
    119.   }**/
    120.  
    121. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    122. if(cmd.getName().equalsIgnoreCase("admin")) {
    123. Player p = (Player) sender;
    124. p.openInventory(main);
    125. }
    126. return true;
    127. }
    128. }
     
  10. Offline

    PolarCraft

    97waterpolo It just says this.

    [07:04:23] [Server thread/ERROR]: Could not load 'plugins/test.jar' in folder 'plugins'
    org.bukkit.plugin.InvalidPluginException: java.lang.ClassNotFoundException: net.jc.minecraft.Main
     
  11. Offline

    97WaterPolo

    PolarCraft
    Means the server can not find net.jc.minecraft.Main? Is it in the right package? This has happened to me numerous times, usually its just a simple misspelling. I would say double/triple check, need to make sure packages match up exactly in the IDE and plugin.yml.
     
  12. Offline

    PolarCraft

    97waterpolo It works now but the only problem is that i can still drag the items out of the inventory.
     
  13. Offline

    97WaterPolo

    PolarCraft
    Really? It isn't closing it on the click? Is it closing it at all?
     
  14. Offline

    PolarCraft

  15. Offline

    97WaterPolo

    PolarCraft
    I am assuming you are using a resource that someone had for creating inventories? I recall that I had this problem once, I ended up finding another one that works. I can step back and hopefully someone else has an answer, or you could try my code, same idea as yours.

    Code:java
    1. public static Inventory myInventory = Bukkit.createInventory(null, 18, "" + ChatColor.RED + ChatColor.BOLD +"Server Tools!");
    2. // The first parameter, is the inventory owner. I make it null to let everyone use it.
    3. //The second parameter, is the slots in a inventory. Must be a multiple of 9. Can be up to 54.
    4. //The third parameter, is the inventory name. This will accept chat colors
    5.  
    6. static
    7. {
    8. ItemStack watch = new ItemStack(Material.WATCH, 1);
    9. ItemMeta meta = watch.getItemMeta();
    10. ArrayList<String> watchlore = new ArrayList<String>();
    11. watchlore.add(white + "" + italic + "Change time of current world to day!");
    12. meta.setDisplayName("" + ChatColor.YELLOW + ChatColor.BOLD + "World Time");
    13. meta.setLore(watchlore);
    14. watch.setItemMeta(meta);
    15.  
    16.  
    17. //The intger is the slot, ItemStack is the one you created above.
    18. myInventory.setItem(0, watch);
    19.  
    20. @EventHandler
    21. public void onInventoryClick(InventoryClickEvent event)
    22. {
    23. final Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    24. ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    25. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    26. if (inventory.getName().equals(this.myInventory.getName())
    27. {
    28. if (clicked.getType() == Material.WATCH)
    29. {
    30. player.sendMessage("You clicked the watch");
    31. }
    32. player.closeInventory();
    33. player.updateInventory();
    34. }
    35. }


    If you decide to use my code and you need an easier way to make the ItemStack besides all of that, let me know. I have a method to create an item with lore, title, etc in one line.
     
  16. Offline

    Konkz

    May be because the event is not cancelled. 97waterpolo PolarCraft

    Code:java
    1. @EventHandler
    2. public void onInventoryClick(InventoryClickEvent event)
    3. {
    4. final Player player = (Player) event.getWhoClicked(); // The player that clicked the item
    5. ItemStack clicked = event.getCurrentItem(); // The item that was clicked
    6. Inventory inventory = event.getInventory(); // The inventory that was clicked in
    7. if (inventory.getName().equals(this.myInventory.getName())
    8. {
    9. if (clicked.getType() == Material.WATCH)
    10. {
    11. player.sendMessage("You clicked the watch");
    12. }
    13. event.setCancelled(true);
    14. player.closeInventory();
    15. player.updateInventory();
    16. }
    17. }
     
    PolarCraft likes this.
  17. Offline

    97WaterPolo

    Konkz
    *facepalm* thank you. Would make sense for his inventory. For some reason I don't need it on mine, it works fine without it, never bothered to figure out why.

    Edit: that's my code, for me it works fine without the event.setCancelled?
     
  18. Offline

    Konkz


    It may be because of the function that you're doing when clicked. If you teleport to a world or so then it would close your inventory. I'm not too sure, but I always cancel the event in case some lucky lad destroys my plugin by bugging it out.
     
  19. Offline

    97WaterPolo

    Konkz
    Bugging out... Oh my god I believe you just solved a problem that's been plaguing me for some time. For mine the inventory closes, but occasionally it starts to glitch put players inventory, etc.... Thanks for the clarification!
     
  20. Offline

    Konkz

    No problem.
     
    97waterpolo likes this.
  21. Offline

    PolarCraft

    Konkz You solved my issue. But with your code how do I add more lines? Like else if statements?
     
  22. Offline

    97WaterPolo

    PolarCraft
    Are you talking about mine? If you are are you add another else if in the event. If you want to add another item, you use

    m yInventory.setItem((slot) (item stack));

    Slot bring the slot in the inventory. Starts at 0. Item stack is the one you make with lore and such.
     
  23. Offline

    PolarCraft

    97waterpolo No I am using my code still but i used Konkz fix for the click event. But how could i add the else if statement without causing an issue?
     
  24. Offline

    Konkz


    What are you trying to achieve?

    EDIT: Do you mean adding more checks? I recommend using this, I took yours and edited it a bit. I got rid of some checks as I did not see the point in them, but if you see them necessary re-add them.

    Code:java
    1. @EventHandler
    2. public void onClick(InventoryClickEvent e) {
    3.  
    4. Player p = (Player) e.getWhoClicked();
    5. String displayName = e.getCurrentItem().getItemMeta().getDisplayName();
    6.  
    7. if(displayName.contains("Change Pvp")) {
    8. p.sendMessage("Time has been set to night!");
    9. for (World world : Darkness.getInstance()
    10. .getServer().getWorlds()) {
    11. world.setTime(18000L);
    12. }
    13. e.setCancelled(true);
    14. }
    15.  
    16. if (displayName.contains("Change Time")) {
    17. p.sendMessage("Time has been set to day!");
    18. for (World world : Darkness.getInstance()
    19. .getServer().getWorlds()) {
    20. world.setTime(0L);
    21. }
    22. e.setCancelled(true);
    23. }
    24. }
    25. }


    This is very rough code, made it by hand on phone so it may contain mistakes.
     
Thread Status:
Not open for further replies.

Share This Page