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 public Main() { main = Bukkit.getServer().createInventory(null, 9, "Admin Controls"); ItemMeta time = main.getItem(2).getItemMeta(); //Throws Null Pointer ItemMeta weather = main.getItem(6).getItemMeta(); //Throws Null Pointer time.setDisplayName(ChatColor.DARK_RED + "Change Time"); weather.setDisplayName(ChatColor.DARK_RED + "Change Weather"); main.getItem(2).setItemMeta(time); main.getItem(6).setItemMeta(weather); main.setItem(2, new ItemStack(Material.WATCH, 1)); main.setItem(6, new ItemStack(Material.STICK, 1)); timeChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls"); dayItem = createItem(DyeColor.GREEN, ChatColor.DARK_RED + "Day"); nightItem = createItem(DyeColor.BLACK, ChatColor.DARK_RED + "Night"); timeChange.setItem(2, dayItem); timeChange.setItem(6, nightItem); }
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
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
Chinwe Nevermind. I fixed the npe. But how could I add commands once you click the item? (I know stupid question)
PolarCraft You'd listen to when they click on the specific item in your specific inventory, and then use Bukkit.dispatchCommand(playerWhoClicked, String)
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 @EventHandler public void timeDay(InventoryClickEvent e) { if(!e.getInventory().getName().equalsIgnoreCase(timeChange.getName())) return; if(e.getCurrentItem().getItemMeta() == null) return; if(e.getCurrentItem().getItemMeta().getDisplayName().contains("day")) { e.setCancelled(true); Bukkit.dispatchCommand(e, Bukkit.getWorlds().setTime(6500L)); } }
PolarCraft Ugh.... You need to iterate over the worlds.... for (World w : Bukkit.getWorlds()) w.setTime(6500L);
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.
Forseth11 What other code? And newboyhun when you right click either option it will go ahead and shoot a command and close the inventory.
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.
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.
Forseth11 It has to be a command sender. Forseth11 I am trying p.chat instead but It will not fire the command. Code:java @EventHandler public void onInventoryClick2(InventoryClickEvent e) { Player p = (Player) e.getWhoClicked(); if(!e.getInventory().getName().equalsIgnoreCase(timeChange.getName())) return; if(e.getCurrentItem().getItemMeta() == null) return; if(e.getCurrentItem().getItemMeta().getDisplayName().contains("night")) { e.setCancelled(true); p.chat("/time set night"); e.getWhoClicked().closeInventory(); } } EDIT by Moderator: merged posts, please use the edit button instead of double posting.
Instead of making them send the command; just do: Code:java Bukkit.getWorld(player.getWorld().getName()).setTime(15000);
Regagames Yeah still does not work. Here is my full class: Code:java package net.jc.minecraft; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.DyeColor;import org.bukkit.Material;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.event.EventHandler;import org.bukkit.event.Listener;import org.bukkit.event.inventory.InventoryClickEvent;import org.bukkit.inventory.Inventory;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.meta.ItemMeta;import org.bukkit.material.Wool;import org.bukkit.plugin.java.JavaPlugin; public final class Main extends JavaPlugin implements Listener { public static Inventory main; public static Inventory pvpChange; public static Inventory timeChange; public static Inventory weatherChange; public ItemStack dayItem, nightItem; public ItemStack onItem, offItem; public ItemStack sunItem, rainItem; public static CommandSender day; public void onEnable() { Bukkit.getServer().getPluginManager().registerEvents(this, this); getCommand("admin").setExecutor(this); } public Main() { main = Bukkit.getServer().createInventory(null, 9, "Admin Controls"); main.setItem(2, new ItemStack(Material.WATCH, 1)); //3 main.setItem(4, new ItemStack(Material.DIAMOND_SWORD)); //5 main.setItem(6, new ItemStack(Material.BLAZE_ROD, 1)); ItemMeta time = main.getItem(2).getItemMeta(); //3 ItemMeta pvp = main.getItem(4).getItemMeta(); //5 ItemMeta weather = main.getItem(6).getItemMeta(); pvp.setDisplayName(ChatColor.DARK_RED + "Change Pvp"); time.setDisplayName(ChatColor.DARK_RED + "Change Time"); weather.setDisplayName(ChatColor.DARK_RED + "Change Weather"); main.getItem(2).setItemMeta(time); main.getItem(4).setItemMeta(pvp); main.getItem(6).setItemMeta(weather); timeChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls"); dayItem = createItem(DyeColor.LIME, ChatColor.DARK_RED + "Day"); nightItem = createItem(DyeColor.RED, ChatColor.DARK_RED + "Night"); timeChange.setItem(2, dayItem); timeChange.setItem(6, nightItem); weatherChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls"); sunItem = createItem(DyeColor.ORANGE, ChatColor.DARK_RED + "Sun"); rainItem = createItem(DyeColor.PURPLE, ChatColor.DARK_RED + "Rain"); weatherChange.setItem(2, sunItem); weatherChange.setItem(6, rainItem); pvpChange = Bukkit.getServer().createInventory(null, 9, "Admin Controls"); onItem = createItem(DyeColor.LIME, ChatColor.GREEN + "On"); offItem = createItem(DyeColor.RED, ChatColor.DARK_RED + "Off"); pvpChange.setItem(2, onItem); pvpChange.setItem(6, offItem); } private ItemStack createItem(DyeColor dc, String name) { ItemStack i = new Wool(dc).toItemStack(1); ItemMeta im = i.getItemMeta(); im.setDisplayName(name); i.setItemMeta(im); return i; } @EventHandler public void onInventoryClick(InventoryClickEvent e) { if(!e.getInventory().getName().equalsIgnoreCase(main.getName())) return; if(e.getCurrentItem().getItemMeta() == null) return; if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Time")) { e.setCancelled(true); e.getWhoClicked().closeInventory(); e.getWhoClicked().openInventory(timeChange); } } @EventHandler public void onInventoryClick1(InventoryClickEvent e) { Player p = (Player)e.getWhoClicked(); if(!e.getInventory().getName().equalsIgnoreCase(timeChange.getName())) return; if(e.getCurrentItem().getItemMeta() == null) return; if(e.getCurrentItem().getItemMeta().getDisplayName().contains("day")) { e.setCancelled(true); p.chat("/time set day"); e.getWhoClicked().closeInventory(); } } @EventHandler public void onInventoryClick2(InventoryClickEvent e) { Player p = (Player) e.getWhoClicked(); if(!e.getInventory().getName().equalsIgnoreCase(timeChange.getName())) return; if(e.getCurrentItem().getItemMeta() == null) return; if(e.getCurrentItem().getItemMeta().getDisplayName().contains("night")) { e.setCancelled(true); Bukkit.getWorld(p.getWorld().getName()).setTime(15000); e.getWhoClicked().closeInventory(); } } @EventHandler public void onInventoryClick3(InventoryClickEvent e) { if(!e.getInventory().getName().equalsIgnoreCase(main.getName())) return; if(e.getCurrentItem().getItemMeta() == null) return; if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Pvp")) { e.setCancelled(true); e.getWhoClicked().closeInventory(); e.getWhoClicked().openInventory(pvpChange); } } @EventHandler public void onInventoryClick4(InventoryClickEvent e) { if(!e.getInventory().getName().equalsIgnoreCase(main.getName())) return; if(e.getCurrentItem().getItemMeta() == null) return; if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Change Weather")) { e.setCancelled(true); e.getWhoClicked().closeInventory(); e.getWhoClicked().openInventory(weatherChange); } } /**@EventHandler public void onPlayerJoin(PlayerJoinEvent e) { Player p = e.getPlayer(); if(p.hasPermission("at")) { ItemStack r = new ItemStack (Material.RED_ROSE); p.getInventory().addItem(r); } }**/ public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) { if(cmd.getName().equalsIgnoreCase("admin")) { Player p = (Player) sender; p.openInventory(main); } return true; }}
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
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.