InventoryGUI Teleporting

Discussion in 'Plugin Development' started by Ty Cleere, Dec 24, 2014.

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

    Ty Cleere

    I know bukkit is starting to die :( but i still wont give up coding. Therefor i am trying to make a inventory that hows all of the players online and you can teleport to one of them if you click on their head.

    Everything works except one thing. It doesn't teleport.

    Everything opens like it should and what not but i cant figure out why this line of code wont work..

    Code:
    Player playerBeingClicked=Bukkit.getPlayer(((SkullMeta)e.getCurrentItem().getItemMeta()).getOwner());
    Here is all of my code.

    Listener Code (open)

    Code:
        @EventHandler
        public void onClick(InventoryClickEvent e)
        {
            if(e.getInventory().getTitle().equalsIgnoreCase("CLICK on a name"))
            {
                if(e.getCurrentItem() != null && e.getCurrentItem().getType() != null)
                {
                    e.setCancelled(true);
                    Player player = (Player) e.getWhoClicked();
                    Player playerBeingClicked = Bukkit.getPlayer(((SkullMeta) e.getCurrentItem().getItemMeta()).getOwner());
                    if(player.getName() == playerBeingClicked.getDisplayName()) return;
                    if(playerBeingClicked != null)
                    {
                        player.teleport(playerBeingClicked);
                    }
                }
            }
        }
       
        @EventHandler
        public void compassmenu(PlayerInteractEvent  e) {
            Action a = e.getAction();
            ItemStack is = e.getItem();
           
            if (a == Action.PHYSICAL || is == null || is.getType()==Material.AIR) {
                return;
            }
           
            if(is.getType()==Material.COMPASS ){
                Main.openSpectateGUI(e.getPlayer(), Bukkit.getOnlinePlayers(), 1, new ItemStack(Material.SKULL_ITEM, 1, (byte) 3));
            }
        }


    MainCode (open)

    Code:
    public class SpectatorGUI implements Listener {
        static Plugin plugin;
        private Player player;
        private Player[] players;
        private SpectatorGUIHandler handler;
    
        public SpectatorGUI(Plugin plugin, Player player, Player[] players,
                SpectatorGUIHandler handler) {
            SpectatorGUI.plugin = plugin;
            this.player = player;
            this.players = players;
            this.handler = handler;
    
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }
    
        public void openSpectateGUI(Player p, Player[] players, int rows,
                ItemStack item) {
    
            int invSize = ((players.length + 8) / 9) * 9;
    
            ItemStack i = item;
            ItemMeta m = i.getItemMeta();
            Inventory inventory = Bukkit.createInventory(null, invSize,
                    "Select a player...");
            for (int z = 0; z < players.length; z++)
    
            {
                ArrayList<String> nlore = new ArrayList<String>();
    
                m.setDisplayName(players[z].getName());
                nlore.add(ChatColor.WHITE + "Current Level : " + ChatColor.GREEN
                        + players[z].getLevel());
                nlore.add("" + ChatColor.WHITE + "HEALTH : " + ChatColor.GREEN
                        + players[z].getHealth());
                nlore.add(ChatColor.WHITE + "Food Level : " + ChatColor.GREEN
                        + players[z].getFoodLevel());
                m.setLore(nlore);
                i.setItemMeta(m);
                inventory.setItem(z, i);
            }
            p.openInventory(inventory);
        }
    
        @EventHandler
        public void onClick(InventoryClickEvent event) {
            Inventory inv = event.getInventory();
    
            if (inv.getTitle().equalsIgnoreCase("Select a player...")) {
                if (event.getCurrentItem() != null
                        && event.getCurrentItem().getType() != null) {
                    handler.onItemClicked(event);
                    event.setCancelled(true);
                }
            }
        }
    
        public interface SpectatorGUIHandler {
            public void onItemClicked(InventoryClickEvent event);
        }
    }
     
  2. Offline

    NicksTheBoss

    @Ty Cleere

    Instead of
    Player playerBeingClicked=Bukkit.getPlayer(((SkullMeta)e.getCurrentItem().getItemMeta()).getOwner());
    Do
    Player playerBeingClicked=Bukkit.getPlayer(((SkullMeta)e.getCurrentItem().getItemMeta()).getDisplayName());

    Since the player you want to teleport to's name it on the skull, isn't it?
     
  3. Offline

    Ty Cleere

    @NicksTheBoss I actually thought of that. I tried using .getDisplayName() and .getName() But i get this when i try that. My error moves to line 35 instead of the first one.

    Code:
    [14:24:26 ERROR]: Could not pass event InventoryClickEvent to SpecMode v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:320) ~[craftbukki
    t.jar:git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[craftbukkit.
    jar:git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:481) [craftbukki
    t.jar:git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:466) [craftbukki
    t.jar:git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at net.minecraft.server.v1_7_R1.PlayerConnection.a(PlayerConnection.java:1351) [craftbukkit.
    jar:git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.a(SourceFile:32) [craftbukkit.jar:gi
    t-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at net.minecraft.server.v1_7_R1.PacketPlayInWindowClick.handle(SourceFile:10) [craftbukkit.j
    ar:git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at net.minecraft.server.v1_7_R1.NetworkManager.a(NetworkManager.java:146) [craftbukkit.jar:g
    it-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at net.minecraft.server.v1_7_R1.ServerConnection.c(SourceFile:134) [craftbukkit.jar:git-Bukk
    it-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.u(MinecraftServer.java:655) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at net.minecraft.server.v1_7_R1.DedicatedServer.u(DedicatedServer.java:250) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.t(MinecraftServer.java:545) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at net.minecraft.server.v1_7_R1.MinecraftServer.run(MinecraftServer.java:457) [craftbukkit.j
    ar:git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            at net.minecraft.server.v1_7_R1.ThreadServerApplication.run(SourceFile:617) [craftbukkit.jar
    :git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
    Caused by: java.lang.NullPointerException
            at me.simpleace.spectatormode.eventlistener.onClick(eventlistener.java:35) ~[?:?]
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_25]
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_25]
            at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_25]
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:318) ~[craftbukki
    t.jar:git-Bukkit-1.7.2-R0.2-10-gd51ccd1-b2987jnks]
            ... 13 more
    >
     
  4. Offline

    SuperOriginal

    More null checks, line 35 of eventlistener is null
     
  5. Offline

    Ty Cleere

    @SuperOriginal I see that it is a null on line 35. But where would i check to see if it is null. Where would i put that null check in? What is null about it?
     
  6. Offline

    nverdier

    Well the only thing that really could be null is players and index z...
     
  7. Offline

    SuperOriginal

    I don't know what line that is, check the components of the line and see which ones could possibly be null, and check for it before you use it.
     
  8. Offline

    TwerkinCraft

    if(e.getInventory().getTitle().equalsIgnoreCase("CLICK on a name")

    Inventory inventory = Bukkit.createInventory(null, invSize, "Select a player...");

    When does the name of the inventory change to "CLICK on a name"?
     
  9. Offline

    Ty Cleere

    @TwerkinCraft Oh i see that.. i changed that to match in both spots and i dont get the error. but now it just doesnt do anything. so imma do some testing and see what i get. Thanks
     
    TwerkinCraft likes this.
  10. Offline

    Experminator

    Off-topic:
    @Ty Cleere Thanks, i get a idea from you! :)
     
  11. Offline

    Ty Cleere

    @Experminator haha no problem! Im trying to make something for my server haha
     
  12. Offline

    Experminator

    @Ty Cleere If you want help, Ask me i help always!
    I can help with Development, in plugins, in a website or program.
    Or to configure anything.

    If you want personal help of me, pm me. I'm ready to help!
     
  13. Offline

    Ty Cleere

Thread Status:
Not open for further replies.

Share This Page