Solved My replace chat isn't working

Discussion in 'Plugin Development' started by Fiddy_percent, Jul 15, 2014.

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

    Fiddy_percent

    it use to work fine but I cant figure out what happened.
    Code:java
    1. @EventHandler(priority = EventPriority.HIGHEST)
    2. public void onChat(AsyncPlayerChatEvent e){
    3.  
    4. Player p = e.getPlayer();
    5.  
    6. if(plugin.getConfig().getString("Drunk") != null){
    7.  
    8. if(plugin.getConfig().contains("Drunk." + p.getUniqueId().toString())){
    9. String d = e.getMessage();
    10. int dp = plugin.getConfig().getInt("Drunk." + p.getUniqueId().toString());
    11. HashMap<UUID, String> m = new HashMap<UUID, String>();
    12. m.put(p.getUniqueId(), d);
    13.  
    14. String msg = m.get(p.getUniqueId());
    15.  
    16.  
    17. if(dp < 30 && dp > 19){
    18. //SLOW
    19. String t = msg.replaceAll("the","thee").replaceAll("this", "thiss").replaceAll("you", "yallls").replaceAll("lol", "lul").replaceAll("drink", "drunk");
    20. m.put(p.getUniqueId(), t);
    21. }else if(dp > 49 && dp < 99){
    22. //SLOW AND CONFUSED
    23. String t = msg.replaceAll("so", " *hick* ").replaceAll("lol", "I love my anus").replaceAll("this", p.getName()).replaceAll("why ", "I").replaceAll("its", "I'm"
    24. ).replaceAll("the", "thuur").replaceAll("club", "the dancy place").replaceAll("milk", "more drinks").replaceAll("no", "yes");
    25. m.put(p.getUniqueId(), t);
    26. //SLOW CONFUSION BLIDNESS
    27.  
    28. }else if(dp > 100 && dp < 149){
    29. String t = msg.replaceAll("so", "...").replaceAll("lol", "crap I peed myself").replaceAll("this", p.getName()).replaceAll("why", "I").replaceAll("no", "yes").replaceAll("yes", "no").replaceAll("help", "I will kill you").replaceAll(
    30. "ugh", "man I'm sexy").replaceAll("sucks", "rocks").replaceAll("fun", "furrn").replaceAll("guys", "I'm so alone").replaceAll("the", "thuur").replaceAll("why", "I think").replaceAll("drink", "strip").replaceAll("come here", "get lost").replaceAll("like", "hate")
    31. .replaceAll("this", "diss").replaceAll("jk", "I'm serious");
    32. m.put(p.getUniqueId(), t);
    33.  
    34. }else if(dp > 150){
    35. //SLOW CONFUSION BLINDNESS NIGHTVISION
    36. String t = msg.replaceAll("so", "...").replaceAll("lol", "crap I peed myself").replaceAll("this", p.getName()).replaceAll("why", "I").replaceAll("no", "yes").replaceAll("yes", "no").replaceAll("help", "I will kill you").replaceAll(
    37. "ugh", "man I'm sexy").replaceAll("sucks", "rocks").replaceAll("fun", "furrn").replaceAll("bad", "hot").replaceAll("the", "thuur").replaceAll("why", "I think").replaceAll("drink", "strip").replaceAll("hi", "get lost").replaceAll("like", "hate")
    38. .replaceAll("this", "diss").replaceAll("jk", "I'm serious").replaceAll("-", "").replaceAll("brb", "I'm so alone").replaceAll("afk", "I want you bad").replaceAll("great", "greatsss").replaceAll("lag", "white power").replaceAll("nuns", "buns").replaceAll(
    39. "boone", "booze").replaceAll("funny", "getting really hot").replaceAll("stupid", "..Who touched me").replaceAll("omg", "I think your sexy").replaceAll("wow", "amuzing").replaceAll("see", "rape");
    40. m.put(p.getUniqueId(), t);
    41. }else if(dp <20){
    42. String t =msg.replaceAll(" ", " ");
    43. m.put(p.getUniqueId(), t);
    44. //NEAR SOBER
    45. }
    46.  
    47.  
    48. e.setMessage(m.get(p.getUniqueId()));
    49.  
    50. }
    51. }
    52. }
    53.  
    54.  


    I confirmed that the drunk points dp are entering correctly but it doesn't change any of the chat text anymore and I cant figure out why
     
  2. Offline

    rfsantos1996

    Since you're doing all these things on an event instead of creating an HashMap on your class, you could just:
    Code:java
    1. if(<true if player is drunk>) {
    2. String message = event.getMessage();
    3. /*
    4. * replace all thingies here, also, this is really creative IMO,
    5. * didnt saw this anywhere and it must be fun to watch
    6. * this on a big server
    7. */
    8. event.setMessage(message);
    9. }
     
  3. Offline

    Fiddy_percent

    rfsantos1996

    Still not working even tho I made it simpler
    and I have checked that the message does in fact change *broadcast shows changed message* e.setmessage(t) however does not and Ive changed the priority around and still no change

    Code:java
    1. @EventHandler(priority = EventPriority.NORMAL)
    2. public void onChat(AsyncPlayerChatEvent e){
    3. Player p = e.getPlayer();
    4. if(plugin.getConfig().getString("Drunk") != null){
    5. if(plugin.getConfig().contains("Drunk." + p.getUniqueId().toString())){
    6. int dp = plugin.getConfig().getInt("Drunk." + p.getUniqueId().toString());
    7.  
    8. String msg = e.getMessage();
    9.  
    10. if(dp < 30 && dp > 19){
    11. Bukkit.broadcastMessage("slow");
    12. String t = msg.replaceAll("the","thee").replaceAll("this", "thiss").replaceAll("you", "yallls").replaceAll("lol", "lul").replaceAll("drink", "drunk");
    13. // Bukkit.broadcastMessage(t);
    14. e.setMessage(t);
    15. Bukkit.broadcastMessage(ChatColor.RED + t);
    16. }else if(dp > 49 && dp < 99){
    17. //SLOW AND CONFUSED
    18. Bukkit.broadcastMessage("slow and confused");
    19. String t = msg.replaceAll("so", " *hick* ").replaceAll("lol", "I love my anus").replaceAll("this", p.getName()).replaceAll("why ", "I").replaceAll("its", "I'm"
    20. ).replaceAll("the", "thuur").replaceAll("club", "the dancy place").replaceAll("milk", "more drinks").replaceAll("no", "yes");
    21. // Bukkit.broadcastMessage(t);
    22. e.setMessage(t);
    23. Bukkit.broadcastMessage(ChatColor.RED + t);
    24. //SLOW CONFUSION BLIDNESS
    25. }else if(dp > 100 && dp < 149){
    26. Bukkit.broadcastMessage("slow confused blindess");
    27. String t = msg.replaceAll("so", "...").replaceAll("lol", "crap I peed myself").replaceAll("this", p.getName()).replaceAll("why", "I").replaceAll("no", "yes").replaceAll("yes", "no").replaceAll("help", "I will kill you").replaceAll(
    28. "ugh", "man I'm sexy").replaceAll("sucks", "rocks").replaceAll("fun", "furrn").replaceAll("guys", "I'm so alone").replaceAll("the", "thuur").replaceAll("why", "I think").replaceAll("drink", "strip").replaceAll("come here", "get lost").replaceAll("like", "hate")
    29. .replaceAll("this", "diss").replaceAll("jk", "I'm serious");
    30. e.setMessage(t);
    31. Bukkit.broadcastMessage(ChatColor.RED + t);
    32. }else if(dp > 150){
    33. //SLOW CONFUSION BLINDNESS NIGHTVISION
    34. Bukkit.broadcastMessage("nightvisoion");
    35. String t = msg.replaceAll("so", "...").replaceAll("lol", "crap I peed myself").replaceAll("this", p.getName()).replaceAll("why", "I").replaceAll("no", "yes").replaceAll("yes", "no").replaceAll("help", "I will kill you").replaceAll(
    36. "ugh", "man I'm sexy").replaceAll("sucks", "rocks").replaceAll("fun", "furrn").replaceAll("bad", "hot").replaceAll("the", "thuur").replaceAll("why", "I think").replaceAll("drink", "strip").replaceAll("hi", "get lost").replaceAll("like", "hate")
    37. .replaceAll("this", "diss").replaceAll("jk", "I'm serious").replaceAll("-", "").replaceAll("brb", "I'm so alone").replaceAll("afk", "I want you bad").replaceAll("great", "greatsss").replaceAll("lag", "white power").replaceAll("nuns", "buns").replaceAll(
    38. "boone", "booze").replaceAll("funny", "getting really hot").replaceAll("stupid", "..Who touched me").replaceAll("omg", "I think your sexy").replaceAll("wow", "amuzing").replaceAll("see", "rape");
    39. Bukkit.broadcastMessage(ChatColor.RED + t);
    40. e.setMessage(t);
    41.  
    42. }else if(dp <20){
    43. Bukkit.broadcastMessage("almost sober");
    44. String t =msg.replaceAll(" ", " ");
    45. e.setMessage(t);
    46. //NEAR SOBER
    47. }else{
    48. Bukkit.broadcastMessage("nothing");
    49. }
    50. }
    51. }
    52. }
    53.  
     
  4. Offline

    mythbusterma

    Fiddy_percent I doubt this is what's causing the problem, but it could.

    You are accessing Bukkit methods that are expressly not thread-safe from a thread other the main thread, which could create inconsistancies, especially with things like File I/O (reading and writing from config).
     
    Gater12 likes this.
  5. Offline

    lualzockt

    The replaceAll method expects a regular expression. You should try the replace method.
     
  6. Offline

    Fiddy_percent

    mythbusterma it was working fine before (I changed something a while back and never new how to fix it again)
    lualzockt No dice
     
  7. Offline

    mythbusterma

    Fiddy_percent

    Regardless of whether or not it was working before, you should really still fix that. Plus, thread-safety issues are so finicky that a version update could have broken previously "working" (read: not thread-safe) code.
     
  8. Offline

    Fiddy_percent

    what do I need to know how to do in order to fix that? I know there is a check if async command but if thats not it im totally in the dark
     
  9. Offline

    mythbusterma

    Fiddy_percent

    9/10 it will be asynchronous, as the only way it is synchronous is if a player is compeled to chat via Player#chat(...).

    In this case the simplest solution would be to use the deprecated PlayerChatEvent which is Synchronous, and is only deprecated because it can cause server lag if you're doing a lot of work inside of it (which, in this case, you're not, so it should be okay).

    If you're interested in learning about how to make this thread-safe, just ask.
     
  10. Offline

    Fiddy_percent

    Yeah I would love to know that (no point in staying ignorant)
     
  11. Offline

    mythbusterma

    Fiddy_percent

    Well, any task that requires Bukkit methods (or native Craftbukkit/Minecraft methods, for that matter) is not guaranteed to be thread-safe, so any time you make a call to these methods, it must be done on the main thread.

    The BukkitScheduler provides some helpful tools for doing this, namely:

    <T> Future<T> callSyncMethod(Plugin plugin, Callable<T> task)

    And the series of functions related to this one:

    BukkitTask runTask(Plugin plugin, Runnable task)

    If you're looking for a value from the main thread, for example if a player is in a HashMap (although you could use ConcurrentHashMap for this) or asking if a player has a permission, you can use the first one which will return a future that the other thread can either block on, or do some other work until it's complete (using the Future#isDone() and Future#get() or Future#get(), respectively).

    If you're passing data back to the main thread for execution, for example calling BukkitServer#broadcast(...) you can can run an anonymous runnable on the main thread via the second option, like:

    Code:java
    1. Bukkit.getScheduler().runTask(Plugin, new Runnable () {
    2. @Override
    3. public void run () {
    4. // do something
    5. }
    6. });


    Or you can run it with a delay using one of the delay methods.

    Any other questions?
     
    jthort likes this.
  12. Offline

    Fiddy_percent

    Thats really useful :D thanks ill try using that


    And by the way I fixed it
    hacked an old backed up jar file all i needed to do to my original code was add (String)

    Code:java
    1. e.setMessage((String)m.get(p.getUniqueId()));
     
Thread Status:
Not open for further replies.

Share This Page