Solved Translating text

Discussion in 'Plugin Development' started by plobnob, Jul 31, 2015.

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

    plobnob

    So I am having problems involving chat packets being sent to clients with ProtocolLib and changing said chat messages. I am attempting to replace a specific string within the message with another string. However, the message I am getting from ProtocolLib's events seems to be very different from regular colour codes, meaning checking if it contains it is not proving at all easy.

    Example of text from ProtocolLib message:
    Message (open)
    {"extra":["\u003c",{"color":§7"text":"["},{"bold":true,"color":§4"text":"Owner"},{"color":§7"text":"]"},{"color":§4"text":"~"},{"color":§e"text":"Plob"},{"color":§c"text":"Coding"},"\u003e Hey"],"text":""}


    Example of what I would want for comparison:
    Expected (open)
    <&7[&4&lOwner&7]&4~&ePlob&cCoding&r> Hey


    Code I am using to get the messages:
    Code:
            ProtocolLibrary.getProtocolManager().addPacketListener(new PacketAdapter(this, PacketType.Play.Server.CHAT)
            {
                private JSONParser parser = new JSONParser();
            
                @Override
                public void onPacketSending(PacketEvent event)
                {
                    Player player = event.getPlayer();
                    final PacketContainer packet = event.getPacket();
                    StructureModifier<WrappedChatComponent> components = packet.getChatComponents();
                    try
                    {
                        final Object data = parser.parse(components.read(0).getJson());
                        final boolean[] result = new boolean[1];
                    
                        transformPrimitives(data, null, new Processor()
                        {
                            @SuppressWarnings("unchecked")
                            @Override
                            public Object process(Object value, Object parent)
                            {
                                if (value instanceof String)
                                {
                                    int counter = 1;
                                    do
                                    {
                                        List<String> val = (List<String>) ranks.getList((String.valueOf(counter)));
                                        WrappedChatComponent chat = packet.getChatComponents().read(0);
                                        String oldJson = chat.getJson();
                                        String check = colour(val.get(0));
                                        String replace = colour(val.get(1));
                                        for(ChatColor c : ChatColor.values())
                                        {
                                            oldJson = oldJson.replace("\"" + c.getName().toLowerCase() + "\",", c.toString());
                                        }                
                                        System.out.print(oldJson);
                                        chat.setJson(newJson);
                                        //packet.getChatComponents().write(0, chat);
                                        counter++;
                                    } while (ranks.get(String.valueOf(counter)) != null);
                                }
                                return value;
                            }
                        });
                    }
                    catch (ParseException e)
                    {
                        e.printStackTrace();
                    }
                }
            });
    I have also tried using (String) value. However, this does not include all of the values I would like (for example, it does not include bold tags).

    Anyone have any idea on how to either translate this, or gather the message in a form which I will be able to understand?

    Thanks, Plob.

    EDIT: Nevermind, just had to get the json, make a list of IChatBaseComponents from it, then you can get modifiers, etc. Silly me, will mark as solved!
     
    Last edited: Jul 31, 2015
Thread Status:
Not open for further replies.

Share This Page