Solved java.lang.ArrayIndexOutOfBoundsException: 9

Discussion in 'Plugin Development' started by samTB, Jun 4, 2021.

  1. Offline

    samTB

    i were writing a minecraft plugin with leather armor and gui and i have this prob
    here is the error:
    Code:
    [Server thread/ERROR]: Error occurred while enabling firstgui v1.0 (Is it up to date?)
    java.lang.ArrayIndexOutOfBoundsException: 9
            at java.util.Arrays$ArrayList.set(Unknown Source) ~[?:1.8.0_51]
            at net.minecraft.server.v1_16_R3.NonNullList.set(SourceFile:53) ~[spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.inventory.CraftInventoryCustom$MinecraftInventory.setItem(CraftInventoryCustom.java:108) ~[spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.inventory.CraftInventory.setItem(CraftInventory.java:99) ~[spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at me.SamTB.FGUI.Main.createinv(Main.java:160) ~[?:?]
            at me.SamTB.FGUI.Main.onEnable(Main.java:30) ~[?:?]
            at org.bukkit.plugin.java.JavaPlugin.setEnabled(JavaPlugin.java:263) ~[spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.plugin.java.JavaPluginLoader.enablePlugin(JavaPluginLoader.java:351) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.plugin.SimplePluginManager.enablePlugin(SimplePluginManager.java:480) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugin(CraftServer.java:494) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.enablePlugins(CraftServer.java:408) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.reload(CraftServer.java:881) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.Bukkit.reload(Bukkit.java:651) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.command.defaults.ReloadCommand.execute(ReloadCommand.java:27) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.command.SimpleCommandMap.dispatch(SimpleCommandMap.java:149) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchCommand(CraftServer.java:763) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at org.bukkit.craftbukkit.v1_16_R3.CraftServer.dispatchServerCommand(CraftServer.java:748) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.DedicatedServer.handleCommandQueue(DedicatedServer.java:387) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.DedicatedServer.b(DedicatedServer.java:356) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.a(MinecraftServer.java:1008) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.w(MinecraftServer.java:847) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer.lambda$0(MinecraftServer.java:164) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
            at net.minecraft.server.v1_16_R3.MinecraftServer$$Lambda$3223/1515971318.run(Unknown Source) [spigot.jar:2991-Spigot-018b9a0-f3f3094]
    and the code i wrote:
    Code:
    package me.SamTB.FGUI;
    
    
    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);
            createinv();
        }
    
        @Override
        public void onDisable() {
    
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("changeteam") || label.equalsIgnoreCase("cht")) {
                if (!(sender instanceof Player)) {
                    //console
                    sender.sendMessage("You cannot do this!");
                    return true;
                }
                //player
                Player p = (Player) sender;
                //open gui
                p.openInventory(inv);
             
             
            }
            return false;
        }
     
        @EventHandler()
        public void onClick(InventoryClickEvent e) {
            if (!e.getInventory().equals(inv)) return;
            if (e.getCurrentItem() == null) return;
            if (e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName() == null) return;
            e.setCancelled(true);
            Player p = (Player) e.getWhoClicked();
            if (e.getSlot() == 0) {
                //blue team
                ItemStack[] armor = p.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.BLUE);
                p.getEquipment().setArmorContents(armor);
                p.sendMessage(ChatColor.GOLD + "You changed your team");
            }
            if (e.getSlot() == 1) {
                //red team
                ItemStack[] armor = p.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.RED);
                p.getEquipment().setArmorContents(armor);
                p.sendMessage(ChatColor.GOLD + "You changed your team");
            }
            if (e.getSlot() == 2) {
                //yellow team
                ItemStack[] armor = p.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.YELLOW);
                p.getEquipment().setArmorContents(armor);
                p.sendMessage(ChatColor.GOLD + "You changed your team");
            }
            if (e.getSlot() == 3) {
                //green team
                ItemStack[] armor = p.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.GREEN);
                p.getEquipment().setArmorContents(armor);
                p.sendMessage(ChatColor.GOLD + "You changed your team");
            }
            if (e.getSlot() == 9) {
                //go back
                p.closeInventory();
                p.sendMessage(ChatColor.YELLOW + "[!]" + ChatColor.GOLD +
                        "You closed the team menu!");
            }
            return;
        }
     
        public ItemStack[] changeColor(ItemStack[] a, Color color) {
         
            for (ItemStack item : a) {
                try {
                    if (item.getType() == Material.LEATHER_BOOTS ||
                            item.getType() == Material.LEATHER_LEGGINGS ||
                            item.getType() == Material.LEATHER_CHESTPLATE ||
                            item.getType() == Material.LEATHER_HELMET) {
                                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.GOLD + "" +
            ChatColor.ITALIC + "Select team");
         
            ItemStack item = new ItemStack(Material.BLUE_WOOL);
            ItemMeta meta = item.getItemMeta();
         
            //blue team
            meta.setDisplayName(ChatColor.BLUE + "Blue team");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Click tp join team!");
         
            item.setItemMeta(meta);
            inv.setItem(0, item);
         
            //red team
            item.setType(Material.RED_WOOL);
            meta.setDisplayName(ChatColor.RED + "Red team");
            item.setItemMeta(meta);
            inv.setItem(1, item);
         
            //yellow team
            item.setType(Material.YELLOW_WOOL);
            meta.setDisplayName(ChatColor.YELLOW + "Yellow team");
            item.setItemMeta(meta);
            inv.setItem(2, item);
    
            //green team
            item.setType(Material.GREEN_WOOL);
            meta.setDisplayName(ChatColor.GREEN + "Green team");
            item.setItemMeta(meta);
            inv.setItem(3, item);
    
            //Go back
            item.setType(Material.BARRIER);
            meta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Go Back");
            lore.clear();
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.setItem(9, item);
        }
    }
    
    
    
    
    
    
    
    
    
    package me.SamTB.FGUI;
    
    
    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);
            createinv();
        }
    
        @Override
        public void onDisable() {
    
        }
    
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
            if (label.equalsIgnoreCase("changeteam") || label.equalsIgnoreCase("cht")) {
                if (!(sender instanceof Player)) {
                    //console
                    sender.sendMessage("You cannot do this!");
                    return true;
                }
                //player
                Player p = (Player) sender;
                //open gui
                p.openInventory(inv);
             
             
            }
            return false;
        }
     
        @EventHandler()
        public void onClick(InventoryClickEvent e) {
            if (!e.getInventory().equals(inv)) return;
            if (e.getCurrentItem() == null) return;
            if (e.getCurrentItem().getItemMeta() == null) return;
            if (e.getCurrentItem().getItemMeta().getDisplayName() == null) return;
            e.setCancelled(true);
            Player p = (Player) e.getWhoClicked();
            if (e.getSlot() == 0) {
                //blue team
                ItemStack[] armor = p.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.BLUE);
                p.getEquipment().setArmorContents(armor);
                p.sendMessage(ChatColor.GOLD + "You changed your team");
            }
            if (e.getSlot() == 1) {
                //red team
                ItemStack[] armor = p.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.RED);
                p.getEquipment().setArmorContents(armor);
                p.sendMessage(ChatColor.GOLD + "You changed your team");
            }
            if (e.getSlot() == 2) {
                //yellow team
                ItemStack[] armor = p.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.YELLOW);
                p.getEquipment().setArmorContents(armor);
                p.sendMessage(ChatColor.GOLD + "You changed your team");
            }
            if (e.getSlot() == 3) {
                //green team
                ItemStack[] armor = p.getEquipment().getArmorContents();
                armor = changeColor(armor, Color.GREEN);
                p.getEquipment().setArmorContents(armor);
                p.sendMessage(ChatColor.GOLD + "You changed your team");
            }
            if (e.getSlot() == 9) {
                //go back
                p.closeInventory();
                p.sendMessage(ChatColor.YELLOW + "[!]" + ChatColor.GOLD +
                        "You closed the team menu!");
            }
            return;
        }
     
        public ItemStack[] changeColor(ItemStack[] a, Color color) {
         
            for (ItemStack item : a) {
                try {
                    if (item.getType() == Material.LEATHER_BOOTS ||
                            item.getType() == Material.LEATHER_LEGGINGS ||
                            item.getType() == Material.LEATHER_CHESTPLATE ||
                            item.getType() == Material.LEATHER_HELMET) {
                                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.GOLD + "" +
            ChatColor.ITALIC + "Select team");
         
            ItemStack item = new ItemStack(Material.BLUE_WOOL);
            ItemMeta meta = item.getItemMeta();
         
            //blue team
            meta.setDisplayName(ChatColor.BLUE + "Blue team");
            List<String> lore = new ArrayList<String>();
            lore.add(ChatColor.GRAY + "Click tp join team!");
         
            item.setItemMeta(meta);
            inv.setItem(0, item);
         
            //red team
            item.setType(Material.RED_WOOL);
            meta.setDisplayName(ChatColor.RED + "Red team");
            item.setItemMeta(meta);
            inv.setItem(1, item);
         
            //yellow team
            item.setType(Material.YELLOW_WOOL);
            meta.setDisplayName(ChatColor.YELLOW + "Yellow team");
            item.setItemMeta(meta);
            inv.setItem(2, item);
    
            //green team
            item.setType(Material.GREEN_WOOL);
            meta.setDisplayName(ChatColor.GREEN + "Green team");
            item.setItemMeta(meta);
            inv.setItem(3, item);
    
            //Go back
            item.setType(Material.BARRIER);
            meta.setDisplayName(ChatColor.RED + "" + ChatColor.BOLD + "Go Back");
            lore.clear();
            meta.setLore(lore);
            item.setItemMeta(meta);
            inv.setItem(9, item);
        }
    }
    and there is nothing with yml. soo...
    helppppppppp me plls
     
    Last edited: Jun 5, 2021
  2. Online

    timtower Administrator Administrator Moderator

    @samTB There is no item 9, max is 8
     
    davidclue likes this.
  3. Offline

    samTB

    thx
     

Share This Page