ArrayList coming up empty?

Discussion in 'Plugin Development' started by ExtremeOrphan, Sep 19, 2019.

Thread Status:
Not open for further replies.
  1. Offline

    ExtremeOrphan

    Hello, I have created a plugin for assigned "callsigns" to players, storing their name and associated callsign in an ArrayList. Players and their callsigns are added to the ArrayList in my command ( /callsign assign <player> <callsign> ) and players callsigns can be obtained (for player knowledge) using ( /callsign get <player>) which returns the player's callsign, and will return "[]" if there is no assigned callsign.

    The problem I am encountering is, when I check the ArrayList from a different command ( /911broadcast <message>) it claims the player is not in the ArrayList, however when I mirror the same code for obtaining the callsign in ( /callsign get <player> ) it works perfectly fine.

    With further testing any subcommand (defined by cmd-arguments) of the /callsign command the list returns the player and their callsign perfectly fine, but when I use a different base command /911b (in the same class) it does not work.

    Very confused, all help is greatly appreciated.

    All relevant code below.

    (CommunicationCommands.class) Setting up ArrayList and methods to return player's callsign and name, depending on situation:

    Code:
    List<CallsignManagement> list = new ArrayList<CallsignManagement>();
    
        public String getCallsign(String player_name) {
                
            for (CallsignManagement w : list) {
                
                if (w.getName().equalsIgnoreCase(player_name)) {
                    
                    return w.getCallsign();
                }
            }
            return ChatColor.DARK_RED + "[]";
        }
    
        public CallsignManagement getIfAssignedCallsign(String player_name) {
        
            for (CallsignManagement m : list) {
            
                if (m.getName().equalsIgnoreCase(player_name));
    
                return m;
    
            }
    
            return null;
    
        }
    
        public List<CallsignManagement> getCallsignArray() {
        
            return list;
        
        }
    
    (CallsignManagement.class) Class with getters and setters which are used to set up the methods to return callsign and player's name in code chunk above:

    Code:
    package com.jeremyrpeters.vcpoliceessentials;
    
    public class CallsignManagement {
      
        private String player_name;
        private String callsign;
    
        public CallsignManagement(String player_name, String charges) {
          
          
            this.player_name = player_name;
            this.callsign = charges;
      
        }
    
        public String getName() {
    
            return this.player_name;
      
        }
      
        public String getCallsign() {
    
            return this.callsign;
      
        }
    
        public void setName(String new_name) {
          
            this.player_name = new_name;
      
        }
    }
    (CommunicationCommands.class) Callsign command with "assign" subcommand (Players are added to the ArrayList here):

    Code:
     
    @Override
        public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
        
            if(cmd.getName().equalsIgnoreCase("callsign")) {
            
                Player p = (Player) sender;
            
                if (args.length >= 1) {
                
                    if (args[0].equalsIgnoreCase("assign")) {
                    
                        Player target = Bukkit.getPlayerExact(args[1]);
                    
                        if (p.hasPermission("policeessentials.callsign.control")) {
                        
                            if (args.length == 3 ) {
                            
                                if (Bukkit.getOnlinePlayers().contains(target)) {
                                
                                    if (target.hasPermission("policeessentials.callsign.ispolice")) {
                                
                                        if (!(list.contains(getIfAssignedCallsign(target.getName())))) {
                                    
                                            String callsign = args[2];
                                    
                                            CallsignManagement cs = new CallsignManagement(target.getName(), callsign);
    
                                            list.add(cs);
                                    
                                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Prefixs.Callsigns"))
                                                + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Callsigns.Messages.Given Callsign")
                                                    
                                                .replace("%player%", target.getPlayer().getName())
                                                .replace("%callsign%", callsign)));
    
                                            target.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Prefixs.Callsigns"))
                                                + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Callsigns.Messages.Recieved Callsign")
                                            
                                                .replace("%player%", p.getName())
                                                .replace("%callsign%", callsign)));
    
    
    (CommunicationCommands.class) Callsign command with "get" subcommand (Returns in chat the player's callsign if it is present in the ArrayList), this command SUCCESSFULLY returns the callsign:

    Code:
                    if (args[0].equalsIgnoreCase("get")) {
                    
                        Player target = Bukkit.getPlayerExact(args[1]);
                    
                        if (p.hasPermission("policeessentials.callsign.control")) {
                    
                            if (args.length == 2 ) {
                            
                                if (Bukkit.getOnlinePlayers().contains(target)) {
                                
                                    if (target.hasPermission("policeessentials.callsign.ispolice")) {
                                
                                        if (list.contains(getIfAssignedCallsign(target.getName()))) {
                                        
                                            String callsign = getCallsign(target.getName());
                                        
                                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Prefixs.Callsigns"))
                                                + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Callsigns.Messages.Get Callsign")
                                                                
                                                .replace("%player%", target.getPlayer().getName())
                                                .replace("%callsign%", callsign)));
                                        }
                                    
                                        else {
                                        
                                            p.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Prefixs.Callsigns"))
                                                + ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Callsigns.Messages.Player Has No Callsign")
                                                            
                                                .replace("%player%", target.getPlayer().getName())));
    
    (CommunicationCommands.class) 911Broadcast command, which will broadcast a message to everyone with a permission, and the command should return the callsign, if its assigned in the ArrayList, or "[]", however ALWAYS even if callsign is set and verified through ( /callsign get <player> ):

    Code:
             if(cmd.getName().equalsIgnoreCase("911broadcast") || cmd.getName().equalsIgnoreCase("911b")) {
            
                Player p = (Player) sender;
            
                if (p.hasPermission("policeessentials.911broadcast.use")) {
    
                    if (args.length > 0) {
                                
                        String message = "";
                
                        StringBuilder sb = new StringBuilder();
                
                        for (int i = 0; i < args.length; i++){
                    
                            sb.append(args[i]).append(" ");
                
                        }
                 
                        message = sb.toString().trim();
                                
                        String message_refined = ChatColor.stripColor(ChatColor.translateAlternateColorCodes('&', message));
                    
                        String callsign = this.getCallsign(p.getName());
                    
                        for (Player leo : Bukkit.getOnlinePlayers()) {
                    
                            if (leo.hasPermission("policeessentials.911broadcast.view")) {
                        
                                leo.sendMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Police Radio.Format")
                            
                                        .replace("%sender%", p.getName())
                                        .replace("%message%", message_refined)
                                        .replace("%callsign%", callsign)));
    
     
  2. Online

    timtower Administrator Administrator Moderator

    @ExtremeOrphan Please post the code where you register these classes and CommandExecutors
     
  3. Offline

    ExtremeOrphan

    Code:
    package com.jeremyrpeters.vcpoliceessentials;
    
    import java.util.HashSet;
    
    import org.bukkit.configuration.file.FileConfiguration;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    
    public class Main extends JavaPlugin {
    
        FileConfiguration config = this.getConfig();
       
        public static HashSet<String> wanted_list = new HashSet<String>();
       
        @Override
        public void onEnable() {
    
            PluginManager pm = getServer().getPluginManager();
           
            pm.registerEvents(new WantedListEventListener(this), this);
           
            getCommand("wanted").setExecutor(new WantedCommand(this));
            getCommand("callsign").setExecutor(new CommunicationCommands(this));
            getCommand("911b").setExecutor(new CommunicationCommands(this));
            getCommand("911broadcast").setExecutor(new CommunicationCommands(this));
            getCommand("csdebug").setExecutor(new CommunicationCommands(this));
           
            saveDefaultConfig();
           
        }
       
        @Override
        public void onDisable() {
           
            Main.wanted_list.clear();
           
        }
    }
    
     
  4. Online

    timtower Administrator Administrator Moderator

    @ExtremeOrphan You have 4 instances of the same class.
    Make it 1 instance.
     
Thread Status:
Not open for further replies.

Share This Page