team picker plugin error

Discussion in 'Plugin Development' started by Thatguyplsstop, Sep 8, 2020.

  1. Offline

    Thatguyplsstop

    Hello, I am trying to make this plugin to opens a GUI and allows the player to pick a team and they are assigned that team with matching leather armour, but when I try to run the command I get this error


    Here is the plugin code

    Code:
    package com.trobanko.teampicker;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import org.bukkit.Bukkit;
    import org.bukkit.ChatColor;
    import org.bukkit.Color;
    import org.bukkit.Material;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    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.inventory.meta.LeatherArmorMeta;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin implements Listener {
    
        public Inventory inv;
      
        @Override
        public void onEnable() {
            this.getServer().getPluginManager().registerEvents(this, this);
        }
      
        @Override
        public void onDisable() {
          
        }
      
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("changeteam")) {
                if (!(sender instanceof Player)) {
                    sender.sendMessage("Console may not execute this command");
                    return false;
                }
                Player player = (Player) sender;
                player.openInventory(inv);
                return true;
            }
            return false;
        }
      
      
        @EventHandler()
        public void onClick(InventoryClickEvent event) {
            if (!event.getInventory().equals(inv))
                return;
            if (event.getCurrentItem() == null) return;
            if (event.getCurrentItem().getItemMeta() == null) return;
            if (event.getCurrentItem().getItemMeta().getDisplayName() == null) return;
          
            event.setCancelled(true);
          
            Player player = (Player) event.getWhoClicked();
          
            if (event.getSlot() == 0) {
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.BLUE);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD + "Team has been changed");     
            }
            if (event.getSlot() == 1) {
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.RED);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD + "Team has been changed");
          
            }        if (event.getSlot() == 2) {
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.GREEN);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD + "Team has been changed");
          
            }        if (event.getSlot() == 3) {
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.PURPLE);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD + "Team has been changed");
          
            }        if (event.getSlot() == 4) {
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.ORANGE);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD + "Team has been changed");
          
            }        if (event.getSlot() == 5) {
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.LIME);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD + "Team has been changed");
          
            }        if (event.getSlot() == 6) {
                ItemStack[] armor = player.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.AQUA);
                player.getEquipment().setArmorContents(armor);
                player.sendMessage(ChatColor.GOLD + "Team has been changed");
            }
            if (event.getSlot() == 8) {
                player.closeInventory();
            }
            return;
        }
      
        public ItemStack[] changeColor(ItemStack[] a, Color color) {
            for (ItemStack item : a) {
                try {
                    if (item.getType() == Material.LEATHER_BOOTS || item.getType() == Material.LEATHER_CHESTPLATE ||
                            item.getType() == Material.LEATHER_HELMET || item.getType() == Material.LEATHER_LEGGINGS) {
                        LeatherArmorMeta meta = (LeatherArmorMeta) item.getItemMeta();
                        meta.setColor(color);
                        item.setItemMeta(meta);
                    }
                  
                }
                    catch (Exception e) {
                      
                    }
                }
          
          
          
            return a;
        }
      
        public void createInv() {
            inv = Bukkit.createInventory(null, 9,ChatColor.AQUA + "" + ChatColor.BOLD + "Select Team");
          
            ItemStack item = new ItemStack(Material.BLUE_CONCRETE);
            ItemMeta meta = item.getItemMeta();
          
            meta.setDisplayName(ChatColor.DARK_BLUE + "BLUE TEAM");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Click to join this team");
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.setItem(0, item);
          
            item.setType(Material.RED_CONCRETE);
            meta.setDisplayName(ChatColor.DARK_RED + "RED TEAM");
            item.setItemMeta(meta);
            inv.setItem(1, item);
          
            item.setType(Material.GREEN_CONCRETE);
            meta.setDisplayName(ChatColor.DARK_GREEN + "GREEN TEAM");
            item.setItemMeta(meta);
            inv.setItem(2, item);
          
            item.setType(Material.PURPLE_CONCRETE);
            meta.setDisplayName(ChatColor.DARK_PURPLE + "PURPLE TEAM");
            item.setItemMeta(meta);
            inv.setItem(3, item);
          
            item.setType(Material.ORANGE_CONCRETE);
            meta.setDisplayName(ChatColor.GOLD + "ORANGE TEAM");
            item.setItemMeta(meta);
            inv.setItem(4, item);
          
            item.setType(Material.LIME_CONCRETE);
            meta.setDisplayName(ChatColor.GREEN + "LIME TEAM");
            item.setItemMeta(meta);
            inv.setItem(5, item);
          
            item.setType(Material.CYAN_CONCRETE);
            meta.setDisplayName(ChatColor.BLUE + "CYAN TEAM");
            item.setItemMeta(meta);
            inv.setItem(6, item);
          
            item.setType(Material.BARRIER);
            meta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Close menu");
            lore.clear();
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.setItem(8, item);
          
        }
    }
    [B]
    
    Here is the error code when you type in the command
    [/B]
    Code:
    org.bukkit.command.CommandException: Unhandled exception executing command 'changeteam' in plugin TeamPicker v1.0
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:47) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at org.bukkit.craftbukkit.v1_16_R2.CraftServer.dispatchCommand(CraftServer.java:756) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.PlayerConnection.handleCommand(PlayerConnection.java:1697) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.PlayerConnection.a(PlayerConnection.java:1540) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.PacketPlayInChat.a(PacketPlayInChat.java:47) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.PacketPlayInChat.a(PacketPlayInChat.java:1) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.PlayerConnectionUtils.lambda$0(PlayerConnectionUtils.java:19) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.TickTask.run(SourceFile:18) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeTask(SourceFile:144) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.IAsyncTaskHandlerReentrant.executeTask(SourceFile:23) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.executeNext(SourceFile:118) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.MinecraftServer.ba(MinecraftServer.java:941) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.MinecraftServer.executeNext(MinecraftServer.java:934) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.IAsyncTaskHandler.awaitTasks(SourceFile:127) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.MinecraftServer.sleepForTick(MinecraftServer.java:918) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.MinecraftServer.w(MinecraftServer.java:850) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at net.minecraft.server.v1_16_R2.MinecraftServer.lambda$0(MinecraftServer.java:164) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at java.lang.Thread.run(Thread.java:832) [?:?]
    Caused by: java.lang.NullPointerException
            at org.bukkit.craftbukkit.v1_16_R2.inventory.CraftContainer.getNotchInventoryType(CraftContainer.java:125) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at org.bukkit.craftbukkit.v1_16_R2.entity.CraftHumanEntity.openInventory(CraftHumanEntity.java:298) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            at com.trobanko.teampicker.Main.onCommand(Main.java:43) ~[?:?]
            at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[spigot.jar:git-Spigot-b5a13e6-a939fbd]
            ... 18 more
    Line 43 is:
    Code:
     player.openInventory(inv);
    
     
  2. Online

    timtower Moderator Moderator

  3. Offline

    xelatercero

    the inventory is null, where do you call the #createInv() method?
     

Share This Page