Item Gui.

Discussion in 'Plugin Development' started by PolarCraft, Jan 2, 2014.

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

    PolarCraft

    Okay so I get two nullpointers. At these lines.
    ItemMeta time = main.getItem(2).getItemMeta(); //Throws Null Pointer
    And.
    ItemMeta weather = main.getItem(6).getItemMeta(); //Throws Null Pointer

    Do you guys know why? Is it because i am not check to see if it is null?

    Code:java
    1. public Main() {
    2. main = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    3. ItemMeta time = main.getItem(2).getItemMeta(); //Throws Null Pointer
    4. ItemMeta weather = main.getItem(6).getItemMeta(); //Throws Null Pointer
    5. time.setDisplayName(ChatColor.DARK_RED + "Change Time");
    6. weather.setDisplayName(ChatColor.DARK_RED + "Change Weather");
    7. main.getItem(2).setItemMeta(time);
    8. main.getItem(6).setItemMeta(weather);
    9. main.setItem(2, new ItemStack(Material.WATCH, 1));
    10. main.setItem(6, new ItemStack(Material.STICK, 1));
    11.  
    12. timeChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    13. dayItem = createItem(DyeColor.GREEN, ChatColor.DARK_RED + "Day");
    14. nightItem = createItem(DyeColor.BLACK, ChatColor.DARK_RED + "Night");
    15. timeChange.setItem(2, dayItem);
    16. timeChange.setItem(6, nightItem);
    17. }
     
  2. Offline

    werter318

    because item 2 and 6 aren't there yet
     
  3. Offline

    PolarCraft

    werter318 What do you mean they are not their?
     
  4. Offline

    Chinwe

    As in those slots are empty, so it seems they're returning null - you're then calling .setItemMeta() on them, which throws the NPE - move those two lines after you set the 2 slots to a watch and stick :)
     
  5. Offline

    PolarCraft

    Chinwe Then the variables will not be their.
     
  6. Offline

    Chinwe

    You have to create the itemstacks first, then apply the itemmeta -

    Code:
    // make the itemstack
    ItemStack watch = new ItemStack(Material.WATCH);
     
    // create meta
    ItemMeta meta = watch.getItemMeta(); // 'watch' exists, so no NPE
     
    // customize your meta
    meta.setDisplayName("Your New Displayname");
     
    // apply the meta
    watch.setItemMeta(bleh);
     
    // add to gui
    main.setItem(2, watch);
     
    // do the same with the stick
     
  7. Offline

    PolarCraft

    Chinwe Nevermind. I fixed the npe. But how could I add commands once you click the item? (I know stupid question)
     
  8. Offline

    Chinwe

  9. Offline

    PolarCraft

    Chinwe I am trying to set time to day. But i am getting this error:
    The method setTime(long) is undefined for the type List<World>

    Code:
    Code:java
    1. @EventHandler
    2. public void timeDay(InventoryClickEvent e) {
    3. if(!e.getInventory().getName().equalsIgnoreCase(timeChange.getName())) return;
    4. if(e.getCurrentItem().getItemMeta() == null) return;
    5. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("day")) {
    6. e.setCancelled(true);
    7. Bukkit.dispatchCommand(e, Bukkit.getWorlds().setTime(6500L));
    8. }
    9. }
     
  10. Offline

    newboyhun

    PolarCraft
    Ugh....
    You need to iterate over the worlds....
    for (World w : Bukkit.getWorlds()) w.setTime(6500L);
     
  11. Offline

    PolarCraft

    newboyhun How could I dispatch the command?
    Bukkit.dispatchCommand(e, w);
     
  12. Offline

    lenis0012

    playerWhoClicked.performCommand("time day");

    Not sure if its with or without slash
     
  13. Offline

    newboyhun

    PolarCraft
    Why do you want to use command for thing like this?
     
  14. Offline

    Forseth11

    PolarCraft I helped you with this piece of code earlier. Like I said on the other post, you need to make them no null. How do you do this? You create an ItemStack then you can get its meta. After that you can add it to the inventory.
    Tahg me if you have any questions.

    EDIT: Fixed spelling error.
     
  15. Offline

    PolarCraft

    Forseth11 What other code? And newboyhun when you right click either option it will go ahead and shoot a command and close the inventory.
     
  16. Offline

    Forseth11

    PolarCraft You posted another form with this code where you had an error where it would not run, so I fixed it for you and I pointed out this error.
     
  17. Offline

    PolarCraft

    Forseth11 No this is my first time posting this.

    lenis0012 That does not work. Any other ways?
    The method performCommand(String) is undefined for the type HumanEntity

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

    Chinwe

    PolarCraft
    You'd need to cast to Player - or use Bukkit.dispatchCommand() :)
     
  19. Offline

    Forseth11

    PolarCraft Very very very weird. Remember I had to help you with the plugin.yml. Oh whatever.
     
  20. Offline

    PolarCraft

    Chinwe Forseth11 What would I replace null with? Bukkit.dispatchCommand(null, "/time set day");
     
  21. Offline

    Forseth11

    PolarCraft You create an ItemStack so it is not null.
     
  22. Offline

    PolarCraft

    Forseth11 It has to be a command sender.

    Forseth11 I am trying p.chat instead but It will not fire the command.
    Code:java
    1. @EventHandler
    2. public void onInventoryClick2(InventoryClickEvent e) {
    3. Player p = (Player) e.getWhoClicked();
    4. if(!e.getInventory().getName().equalsIgnoreCase(timeChange.getName())) return;
    5. if(e.getCurrentItem().getItemMeta() == null) return;
    6. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("night")) {
    7. e.setCancelled(true);
    8. p.chat("/time set night");
    9. e.getWhoClicked().closeInventory();
    10. }
    11. }


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

    Regagames

    Instead of making them send the command; just do:
    Code:java
    1. Bukkit.getWorld(player.getWorld().getName()).setTime(15000);
     
  24. Offline

    PolarCraft

    Regagames Yeah still does not work. Here is my full class:
    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. getCommand("admin").setExecutor(this);
    33. }
    34.  
    35. public Main() {
    36. main = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    37. main.setItem(2, new ItemStack(Material.WATCH, 1));
    38. //3
    39. main.setItem(4, new ItemStack(Material.DIAMOND_SWORD));
    40. //5
    41. main.setItem(6, new ItemStack(Material.BLAZE_ROD, 1));
    42.  
    43. ItemMeta time = main.getItem(2).getItemMeta();
    44. //3
    45. ItemMeta pvp = main.getItem(4).getItemMeta();
    46. //5
    47. ItemMeta weather = main.getItem(6).getItemMeta();
    48.  
    49. pvp.setDisplayName(ChatColor.DARK_RED + "Change Pvp");
    50. time.setDisplayName(ChatColor.DARK_RED + "Change Time");
    51. weather.setDisplayName(ChatColor.DARK_RED + "Change Weather");
    52. main.getItem(2).setItemMeta(time);
    53. main.getItem(4).setItemMeta(pvp);
    54. main.getItem(6).setItemMeta(weather);
    55.  
    56. timeChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    57. dayItem = createItem(DyeColor.LIME, ChatColor.DARK_RED + "Day");
    58. nightItem = createItem(DyeColor.RED, ChatColor.DARK_RED + "Night");
    59. timeChange.setItem(2, dayItem);
    60. timeChange.setItem(6, nightItem);
    61.  
    62. weatherChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    63. sunItem = createItem(DyeColor.ORANGE, ChatColor.DARK_RED + "Sun");
    64. rainItem = createItem(DyeColor.PURPLE, ChatColor.DARK_RED + "Rain");
    65. weatherChange.setItem(2, sunItem);
    66. weatherChange.setItem(6, rainItem);
    67.  
    68. pvpChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls");
    69. onItem = createItem(DyeColor.LIME, ChatColor.GREEN + "On");
    70. offItem = createItem(DyeColor.RED, ChatColor.DARK_RED + "Off");
    71. pvpChange.setItem(2, onItem);
    72. pvpChange.setItem(6, offItem);
    73.  
    74. }
    75.  
    76. private ItemStack createItem(DyeColor dc, String name) {
    77. ItemStack i = new Wool(dc).toItemStack(1);
    78. ItemMeta im = i.getItemMeta();
    79. im.setDisplayName(name);
    80. i.setItemMeta(im);
    81. return i;
    82. }
    83.  
    84. @EventHandler
    85. public void onInventoryClick(InventoryClickEvent e) {
    86. if(!e.getInventory().getName().equalsIgnoreCase(main.getName())) return;
    87. if(e.getCurrentItem().getItemMeta() == null) return;
    88. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Time")) {
    89. e.setCancelled(true);
    90. e.getWhoClicked().closeInventory();
    91. e.getWhoClicked().openInventory(timeChange);
    92. }
    93. }
    94.  
    95. @EventHandler
    96. public void onInventoryClick1(InventoryClickEvent e) {
    97. Player p = (Player)e.getWhoClicked();
    98. if(!e.getInventory().getName().equalsIgnoreCase(timeChange.getName())) return;
    99. if(e.getCurrentItem().getItemMeta() == null) return;
    100. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("day")) {
    101. e.setCancelled(true);
    102. p.chat("/time set day");
    103. e.getWhoClicked().closeInventory();
    104. }
    105. }
    106.  
    107. @EventHandler
    108. public void onInventoryClick2(InventoryClickEvent e) {
    109. Player p = (Player) e.getWhoClicked();
    110. if(!e.getInventory().getName().equalsIgnoreCase(timeChange.getName())) return;
    111. if(e.getCurrentItem().getItemMeta() == null) return;
    112. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("night")) {
    113. e.setCancelled(true);
    114. Bukkit.getWorld(p.getWorld().getName()).setTime(15000);
    115. e.getWhoClicked().closeInventory();
    116. }
    117. }
    118.  
    119. @EventHandler
    120. public void onInventoryClick3(InventoryClickEvent e) {
    121. if(!e.getInventory().getName().equalsIgnoreCase(main.getName())) return;
    122. if(e.getCurrentItem().getItemMeta() == null) return;
    123. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Pvp")) {
    124. e.setCancelled(true);
    125. e.getWhoClicked().closeInventory();
    126. e.getWhoClicked().openInventory(pvpChange);
    127. }
    128. }
    129.  
    130. @EventHandler
    131. public void onInventoryClick4(InventoryClickEvent e) {
    132. if(!e.getInventory().getName().equalsIgnoreCase(main.getName())) return;
    133. if(e.getCurrentItem().getItemMeta() == null) return;
    134. if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Weather")) {
    135. e.setCancelled(true);
    136. e.getWhoClicked().closeInventory();
    137. e.getWhoClicked().openInventory(weatherChange);
    138. }
    139. }
    140. /**@EventHandler
    141.   public void onPlayerJoin(PlayerJoinEvent e) {
    142.   Player p = e.getPlayer();
    143.   if(p.hasPermission("at")) {
    144.   ItemStack r = new ItemStack (Material.RED_ROSE);
    145.   p.getInventory().addItem(r);
    146.   }
    147.   }**/
    148.  
    149. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    150. if(cmd.getName().equalsIgnoreCase("admin")) {
    151. Player p = (Player) sender;
    152. p.openInventory(main);
    153. }
    154. return true;
    155. }
    156. }
     
  25. Offline

    Chinwe

    Why on earth do you have 5 InventoryClickEvent listeners? Just the one is needed - use else ifs.
    Also, why are you putting all inventory creation code in a new constructor? I doubt it is being called, put it into onEnable() instead.

    And you should use the same inventory for each of them, you're making an inventory for each item at the moment :eek:
     
  26. Offline

    PolarCraft

    Chinwe I rather keep them separate so if i need to add more to it i can. And I need it to open new inventory's.
    [EDIT] Forget what I said. I decided to do a different way like you said. It is just a whole bunch of code. That is worthless. So If anything happens I will tahg you.
     
Thread Status:
Not open for further replies.

Share This Page