Get message before sent

Discussion in 'Spigot Plugin Development' started by Nort721, May 29, 2018.

  1. pretty simple question, after some searching I am pretty sure this is impossible to do but who knows maybe one of you guys have already done it (if it's possible). is there a way to get what the player is typing before he is sending the message to chat.
     
  2. Can you use the AsyncPlayerChatEvent ?
     
  3. but it will not get the chat message BEFORE the player has sent it before he pressed enter
     
  4. But once he does if you need to stop the chat you can just cancel the event ?
    What are you trying to achieve ?
     
  5. It is not possible to get the message while typing without client modifications, the only option kind of in this direction is tab complete. I works on every chat message, the message doesn't have to contain a command, and it sends the contents of the message which is being written to the server.
    What do you want to achieve by capturing what the user types?
     
  6. What are you trying to do specifically?
     
  7. That's like asking Google to search something you haven't told it to search yet..
     
    • Agree Agree x 1
    • Winner Winner x 1
  8. I am trying to get the text before it's being sent after the player has tried to use the tab complete here, is my code:
    Code (Text):

        public void onTabPacket() {
            this.protocolManager = ProtocolLibrary.getProtocolManager();
            this.protocolManager.addPacketListener(
                    new PacketAdapter(this, ListenerPriority.HIGH, new PacketType[] { PacketType.Play.Client.TAB_COMPLETE }) {
                        public void onPacketReceiving(PacketEvent e) {
                            if (e.getPacketType() == PacketType.Play.Client.TAB_COMPLETE) {
                                PacketContainer p = e.getPacket();
                                // some how get the message before it's sent
                            }
                        }
                    });
        }
     
     
  9. sorry for asking questions but not posting code before...
     
  10. If you’re asking whether it’s possible to get all the text before the player has pressed tab, it’s impossible as it’s purely client-side. You would otherwise probably be interested in the TabCompleteEvent which would save you the hassle of using ProtocolLib.
     
  11. this event really looks like it makes life easy but it not seems to have an option to get the command that the player has tried to complete with tab
     
  12. getBuffer maybe?
     
  13. At this point it sounds like an X Y problem, we could perhaps help you better if you mention what you are trying to accomplish with the tab completion.
     
  14. I want to change the message that is sent when you do /about [TAB]
     
  15. Assuming that by message you mean possible completions for a given command argument, you could use implement either the TabCompleter or TabExecutor interface. Both interfaces gives you more finegrained control on the completions to return. In this case, your /about command could implement the TabExecutor interface.
     
  16. Code (Java):
    public class CmdAbout implements TabExecutor {
        @Override
        public List<String> onTabComplete(CommandSender sender, Command command, String label, String[] args) {
            // If they've started typing the first argument,
            // lets find some matches to show them. This
            // searches all online player names and returns matches.
            if (args.length == 1) {
                return Bukkit.getOnlinePlayers().stream()
                    .filter(player -> player.getName().toLowerCase().startsWith(args[0].toLowerCase()))
                    .map(Player::getName).collect(Collectors.toList());
            }

            // this example only cares for the first argument.
            // returning null will prevent the server from doing
            // it's own tab completes (aka, nothing will happen
            // now if this isn't for the first argument)
            return null;
        }

        @Override
        public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
            // does some magical command stuffs
        }
    }
    This is just a very common example. You can return anything you want for whatever reason you want for onTabComplete()