How can i do this?

Discussion in 'Spigot Plugin Development' started by AntonioC94, Jan 28, 2019.

  1. Hello there!
    So.. i will explain what i want to do...

    Actually, Im using a method to print on chat 2 lines with a message json (Using TextComponent).
    The message to replace and use is this:

    Code (Text):

    announced: "The class of %class% will start soon."
    clickHere: "click here!"
    announced-click: %click% to join in the queue
     
    And the code is this

    Code (Text):

        public void announce(Player p, String class) {
            String clickHere = this.mensajesConfig.getString("clickHere");
            String clickAnuncio = this.mensajesConfig.getString("announced-click");
            String newAnuncio = clickAnuncio.replace("%click%", clickHere);
            String announced = this.mensajesConfig.getString("announced");
            String newAnnounced = announced.replace("%class%", class);
            TextComponent mensajeInicial = new TextComponent(newAnnounced);
            mensajeInicial.setColor(ChatColor.GREEN);
            TextComponent click = new TextComponent(newAnuncio);
            click.setColor(ChatColor.GOLD);
            click.setBold(true);
            click.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/class join " + class));
            p.spigot().sendMessage(mensajeInicial);
            p.spigot().sendMessage(click);
        }
     
    Actually using this code, I have this result .

    [​IMG]
    (I can execute the command clicking in all the line and not just on click here)

    So, basically i want to separate the phrase "to join in the queue" from "click here!" to just let the properties to click to the words "click here" . (Sorry, i know is a little confused to explain)

    The problem is , when i get the phrase "%click% to join in the queue" and i replace the value %click% for another string, all the phrase get all the properties of the textcomponent. So.. what can i do with "%click% to join in the queue" , to replace click , put on textcomponent, add differents properties and at the same time be the same phrase to print on chat?

    Thanks :D

    Edit; I tried to put "click-here" and "to join in the queue" on differents textcomponent and connecting both in p.spigot().sendMessage(blablabla); , but appear the message repating twice the "click-here"
     
  2. I think this should work:
    Code (Java):
    public void announce(Player p, String class) {
        String clickHere = this.mensajesConfig.getString("clickHere");
        String newAnuncio = this.mensajesConfig.getString("announced-click");
        String announced = this.mensajesConfig.getString("announced");
        String newAnnounced = announced.replace("%class%", class);
        TextComponent mensajeInicial = new TextComponent(newAnnounced);
        mensajeInicial.setColor(ChatColor.GREEN);
        TextComponent click = new TextComponent(clickHere);
        TextComponent clickText = new TextComponent(newAnuncio);
        click.setColor(ChatColor.GOLD);
        click.setBold(true);
        click.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/class join " + class));
        clickText.setColor(ChatColor.GOLD);
        clickText.setBold(true);
        click.addExtra(clickText);
        p.spigot().sendMessage(mensajeInicial);
        p.spigot().sendMessage(click);
    }
     
  3. If you're not opposed to using libraries, I suggest https://github.com/mkremins/fanciful

    You could do what you needed with
    Code (Text):

    new FancyMessage("Click Here!").command("/COMMAND YOU WANT TO DO").then(" to join the queue").send(p);
     
    You could also dig into the source code of this to see how they did it.
     
    • Useful Useful x 1
  4. A
    First , i like the method so much, but my question is.. is possible to do that with the text component?
     
  5. Fanciful is outdated and shouldn't be used anymore, as it says in the README.
    Use https://github.com/KyoriPowered/text

    A good way would be to use the ComponentBuilder like this:
    Code (Java):
    player.sendMessage( new ComponentBuilder( "click here" ).event(new ClickEvent( ClickEvent.Action.RUN_COMMAND, "COMMAND HERE" )).append(", this is more text").create() );
     
    • Agree Agree x 1
  6. Hi! , I tried to edit the code (thanks @MipCraft ) but I have this already..

    asdasd.jpg

    so.. is the problem that i want to solve :C
     
  7. Just replace the /click/ by a textcomponen
     
  8. Does my solution work or not?
     
  9. Sin título.png
    I tried too :C
     
  10. Code (Java):
    public void announce(Player p, String class) {
        String clickHere = this.mensajesConfig.getString("clickHere");
        String newAnuncio = this.mensajesConfig.getString("announced-click");
        String announced = this.mensajesConfig.getString("announced");
        String newAnnounced = announced.replace("%class%", class);
        TextComponent mensajeInicial = new TextComponent(newAnnounced);
        mensajeInicial.setColor(ChatColor.GREEN);
        TextComponent line2 = new TextComponent("");
        TextComponent click = new TextComponent(clickHere);
        TextComponent clickText = new TextComponent(newAnuncio);
        click.setColor(ChatColor.GOLD);
        click.setBold(true);
        click.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/class join " + clasS));
        clickText.setColor(ChatColor.GREEN);
        clickText.setBold(false);
        line2.addExtra(click);
        line2.addExtra(clickText);
        p.spigot().sendMessage(mensajeInicial);
        p.spigot().sendMessage(line2);
    }