TextComponent?

Discussion in 'BungeeCord Plugin Development' started by zakarls, Jun 14, 2015.

  1. How do I append TextComponents together to make a single message. I want to be able to hover over different parts of the sentence and see different things but so far I was not able to find how to do this. Thanks!
     
    • Like Like x 1
  2. No problem! :)
     
  3. @AgentLV I have another question if you know the answer. How would I be able to use a loop to append multiple strings with hover events together. I am trying to create a list with players name like Player1, Player2, Player3 where if you hover over their names it will show something unique for each one. I've been trying all day but I cannot find a way to use a loop to append a new component each time.
     
  4. Well there are different ways of handling this.
    Option 1
    Code (Java):
    ProxiedPlayer p;
    BaseComponent[] bc = null;
    ComponentBuilder cb = new ComponentBuilder("");
    Map<String, String> map = new HashMap<String, String>();

    map.put("AgentLV", "http://www.spigotmc.org/members/agentlv.10067/");
    map.put("zakarls", "http://www.spigotmc.org/members/zakarls.10474/");
           
    for (Entry<String, String> entry : map.entrySet()) {
        BaseComponent[] hoverLink = new ComponentBuilder(entry.getValue()).create();
        cb.append(entry.getKey()).event(new HoverEvent(Action.SHOW_TEXT, hoverLink));
    }

    bc = cb.create();
    p.sendMessage(bc);
    This will send everything as one string, so in one line.

    Option 2
    Code (Java):
                    ProxiedPlayer p;
                    Map<String, String> map = new HashMap<String, String>();
               
                    map.put("AgentLV", "http://www.spigotmc.org/members/agentlv.10067/");
                    map.put("zakarls", "http://www.spigotmc.org/members/zakarls.10474/");
               
                    for (Entry<String, String> entry : map.entrySet()) {
                        BaseComponent[] hoverLink = new ComponentBuilder(entry.getValue()).create();
                        ComponentBuilder cb = new ComponentBuilder(entry.getKey()).event(new HoverEvent(Action.SHOW_TEXT, hoverLink));
                        p.sendMessage(cb.create());
                    }
     
    This would send everything in a new line.
    If you have any other questions, feel free to ask. :)
     
    • Like Like x 1
  5. @AgentLV I wanted to go with Option 1 but there is an issue that is occurring. Every time my code runs it prints this out "Players: [Lnet.md_5.bungee.api.chat.BaseComponent;@(8 random numbers/letters go here)"
    Here is part of my code.
    Code (Text):
    if(!Main.players.isEmpty()){
             String server;
             ProxiedPlayer p;
             BaseComponent[] bc = null;
             ComponentBuilder cb = new ComponentBuilder("");
             for(String t2:Main.players){
                     p = ProxyServer.getInstance().getPlayer(t2);
                     server = p.getServer().getInfo().getName();
                     BaseComponent[] hoverLink = new ComponentBuilder(server).create();
                     cb.append(p.getName() + ", ").event(new HoverEvent(Action.SHOW_TEXT, hoverLink));
             }
             bc = cb.create();
             ((CommandSender) event.getSender()).sendMessage("Players: " + bc);
    }
     
    When a player joins the server they are added to the arraylist players.
    Would you happen to know why this glitch is happening? Thanks.
     
    #7 zakarls, Jun 15, 2015
    Last edited: Jun 15, 2015
  6. Why are you casting at the end?
     
  7. Because event.getSender() is not able to use sendMessage if its not casted.
     
  8. Try casting it to a ProxiedPlayer.
     
  9. I did and it had the same action.
     
  10. Code (Java):
    ((CommandSender) event.getSender()).sendMessage("Players: " + bc);
     
    This is the problem, you should not send a message as a string and a BaseComponent[]. Just change
    Code (Java):
    ComponentBuilder cb = new ComponentBuilder("");
    to
    Code (Java):
    ComponentBuilder cb = new ComponentBuilder("Players: ");
     
  11. @AgentLV Thanks. It finally works. I have a question about another issue if you wouldn't mind answering. I am trying to set configuration lines based on the number of servers on a proxy but it doesn't seem to be working. Here is my code.
    Code (Text):
    Configuration config = ConfigurationProvider.getProvider(YamlConfiguration.class).load("config.yml");
    for(Entry<String, ServerInfo> entry:ProxyServer.getInstance().getServers().entrySet()){
           ServerInfo si = entry.getValue();
           if(config.getString(si.getName()) == null){
           config.set(si.getName(), si.getName());
                     
           }
    }
     
  12. What is the problem? If you can't see any entries in the config this is because you are not saving the file and the file has the changes in RAM but not on the disk, but I would only save them if you really need to.
     
    #14 AgentLV, Jun 16, 2015
    Last edited: Jun 16, 2015
  13. @AgentLV
    I run this code on startup. It does not seem to modify the configuration in any ways. It throws an error.
    Code:
    Code (Text):
    public void setUpConfig(){
            Configuration config = ConfigurationProvider.getProvider(YamlConfiguration.class).load("config.yml");
            for(Entry<String, ServerInfo> entry:ProxyServer.getInstance().getServers().entrySet()){
                ServerInfo si = entry.getValue();
                if(config.getString(si.getName()) == null){
                    config.set(si.getName(), si.getName());
                    try {
                        ConfigurationProvider.getProvider(YamlConfiguration.class).save(config, new File(getDataFolder(), "config.yml"));
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
            }
        }
    Error:
    Code (Text):
    16.06 22:08:35 [Server] INFO at io.netty.handler.codec.MessageToMessageDecoder.channelRead(MessageToMessageDecoder.java:103)
    16.06 22:08:35 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.fireChannelRead(AbstractChannelHandlerContext.java:319)
    16.06 22:08:35 [Server] INFO at io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:333)
    16.06 22:08:35 [Server] INFO at net.md_5.bungee.netty.HandlerBoss.channelRead(HandlerBoss.java:76)
    16.06 22:08:35 [Server] INFO at net.md_5.bungee.protocol.packet.Chat.handle(Chat.java:50)
    16.06 22:08:35 [Server] INFO at net.md_5.bungee.connection.UpstreamBridge.handle(UpstreamBridge.java:113)
    16.06 22:08:35 [Server] INFO at net.md_5.bungee.api.plugin.PluginManager.callEvent(PluginManager.java:367)
    16.06 22:08:35 [Server] INFO at net.md_5.bungee.event.EventBus.post(EventBus.java:47)
    16.06 22:08:35 [Server] INFO at net.md_5.bungee.event.EventHandlerMethod.invoke(EventHandlerMethod.java:19)
    16.06 22:08:35 [Server] INFO at java.lang.reflect.Method.invoke(Method.java:483)
    16.06 22:08:35 [Server] INFO at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    16.06 22:08:35 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    16.06 22:08:35 [Server] INFO at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    16.06 22:08:35 [Server] INFO at me.zakarls.Glist.Hi.onPlayerChat(Hi.java:66)
    16.06 22:08:35 [Server] INFO at net.md_5.bungee.config.YamlConfiguration.load(YamlConfiguration.java:101)
    16.06 22:08:35 [Server] INFO at net.md_5.bungee.config.YamlConfiguration.load(YamlConfiguration.java:108)
    16.06 22:08:35 [Server] INFO at org.yaml.snakeyaml.Yaml.loadAs(Yaml.java:458)
    16.06 22:08:35 [Server] INFO at org.yaml.snakeyaml.Yaml.loadFromReader(Yaml.java:481)
    16.06 22:08:35 [Server] INFO at org.yaml.snakeyaml.constructor.BaseConstructor.getSingleData(BaseConstructor.java:127)
    16.06 22:08:35 [Server] INFO at org.yaml.snakeyaml.constructor.BaseConstructor.constructDocument(BaseConstructor.java:141)
    16.06 22:08:35 [Server] INFO at org.yaml.snakeyaml.constructor.BaseConstructor.constructObject(BaseConstructor.java:182)
    16.06 22:08:35 [Server] INFO at org.yaml.snakeyaml.constructor.Constructor$ConstructYamlObject.construct(Constructor.java:344)
    16.06 22:08:35 [Server] INFO ^
    16.06 22:08:35 [Server] INFO config.yml
    16.06 22:08:35 [Server] INFO in 'string', line 1, column 1:
    16.06 22:08:35 [Server] INFO Can't construct a java object for tag:yaml.org,2002:java.util.LinkedHashMap; exception=Can't construct a java object for scalar tag:yaml.org,2002:java.util.LinkedHashMap; No String constructor found. Exception=java.util.LinkedHashMap.<init>(java.lang.String)
    16.06 22:08:35 [Server] INFO WARNING: Error dispatching event
     
  14. I wouldn't recommend saving the config in the for-each loop, put it at the end of the loop. You can also remove
    Code (Text):
    // TODO Auto-generated catch block
    Is a config even in the plugin folder? What is on line 66 in the onPlayerChat class?
     
  15. @AgentLV
    I changed the saving for config to after the for loop. Also this is my line 66:
    Code (Text):
    Configuration config = ConfigurationProvider.getProvider(YamlConfiguration.class).load("config.yml");
    It still doesnt work when I run the command. Also the config is not changed on startup. It is generated in the plugins folder but it remains blank despite the setUpConfig method I run on startup.
     
  16. Change
    Code (Java):
    if(config.getString(si.getName()) == null)
    to
    Code (Java):
    if (config.getString(si.getName()).equals(""))
     
  17. Do you have a config.yml in your jar?