Last active
June 30, 2023 15:51
-
-
Save sathonay/16c022784378af13afebb79e6f925b1b to your computer and use it in GitHub Desktop.
my little ComponentAPI
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ComponentBuilder Example
ComponentJoiner Example