use of net.md_5.bungee.api.chat.TextComponent in project Denizen-For-Bukkit by DenizenScript.
the class FormattedTextHelper method parse.
public static BaseComponent[] parse(String str, ChatColor baseColor, boolean cleanBase) {
if (str == null) {
return null;
}
str = CoreUtilities.clearNBSPs(str);
int firstChar = str.indexOf(ChatColor.COLOR_CHAR);
if (firstChar == -1) {
if (str.contains("://")) {
firstChar = 0;
} else {
TextComponent base = new TextComponent();
// This is for compat with how Spigot does parsing of plaintext.
base.addExtra(new TextComponent(str));
return new BaseComponent[] { base };
}
}
str = cleanRedundantCodes(str);
if (cleanBase && str.length() < 512 && !str.contains(ChatColor.COLOR_CHAR + "[") && !str.contains("://")) {
return parseSimpleColorsOnly(str);
}
TextComponent root = new TextComponent();
TextComponent base = new TextComponent();
if (cleanBase) {
base.setBold(false);
base.setItalic(false);
base.setStrikethrough(false);
base.setUnderlined(false);
base.setObfuscated(false);
base.setColor(baseColor);
if (firstChar > 0) {
root.addExtra(new TextComponent(str.substring(0, firstChar)));
}
} else {
base.setText(str.substring(0, firstChar));
}
root.addExtra(base);
str = str.substring(firstChar);
char[] chars = str.toCharArray();
int started = 0;
TextComponent nextText = new TextComponent();
TextComponent lastText;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == ChatColor.COLOR_CHAR && i + 1 < chars.length) {
char code = chars[i + 1];
if (!allowedCharCodes.isMatch(code)) {
continue;
}
if (code == '[') {
int endBracket = str.indexOf(']', i + 2);
if (endBracket == -1) {
continue;
}
String innards = str.substring(i + 2, endBracket);
List<String> innardParts = CoreUtilities.split(innards, ';');
List<String> innardBase = CoreUtilities.split(innardParts.get(0), '=', 2);
innardParts.remove(0);
String innardType = CoreUtilities.toLowerCase(innardBase.get(0));
if (innardBase.size() == 2) {
nextText.setText(nextText.getText() + str.substring(started, i));
base.addExtra(nextText);
lastText = nextText;
nextText = copyFormatToNewText(lastText);
nextText.setText("");
if (innardType.equals("score") && innardParts.size() == 2) {
ScoreComponent component = new ScoreComponent(unescape(innardBase.get(1)), unescape(innardParts.get(0)), unescape(innardParts.get(1)));
lastText.addExtra(component);
} else if (innardType.equals("keybind")) {
KeybindComponent component = new KeybindComponent();
component.setKeybind(unescape(innardBase.get(1)));
lastText.addExtra(component);
} else if (innardType.equals("selector")) {
SelectorComponent component = new SelectorComponent(unescape(innardBase.get(1)));
lastText.addExtra(component);
} else if (innardType.equals("translate")) {
TranslatableComponent component = new TranslatableComponent();
component.setTranslate(unescape(innardBase.get(1)));
for (String extra : innardParts) {
for (BaseComponent subComponent : parse(unescape(extra), baseColor, false)) {
component.addWith(subComponent);
}
}
lastText.addExtra(component);
} else if (innardType.equals("click") && innardParts.size() == 1) {
int endIndex = findEndIndexFor(str, "click", i + 5);
if (endIndex == -1) {
continue;
}
TextComponent clickableText = new TextComponent();
clickableText.setClickEvent(new ClickEvent(ClickEvent.Action.valueOf(innardBase.get(1).toUpperCase()), unescape(innardParts.get(0))));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
clickableText.addExtra(subComponent);
}
lastText.addExtra(clickableText);
endBracket = endIndex + "&[/click".length();
} else if (innardType.equals("hover")) {
int endIndex = findEndIndexFor(str, "hover", i + 5);
if (endIndex == -1) {
continue;
}
TextComponent hoverableText = new TextComponent();
HoverEvent.Action action = HoverEvent.Action.valueOf(innardBase.get(1).toUpperCase());
if (HoverFormatHelper.processHoverInput(action, hoverableText, innardParts.get(0))) {
continue;
}
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
hoverableText.addExtra(subComponent);
}
lastText.addExtra(hoverableText);
endBracket = endIndex + "&[/hover".length();
} else if (innardType.equals("insertion")) {
int endIndex = str.indexOf(ChatColor.COLOR_CHAR + "[/insertion]", i);
int backupEndIndex = str.indexOf(ChatColor.COLOR_CHAR + "[insertion=", i + 5);
if (backupEndIndex > 0 && backupEndIndex < endIndex) {
endIndex = backupEndIndex;
}
if (endIndex == -1) {
continue;
}
TextComponent insertableText = new TextComponent();
insertableText.setInsertion(unescape(innardBase.get(1)));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
insertableText.addExtra(subComponent);
}
lastText.addExtra(insertableText);
endBracket = endIndex + "&[/insertion".length();
} else if (innardType.equals("reset")) {
if (innardBase.get(1).length() == 1) {
char subCode = innardBase.get(1).charAt(0);
if (subCode == 'k' || subCode == 'K') {
nextText.setObfuscated(false);
} else if (subCode == 'l' || subCode == 'L') {
nextText.setBold(false);
} else if (subCode == 'm' || subCode == 'M') {
nextText.setStrikethrough(false);
} else if (subCode == 'n' || subCode == 'N') {
nextText.setUnderlined(false);
} else if (subCode == 'o' || subCode == 'O') {
nextText.setItalic(false);
}
} else if (innardBase.get(1).equals("font")) {
nextText.setFont(base.getFont());
} else {
nextText.setColor(base.getColor());
}
} else if (innardType.equals("color")) {
String colorChar = innardBase.get(1);
ChatColor color = null;
if (colorChar.length() == 1) {
color = ChatColor.getByChar(colorChar.charAt(0));
} else if (colorChar.length() == 7) {
color = ChatColor.of(colorChar);
} else if (Debug.verbose) {
Debug.echoError("Text parse issue: cannot interpret color '" + innardBase.get(1) + "'.");
}
if (color != null) {
int endIndex = findEndIndexFor(str, "[color=", "[reset=color]", i + 1);
if (endIndex == -1) {
nextText.setColor(color);
} else {
TextComponent colorText = new TextComponent();
colorText.setColor(color);
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), color, false)) {
colorText.addExtra(subComponent);
}
lastText.addExtra(colorText);
endBracket = endIndex + "&[reset=color".length();
}
}
} else if (innardType.equals("font")) {
int endIndex = findEndIndexFor(str, "[font=", "[reset=font]", i + 1);
if (endIndex == -1) {
nextText.setFont(innardBase.get(1));
} else {
TextComponent fontText = new TextComponent();
fontText.setFont(innardBase.get(1));
for (BaseComponent subComponent : parse(str.substring(endBracket + 1, endIndex), baseColor, false)) {
fontText.addExtra(subComponent);
}
lastText.addExtra(fontText);
endBracket = endIndex + "&[reset=font".length();
}
} else {
if (Debug.verbose) {
Debug.echoError("Text parse issue: cannot interpret type '" + innardType + "' with " + innardParts.size() + " parts.");
}
}
}
i = endBracket;
started = endBracket + 1;
continue;
} else if (code == 'r' || code == 'R') {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(baseColor);
} else if (colorCodesOrReset.isMatch(code)) {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(ChatColor.getByChar(code));
} else if (code == 'x') {
if (i + 13 >= chars.length) {
continue;
}
StringBuilder color = new StringBuilder(12);
color.append("#");
for (int c = 1; c <= 6; c++) {
if (chars[i + c * 2] != ChatColor.COLOR_CHAR) {
color = null;
break;
}
char hexPart = chars[i + 1 + c * 2];
if (!hexMatcher.isMatch(hexPart)) {
color = null;
break;
}
color.append(hexPart);
}
if (color == null) {
continue;
}
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = new TextComponent();
nextText.setColor(ChatColor.of(color.toString()));
i += 13;
started = i + 1;
continue;
} else {
nextText.setText(nextText.getText() + str.substring(started, i));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
nextText = copyFormatToNewText(nextText);
if (code == 'k' || code == 'K') {
nextText.setObfuscated(true);
} else if (code == 'l' || code == 'L') {
nextText.setBold(true);
} else if (code == 'm' || code == 'M') {
nextText.setStrikethrough(true);
} else if (code == 'n' || code == 'N') {
nextText.setUnderlined(true);
} else if (code == 'o' || code == 'O') {
nextText.setItalic(true);
}
}
i++;
started = i + 1;
} else if (i + "https://a.".length() < chars.length && chars[i] == 'h' && chars[i + 1] == 't' && chars[i + 2] == 't' && chars[i + 3] == 'p') {
String subStr = str.substring(i, i + "https://a.".length());
if (subStr.startsWith("https://") || subStr.startsWith("http://")) {
int nextSpace = CoreUtilities.indexOfAny(str, i, ' ', '\t', '\n');
if (nextSpace == -1) {
nextSpace = str.length();
}
String url = str.substring(i, nextSpace);
nextText.setText(nextText.getText() + str.substring(started, i));
base.addExtra(nextText);
lastText = nextText;
nextText = new TextComponent(lastText);
nextText.setText("");
TextComponent clickableText = new TextComponent(url);
clickableText.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
lastText.addExtra(clickableText);
i = nextSpace;
started = nextSpace;
continue;
}
}
}
nextText.setText(nextText.getText() + str.substring(started));
if (!nextText.getText().isEmpty()) {
base.addExtra(nextText);
}
return new BaseComponent[] { cleanBase ? root : base };
}
use of net.md_5.bungee.api.chat.TextComponent in project Denizen-For-Bukkit by DenizenScript.
the class HoverFormatHelper method processHoverInput.
public static boolean processHoverInput(HoverEvent.Action action, TextComponent hoverableText, String input) {
Content content;
if (action == HoverEvent.Action.SHOW_ITEM) {
ItemTag item = ItemTag.valueOf(FormattedTextHelper.unescape(input), CoreUtilities.noDebugContext);
if (item == null) {
return true;
}
// TODO: Why is there not a direct conversion method for Spigot ItemStack -> BungeeChat Item?
String itemNbt = NMSHandler.getItemHelper().getRawHoverText(item.getItemStack());
content = new Item(item.getBukkitMaterial().getKey().toString(), item.getAmount(), net.md_5.bungee.api.chat.ItemTag.ofNbt(itemNbt));
} else if (action == HoverEvent.Action.SHOW_ENTITY) {
EntityTag entity = EntityTag.valueOf(FormattedTextHelper.unescape(input), CoreUtilities.basicContext);
if (entity == null) {
return true;
}
BaseComponent name = null;
if (entity.getBukkitEntity() != null && entity.getBukkitEntity().isCustomNameVisible()) {
name = new TextComponent();
for (BaseComponent component : FormattedTextHelper.parse(entity.getBukkitEntity().getCustomName(), ChatColor.WHITE)) {
name.addExtra(component);
}
}
content = new Entity(entity.getBukkitEntityType().getKey().toString(), entity.getUUID().toString(), name);
} else {
content = new Text(FormattedTextHelper.parse(FormattedTextHelper.unescape(input), ChatColor.WHITE));
}
hoverableText.setHoverEvent(new HoverEvent(action, content));
return false;
}
use of net.md_5.bungee.api.chat.TextComponent in project VotingPlugin by Ben12345rocks.
the class AdminVoteHelp method adminHelpText.
/**
* Admin help text.
*
* @param sender the sender
* @return the array list
*/
public ArrayList<TextComponent> adminHelpText(CommandSender sender) {
ArrayList<TextComponent> msg = new ArrayList<TextComponent>();
HashMap<String, TextComponent> unsorted = new HashMap<String, TextComponent>();
boolean requirePerms = plugin.getConfigFile().getFormatCommandsVoteHelpRequirePermission();
for (CommandHandler cmdHandle : plugin.getAdminVoteCommand()) {
if (!requirePerms || cmdHandle.hasPerm(sender)) {
unsorted.put(cmdHandle.getHelpLineCommand("/adminvote"), cmdHandle.getHelpLine("/adminvote"));
}
}
ArrayList<String> unsortedList = new ArrayList<String>();
unsortedList.addAll(unsorted.keySet());
Collections.sort(unsortedList, String.CASE_INSENSITIVE_ORDER);
for (String cmd : unsortedList) {
msg.add(unsorted.get(cmd));
}
return msg;
}
use of net.md_5.bungee.api.chat.TextComponent in project VotingPlugin by Ben12345rocks.
the class VotingPluginBungee method addVoteParty.
public void addVoteParty() {
if (getConfig().getVotePartyEnabled()) {
addCurrentVotePartyVotes(1);
if (votePartyVotes >= currentVotePartyVotesRequired) {
debug("Vote party reached");
addCurrentVotePartyVotes(-currentVotePartyVotesRequired);
currentVotePartyVotesRequired += getConfig().getVotePartyIncreaseVotesRequired();
voteCacheFile.setVotePartyInreaseVotesRequired(voteCacheFile.getVotePartyInreaseVotesRequired() + getConfig().getVotePartyIncreaseVotesRequired());
if (!getConfig().getVotePartyBroadcast().isEmpty()) {
getProxy().broadcast(new TextComponent(ChatColor.translateAlternateColorCodes('&', getConfig().getVotePartyBroadcast())));
}
for (String command : getConfig().getVotePartyBungeeCommands()) {
getProxy().getPluginManager().dispatchCommand(getProxy().getConsole(), command);
}
if (method.equals(BungeeMethod.PLUGINMESSAGING)) {
if (getConfig().getVotePartySendToAllServers()) {
for (Entry<String, ServerInfo> entry : getProxy().getServers().entrySet()) {
sendVoteParty(entry.getKey(), entry.getValue());
}
} else {
for (String server : getConfig().getVotePartyServersToSend()) {
ServerInfo serverInfo = getProxy().getServerInfo(server);
if (serverInfo != null) {
sendVoteParty(server, serverInfo);
}
}
}
}
}
voteCacheFile.save();
}
}
use of net.md_5.bungee.api.chat.TextComponent in project VotingPlugin by Ben12345rocks.
the class VotingPluginBungeeCommand method execute.
@Override
public void execute(CommandSender sender, String[] args) {
if (sender.hasPermission("votingplugin.admin")) {
if (args.length > 0) {
if (args[0].equalsIgnoreCase("reload")) {
bungee.reload(false);
sender.sendMessage(new TextComponent("&aReloading VotingPluginBungee"));
}
if (args[0].equalsIgnoreCase("reloadmysql")) {
bungee.reload(true);
sender.sendMessage(new TextComponent("&aReloading VotingPluginBungee with MySQL"));
}
if (args[0].equalsIgnoreCase("vote")) {
if (args.length >= 2) {
String user = args[1];
String site = args[2];
bungee.vote(user, site, false);
sender.sendMessage(new TextComponent("Sending vote"));
}
}
if (args[0].equalsIgnoreCase("status")) {
if (bungee.getMethod().equals(BungeeMethod.SOCKETS)) {
sender.sendMessage(new TextComponent("&aChecking status, waiting for response, check console, method: sockets"));
bungee.status(sender);
} else if (bungee.getMethod().equals(BungeeMethod.PLUGINMESSAGING)) {
sender.sendMessage(new TextComponent("&aChecking status, waiting for response, check console, method: plugin messaging"));
bungee.status(sender);
} else {
sender.sendMessage(new TextComponent("&aNot using socket/pluginmessage method, command unavailable"));
}
}
if (args[0].equalsIgnoreCase("help")) {
TextComponent[] msg = new TextComponent[4];
msg[0] = new TextComponent("&avotingpluginbungee reload - Reload plugin");
msg[1] = new TextComponent("&avotingpluginbungee reloadmysql - Reload plugin including mysql");
msg[2] = new TextComponent("&avotingpluginbungee vote (player) (servicesite) - Send bungee vote");
msg[3] = new TextComponent("&avotingpluginbungee status - Check socket connection status");
sender.sendMessage(msg);
}
}
} else {
sender.sendMessage(new TextComponent("&cYou do not have permission to do this!"));
}
}
Aggregations