Solved ArrayList not storing data

Discussion in 'Plugin Development' started by mike546378, Jul 24, 2013.

Thread Status:
Not open for further replies.
  1. so part of my plugin includes a halt feature, type /halt <player> and it freezes the player in place, /haltlist shows frozen players and /unhalt to let them move again. I store the players names in an arraylist and when a player moves their name is checked against this list to see if they are halted. It was all working until I decided to clean up the code and make new classes, move pieces of code around the place etc, now the name dosent seem to be storing in the array correctly.
    code (open)

    Code:java
    1. private ArrayList<String> halted = new ArrayList<String>();
    2.  
    3. public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
    4.  
    5. //On use of the /HaltList
    6. if(cmd.getName().equalsIgnoreCase("Haltlist")) {
    7. if(sender.hasPermission("ftbhelper.halt.list")){
    8. sender.sendMessage(ChatColor.BLUE+"-------------------------------------------------");
    9. sender.sendMessage(ChatColor.BLUE+"-----------"+ChatColor.AQUA+ "FTBHelper V" + plugin.getConfig().getString("version")+ " - By mike546378"+ChatColor.BLUE+"-----------");
    10. sender.sendMessage(ChatColor.BLUE+"-------------------------------------------------");
    11. sender.sendMessage(ChatColor.DARK_GREEN+"Halted Players:");
    12. //For Debugging
    13. if(halted.isEmpty()){
    14. sender.sendMessage("Array is Empty...");
    15. }
    16.  
    17. if(!halted.isEmpty()){
    18. for(int x = 0; x <= halted.size() && x >= 0; x++){
    19. sender.sendMessage(ChatColor.RED+halted.get(x));
    20. }
    21. }
    22. }else{
    23. sender.sendMessage(ChatColor.DARK_RED+"You do not have sufficent permissions to perform this action.");
    24. }
    25. return true;
    26. }
    27. //On use of the /halt <player>
    28. if(cmd.getName().equalsIgnoreCase("Halt")){
    29. if(sender.hasPermission("ftbhelper.halt.halt")){
    30. if(args.length == 1){
    31. sender.sendMessage(ChatColor.RED + "Size: " + halted.size());
    32. halted.add("CraftPlayer{name="+args[0]+"}");
    33. sender.sendMessage(ChatColor.GREEN+args[0]+" has now been halted.");
    34. sender.sendMessage(ChatColor.RED + "Size: " + halted.size());
    35. sender.sendMessage(halted.get(halted.size() - 1));
    36. }else{
    37. sender.sendMessage(ChatColor.BLUE+"-------------------------------------------------");
    38. sender.sendMessage(ChatColor.BLUE+"-----------"+ChatColor.AQUA+ "FTBHelper "+plugin.getConfig().getString("version") + " - By mike546378"+ChatColor.BLUE+"-----------");
    39. sender.sendMessage(ChatColor.BLUE+"-------------------------------------------------");
    40. sender.sendMessage(ChatColor.GOLD+"Usage:"+ChatColor.WHITE+" /Halt <player>");
    41. }
    42. }else{
    43. sender.sendMessage(ChatColor.DARK_RED+"You do not have sufficent permissions to perform this action.");
    44. }
    45. return true;
    46. }



    When you type /halt <playername> it says the current size of the array, says player has been halted then says the old size + 1 then the name you just entered implying that the array is storing data however when you type /haltlist it says Array is empty implying that no data was stored and no names come up.. any help is appreciated
     
  2. Offline

    collielimabean

    mike546378

    I just reproduced your code, and I'm willing to bet it has something to do with your CommandExecutor implementation (I'm assuming you used a CommandExecutor). The problem is, when you getCommand().setExecutor(new Executor()), you are creating new instances of the CommandExecutor. Because you have different commands, you need to change a few things to enable communication between different commands:

    You need to make the ArrayList<String> a public static object, because it belongs to the class, not an instance. By making the list static, you guarantee that only one instance of the player list is generated.

    EDIT: I just tested my suggested change, and it works now.
     
  3. collielimabean Thanks, working now. Getting some errors w/ my Listeners but I'll try sorting them out on my own :)
     
Thread Status:
Not open for further replies.

Share This Page