GUI Method Not Working || Item not spawning in inventory using onPlayerJoin

Discussion in 'Spigot Plugin Development' started by Unity, Jun 19, 2016.

  1. I have a plugin I ma coding, and it should work, but it isn't... It uses the Bukkit inventory system and is running of Spigot 1.9.4

    Code (Text):
    package me.xjinxhd.AdminGUI.gui;

    import org.bukkit.Bukkit;
    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.event.player.PlayerJoinEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.java.JavaPlugin;

    import org.bukkit.ChatColor;
    import org.bukkit.GameMode;

    @SuppressWarnings("unused")
    public class InvGUI extends JavaPlugin implements Listener {

        public void OnEnable() {
            getServer().getPluginManager().registerEvents(this, this);
        }

        private void openGUI(Player player) {
            Inventory inv = Bukkit.createInventory(null, 27, ChatColor.RED + "AdminGUI");

            ItemStack survival = new ItemStack(Material.DIAMOND);
            ItemMeta survivalMeta = survival.getItemMeta();
            ItemStack creative = new ItemStack(Material.DIAMOND_BLOCK);
            ItemMeta creativeMeta = creative.getItemMeta();

            survivalMeta.setDisplayName(ChatColor.LIGHT_PURPLE + "Survival");
            survival.setItemMeta(survivalMeta);

            creativeMeta.setDisplayName(ChatColor.LIGHT_PURPLE + "Creative");
            creative.setItemMeta(creativeMeta);

            inv.setItem(13, survival);
            inv.setItem(15, creative);

            player.openInventory(inv);
        }

        @EventHandler
        public void onInventoryClick(InventoryClickEvent e) {
            if (!ChatColor.stripColor(e.getInventory().getName()).equalsIgnoreCase("AdminGUI"))
                return;
            Player player =  (Player) e.getWhoClicked();
            e.setCancelled(true);
           
            if(e.getCurrentItem() == null || e.getCurrentItem().getType() == Material.AIR || !e.getCurrentItem().hasItemMeta()) {
                player.closeInventory();
                return;
            }
           
            switch(e.getCurrentItem().getType()) {
            case DIAMOND:
                player.setGameMode(GameMode.SURVIVAL);
                player.closeInventory();
                player.sendMessage(String.format("%sGamemode set to %sSurvival%s!", ChatColor.GOLD, ChatColor.RED, ChatColor.GOLD));
                break;
            case DIAMOND_BLOCK:
                player.setGameMode(GameMode.CREATIVE);
                player.closeInventory();
                player.sendMessage(String.format("%sGamemode set to %sCreative%s!", ChatColor.GOLD, ChatColor.RED, ChatColor.GOLD));
                break;
                default:
                    player.closeInventory();
                    break;
            }
           
           
        }

        @EventHandler
        public void onPlayerJoin(PlayerJoinEvent e) {
                e.getPlayer().getInventory().addItem(new ItemStack(Material.COMPASS));
        }

        @EventHandler
        public void onPlayerInteract(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)
                openGUI(e.getPlayer());
        }
    }
     
    This should open then GUI and give the compass upon logging in, but it doesn't. What's wrong with it and why won't it give me my stuff?
     
  2. you need to update the players inventory after modifying them, you can do this by using the following code:
    Code (Text):
    player.updateInventory();
    If that doesn't work please check for any startup or other errors and post them here.
     
  3. You are using "OnEnable()" instead of "onEnable()" That's why you should always use the Override annotation to prevent this kind of mistakes.
     
    • Agree Agree x 1
    • Winner Winner x 1