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);

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

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

            }

            p.openInventory(inv);
        }

        @EventHandler
        public void onInteract(PlayerInteractEvent e) {

            Player p = e.getPlayer();

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

            }

        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {

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

            e.setCancelled(true);

            ItemStack currentItem = e.getCurrentItem();
            if ((currentItem == null) || (!currentItem.hasItemMeta())) {
                return;
            }
            ItemMeta currentItemMeta = currentItem.getItemMeta();
            if (!currentItemMeta.hasDisplayName()) {
                return;
            }
            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) {
                    e2.printStackTrace();

                }
            }
        }

    }
    Code (Text):
    [22:33:22 ERROR]: Could not pass event PlayerInteractEvent to CustomTabList v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:310) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:487) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at org.bukkit.craftbukkit.v1_8_R3.event.CraftEventFactory.callPlayerInteractEvent(CraftEventFactory.java:228) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:724) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:52) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PacketPlayInBlockPlace.a(PacketPlayInBlockPlace.java:1) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_77]
            at java.util.concurrent.FutureTask.run(Unknown 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(MinecraftServer.java:715) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.DedicatedServer.B(DedicatedServer.java:374) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.A(MinecraftServer.java:654) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.MinecraftServer.run(MinecraftServer.java:557) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_77]
    Caused by: java.lang.NullPointerException
            at us.universalpvp.te.cosmetics.LobbySelector.switchHubInv(LobbySelector.java:39) ~[?:?]
            at us.universalpvp.te.cosmetics.LobbySelector.onInteract(LobbySelector.java:57) ~[?:?]
            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 org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[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 server.properties it says
    Code (Text):
    Hub\#1
    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