Help Creating GUI

Discussion in 'Spigot Plugin Development' started by 112madgamer, May 24, 2016.

  1. Hello this is my first plugin and I have some code to create a GUI but when I run the command in game in errors

    Here is the error

    Code (Text):
    [20:48:27] [Server thread/INFO]: 112madGamer issued server command: /menu
    [20:48:27] [Server thread/ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'menu' in plugin SpigotDev v0.1
       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot.jar:git-Spigot-8a048fe-a022dd2]
       at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot.jar:git-Spigot-8a048fe-a022dd2]
       at org.bukkit.craftbukkit.v1_9_R2.CraftServer.dispatchCommand(CraftServer.java:646) ~[spigot.jar:git-Spigot-8a048fe-a022dd2]
       at net.minecraft.server.v1_9_R2.PlayerConnection.handleCommand(PlayerConnection.java:1349) [spigot.jar:git-Spigot-8a048fe-a022dd2]
       at net.minecraft.server.v1_9_R2.PlayerConnection.a(PlayerConnection.java:1184) [spigot.jar:git-Spigot-8a048fe-a022dd2]
       at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot.jar:git-Spigot-8a048fe-a022dd2]
       at net.minecraft.server.v1_9_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot.jar:git-Spigot-8a048fe-a022dd2]
       at net.minecraft.server.v1_9_R2.PlayerConnectionUtils$1.run(SourceFile:13) [spigot.jar:git-Spigot-8a048fe-a022dd2]
       at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_05]
       at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_05]
       at net.minecraft.server.v1_9_R2.SystemUtils.a(SourceFile:45) [spigot.jar:git-Spigot-8a048fe-a022dd2]
       at net.minecraft.server.v1_9_R2.MinecraftServer.D(MinecraftServer.java:726) [spigot.jar:git-Spigot-8a048fe-a022dd2]
       at net.minecraft.server.v1_9_R2.DedicatedServer.D(DedicatedServer.java:399) [spigot.jar:git-Spigot-8a048fe-a022dd2]
       at net.minecraft.server.v1_9_R2.MinecraftServer.C(MinecraftServer.java:665) [spigot.jar:git-Spigot-8a048fe-a022dd2]
       at net.minecraft.server.v1_9_R2.MinecraftServer.run(MinecraftServer.java:564) [spigot.jar:git-Spigot-8a048fe-a022dd2]
       at java.lang.Thread.run(Thread.java:745) [?:1.8.0_05]
    Caused by: java.lang.NullPointerException
       at com.madgamer.SpigotDev.IconMenu.<init>(IconMenu.java:54) ~[?:?]
       at com.madgamer.SpigotDev.Dev.onCommand(Dev.java:39) ~[?:?]
       at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot.jar:git-Spigot-8a048fe-a022dd2]
       ... 15 more
    Here is my code
    Code (Java):
    package com.madgamer.SpigotDev;


    import de.myzelyam.api.vanish.PlayerShowEvent;
    import org.bukkit.Bukkit;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.PlayerInteractEvent;

    import org.bukkit.inventory.ItemStack;
    import org.bukkit.plugin.Plugin;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Dev extends JavaPlugin implements CommandExecutor, Listener {

        public Plugin plugin;

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

        @Override
        public void onDisable() {

        }
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] split) {
            if (cmd.getName().equalsIgnoreCase("menu")) {
                if (sender instanceof Player) {
                    Player p = ((Player) sender).getPlayer();
                    IconMenu menu = new IconMenu("My Fancy Menu", 9, new IconMenu.OptionClickEventHandler() {
                        @Override
                        public void onOptionClick(IconMenu.OptionClickEvent event) {
                            event.getPlayer().sendMessage("You have chosen " + event.getName());
                            event.setWillClose(true);
                        }
                    }, plugin)
                            .setOption(3, new ItemStack(Material.APPLE, 1), "Food", "The food is delicious")
                            .setOption(4, new ItemStack(Material.IRON_SWORD, 1), "Weapon", "Weapons are for awesome people")
                            .setOption(5, new ItemStack(Material.EMERALD, 1), "Money", "Money brings happiness");

                    menu.open(p);
                    return true;
                } else {
                    return false;
                }
            }
            return true;
        }
    }


     
    and here is the IconMenu code

    Code (Java):
    package com.madgamer.SpigotDev;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.GameMode;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.material.Wool;
    import org.bukkit.plugin.Plugin;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.EventPriority;
    import org.bukkit.event.HandlerList;
    import org.bukkit.event.Listener;
    import org.bukkit.event.inventory.InventoryClickEvent;
    import org.bukkit.inventory.Inventory;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;
    import org.bukkit.plugin.Plugin;

    public class IconMenu implements Listener {

        private String name;
        private int size;
        private OptionClickEventHandler handler;
        private Plugin plugin;

        private String[] optionNames;
        private ItemStack[] optionIcons;

        public IconMenu(String name, int size, OptionClickEventHandler handler, Plugin plugin) {
            this.name = name;
            this.size = size;
            this.handler = handler;
            this.plugin = plugin;
            this.optionNames = new String[size];
            this.optionIcons = new ItemStack[size];
            plugin.getServer().getPluginManager().registerEvents(this, plugin);
        }

        public IconMenu setOption(int position, ItemStack icon, String name, String... info) {
            optionNames[position] = name;
            optionIcons[position] = setItemNameAndLore(icon, name, info);
            return this;
        }

        public void open(Player player) {
            Inventory inventory = Bukkit.createInventory(player, size, name);
            for (int i = 0; i < optionIcons.length; i++) {
                if (optionIcons[i] != null) {
                    inventory.setItem(i, optionIcons[i]);
                }
            }
            player.openInventory(inventory);
        }

        public void destroy() {
            HandlerList.unregisterAll(this);
            handler = null;
            plugin = null;
            optionNames = null;
            optionIcons = null;
        }

        @EventHandler(priority=EventPriority.MONITOR)
        void onInventoryClick(InventoryClickEvent event) {
            if (event.getInventory().getTitle().equals(name)) {
                event.setCancelled(true);
                int slot = event.getRawSlot();
                if (slot >= 0 && slot < size && optionNames[slot] != null) {
                    Plugin plugin = this.plugin;
                    OptionClickEvent e = new OptionClickEvent((Player)event.getWhoClicked(), slot, optionNames[slot]);
                    handler.onOptionClick(e);
                    if (e.willClose()) {
                        final Player p = (Player)event.getWhoClicked();
                        Bukkit.getScheduler().scheduleSyncDelayedTask(plugin, new Runnable() {
                            public void run() {
                                p.closeInventory();
                            }
                        }, 1);
                    }
                    if (e.willDestroy()) {
                        destroy();
                    }
                }
            }
        }

        public interface OptionClickEventHandler {
            void onOptionClick(OptionClickEvent event);
        }

        public class OptionClickEvent {
            private Player player;
            private int position;
            private String name;
            private boolean close;
            private boolean destroy;

            public OptionClickEvent(Player player, int position, String name) {
                this.player = player;
                this.position = position;
                this.name = name;
                this.close = true;
                this.destroy = false;
            }

            public Player getPlayer() {
                return player;
            }

            public int getPosition() {
                return position;
            }

            public String getName() {
                return name;
            }

            public boolean willClose() {
                return close;
            }

            public boolean willDestroy() {
                return destroy;
            }

            public void setWillClose(boolean close) {
                this.close = close;
            }

            public void setWillDestroy(boolean destroy) {
                this.destroy = destroy;
            }
        }

        private ItemStack setItemNameAndLore(ItemStack item, String name, String[] lore) {
            ItemMeta im = item.getItemMeta();
            im.setDisplayName(name);
            im.setLore(Arrays.asList(lore));
            item.setItemMeta(im);
            return item;
        }

    }
     
     
    #1 112madgamer, May 24, 2016
    Last edited: May 3, 2017
  2. You're not setting the plugin variable in your Dev class, and as such it's null. Do this in your onEnable method.
     
  3. I not understand could you show a example?
     
  4. You're not setting this variable anywhere. You need to do so, otherwise it is equal to null. You need to set this to the instance of your main class in your onEnable - using the keyword "this". Not going to give a direct code example, as this is quite basic Java and I'd prefer not to spoonfeed.
     
  5. You mean like this
    Code (Text):
            this.plugin = new Dev();
     
    as I said I a new and this is my first plugin
     
  6. MiniDigger

    Supporter

    NEVER call the constructor of you main class.
    just set the var to 'this' on load or on enable.
     
  7. Okay I did this
    Code (Text):
     this.plugin = plugin;
    it still throws the error
     
  8. DW i fixed it I change the plugin in IconMenu to this and it is working