[Solved] Chat Input

Discussion in 'Spigot Plugin Development' started by EyedLabs, Jun 30, 2015.

  1. Hello! I've got a small issue I've stumbled upon; I don't know how to do this.

    I'm pretty decent with the Spigot API and Java coding, but I just can't seem to get this to work. Here is what I'am trying to do:

    A player clicks a sign saying Prefix (working)
    Then, they get set into a 'Prefix Mode', basicly this is the issue:
    I'm trying to not use a command for setting prefixes but the AsyncPlayerChatEvent to check what the player types in chat if he is in a hashmap after he clicked the sign, after he typed in chat the prefix he wants he gets removed from the hashmap and he can chat normally again, and his prefix is updated to the prefix he typed in chat.

    PHP:
    @EventHandler
    public void checkChat(AsyncPlayerChatEvent e) {
     
        Player p = e.getPlayer();

        //what to do now..

    }
    Help would be appreciated.
     
    • Agree Agree x 1
  2. Thanks, but I tried that but I didn't seem to get it to work.
     
  3. **Shameless self promotion**


    It's very easy to use once you get the hang of it.
     
    • Useful Useful x 1
  4. Didn't see that there was a video tutorial yet, I'll look into it, thanks!
     
  5. I'm not to proud of that video though... it was before i had a decent mic and a good laptop... i just haven't bothered to make any other videos since...
     
  6. Oh well, It was a pretty good tutorial, not as confusing (as you said :3) as on the bukkit forums.
     
  7. Alright, so I got the 'system'/main thing I want working because of your tutorial, there is just this little issue where I don't know how to translate the chat colors (&) of the answer to the § format and apply it as a custom prefix for groupmanager..
     
    #8 EyedLabs, Jul 1, 2015
    Last edited: Jul 1, 2015
  8. Try out using event.setFormat()
    It's kinda weird, you might have to print out event.getFormat() once into console to figure out how it works. But from there you would just add your prefix before the name.
     
  9. I'm currently using the conversation API, and my class looks like this:


    PHP:
    public class PrefixGetter extends StringPrompt implements Listener {

        Title prefixUpdate = new Title("", "§eYour prefix has been updated", 1, 2, 1);
       
        @Override
        public Prompt acceptInput(ConversationContext con, String ans) {
           
            con.getForWhom().sendRawMessage("§aPrefix updated to " + ans);
           
            prefixUpdate.sendConversableTitle(con.getForWhom());
           
           
            return null;
        }

        @Override
        public String getPromptText(ConversationContext arg0) {

            return "§eType the custom prefix you'd like in chat:";
        }
       
       
        @EventHandler
        public void applyPrefix(AsyncPlayerChatEvent e) {
           
            //I want to get 'ans' and apply it as a prefix..
           
           
        }

    }
     
  10. Code (Text):
    String newString = ChatColor.translateAlternateColorcodes('&', "The message you're changing");
    That would change the chat colors, then you'd want to listen to the chat event, check the format and replace "{prefix}"(Or what ever the variable in essentials chat format is) to your prefix.
     
  11. In my case that would be: ?

    Code (Text):
    String newString = ChatColor.translateAlternateColorCodes('&', ans);
    //ans is the player answer
    But shouldn't the '&' be '§' since I'm trying to translate & to §?

    And I'll be more clear what I actually want to do, here is my current code:

    PHP:
    public class PrefixGetter extends StringPrompt implements Listener {
       
        Title prefixUpdate = new Title("", "§eYour prefix has been updated", 1, 2, 1);
       
        @Override
        public Prompt acceptInput(ConversationContext con, String ans /* the string I want to use as prefix in the asyncevent  */) {
           
            String newString = ChatColor.translateAlternateColorCodes('&', ans);
           
            con.getForWhom().sendRawMessage("§aPrefix updated to " + newString);
           
            prefixUpdate.sendConversableTitle(con.getForWhom());
           
            return null;
        }

        @Override
        public String getPromptText(ConversationContext arg0) {

            return "§eType the custom prefix you'd like in chat:";
        }
       
       
        @EventHandler
        public void applyPrefix(AsyncPlayerChatEvent e) {
           
            Player p = e.getPlayer();
           
            String msg = new String(e.getMessage());
           
            e.setFormat( /* get the 'ans' string (from acceptInput(...) + */ GMHook.getPrefix(p) + p.getName() + " " + msg);
           
           
        }

    }
     
    I understand that it's not possible getting a string or any kind of variable from another method because of scope unless you are returning the string by making a calling method, I'm just stuck here.
     
    #12 EyedLabs, Jul 1, 2015
    Last edited: Jul 1, 2015
  12. The way the chatcolor.translatealternate(what ever its called) works is by replacing '&' into the '§'.

    So to color code the players answer it would in fact be:
    Code (Text):
    String newString = ChatColor.translateAlternateColorCodes('&', ans);
           
            con.getForWhom().sendRawMessage("§aPrefix updated to "+ newString);
     
    • Useful Useful x 1
  13. Thanks, I got that fixed. Is there any chance you could help me solve the issue with applying the new prefix to the player from a different method (the event)?
     
  14. You'd want to listen to the chat event,
    check the format and replace "{prefix}"(Or what ever the variable in essentials chat format is) to your prefix.
    then set the format to the replaced version.
     
  15. I tried that, but my IDE tells me that 'newString' can't be resolved to a type when I call it from the event. This is the issue.
     
  16. That is because newString is the answer from your chat prompt, so you'll have to save it to a config, or a hashmap or something, then in the chat event you get it from where ever you saved it.
     
    • Winner Winner x 1
  17. You would want to use the e.setFormat() method dupplied by spigot, the docs provide an okay explanation about how it works, hoever, i think it might interfere with groupmanager, PEX and such plugins so be careful about that
     
  18. Already got it solved! Thanks to @Bimmr , thanks for your reply though!
     
  19. Awesome, mark the thread as solved :)