Inventory for hub selecting

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

  1. ItemStack currentItem = e.getCurrentItem();

    getClickedItem(); might be a better choice.

    You can split the string with '#' to get 2 parts of the name, use the second part which has the lobby number, try to get the integer in a try catch, then teleport them to the lobby, currently 'i' has no meaning inside inventory click event.

    So you would do inside your if where you check if it has Main Lobby in the name

    String[] parts = name.split("#");
    try{
    int loc = Integer.parseInt(parts[1]);
    player.teleport((Location) Bukkit.getWorld("Hub#"+loc));
    } catch (NumberFormatException e) {
    //Log an error message here or leave it empty, if the code doesn't work without errors, it is possible here.
    }
     
  2. Okay. I tried that:
    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 {

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

        private ItemStack lobby = null;

        public LobbySelector() {

        }

        public void switchHubInv(Player p) {

            String connection = null;

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

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

                if (p.getWorld().getPlayers().contains(p)) {
                    connection = ChatColor.RED + "Already connected!";
                } else {
                    connection = ChatColor.YELLOW + "Click to connect!";
                }

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

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

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

            }

            p.openInventory(this.inv);
        }

        @EventHandler
        public void onInteract(PlayerInteractEvent e) {
            for (Player p : Bukkit.getOnlinePlayers())
                if (e.getAction() == Action.RIGHT_CLICK_BLOCK) {
                    if (e.getMaterial() == Material.NETHER_STAR) {
                        switchHubInv(p);
                    }

                }

        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!e.getInventory().getName().equalsIgnoreCase(this.inv.getName())) {
                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();

                }
            }
        }

    }
    and I got a NPE at this line:
    Code (Text):
                        ChatColor.GRAY + "Players: " + Bukkit.getServer().getWorld("Hub#" + i).getPlayers().size(), " ",
     
     
  3. In your world literally named Hub#1 with the #
     
  4. Yes
     
  5. Try someone without the array so its just .setlore(Bukkit.blah lah);
     
  6. I don't think that's the problem. Plus, the inventory is not opening when I right click the nether star, it shows the error I stated above when I right click.
     
  7. Here is the error:

    Code (Text):
    [15:14:50 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.PlayerInteractManager.interact(PlayerInteractManager.java:463) [spigot.jar:git-Spigot-db6de12-18fbb24]
            at net.minecraft.server.v1_8_R3.PlayerConnection.a(PlayerConnection.java:759) [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:47) ~[?:?]
            at us.universalpvp.te.cosmetics.LobbySelector.onInteract(LobbySelector.java:66) ~[?:?]
            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]
            ... 17 more
     
  8. You never define the inventory. This should work:

    Code (Text):
    Inventory inv = Bukkit.createInventory(null, 9, ChatColor.YELLOW + ChatColor.BOLD.toString() + "My Sexy Inventory");

    Then p.openInventory(inv); in that method.
     
  9. Don't I do that here?
    Code (Text):
        private Inventory inv = Bukkit.createInventory(null, 45, ChatColor.DARK_GRAY + "Lobby Selector");;
     
    CHECK MY CODE XD
     
  10. Fixed some things, now there are no errors in console. The only problem is when I right click the star, nventory doesn't open.
     
  11. Here is the updated code:

    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 {

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

        public LobbySelector() {

        }

        public void switchHubInv(Player p) {

            String connection = null;

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

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

                if (p.getWorld().getPlayers().contains(p)) {
                    connection = ChatColor.RED + "Already connected!";
                } else {
                    connection = ChatColor.YELLOW + "Click to connect!";
                }

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

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

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

            }

            p.openInventory(this.inv);
        }

        @EventHandler
        public void onInteract(PlayerInteractEvent e) {

            Player p = e.getPlayer();

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

            }

        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!e.getInventory().getName().equalsIgnoreCase(this.inv.getName())) {
                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();

                }
            }
        }

    }
     
  12. Lol im dumb. Try putting it in the method and just use .openInventory(inv);
     
  13. That won't help thougg :/
     
  14. Well it's worked for me everytime. Meh who cares I'm not even helping I fell sick xD
     
  15. You're checking if the block that you're clicking is a nether star. Check if the item in the player's hand is a nether star instead.

    Also,
    You're checking if the player's world contains the player. So connection is always going to be "Already connected!".
     
    #35 liveinacupboard, May 14, 2016
    Last edited: May 14, 2016
  16. F
    Fixed that. I still get an NPE pointing at that line.
     
  17. Check if the item is null then?
     
  18. This line:

    Code (Text):
                        ChatColor.GRAY + "Players: " + Bukkit.getServer().getWorld("Hub#" + i).getPlayers().size()));
     
     
  19. The folder name "Hub#1" is null (1-9) then...
     
  20. Ohh, I think it is coming as null because if the int is 0, it is detecting it as Hub#0?
     
    • Like Like x 1