ChatListener/PlayerListener Help

Discussion in 'Plugin Development' started by gwas10, Oct 1, 2013.

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

    gwas10

    I know how to make a chat listener for players, but I only know how to make say something back to the player. I was having trouble finding out how to make it so when the player says something, it will be broadcasted differently. For example, I type "Hi! My name is gwas10 : )" people would see "Hi! My name is gwas10 ☺". This would work for any given sentence, it just tracks the ": )" and makes it "☺" when shown. Thank you,

    -gwas10
     
  2. Offline

    xepisolonxx

    Search replace text asynctextevent you will find it. :)
     
  3. Offline

    xTrollxDudex

    gwas10
    AsyncPlayerChatEvent. Check if the message contains :) and if so, create the smiley
     
  4. Offline

    gwas10

    xTrollxDudex
    I am afraid I do not understand. What do I do exactly with AsyncPlayerChatEvent? Like how it it with the code? Also, how do I make it so that what the player said is broadcasted to the rest of the server with the smiley? Thanks,

    -gwas10
     
  5. Offline

    Chinwe

    You can use event.setMessage(newMessage), so what you would want to do is something like event.setMessage(event.getMessage().replaceAll(":)", **smiley character**));
     
  6. Offline

    gwas10

    Chinwe
    Thank you for explaining it a little more for me, but can you (or anyone else) tell me where that line of code goes? And if I need a separate class? Thanks,

    -gwas10
     
  7. Offline

    Gater12

    gwas10 Use PlayerListerner??
     
  8. Offline

    gwas10

    Gater12
    No, I need help with AsyncPlayerChatEvent I think. Player listener doesn't edit one small part of the chat and then broadcast it as something else. I think it might, but it is a little bit more complicated. Actually I think thats what AsyncPlayerChatEvent is under
     
  9. Offline

    Gater12

    gwas10 Isn't it like
    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent e){
    3. event.setMessage(e.getMessage().replaceAll(':)', **smiley character**));
    4. }

    I may be confusing myself.
     
  10. Offline

    gwas10

    Gater12
    Ok, thanks! Where do I place it so I can test it out?
     
  11. Offline

    Gater12

    gwas10 You can put it in your main class file and where you extend JavaPlugin also put implements Listener so it would be:
    Code:java
    1. public class main extends JavaPlugin implements Listener

    Then in your onEnable() put:
    Code:
    PluginManager pm = getServer().getPluginManager();
    pm.registerEvents(this, this);
    to register the event.
     
  12. Offline

    peteyandpika

    Gater12
    Corrrection, you forgot to name the initialized method. Should be like this:
    Code:java
    1. @EventHandler
    2. public void onChat(AsyncPlayerChatEvent e)
    3. { ... }
    4.  
     
    Gater12 likes this.
  13. Offline

    Gater12

  14. Offline

    gwas10

    peteyandpika
    So then what would go between the { ... }? Sorry, I'm new to making plugins
     
  15. Offline

    peteyandpika

    what gater12 said, i was just correcting his mistake as of not naming the method.
     
  16. Offline

    gwas10

    peteyandpika
    Oh, ok. Thanks, I see it now. Thanks,

    gwas10

    Gater12
    So would all of that code that you put there go into a separate class? If so, what would be the name of the class

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  17. Offline

    Gater12

    gwas10 If you want to put it in another class... The name could be anything, really.
     
  18. Offline

    gwas10

    Gater12
    So all of this goes under onEnable()? Or what method do I create where?
     
  19. Offline

    Gater12

    gwas10 If it's in your main class file it would like this:
    Code:java
    1. public class main extends JavaPlugin implements Listener{
    2. public void onEnable(){
    3. //stuff that goes here
    4. PluginManager pm = getServer().getPluginManager();
    5. pm.registerEvents(this, this);
    6. }
    7. @EventHandler
    8. public void onChat(AsyncPlayerChatEvent e){
    9. //code
    10. }


    gwas10 Where your class declaration goes. Also where you extends JavaPlugin
    Code:
    public class MainClassName extends JavaPlugin implements Listener
    Make sure you import it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  20. Offline

    gwas10

    Gater12
    Ok thank you! Everything is working perfectly except for one thing:
    Code:java
    1. event.setMessage(e.getMessage().replaceAll(':)', **smiley character**));

    It gives me an error at the
    that says "Invalid character constant Thanks,

    -gwas10
     
  21. Offline

    Gater12

    gwas10 Try with ":)" I think i screwed that up.
     
  22. Offline

    gwas10

    Gater12
    Another error:
    Code:java
    1. event.setMessage(e.getMessage().replaceAll(':)', hi));
    2. hi));

    When I tried it with "hi" it says
    Code:java
    1. event.setMessage(e.getMessage().replaceAll(':)', ☺));
    2. hi));

    When I tried it with "☺" it said
    Also, are you going to be available tomorrow morning?
     
  23. Offline

    Gater12

    gwas10 It should be:
    Code:
    e.setMessage(e.getMessage().replaceAll(":)", "☺"));
    Because I made a type and put event.setMessage. It should be the above.
    Yes I am available tomorrow.
    EDIT: Updated code. So many typos today.
     
  24. Offline

    gwas10

    Gater12
    Hopefully last and tiniest error:
    Code:java
    1. e.setMessage(e.getMessage().replaceAll(":)"), "☺");
    2. .replaceAll(":)"), "☺");
    It gives an error under .replaceAll
    Would I fix that by making the code
    Code:java
    1. e.setMessage(e.getMessage().replaceAll(":)", null), "☺");
    2. .replaceAll(":)", null), "☺");
    ? Thanks,

    -gwas10
     
  25. Offline

    mazentheamazin

    gwas10
    I think this should wok:
    Code:java
    1. e.setMessage(e.setMessage().replaceAll(":)", null), "(Dat Symbol)").replaceAll(":)", null), "(Dat Symbol)");
    2. //If it doesnt work Tag Me.


    Happy Coding!
    Mazen
     
  26. Offline

    Ultimate_n00b

    Chinwe and jimuskin like this.
  27. Offline

    gwas10

    mazentheamazin
    Ok, thank you all for helping me! But there are only two errors. I don't want to change them because I'm afraid to mess it up.
    Code:java
    1. e.setMessage(e.setMessage().replaceAll(": )", null), "(Dat Symbol)").replaceAll(": )", null), "(Dat Symbol)");
    2. (e.setMessage()
    3. null)

    On the e.setMessage() in the parenthesis(second e.setMessage()), it has an error, and the parenthesis on the second null.
    Here is the places of the issues
    and then the error messages...
    Thanks,

    -gwas10
     
  28. Offline

    d33k40

    Use this for more chars:

    Code:java
    1. //The code below is made by "Father Of Time" on bukkit forum.
    2. //Thanks you.
    3. {
    4. Field field = SharedConstants.class.getDeclaredField("allowedCharacters");
    5. field.setAccessible(true);
    6. Field modifiersField = Field.class.getDeclaredField( "modifiers" );
    7. modifiersField.setAccessible( true );
    8. modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL);
    9. String oldallowedchars = (String)field.get(null);
    10. String suits = "\u2665\u2666\u2663\u2660\u263A\u263B\u266A\u266B\u2642\u2640\u2625\u2680\u2681\u2682\u2683\u2684\u2685";//chars you want to add.
    11. StringBuilder sb = new StringBuilder();
    12. sb.append( oldallowedchars );
    13. sb.append( suits );
    14. field.set( null, sb.toString() );
    15. }


    onEnable:
    Code:java
    1. try {
    2. ModifyAllowedCharacters();
    3. } catch (NoSuchFieldException e) {
    4. e.printStackTrace();
    5. } catch (SecurityException e) {
    6. e.printStackTrace();
    7. e.printStackTrace();
    8. e.printStackTrace();
    9. }


    Then use in your chat events:
    Code:java
    1. char face1 = 0x263A;
    2. char face2 = 0x263B;
    3. mensaje = mensaje.replace(":c1:", Character.toString(face1));
    4. mensaje = mensaje.replace(":c2:", Character.toString(face2));
     
  29. Offline

    gwas10

    d33k40 Thank you for all that wonderful code, but where do you put the code on top, and the code on bottom? I am not new to programming, just new at making bukkit plugins
     
  30. Offline

    d33k40

    The method to add chars in your main or where you want, just call it in onEnable

    The replace in your chat async event, replace the string you want into the chars, for example the face.
     
Thread Status:
Not open for further replies.

Share This Page