Solved Can Anyone tell me what's wrong with my Inventory Menu?

Discussion in 'Spigot Plugin Development' started by Surprisejedi, May 31, 2017.

  1. Main Class:
    Code (Java):
    package me.myself.st;

    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Listener;
    import org.bukkit.plugin.java.JavaPlugin;

    public class Main extends JavaPlugin implements Listener {

        private Effects menu;
     
        public void onEnable() {
            menu = new Effects(this);
            Bukkit.getServer().getPluginManager().registerEvents(this, this);
        }
     

        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        if (cmd.getName().equalsIgnoreCase("ef")) {
            Player player = (Player) sender;
            menu.show(player);
            return true;
        }
        return false;

    }
    }
    Effects Class:
    Code (Java):
    package me.myself.st;

    import java.util.Arrays;

    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.DyeColor;
    import org.bukkit.Material;
    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.plugin.Plugin;
    import org.bukkit.potion.PotionEffect;
    import org.bukkit.potion.PotionEffectType;
    public class Effects implements Listener {
            private Inventory inv, invSp;

            private ItemStack s, f, n, sp /* speed stuff */;
            public int speedLevel = 1;
            public Effects(Plugin p) {
                 
                 
                    int speedLevel = 1;
                    //Main Effects Chooser
                    @SuppressWarnings("deprecation")
                    ItemStack c = new ItemStack(Material.STAINED_GLASS_PANE, 1, DyeColor.ORANGE.getDyeData());
                    ItemMeta meta = c.getItemMeta();
                    meta.setDisplayName(ChatColor.DARK_RED + "Remove Effects");
                    meta.setLore(Arrays.asList(ChatColor.RED + "Removes All Current Effects"));
                    c.setItemMeta(meta);
                 
                    //Confirm Speed
                 
                    ItemStack conf = new ItemStack(Material.GHAST_TEAR, 1);
                    ItemMeta confm = conf.getItemMeta();
                    confm.setDisplayName(ChatColor.GREEN + "Confirm");
                    confm.setLore(Arrays.asList(ChatColor.DARK_GREEN + "Give yourself Speed" + speedLevel));
                    conf.setItemMeta(meta);

                    //stuff
                    invSp.setItem(5, conf);
                    s = createItem(Material.IRON_SWORD, ChatColor.RED + "Strength");
                    f = createItem(Material.BLAZE_POWDER, ChatColor.GOLD + "Fire Resistance");
                    n = createItem(Material.SPLASH_POTION, ChatColor.BLUE + "Night Vision");
                    sp = createItem(Material.DIAMOND_BOOTS, ChatColor.WHITE + "Speed");
                    inv.setItem(2, f);
                    inv.setItem(4, s);
                    inv.setItem(6, n);
                    inv.setItem(49, c);
                    inv.setItem(10, sp);
                 
                    Bukkit.getServer().getPluginManager().registerEvents(this, p);
            }

            private ItemStack createItem(Material material, String name) {
                ItemStack i = new ItemStack(material);
                ItemMeta im = i.getItemMeta();
             
                im.setDisplayName(name);
                im.setLore(Arrays.asList("Give yourself",  name.toLowerCase()));
                i.setItemMeta(im);
             
                return i;
            }
            public void show(Player p) {
                    p.openInventory(inv);
            }

            @EventHandler
            public void onInventoryClick(InventoryClickEvent e) {
                    if (e.getInventory().getName().equalsIgnoreCase(inv.getName())) {
                        if (e.getCurrentItem().getItemMeta() == null) return;
                        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Fire")) {
                            e.setCancelled(true);
                            e.getWhoClicked().addPotionEffect(new PotionEffect(PotionEffectType.FIRE_RESISTANCE, 1000000, 250, true));
                            e.getWhoClicked().sendMessage(ChatColor.GREEN + "You Now Have " + e.getCurrentItem().getItemMeta().getDisplayName());
                            e.getWhoClicked().closeInventory();
                        }
                        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Strength")) {
                            e.setCancelled(true);
                            e.getWhoClicked().addPotionEffect(new PotionEffect(PotionEffectType.INCREASE_DAMAGE, 1000000, 250, true));
                            e.getWhoClicked().sendMessage(ChatColor.GREEN + "You Now Have " + e.getCurrentItem().getItemMeta().getDisplayName());
                            e.getWhoClicked().closeInventory();
                        }
                        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Night")) {
                            e.setCancelled(true);
                            e.getWhoClicked().addPotionEffect(new PotionEffect(PotionEffectType.NIGHT_VISION, 1000000, 250, true));
                            e.getWhoClicked().sendMessage(ChatColor.GREEN + "You Now Have " + e.getCurrentItem().getItemMeta().getDisplayName());
                            e.getWhoClicked().closeInventory();
                        }
                        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Speed")) {
                            e.setCancelled(true);
                            e.getWhoClicked().sendMessage(ChatColor.DARK_RED + "YOUCLICKED");
                            e.getWhoClicked().openInventory(invSp);
                            //e.getWhoClicked().closeInventory(iv);
                        }
                        if (e.getCurrentItem().getItemMeta().getDisplayName().contains("Remove")) {
                            e.setCancelled(true);
                            for (PotionEffect effect : e.getWhoClicked().getActivePotionEffects()) {
                                e.getWhoClicked().removePotionEffect(effect.getType());
                            }
                            e.getWhoClicked().sendMessage(ChatColor.GREEN + "Removed All Potion Effects!");
                            e.getWhoClicked().closeInventory();
                        }
                    }
                   if(e.getInventory().getName().equalsIgnoreCase(invSp.getName())) {
                        if (e.getCurrentItem().getItemMeta() == null) return;
                        if(e.getCurrentItem().getItemMeta().getDisplayName().contains("Back"))
                            e.getWhoClicked().openInventory(inv);
                            e.getWhoClicked().sendMessage("You Clicked To Go Back");
                           }
                           if(e.getCurrentItem().getItemMeta().getDisplayName().matches("Confirm")) {
                               e.getWhoClicked().addPotionEffect(new PotionEffect(PotionEffectType.SPEED, 1000000, speedLevel, true));
                           }
                 
            }
    }
    Stacktrace:
    Code (Text):
    [13:37:15 INFO]: surprisejedi issued server command: /ef
    [13:37:16 ERROR]: null
    org.bukkit.command.CommandException: Unhandled exception executing command 'ef' in plugin MenuInv v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:46) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:141) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at org.bukkit.craftbukkit.v1_11_R1.CraftServer.dispatchCommand(CraftServer.java:650) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnection.handleCommand(PlayerConnection.java:1354) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1189) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:45) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PacketPlayInChat.a(PacketPlayInChat.java:1) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.PlayerConnectionUtils$1.run(SourceFile:13) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_101]
            at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_101]
            at net.minecraft.server.v1_11_R1.SystemUtils.a(SourceFile:46) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.D(MinecraftServer.java:747) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.DedicatedServer.D(DedicatedServer.java:399) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.C(MinecraftServer.java:678) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at net.minecraft.server.v1_11_R1.MinecraftServer.run(MinecraftServer.java:576) [spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            at java.lang.Thread.run(Unknown Source) [?:1.8.0_101]
    Caused by: java.lang.NullPointerException
            at me.surprisejedi.st.Main.onCommand(Main.java:23) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:44) ~[spigot-1.11.2.jar:git-Spigot-3fb9445-6e3cec8]
            ... 15 more
    >
    I know it says 'Null', but how do I fix that?
     
  2. GC.

    GC.

    The null error comes from "menu.show(player);"
     
  3. I know that.
     
  4. How do I fix that? If it is obvious, I am very sorry :(
     
  5. GC.

    GC.

    You never create the actual inventory, use Bukkit.createInventory(owner, slots, title); to initialize it on the original Inventory variable.
     
  6. Whoops! Sorry, I must've deleted it somehow :p
    Thanks for the help!