Need help with ArrayList

Discussion in 'Plugin Development' started by Azuleee_, Jan 12, 2020.

  1. Online

    Azuleee_

    So i'm trying to make a coins plugin, and i'm trying to make the money top in gui and i'm getting this with arraylist, and i need some help ;-;

    This is the error:
    Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
    at java.util.ArrayList.rangeCheck(Unknown Source) ~[?:1.8.0_191]
    at java.util.ArrayList.get(Unknown Source) ~[?:1.8.0_191]
    at net.azulecash.API.CashTop(API.java:62) ~[?:?]

    My code:
    public static List<String> cash = new ArrayList();
    @SuppressWarnings("unchecked")
    public static void CashTop(Player p)
    {
    cash.clear();
    for (String name : Main.getInstance().data.getConfig().getConfigurationSection("Data").getKeys(false)) {
    int quantiaCash = Main.getInstance().data.getInt("Data." + name + ".Coins");;
    String prefix = PermissionsEx.getUser(name).getPrefix().replace("&", "§");
    cash.add(name + ": " + quantiaCash + " §6✪§f de Cash.");

    Collections.sort(cash, new Comparator()
    {
    public int compare(Object arg0, Object arg1)
    {
    int one = Integer.parseInt(((String) arg0).split(" ")[1]);
    int two = Integer.parseInt(((String) arg1).split(" ")[1]) + 1;
    return Integer.compare(one, two);
    }
    });
    Collections.reverse(cash);
    Inventory inv = Bukkit.createInventory(null, 54, "Cash - TOP");

    inv.setItem(11, Criar.add2("§6#§f1§8 -§e " + ((String)cash.get(0)).replace(":", " §fpossui§6" + ""), "donotfight", new String[] { "§6■§e Este jogador(a) está em primeiro lugar!"}, 1));
    inv.setItem(12, Criar.add2("" + ((String)cash.get(1)).replace(":", " §fpossui§6" + ""), "donotfight", new String[] { "§6■§e Este jogador(a) está em terceiro lugar!"}, 1));
    p.openInventory(inv);
    }
    }
    My english is awful so sorry if there's any orthographic error xd
     
  2. Offline

    Strahan

    That's.. an interesting way to do it. Personally, I'd read them into a Map then work against that. Putting Strings in your list and having to split them is pretty awkward.

    Also you should not be embedding the color character anywhere. ChatColor exists for a reason; use it. Like instead of:
    Code:
    String prefix = PermissionsEx.getUser(name).getPrefix().replace("&", "§");
    do
    Code:
    String prefix = ChatColor.translateAlternateColorCodes('&', PermissionsEx.getUser(name).getPrefix());
    ...though I'd also be doing a validation on the return of getUser before hanging methods off of it.

    Also other places you embed things, like:
    Code:
    ash.add(name + ": " + quantiaCash + " §6✪§f de Cash.");
    You should be doing
    Code:
    ash.add(name + ": " + quantiaCash + ChatColor.GOLD + "✪" + ChatColor.RESET + " de Cash.");
     
  3. Online

    Azuleee_

    Thank you... but how can i fix the error?

    I fixed it! close this pls :D

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jan 12, 2020

Share This Page