Solved [1.9.x] Inventory GUI not working

Discussion in 'Spigot Plugin Development' started by eekelmo, May 22, 2016.

  1. So I've been trying to code a plugin for a friend that opens up a GUI, then you click the blocks on it and you teleport somewhere, pretty simple, however I'm running into a few issues:
    - Players can take the items out of the inventory and keep them, re-open the GUI and take them again for infinite items
    - The teleportation doesn't work
    - I'm not sure how to do it on a world to world basis

    I understand that there are plugins like this already, but I'm just wanting to make it myself so I can learn.

    Code (Text):

    package me.Eekelmo.GUITP;

    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 gui extends JavaPlugin implements Listener {
        // Enable
        public void onEnable() {
            getServer().getPluginManager().registerEvents(this, this);
            getLogger().info("-------- GUI TP enabled -------");

        // Disable
        public void onDisable() {
            getLogger().info("-------- GUI TP disabled -------");

        // Teleport thing
        private void teleportInWorld(Player player, int x, int y, int z) {
            player.teleport(new Location(player.getWorld(), x, y, z));


        private void openGUI(Player player) {
            Inventory inv = Bukkit.createInventory(null, 9, ChatColor.GREEN + "World Selector");
            // Main world
            ItemStack mainworld = new ItemStack(Material.NETHER_STAR);
            ItemMeta mainworldMeta = mainworld.getItemMeta();
            // Mining
            ItemStack miningworld = new ItemStack(Material.DIAMOND_PICKAXE);
            ItemMeta miningworldMeta = miningworld.getItemMeta();
            // Setting text
            mainworldMeta.setDisplayName(ChatColor.DARK_RED + "Main");
            // Setting text
            miningworldMeta.setDisplayName(ChatColor.GREEN + "Mining");
            // Setting placement
            inv.setItem(3, mainworld);
            inv.setItem(5, miningworld);
            // Opening

        public void onInventoryClick(InventoryClickEvent event) {
            if (!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("World Selector"))
            Player player = (Player) event.getWhoClicked();

            if (event.getCurrentItem() == null || event.getCurrentItem().getType() == Material.AIR
                    || !event.getCurrentItem().hasItemMeta()) {
            switch (event.getCurrentItem().getType()) {
            case NETHER_STAR:
                teleportInWorld(player, -1034, 76, -849);
                player.sendMessage(String.format("&sTeleported to the &sMain World&s!", ChatColor.GOLD, ChatColor.DARK_RED, ChatColor.GOLD));
                switch (event.getCurrentItem().getType()) {
                case DIAMOND_PICKAXE:
                    teleportInWorld(player, -945, 69, -873);
                    player.sendMessage(String.format("&sTeleported to the &sMining World&s!", ChatColor.GOLD, ChatColor.GREEN, ChatColor.GOLD));

        public void onPlayerInteract(PlayerInteractEvent event) {
            Action a = event.getAction();
            ItemStack is = event.getItem();
            if (a == Action.PHYSICAL || is == null || is.getType() == Material.AIR)
            if(is.getType() == Material.COMPASS)
                getLogger().info("Compass used successfully");

    Help appreciated.
  2. Code (Text):
            if (!ChatColor.stripColor(event.getInventory().getName()).equalsIgnoreCase("World Selector"))
    This is probably the issue, instead of doing it like that and hardcoding it every time, store it in a value on startup and use the same value to create the inventory and to compare so you get the exact same. So make a string with the inventory names (including colors) then create inventory with that string as name, and compare it on the event, should work.
  3. This won't fix your problem; however, you should not be using two switch statements. Take out the second switch statement and add the case DIAMOND_PICKAXE to the first switch statement.
  4. It is very likely the problem that it returns before checking. He said that people could take the items out. He cancels the event in the start of the code meaning it wouldnt be possible, unless... it returned.
  5. Fixed, thanks for pointing out that mistake.

    I cancelled the event and now it works, I'd thank the person that suggested that but it seems their post was deleted.
    Unfortunately I'm quite new to Java and Bukkit/Spigot so this is just jargon to me, sorry. The code does however work and I thank you for your assistance.

    To anyone that stumbles upon this through Googling the same issue, this is my code that works: