Solved Double Chat

Discussion in 'Plugin Development' started by 1Camer0471, May 2, 2015.

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

    1Camer0471

    Hello, I am creating a chat system but for some reason it is sending the message twice.

    here is the code that I think has the problem:

    Code:Java
    1.  
    2. if (main.getChatChannels().get(p.getName()) == 1) {
    3. for (String toSend : main.getChatChannels().keySet()) {
    4. if (main.getChatChannels().get(toSend) == 1) {
    5. for (Player toSendP : Bukkit.getOnlinePlayers()) {
    6. if (toSendP.getName().equals(toSend)) {
    7. e.getRecipients().add(toSendP);
    8. for (Player toSendF : e.getRecipients()) {
    9. toSendF.sendMessage(msg);
    10. }
    11. }
    12. }
    13. }
    14. }
    15. }
    16.  

    Note: main is my main class, getChatChannels() returns HashMap<String, Integer>

    and just in case here is the rest of my chat system:

    Code:Java
    1.  
    2. package me.Zacx;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.entity.Player;
    6. import org.bukkit.event.EventHandler;
    7. import org.bukkit.event.Listener;
    8. import org.bukkit.event.player.AsyncPlayerChatEvent;
    9.  
    10. public class TerritoriesChat implements Listener {
    11.  
    12. static TerritoriesMain main;
    13.  
    14. public TerritoriesChat(TerritoriesMain plugin) {
    15. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    16. main = plugin;
    17. }
    18.  
    19. String tPrefix = "§0[§2Territories§0]";
    20.  
    21. String cosmicPrefix = "§2[§c§lCosmic§2]";
    22. String atlasPrefix = "§2[§1§lAtlas§2]";
    23. String atomPrefix = "§2[§7§lAtom§2]";
    24. String horizonPrefix = "§2[§e§lHorizon§2]";
    25.  
    26. String recruit = " §7(§7Recruit§7)§r ";
    27. String warrior = " §7(§fWarrior§7)§r ";
    28. String knight = " §7(§fKnight§7)§r ";
    29. String highKnight = " §7(§f§lHighKnight§7)§r ";
    30. String royalKnight = " §7(§cRoyalKnight§7)§r ";
    31. String marshal = " §7(§c§lMarshal§7)§r ";
    32. String governor = " §7(§2Governor§7)§r ";
    33. String admiral = " §7(§aAdmiral§7)§r ";
    34. String grandAdmiral = " §7(§a§lGrandAdmiral§7)§r ";
    35. String king = " §7(§eKing§7)§r ";
    36. String lord = " §7(§4Lord§7)§r ";
    37.  
    38. @EventHandler
    39. public void onChat(AsyncPlayerChatEvent e) {
    40.  
    41. e.getRecipients().clear();
    42. e.setCancelled(true);
    43.  
    44. final Player p = e.getPlayer();
    45. // Mute Checker
    46. if (main.getMutedPlayers().contains(p.getUniqueId())) {
    47. p.sendMessage(tPrefix + " §4You are muted and cannot speak!");
    48. e.setCancelled(true);
    49. } else {
    50.  
    51. String rawmsg = e.getMessage();
    52. String msg = "";
    53.  
    54. // COSMIC
    55. if (main.getTerritory(1).containsKey(p.getUniqueId())) {
    56. Integer rank = main.getTerritory(1).get(p.getUniqueId());
    57.  
    58. if (rank == 1) {
    59. msg = cosmicPrefix + recruit + p.getName() + ": " + rawmsg;
    60. }
    61. if (rank == 2) {
    62. msg = cosmicPrefix + warrior + p.getName() + ": " + rawmsg;
    63. }
    64. if (rank == 3) {
    65. msg = cosmicPrefix + knight + p.getName() + ": " + rawmsg;
    66. }
    67. if (rank == 4) {
    68. msg = cosmicPrefix + highKnight + p.getName() + ": " + rawmsg;
    69. }
    70. if (rank == 5) {
    71. msg = cosmicPrefix + royalKnight + p.getName() + ": " + rawmsg;
    72. }
    73. if (rank == 6) {
    74. msg = cosmicPrefix + marshal + p.getName() + ": " + rawmsg;
    75. }
    76. if (rank == 7) {
    77. msg = cosmicPrefix + governor + p.getName() + ": " + rawmsg;
    78. }
    79. if (rank == 8) {
    80. msg = cosmicPrefix + admiral + p.getName() + ": " + rawmsg;
    81. }
    82. if (rank == 9) {
    83. msg = cosmicPrefix + grandAdmiral + p.getName() + ": " + rawmsg;
    84. }
    85. if (rank == 10) {
    86. msg = cosmicPrefix + king + p.getName() + ": " + rawmsg;
    87. }
    88. if (rank == 11) {
    89. msg = cosmicPrefix + lord + p.getName() + ": " + rawmsg;
    90. }
    91.  
    92. }
    93.  
    94. // ATLAS
    95. if (main.getTerritory(2).containsKey(p.getUniqueId())) {
    96. Integer rank = main.getTerritory(2).get(p.getUniqueId());
    97.  
    98. if (rank == 1) {
    99. msg = atlasPrefix + recruit + p.getName() + ": " + rawmsg;
    100. }
    101. if (rank == 2) {
    102. msg = atlasPrefix + warrior + p.getName() + ": " + rawmsg;
    103. }
    104. if (rank == 3) {
    105. msg = atlasPrefix + knight + p.getName() + ": " + rawmsg;
    106. }
    107. if (rank == 4) {
    108. msg = atlasPrefix + highKnight + p.getName() + ": " + rawmsg;
    109. }
    110. if (rank == 5) {
    111. msg = atlasPrefix + royalKnight + p.getName() + ": " + rawmsg;
    112. }
    113. if (rank == 6) {
    114. msg = atlasPrefix + marshal + p.getName() + ": " + rawmsg;
    115. }
    116. if (rank == 7) {
    117. msg = atlasPrefix + governor + p.getName() + ": " + rawmsg;
    118. }
    119. if (rank == 8) {
    120. msg = atlasPrefix + admiral + p.getName() + ": " + rawmsg;
    121. }
    122. if (rank == 9) {
    123. msg = atlasPrefix + grandAdmiral + p.getName() + ": " + rawmsg;
    124. }
    125. if (rank == 10) {
    126. msg = atlasPrefix + king + p.getName() + ": " + rawmsg;
    127. }
    128. if (rank == 11) {
    129. msg = atlasPrefix + lord + p.getName() + ": " + rawmsg;
    130. }
    131.  
    132. }
    133.  
    134. // ATOM
    135. if (main.getTerritory(3).containsKey(p.getUniqueId())) {
    136. Integer rank = main.getTerritory(3).get(p.getUniqueId());
    137.  
    138. if (rank == 1) {
    139. msg = atomPrefix + recruit + p.getName() + ": " + rawmsg;
    140. }
    141. if (rank == 2) {
    142. msg = atomPrefix + warrior + p.getName() + ": " + rawmsg;
    143. }
    144. if (rank == 3) {
    145. msg = atomPrefix + knight + p.getName() + ": " + rawmsg;
    146. }
    147. if (rank == 4) {
    148. msg = atomPrefix + highKnight + p.getName() + ": " + rawmsg;
    149. }
    150. if (rank == 5) {
    151. msg = atomPrefix + royalKnight + p.getName() + ": " + rawmsg;
    152. }
    153. if (rank == 6) {
    154. msg = atomPrefix + marshal + p.getName() + ": " + rawmsg;
    155. }
    156. if (rank == 7) {
    157. msg = atomPrefix + governor + p.getName() + ": " + rawmsg;
    158. }
    159. if (rank == 8) {
    160. msg = atomPrefix + admiral + p.getName() + ": " + rawmsg;
    161. }
    162. if (rank == 9) {
    163. msg = atomPrefix + grandAdmiral + p.getName() + ": " + rawmsg;
    164. }
    165. if (rank == 10) {
    166. msg = atomPrefix + king + p.getName() + ": " + rawmsg;
    167. }
    168. if (rank == 11) {
    169. msg = atomPrefix + lord + p.getName() + ": " + rawmsg;
    170. }
    171.  
    172. }
    173.  
    174. // HORIZON
    175. if (main.getTerritory(4).containsKey(p.getUniqueId())) {
    176. Integer rank = main.getTerritory(4).get(p.getUniqueId());
    177.  
    178. if (rank == 1) {
    179. msg = horizonPrefix + recruit + p.getName() + ": " + rawmsg;
    180. }
    181. if (rank == 2) {
    182. msg = horizonPrefix + warrior + p.getName() + ": " + rawmsg;
    183. }
    184. if (rank == 3) {
    185. msg = horizonPrefix + knight + p.getName() + ": " + rawmsg;
    186. }
    187. if (rank == 4) {
    188. msg = horizonPrefix + highKnight + p.getName() + ": " + rawmsg;
    189. }
    190. if (rank == 5) {
    191. msg = horizonPrefix + royalKnight + p.getName() + ": " + rawmsg;
    192. }
    193. if (rank == 6) {
    194. msg = horizonPrefix + marshal + p.getName() + ": " + rawmsg;
    195. }
    196. if (rank == 7) {
    197. msg = horizonPrefix + governor + p.getName() + ": " + rawmsg;
    198. }
    199. if (rank == 8) {
    200. msg = horizonPrefix + admiral + p.getName() + ": " + rawmsg;
    201. }
    202. if (rank == 9) {
    203. msg = horizonPrefix + grandAdmiral + p.getName() + ": " + rawmsg;
    204. }
    205. if (rank == 10) {
    206. msg = horizonPrefix + king + p.getName() + ": " + rawmsg;
    207. }
    208. if (rank == 11) {
    209. msg = horizonPrefix + lord + p.getName() + ": " + rawmsg;
    210. }
    211.  
    212. }
    213. if (!(main.getChatChannels().containsKey(p.getName()))) {
    214. main.getChatChannels().put(p.getName(), 1);
    215. } else {
    216. if (main.getChatChannels().get(p.getName()) == 1) {
    217. for (String toSend : main.getChatChannels().keySet()) {
    218. if (main.getChatChannels().get(toSend) == 1) {
    219. for (Player toSendP : Bukkit.getOnlinePlayers()) {
    220. if (toSendP.getName().equals(toSend)) {
    221. e.getRecipients().add(toSendP);
    222. for (Player toSendF : e.getRecipients()) {
    223. toSendF.sendMessage(msg);
    224. }
    225. }
    226. }
    227. }
    228. }
    229. }
    230. if (main.getChatChannels().get(p.getName()) == 2) {
    231. for (String toSend : main.getChatChannels().keySet()) {
    232. if (main.getChatChannels().get(toSend) == 2) {
    233. for (Player toSendP : Bukkit.getOnlinePlayers()) {
    234. if (toSendP.getName().equals(toSend)) {
    235. e.getRecipients().add(toSendP);
    236. for (Player toSendF : e.getRecipients()) {
    237. toSendF.sendMessage(msg);
    238. }
    239. }
    240. }
    241. }
    242. }
    243. }
    244. if (main.getChatChannels().get(p.getName()) == 3) {
    245. for (String toSend : main.getChatChannels().keySet()) {
    246. if (main.getChatChannels().get(toSend) == 3) {
    247. for (Player toSendP : Bukkit.getOnlinePlayers()) {
    248. if (toSendP.getName().equals(toSend)) {
    249. e.getRecipients().add(toSendP);
    250. for (Player toSendF : e.getRecipients()) {
    251. toSendF.sendMessage(msg);
    252. }
    253. }
    254. }
    255. }
    256. }
    257. }
    258. }
    259. }
    260. }
    261. }
    262.  
    263.  
     
  2. Because you send the message to the players AND the normal chat message gets sent.
    A better solution that won't make problems with other plugins is:
    e.setFormat
     
    Last edited: May 2, 2015
  3. Offline

    1Camer0471

    @FisheyLP no, I use e.setCanceled(true) only one message should be getting sent, plus, the double messages are using the format I made.
     
  4. Here you're sending the message two times
     
  5. Offline

    1Camer0471

    @FisheyLP That sends the message only if they are in cannel one or two, and its impossible to be in both.
     
  6. At your onEnable, are you calling new TerritoriesChat(this) or PluginManager#registerEvents(new TerritoriesChat(this)) ?
     
  7. Offline

    1Camer0471

  8. Offline

    1Camer0471

    bumpity
     
  9. Try changing "if" for "else if" and add some debug messages where you send messages.

    Code:
    if (main.getChatChannels().get(p.getName()) == 1) {
       for (String toSend : main.getChatChannels().keySet()) {
          if (main.getChatChannels().get(toSend) == 1) {
             Player player = Bukkit.getPlayer(toSend);
             if(player != null) {
                 player.sendMessage(msg); //You don't have to add the player to recipes.
    
    Also try using Custom Objects it will improve your code.
     
  10. Offline

    Tecno_Wizard

    @1Camer0471, let me put this as politely as a can-- your code is a mess.
    Code:
                // COSMIC
                if (main.getTerritory(1).containsKey(p.getUniqueId())) {
                    Integer rank = main.getTerritory(1).get(p.getUniqueId());
                    if (rank == 1) {
                        msg = cosmicPrefix + recruit + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 2) {
                        msg = cosmicPrefix + warrior + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 3) {
                        msg = cosmicPrefix + knight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 4) {
                        msg = cosmicPrefix + highKnight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 5) {
                        msg = cosmicPrefix + royalKnight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 6) {
                        msg = cosmicPrefix + marshal + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 7) {
                        msg = cosmicPrefix + governor + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 8) {
                        msg = cosmicPrefix + admiral + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 9) {
                        msg = cosmicPrefix + grandAdmiral + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 10) {
                        msg = cosmicPrefix + king + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 11) {
                        msg = cosmicPrefix + lord + p.getName() + ": " + rawmsg;
                    }
                }
                // ATLAS
                if (main.getTerritory(2).containsKey(p.getUniqueId())) {
                    Integer rank = main.getTerritory(2).get(p.getUniqueId());
                    if (rank == 1) {
                        msg = atlasPrefix + recruit + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 2) {
                        msg = atlasPrefix + warrior + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 3) {
                        msg = atlasPrefix + knight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 4) {
                        msg = atlasPrefix + highKnight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 5) {
                        msg = atlasPrefix + royalKnight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 6) {
                        msg = atlasPrefix + marshal + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 7) {
                        msg = atlasPrefix + governor + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 8) {
                        msg = atlasPrefix + admiral + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 9) {
                        msg = atlasPrefix + grandAdmiral + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 10) {
                        msg = atlasPrefix + king + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 11) {
                        msg = atlasPrefix + lord + p.getName() + ": " + rawmsg;
                    }
                }
                // ATOM
                if (main.getTerritory(3).containsKey(p.getUniqueId())) {
                    Integer rank = main.getTerritory(3).get(p.getUniqueId());
                    if (rank == 1) {
                        msg = atomPrefix + recruit + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 2) {
                        msg = atomPrefix + warrior + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 3) {
                        msg = atomPrefix + knight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 4) {
                        msg = atomPrefix + highKnight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 5) {
                        msg = atomPrefix + royalKnight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 6) {
                        msg = atomPrefix + marshal + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 7) {
                        msg = atomPrefix + governor + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 8) {
                        msg = atomPrefix + admiral + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 9) {
                        msg = atomPrefix + grandAdmiral + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 10) {
                        msg = atomPrefix + king + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 11) {
                        msg = atomPrefix + lord + p.getName() + ": " + rawmsg;
                    }
                }
                // HORIZON
                if (main.getTerritory(4).containsKey(p.getUniqueId())) {
                    Integer rank = main.getTerritory(4).get(p.getUniqueId());
                    if (rank == 1) {
                        msg = horizonPrefix + recruit + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 2) {
                        msg = horizonPrefix + warrior + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 3) {
                        msg = horizonPrefix + knight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 4) {
                        msg = horizonPrefix + highKnight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 5) {
                        msg = horizonPrefix + royalKnight + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 6) {
                        msg = horizonPrefix + marshal + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 7) {
                        msg = horizonPrefix + governor + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 8) {
                        msg = horizonPrefix + admiral + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 9) {
                        msg = horizonPrefix + grandAdmiral + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 10) {
                        msg = horizonPrefix + king + p.getName() + ": " + rawmsg;
                    }
                    if (rank == 11) {
                        msg = horizonPrefix + lord + p.getName() + ": " + rawmsg;
                    }
    This requires switch statements. A switch is essentially a very clean list of if statements.
    For example, if i is 0, i want to send hi, if 1, i, 2, am.
    Code:
    int i =  (int) Math.random()*3;
    switch(i){
        case 0: System.out.println("hi");
            break;
        case 1: System.out.println("i");
            break;
        case 2: System.out.println("am");
            break;
    }
    essentially, if the case is true, the code will continue from there on and is stopped like a loop with a break. If you forget the break, everything below it will run. Warning you now, that's a mistake I still make.

    Also, main seems to have a lot of references to it, and that means that you're probably passing a reference to it EVERYWHERE. Try using a singleton to clean this up.

    Once you clean things up, your problem will likely become a lot more evident...

    Also, is anyone else having or has had strange bugs with firefox's textboxes flashing when trying to backspace and characters that were backspaced stay there for a few seconds, because it's driving me nuts, likely just as much as this run-on sentence is.
     
    1Camer0471 likes this.
  11. Offline

    1Camer0471

    @Tecno_Wizard ohhhhmaiiigud! switches are SEXY, sorry xD I know a lot of Java but not everything, and I am still learning the conventions :p And for this 'singleton' I am referencing main here:
    Code:Java
    1.  
    2. static TerritoriesMain main;
    3.  
    4. public TerritoriesChat(TerritoriesMain plugin) {
    5. plugin.getServer().getPluginManager().registerEvents(this, plugin);
    6. main = plugin;
    7. }
    8.  


    Edit:
    My problem is still not evident at all :/ if you could look through my code and see if you could help out that would be awesome!
    Even if you can't, thanks for being so awesome! <3
     
    Last edited: May 5, 2015
  12. Offline

    teej107

    What's even sexier is for-loops and polymorphism! (or a List :p)

    Example using a List
    Code:
    for(int i = 1; i < 5; i++) //Your super long if statements checking the territory can be condensed to a loop
    {
      if(myPlugin.getTerritory(i).containsKey(player.getUniqueId())
      {
         msg = list.get(rankNumber);
         break; //if you don't need to change the msg anymore, break out of the loop
      }
    }
    The List object type can be anything like a class to make the String from any given data

    That's not a singleton approach. Remove the static. You don't need it.
     
    Last edited: May 5, 2015
  13. Offline

    Tecno_Wizard

    @1Camer0471, you're misunderstanding singleton, but it is an extremely advanced java topic. Don't worry about it.


    @teej107, that is something that you better be doing synchronously. Lol.
     
    Last edited: May 6, 2015
    1Camer0471 likes this.
  14. Why?
     
    1Camer0471 likes this.
  15. Offline

    teej107

    @AdamQpzm I think he meant synchronously. (I hope)
     
    1Camer0471 likes this.
  16. Offline

    Tecno_Wizard

    @teej107, yea, I did. Nothing like autocorrect when you don't notice it.

    @AdamQpzm, bad things happen when you mess with chat asyc. Player.send message() tends to fail, at least in my experience. When the player moves, the object isn't equal to the current player and the call fails.
     
    1Camer0471 likes this.
  17. I think I found it, it probably is
    Code:
    for (Player toSendP : Bukkit.getOnlinePlayers()) {
                                    if (toSendP.getName().equals(toSend)) {
                                        e.getRecipients().add(toSendP);
                                        for (Player toSendF : e.getRecipients()) {
                                            toSendF.sendMessage(msg);
                                        }
                                    }
                                }
    For each online player, you add them to the recipent list, and then you cycle over all recipents again, so it also sends the message to all players who recently were added to the recipents, use this instead of the above given code:
    Code:
    for (Player toSendP : Bukkit.getOnlinePlayers()) {
                                    if (toSendP.getName().equals(toSend)) {
                                        toSendP.sendMessage(msg);
                                    }
                                }
     
    1Camer0471 likes this.
  18. Right, thread safety and what not. But due to your mistake, I misunderstood. It looked like you were suggesting this would need to be done async, as if checking 5 different maps for a key was an operation that you saw as likely to freeze the main thread, hence my confusion :p
     
  19. Offline

    1Camer0471

Thread Status:
Not open for further replies.

Share This Page