1. Welcome to skUnity!

    Welcome to skUnity! This is a forum where members of the Skript community can communicate and interact. Skript Resource Creators can post their Resources for all to see and use.

    If you haven't done so already, feel free to join our official Discord server to expand your level of interaction with the comminuty!

    Now, what are you waiting for? Join the community now!

Dismiss Notice
This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Solved JSON text to colored string

Discussion in 'Java' started by i998979, May 29, 2018.

  1. i998979

    i998979 Member

    Joined:
    Jan 26, 2017
    Messages:
    24
    Likes Received:
    2
    So the code I have is the following
    Code (Text):
    1.  
    2.     private ProtocolManager protocolManager;
    3.  
    4.     public void onEnable() {
    5.  
    6.         protocolManager = ProtocolLibrary.getProtocolManager();
    7.  
    8.         protocolManager.addPacketListener(new PacketAdapter(this, PacketType.Play.Server.CHAT) {
    9.             @Override
    10.             public void onPacketSending(PacketEvent event) {
    11.                 PacketContainer packet = event.getPacket();
    12.                 StructureModifier<WrappedChatComponent> chatComponents = packet.getChatComponents();
    13.                 String jsonText = chatComponents.read(0).getJson();
    14.                 Bukkit.getServer().getLogger().info(jsonText);
    15.                
    16.             }
    17.         });
    18.     }
    19.  
    For example, I executed a command "plugman", it will display a text like this
    Code (Text):
    1. [PlugMan] You do not have permission to do this.
    What I want to do is, get the exact same string from the packet
    But currently I can only get this
    Code (Text):
    1. {"extra":[{"color":"gray","text":"["},{"color":"green","text":"
    2. PlugMan"},{"color":"gray","text":"] "},{"color":"red","text":"You do not have pe
    3. rmission to do this."}],"text":""}
    Is there any way to parse the message above to the exact text that the player receives?
     
  2. Best Answer:
    Post #2 by Donut, May 29, 2018
  3. Donut

    Donut Well-Known Member

    Joined:
    Mar 27, 2017
    Messages:
    1,340
    Likes Received:
    176
    Medals:
    Code (Java):
    1. String message = BaseComponent.toLegacyText(ComponentSerializer.parse(jsonText));
     
  4. i998979

    i998979 Member

    Joined:
    Jan 26, 2017
    Messages:
    24
    Likes Received:
    2
    You saved my life. Thank you <3
     

Share This Page

Loading...