Skip to content

Instantly share code, notes, and snippets.

@sathonay
Last active June 30, 2023 15:51
Show Gist options
  • Save sathonay/16c022784378af13afebb79e6f925b1b to your computer and use it in GitHub Desktop.
Save sathonay/16c022784378af13afebb79e6f925b1b to your computer and use it in GitHub Desktop.
my little ComponentAPI
import net.md_5.bungee.api.ChatColor;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ClickEvent.Action;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
public class ComponentBuilder {
private TextComponent component;
public ComponentBuilder(String text) {
this.component = new TextComponent(text);
}
public ComponentBuilder(TextComponent component) {
this.component = component;
}
public ComponentBuilder setClickEvent(Action action, String value) {
this.component.setClickEvent(new ClickEvent(action, ChatColor.translateAlternateColorCodes('&', value)));
return this;
}
public ComponentBuilder setHoverEvent(HoverEvent.Action action, String value) {
this.component.setHoverEvent(new HoverEvent(action, (new net.md_5.bungee.api.chat.ComponentBuilder(ChatColor.translateAlternateColorCodes('&', value))).create()));
return this;
}
public ComponentBuilder addExtra(String text) {
this.component.addExtra(text);
return this;
}
public ComponentBuilder addExtra(BaseComponent component) {
this.component.addExtra(component);
return this;
}
public ComponentBuilder addExtra(ComponentBuilder component) {
this.component.addExtra(component.toTextComponent());
return this;
}
public ComponentBuilder addExtra(ComponentJoiner component) {
this.component.addExtra(component.toTextComponent());
return this;
}
public ComponentBuilder setColor(ChatColor color) {
this.component.setColor(color);
return this;
}
public ComponentBuilder setColor(org.bukkit.ChatColor color) {
return this.setColor(ChatColor.getByChar(color.getChar()));
}
public TextComponent toTextComponent() {
return this.component;
}
}
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.TextComponent;
import com.sathonay.core.api.componentapi.ComponentBuilder;
public class ComponentJoiner {
private String delimiter;
private String endLimiter;
private TextComponent component = new TextComponent("");
public ComponentJoiner(String delimiter) {
this.delimiter = delimiter;
this.endLimiter = null;
}
public ComponentJoiner(String delimiter, String endLimiter) {
this.delimiter = delimiter;
this.endLimiter = endLimiter;
}
public ComponentJoiner add(String newElement) {
if (this.component.getExtra() != null) {
this.component.addExtra(this.delimiter);
}
this.component.addExtra(newElement);
return this;
}
public ComponentJoiner add(BaseComponent newElement) {
if (this.component.getExtra() != null) {
this.component.addExtra(this.delimiter);
}
this.component.addExtra(newElement);
return this;
}
public ComponentJoiner add(ComponentBuilder newElement) {
this.add(newElement.toTextComponent());
return this;
}
public TextComponent toTextComponent() {
if (endLimiter != null) this.component.addExtra(this.endLimiter);
return this.component;
}
}
@sathonay
Copy link
Author

sathonay commented Oct 27, 2022

ComponentBuilder Example

        player.spigot().sendMessage(
                new ComponentBuilder("Click me")
                        .setColor(ChatColor.LIGHT_PURPLE)
                        .setClickEvent(ClickEvent.Action.OPEN_URL, "https://github.com/sathonay")
                        .setHoverEvent(HoverEvent.Action.SHOW_TEXT, "Click here to open sathonay's github profile")
                        .toTextComponent()
        );

ComponentJoiner Example

        String[] dummy = {"A", "B", "C"};
        ComponentJoiner joiner = new ComponentJoiner(", ", ".");
        for (String s : dummy) joiner.add(new ComponentBuilder(s).setColor(ChatColor.GREEN).setHoverEvent(HoverEvent.Action.SHOW_TEXT, s));
        player.spigot().sendMessage(joiner.toTextComponent());

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment