Inventory for hub selecting

Discussion in 'Spigot Plugin Development' started by avighnash, May 11, 2016.

  1. Never mind, that was not the solution. Still same NPE.
  2. Give us the error and the latest code
  3. Code (Text):
    package us.universalpvp.te.cosmetics;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Location;
    import org.bukkit.Material;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.block.Action;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.event.player.PlayerInteractEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class LobbySelector implements Listener {

        public LobbySelector() {


        public void switchHubInv(Player p) {

            String connection = null;

            Inventory inv = Bukkit.createInventory(null, 45, ChatColor.DARK_GRAY + "Lobby Selector");

            for (int i = 0; i < Bukkit.getServer().getWorlds().size(); i++) {

                ItemStack lobby = new ItemStack(Material.QUARTZ_BLOCK);
                ItemMeta meta = lobby.getItemMeta();

                meta.setDisplayName(ChatColor.GREEN + "Main Lobby #" + i);

                        ChatColor.GRAY + "Players: " + Bukkit.getServer().getWorld("Hub#" + i).getPlayers().size()));

                for (int slots = 0; slots < Bukkit.getServer().getWorlds().size(); slots++) {
                    inv.setItem(slots, lobby);



        public void onInteract(PlayerInteractEvent e) {

            Player p = e.getPlayer();

            if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (p.getItemInHand().getType() == Material.NETHER_STAR) {



        public void onInventoryClick(InventoryClickEvent e) {

            if (!e.getInventory().getName().equalsIgnoreCase(ChatColor.DARK_GRAY + "Lobby Selector"))


            ItemStack currentItem = e.getCurrentItem();
            if ((currentItem == null) || (!currentItem.hasItemMeta())) {
            ItemMeta currentItemMeta = currentItem.getItemMeta();
            if (!currentItemMeta.hasDisplayName()) {
            String name = currentItemMeta.getDisplayName();

            name = ChatColor.stripColor(name);

            Player player = (Player) e.getWhoClicked();
            if (name.contains("Main Lobby")) {
                String[] parts = name.split("#");
                try {
                    int loc = Integer.parseInt(parts[1]);
                    player.teleport((Location) Bukkit.getWorld("Hub#" + loc));
                } catch (NumberFormatException e2) {


    Code (Text):
    [22:33:22 ERROR]: Could not pass event PlayerInteractEvent to CustomTabList v1.0
            at$1.execute( ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent( ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$ [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$ Source) [?:1.8.0_77]
            at Source) [?:1.8.0_77]
            at net.minecraft.server.v1_8_R3.SystemUtils.a(SourceFile:44) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.B( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A( [spigot.jar:git-Spigot-db6de12-18fbb24]
            at [spigot.jar:git-Spigot-db6de12-18fbb24]
            at Source) [?:1.8.0_77]
    Caused by: java.lang.NullPointerException
            at us.universalpvp.te.cosmetics.LobbySelector.switchHubInv( ~[?:?]
            at us.universalpvp.te.cosmetics.LobbySelector.onInteract( ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_77]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_77]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_77]
            at$1.execute( ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            ... 16 more
  4. Yep... XD
  5. @avighnash
    You didn't change anything?

    Print out all your worlds to the console.
  6. I only have one, and that's Hub#1
  7. Sorry my old answer was wrong.

    Why not loop through the Worlds available in the server instead?
  8. How? And whY?
  9. Bukkit.getWorlds(); returns a list of worlds, so that you can get precisely every world and use them on your GUI however you like.
  10. But, I like it to be simple, and I don't want to create an itemstack for everyworld. For instance, I don't have to create an itemstack everytime I create a new world, it would atomatically do that.
  11. In it says
    Code (Text):
    for the world name. Should I try that?
  12. Oh right.... # is used in regex code. Use what you just said.
  13. It marks red.
  14. Use \\ then
  15. Still there is a NPE..
  16. Do some debugging with the name