Archived
0

Fixed coloring and links

This commit is contained in:
Pepich 2018-01-15 03:02:23 +01:00
parent c29e1523da
commit 3311330bc4

View File

@ -3,129 +3,180 @@ package net.nemez.chatapi.click;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.nemez.chatapi.ChatAPI;
public class Message {
public class Message
{
private CommandSender sender;
private CommandSender permission;
private TextComponent message;
private String rawMessage;
public Message(CommandSender sender, CommandSender permission) {
public Message(CommandSender sender, CommandSender permission)
{
this.sender = sender;
this.permission = permission;
message = new TextComponent("");
rawMessage = "";
}
public Message appendText(String text) {
public Message appendText(String text)
{
text = ChatAPI.colorify(permission, text);
message.addExtra(text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components)
{
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendLink(String text, String url) {
public Message appendLink(String text, String url)
{
text = ChatAPI.colorify(permission, text);
TextComponent component = new TextComponent(text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components)
{
component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendSendChat(String text, String msg) {
public Message appendSendChat(String text, String msg)
{
text = ChatAPI.colorify(permission, text);
TextComponent component = new TextComponent(text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components)
{
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, msg));
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendSuggest(String text, String suggestion) {
public Message appendSuggest(String text, String suggestion)
{
text = ChatAPI.colorify(permission, text);
TextComponent component = new TextComponent(text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components)
{
component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestion));
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendCallback(String text, ClickCallback callback) {
if (sender instanceof Player) {
public Message appendCallback(String text, ClickCallback callback)
{
if (sender instanceof Player)
{
int id = CallbackMap.register(((Player) sender).getUniqueId(), callback);
return appendSendChat(text, "/" + ChatAPI.getInternalCallbackCommand() + " " + id);
}else{
}
else
{
return appendText(text);
}
}
public Message appendTextHover(String text, String hover) {
public Message appendTextHover(String text, String hover)
{
text = ChatAPI.colorify(permission, text);
TextComponent component = new TextComponent(text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components)
{
addHoverText(component, hover);
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendLinkHover(String text, String url, String hover) {
public Message appendLinkHover(String text, String url, String hover)
{
text = ChatAPI.colorify(permission, text);
TextComponent component = new TextComponent(text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components)
{
component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
addHoverText(component, hover);
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendSendChatHover(String text, String msg, String hover) {
public Message appendSendChatHover(String text, String msg, String hover)
{
text = ChatAPI.colorify(permission, text);
TextComponent component = new TextComponent(text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components)
{
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, msg));
addHoverText(component, hover);
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendSuggestHover(String text, String suggestion, String hover) {
public Message appendSuggestHover(String text, String suggestion, String hover)
{
text = ChatAPI.colorify(permission, text);
TextComponent component = new TextComponent(text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components)
{
component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestion));
addHoverText(component, hover);
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendCallbackHover(String text, ClickCallback callback, String hover) {
if (sender instanceof Player) {
public Message appendCallbackHover(String text, ClickCallback callback, String hover)
{
if (sender instanceof Player)
{
int id = CallbackMap.register(((Player) sender).getUniqueId(), callback);
return appendSendChatHover(text, "/" + ChatAPI.getInternalCallbackCommand() + " " + id, hover);
}else{
}
else
{
return appendTextHover(text, hover);
}
}
public void send() {
if (sender == null || !ChatAPI.canChat(this.permission)) {
public void send()
{
if (sender == null || !ChatAPI.canChat(this.permission))
{
return;
}
if (sender instanceof Player) {
((Player)sender).spigot().sendMessage(message);
}else{
if (sender instanceof Player)
{
((Player) sender).spigot().sendMessage(message);
}
else
{
sender.sendMessage(rawMessage);
}
}
private void addHoverText(TextComponent comp, String text) {
comp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatAPI.colorify(permission, text)).create()));
private void addHoverText(BaseComponent comp, String text)
{
comp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder(ChatAPI.colorify(permission, text)).create()));
}
}