What have I done wrong? VotifierListener

Discussion in 'Plugin Development' started by TheFluffyWalrus, Nov 6, 2014.

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

    TheFluffyWalrus

    Commands run as they should... Only problem is if someone votes it will say that players name and its like its stuck in a loop every other person who votes it will say the first person voted below i have attached the code... It will give the person the proper reward but it will also give the same reward to the first person who voted

    Code:java
    1. import java.io.File;
    2. import java.io.FileReader;
    3. import java.io.FileWriter;
    4. import java.util.Properties;
    5. import java.util.logging.Level;
    6. import java.util.logging.Logger;
    7.  
    8. import org.bukkit.Bukkit;
    9. import org.bukkit.entity.Player;
    10.  
    11. import com.vexsoftware.votifier.Votifier;
    12. import com.vexsoftware.votifier.model.Vote;
    13. import com.vexsoftware.votifier.model.VoteListener;
    14.  
    15. public class ThetaListener
    16. implements VoteListener
    17. {
    18. private static Logger logger = Logger.getLogger("ThetaListener");
    19. private String command_1 = "";
    20. private String command_2 = "";
    21. private String command_3 = "";
    22. private String command_4 = "";
    23. private String command_5 = "";
    24. private String playermessage = "";
    25. private String broadcastmessage = "";
    26.  
    27. public ThetaListener()
    28. {
    29. Properties props = new Properties();
    30. try
    31. {
    32. File configFile = new File("./plugins/Votifier/commands.ini");
    33. if (!configFile.exists())
    34. {
    35. configFile.createNewFile();
    36.  
    37.  
    38. props.load(new FileReader(configFile));
    39.  
    40.  
    41. props.setProperty("Command_1", this.command_1);
    42. props.setProperty("Command_2", this.command_2);
    43. props.setProperty("Command_3", this.command_3);
    44. props.setProperty("Command_4", this.command_4);
    45. props.setProperty("Command_5", this.command_5);
    46. props.setProperty("Player_Message", this.playermessage);
    47. props.setProperty("Broadcast_Message", this.broadcastmessage);
    48. props.store(new FileWriter(configFile), "ThetaListener Configuration | {user} = Player who voted | {site} = Site voted on | & for colors |");
    49. }
    50. else
    51. {
    52. props.load(new FileReader(configFile));
    53. }
    54. this.command_1 = props.getProperty("Command_1", this.command_1);
    55. this.command_2 = props.getProperty("Command_2", this.command_2);
    56. this.command_3 = props.getProperty("Command_3", this.command_3);
    57. this.command_4 = props.getProperty("Command_4", this.command_4);
    58. this.command_5 = props.getProperty("Command_5", this.command_5);
    59. this.playermessage = props.getProperty("Player_Message", this.playermessage);
    60. this.broadcastmessage = props.getProperty("Broadcast_Message", this.broadcastmessage);
    61. }
    62. catch (Exception ex)
    63. {
    64. logger.log(Level.WARNING, "Unable to load commands.ini");
    65. }
    66. }
    67.  
    68. public void voteMade(Vote vote)
    69. {
    70. String username = vote.getUsername();
    71. {
    72. try
    73. {
    74. this.broadcastmessage = this.broadcastmessage.replace('&', '§').replace("§§", "&").replace("{user}", username).replace("{site}", vote.getServiceName());
    75. Votifier.getInstance().getServer().broadcastMessage(this.broadcastmessage);
    76. }
    77. catch (Exception ex)
    78. {
    79. System.out.println("[Votifier] ThetaListener error: " + vote);
    80. }
    81. Player player = Bukkit.getServer().getPlayer(username);
    82. if (player != null)
    83. if (this.command_1 != "") {
    84. this.command_1 = this.command_1.replace('&', '§').replace("§§", "&").replace("{user}", username).replace("{site}", vote.getServiceName());
    85. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), this.command_1);
    86. }
    87. if (this.command_2 != "") {
    88. this.command_2 = this.command_2.replace('&', '§').replace("§§", "&").replace("{user}", username).replace("{site}", vote.getServiceName());
    89. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), this.command_2);
    90. }
    91. if (this.command_3 != "") {
    92. this.command_3 = this.command_3.replace('&', '§').replace("§§", "&").replace("{user}", username).replace("{site}", vote.getServiceName());
    93. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), this.command_3);
    94. }
    95. if (this.command_4 != "") {
    96. this.command_4 = this.command_4.replace('&', '§').replace("§§", "&").replace("{user}", username).replace("{site}", vote.getServiceName());
    97. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), this.command_4);
    98. }
    99. if (this.command_5 != "") {
    100. this.command_5 = this.command_5.replace('&', '§').replace("§§", "&").replace("{user}", username).replace("{site}", vote.getServiceName());
    101. Bukkit.getServer().dispatchCommand(Bukkit.getServer().getConsoleSender(), this.command_5);
    102. }
    103. if (this.playermessage != "") {
    104. this.playermessage = this.playermessage.replace('&', '§').replace("§§", "&").replace("{user}", username).replace("{site}", vote.getServiceName());
    105. player.sendMessage(this.playermessage);
    106. }
    107. }}}
    108.  
    109.  
    110.  
     
  2. Offline

    Watto

    TheFluffyWalrus

    Just as a note you shouldn't compare strings with != you should do .equals()
     
    TheFluffyWalrus likes this.
  3. Offline

    TheFluffyWalrus

    Watto
    changing that now I am still very new at this...
     
  4. Offline

    guitargun

    dont use
    1. private static Logger logger = Logger.getLogger("ThetaListener");
     
    TheFluffyWalrus likes this.
  5. Offline

    Watto

    TheFluffyWalrus

    Also your problem i believe is at line 74.
    Since you're replacing the {user} and {site} on the first player, so those variables no longer exist in the string.
    To fix that just set it as a new String rather than broadcastMessage.
     
    TheFluffyWalrus likes this.
  6. Offline

    TheFluffyWalrus

    Set a new string in what way?
     
  7. TheFluffyWalrus I'll try to demonstrate the point Watto made

    Okay, so you have your broadcastMessage as just "{user}". Then later on, once someone votes, you go to replace {user} with the name of whoever voted. Doing so creates a new String, as Strings are immutable. This would be fine, except you then change broadcastMessage to point to that new message. For example, if I'd voted, the message would now be "AdamQpzm" not "{user}". When another person goes to vote, it can't replace {user}, since it's no longer {user}, it's AdamQpzm...

    The solution? Don't do the this.broadcastMessage = ... bit ;)
     
    TheFluffyWalrus and Watto like this.
  8. Offline

    TheFluffyWalrus

    soo...
    Code:java
    1. try
    2. {
    3. this.broadcastmessage.replace('&', '§').replace("§§", "&").replace("{user}", username).replace("{site}", vote.getServiceName());
    4. Votifier.getInstance().getServer().broadcastMessage(this.broadcastmessage);
    5. }
    6. catch (Exception ex)
     
  9. TheFluffyWalrus Not quite. You should assign the replace stuff to a local variable, and then broadcast the local variable.
     
    TheFluffyWalrus likes this.
  10. Offline

    TheFluffyWalrus

    I guess I am still lost lol are we talking more like this
    Code:java
    1. private String format(String broadcastmessage, String playermessage, String command_1, String command_2, String command_3, String command_4, String command_5)
    2.  

    or am i far off again lol

    or

    Code:java
    1. private String format.replace('&', '§').replace("§§", "&").replace("{player}", player).replace("{site}", site.getServiceName());
    2.  
     
  11. Offline

    Watto

    TheFluffyWalrus

    Rather than set this.broadcastmessage to itself, Create a local String variable in the method, assign this.broadcast message to that String and send the player that String.

    This is really basic, perhaps we just aren't great at explaining :p

    Code:java
    1. String newString = oldString;
    2.  
    3. newString = newString+" Hey";
    4.  
    5. player.sendMessage(newString);


    This adds "Hey" to the end of newString but not to the end of oldString. Hopefully that makes it easier to understand.
     
    TheFluffyWalrus likes this.
  12. Offline

    TheFluffyWalrus

    ok so let me try this one more time

    Code:java
    1. String bm1 = broadcastmessage;
    2. bm1 = bm1.replace('&', '§').replace("§§", "&").replace("{user}", username).replace("{site}", vote.getServiceName());;
    3. Votifier.getInstance().getServer().broadcastMessage(bm1);
     
  13. Offline

    Watto

    TheFluffyWalrus

    That will work, you can skip most of those steps and just do

    String bm1 = broadcastmessage.replace and etc.
     
    TheFluffyWalrus likes this.
  14. Offline

    Garris0n

  15. Offline

    TheFluffyWalrus

    Watto, AdamQpzm, I appreciate your help it works perfectly now thank you for all your help! Garris0n BUT underscores are sexy! xD Also thank you for all the great info!
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page