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