Help with Strings and for

Discussion in 'Plugin Development' started by thedjtrollin, Feb 21, 2014.

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

    thedjtrollin

    I've been looking at this code for about a hour trying to figure out what is actually wrong and I cannot figure it out;
    Code:java
    1. package me.milkybarstoner.vitalityapi;
    2.  
    3. import org.bukkit.Bukkit;
    4. import org.bukkit.ChatColor;
    5. import org.bukkit.command.Command;
    6. import org.bukkit.command.CommandExecutor;
    7. import org.bukkit.command.CommandSender;
    8. import org.bukkit.entity.Player;
    9. import org.bukkit.event.EventHandler;
    10. import org.bukkit.event.Listener;
    11.  
    12. import java.util.ArrayList;
    13. import java.util.HashMap;
    14. import java.util.List;
    15.  
    16. /**
    17. * Created by harry_000 on 22/02/14.
    18. */
    19.  
    20. public class MessageManagment implements CommandExecutor, Listener {
    21.  
    22.  
    23. public List<String> pmDisable = new ArrayList<String>();
    24. public List<String> PMSpy = new ArrayList<String>();
    25.  
    26.  
    27. @Override
    28. @EventHandler
    29. public boolean onCommand(CommandSender Sender, Command cmd, String label, String[] args) {
    30. Player player = (Player) Sender;
    31. if(cmd.getName().equalsIgnoreCase("pm")){
    32. if(player.hasPermission("vitality.pm")) {
    33. if(args.length >= 2){
    34. if(player.getServer().getPlayer(args[0]) != null) {
    35. Player reciever = player.getServer().getPlayer(args[0]);
    36. String mensagem = "";
    37. for (int i = 1; i < args.length; i++){
    38. mensagem = mensagem + " " + args[i];
    39. }
    40. reciever.sendMessage(ChatColor.GOLD + "PM > " + ChatColor.AQUA + player.getDisplayName() + ChatColor.GREEN + " ->" + ChatColor.YELLOW + mensagem);
    41. player.sendMessage(ChatColor.GOLD + "PM > " + ChatColor.AQUA + "You -> " + reciever.getDisplayName() + ChatColor.GREEN + " ->" + ChatColor.YELLOW + mensagem);
    42.  
    43. for(Player onlinePlayers : Bukkit.getOnlinePlayers()){
    44. if(PMSpy.contains(onlinePlayers.getName())) {
    45. onlinePlayers.sendMessage(ChatColor.GOLD + "PM Spy > " + ChatColor.AQUA + player.getDisplayName() + ChatColor.GREEN + " ->" + ChatColor.YELLOW + mensagem);
    46. }
    47.  
    48.  
    49. }
    50.  
    51.  
    52.  
    53.  
    54. } else {
    55. player.sendMessage(Main.prefix + ChatColor.RED + "That player is not online!");
    56. }
    57.  
    58.  
    59. } else {
    60. player.sendMessage(Main.prefix + ChatColor.RED + "You have not specified a message!");
    61. }
    62. } else {
    63. player.sendMessage(Main.noPerm);
    64. }
    65.  
    66. }
    67. if(cmd.getName().equalsIgnoreCase("pmspy")) {
    68. if(player.hasPermission("vitality.pmspy")) {
    69. if(PMSpy.contains(player.getName())) {
    70. PMSpy.remove(player.getName());
    71. player.sendMessage(Main.prefix + "You are no longer Spying on PMs!");
    72. } else {
    73. PMSpy.add(player.getName());
    74. player.sendMessage(Main.prefix + "You are now spying on PMs!");
    75. }
    76.  
    77. } else {
    78. player.sendMessage(Main.noPerm);
    79. }
    80.  
    81.  
    82. }
    83. return false;
    84. }
    85.  
    86. }
    87. [/i]


    This is the bit that is not working;
    Code:java
    1.  
    2. for(Player onlinePlayers : Bukkit.getOnlinePlayers()){
    3. if(PMSpy.contains(onlinePlayers.getName())) {
    4. onlinePlayers.sendMessage(ChatColor.GOLD + "PM Spy > " + ChatColor.AQUA + player.getDisplayName() + ChatColor.GREEN + " ->" + ChatColor.YELLOW + mensagem);
    5. }



    Thanks for the help in advance!
     
  2. Offline

    Captain Dory

    What do you mean 'not working'? Post stack-traces or IDE errors.
     
  3. Offline

    alex123099

    thedjtrollin
    Which part isn't working exactly? What is the error you are getting?
     
  4. Offline

    thedjtrollin

    No errors but I know it's the
    Code:java
    1. if(PMSpy.contains(onlinePlayers.getName())) {
    2. onlinePlayers.sendMessage(ChatColor.GOLD + "PM Spy > " + ChatColor.AQUA + player.getDisplayName() + ChatColor.GREEN + " ->" + ChatColor.YELLOW + mensagem);
    3. }
     
  5. Offline

    Niknea

    thedjtrollin So that part doesn't executes? Have you tried adding debugging codes and checking if it stops there?
     
  6. Offline

    alex123099

    thedjtrollin
    Whenever something like that happens, I start printing out all the boolean expressions I am checking, so try printing out: PMSpy.contains(onlinePlayers.getName()) and see what it gives you
     
  7. Offline

    thedjtrollin

    It something to do with the String. When it doesn't do the if statement it works perfectly fine.

    For Example
    Code:
        for(Player onlinePlayers : Bukkit.getOnlinePlayers()){
                          onlinePlayers.sendMessage("This will work perfectly fine") ;
    }
    UPDATE: I did PMSpy.toString() and the output was empty. This doesn't make sense as the /PMSpy works fine and that is checking if you are in the List or not.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 6, 2016
Thread Status:
Not open for further replies.

Share This Page