Error with getPrimaryGroup function

Discussion in 'Spigot Plugin Development' started by 42Jamie42, Apr 14, 2017.

  1. I have made a prison plugin, that needs to get the prison rank(Using PEX groups) and the primary groups. Now apparently when a player has more than 1 group there is no primaryGroup anymore. So i made my own function to find out what a player's highest primary group is. But it gives me an error.

    Error:
    Code (Text):
    [12:59:26] [Async Chat Thread - #13/ERROR]: Could not pass event AsyncPlayerChatEvent to Matrix-Prison v1.3.1
    org.bukkit.event.EventException
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:306) ~[spigot.jar:git-Spigot-f950f8e-622630c]
        at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.java:62) ~[spigot.jar:git-Spigot-f950f8e-622630c]
        at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.java:502) [spigot.jar:git-Spigot-f950f8e-622630c]
        at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.java:484) [spigot.jar:git-Spigot-f950f8e-622630c]
        at net.minecraft.server.v1_11_R1.PlayerConnection.chat(PlayerConnection.java:1257) [spigot.jar:git-Spigot-f950f8e-622630c]
        at net.minecraft.server.v1_11_R1.PlayerConnection.a(PlayerConnection.java:1195) [spigot.jar:git-Spigot-f950f8e-622630c]
        at net.minecraft.server.v1_11_R1.PacketPlayInChat$1.run(PacketPlayInChat.java:39) [spigot.jar:git-Spigot-f950f8e-622630c]
        at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) [?:1.8.0_121]
        at java.util.concurrent.FutureTask.run(Unknown Source) [?:1.8.0_121]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) [?:1.8.0_121]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) [?:1.8.0_121]
        at java.lang.Thread.run(Unknown Source) [?:1.8.0_121]
    Caused by: java.lang.ArrayIndexOutOfBoundsException: 4
        at nl.matrixgaming.java.prison.data.GroupData.getPrimaryGroup(GroupData.java:19) ~[?:?]
        at nl.matrixgaming.java.prison.listener.ChatListener.onPlayerChat(ChatListener.java:17) ~[?:?]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_121]
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.8.0_121]
        at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.8.0_121]
        at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.java:302) ~[spigot.jar:git-Spigot-f950f8e-622630c]
        ... 11 more

    Groupdata:
    Code (Text):
    package nl.matrixgaming.java.prison.data;

    import java.util.List;

    import org.bukkit.entity.Player;

    import nl.matrixgaming.java.prison.Main;

    public class GroupData {
        public static String getPrimaryGroup(Player player){
            String group = null;
            String[] groups = Main.permission.getPlayerGroups(player);
            List<String> primaryGroups = Main.settings.getConfig().getStringList("General.PrimaryGroups");
            for(int i = 0; i < groups.length; i++){
                for(int ip = 1; i < primaryGroups.size(); ip++){
                    if(primaryGroups.
                            get(ip).
                            equalsIgnoreCase(
                                    groups[i])){
                        group = groups[i];
                        i = groups.length;
                    }
                }
            }  
         
            return group;
        }
    }
     
    ChatListener:
    Code (Text):
    package nl.matrixgaming.java.prison.listener;

    import org.bukkit.ChatColor;
    import org.bukkit.event.EventHandler;
    import org.bukkit.event.Listener;
    import org.bukkit.event.player.AsyncPlayerChatEvent;

    import nl.matrixgaming.java.prison.Main;
    import nl.matrixgaming.java.prison.data.GroupData;
    import nl.matrixgaming.java.prison.data.PrestigeData;
    import nl.matrixgaming.java.prison.data.RankData;

    public class ChatListener implements Listener {

        @EventHandler
        public void onPlayerChat(AsyncPlayerChatEvent e){
            String format = Main.settings.getConfig().getString("Chat.Formats.Groups." + GroupData.getPrimaryGroup(e.getPlayer()));
            format = format.replaceAll("%prison_prestige%", PrestigeData.GetPrestige(e.getPlayer(), "current_prestige_formatted"));
            format = format.replaceAll("%prison_rank%", RankData.GetRank(e.getPlayer(), "current_rank_formatted"));
            format = format.replaceAll("%player_rank%", GroupData.getPrimaryGroup(e.getPlayer()));
            format = format.replaceAll("%player_name%", e.getPlayer().getName());
            format = format.replaceAll("%message%", e.getMessage());
            format = ChatColor.translateAlternateColorCodes('&', format);
         
            e.setFormat(format);
        }
    }
     

    Code (Text):
    General:
      Main:
        MainPrisonWorld: Prison
        MainPrisonMinesWorld: Prison
      PrimaryGroups:
      - Owner
      - Co-Owner
      - Manager
      - Admin
      - Mod
      - Builder
      - Obsidian
      - Emerald
      - Diamond
      - Gold
      - Lapiz
      - Iron
      - Stone

    Anyone knows my problem?
     
  2. Anyone knows my problem? Really cant find out?
     
  3. ArrayIndexOutOfBoundsException occurs in the event your code requests an index from an array which does not contain any value. Before you call for an array index (e.g array[0]) you need you verify that the array is not empty and that it contains the number of values you intend to scan.

    For example:

    String[] args (from a command executor)

    If the command that was sent is /tp without any extra arguments, then as soon as you request args[0] you'll get this exception. That's because args[0] does not contain any values :p

    I recommend checking args.length as that gives an integer you can easily work with.
     
  4. Code (Text):
    for(int i = 0; i < groups.length; i++){
    I do right? In this for loop? Maybe i should do
    Code (Text):
    groups.lenght - 1
    cause an array starts with 0 and the length starts with 1?
     
  5. I honestly don't use any method as complicated as that. You can for loop through it and it will stop, or at least for me it does, when it runs out of things to loop through. I simply add the verification code before the loop to make sure it's not empty and that works. However, I don't like telling programmers to use a definite method so I recommend testing to see what method works best for what you're doing :)
     
  6. What do you recommend me or what would you do in the situation Vault's getPrimaryGroup returns null? I'm currently trying to fetch the (What on my server) would be the default group from a list of groups that can be the default group.
     
  7. Never worked with the Vault API before, although from what you've said I would probably make a group in your plugin that is called in the event of error, which has no permissions. Then call an error in the console to tell the administrator to fix their Vault plugin.
     
  8. Thats not my problem, its that i need 3 groups per player(PrisonPrestige0, PrisonRankA, Stone) and PEX doesn't has a primary group anymore when there is more than 1 group. So i need to get the primary group myself(Obviously the players rank[Players get an other rank when donated]).
     
  9. Found solution
    Code (Text):
    package nl.matrixgaming.java.prison.data;

    import java.util.List;

    import org.bukkit.Bukkit;
    import org.bukkit.entity.Player;

    import nl.matrixgaming.java.prison.Main;

    public class GroupData {
        public static String getPrimaryGroup(Player player){
            String group = null;
            String[] groups = Main.permission.getPlayerGroups(player);
            List<String> primaryGroups = Main.settings.getConfig().getStringList("General.PrimaryGroups");
            for(int i = 1; i < groups.length; i++){
                for(int ip = 1; ip < primaryGroups.size(); ip++){
                    if(primaryGroups
                            .
                            get(ip-1)
                            .
                            equalsIgnoreCase(
                                    groups[i-1])){
                        group = groups[i-1];
                        i = groups.length;
                        ip = primaryGroups.size();
                    }
                }
            }  
           
            return group;
        }
    }