Add JSON to any PlaceholderAPI placeholder.

This commit is contained in:
Aust1n46 2021-07-31 17:01:37 -04:00
parent dba9ab7d12
commit 47ee2c3ccf
7 changed files with 180 additions and 250 deletions

View File

@ -6,6 +6,7 @@ import me.clip.placeholderapi.PlaceholderAPIPlugin;
import me.clip.placeholderapi.expansion.PlaceholderExpansion;
import mineverse.Aust1n46.chat.api.MineverseChatAPI;
import mineverse.Aust1n46.chat.api.MineverseChatPlayer;
import mineverse.Aust1n46.chat.channel.ChatChannel;
public class VentureChatPlaceholders extends PlaceholderExpansion {
@Override
@ -24,49 +25,32 @@ public class VentureChatPlaceholders extends PlaceholderExpansion {
return "value for that identifier *";
}
if (identifier.startsWith("channel_")) {
if (mcp.getCurrentChannel() == null) {
ChatChannel currentChannel = mcp.hasQuickChannel() ? mcp.getQuickChannel() : mcp.getCurrentChannel();
if (currentChannel == null) {
return "";
}
switch (identifier) {
case "channel_name":
return mcp.getCurrentChannel().getName();
return currentChannel.getName();
case "channel_alias":
return mcp.getCurrentChannel().getAlias();
return currentChannel.getAlias();
case "channel_color":
return this.textToHex(mcp.getCurrentChannel().getColor());
return currentChannel.getColor();
case "channel_chatcolor":
return this.textToHex(mcp.getCurrentChannel().getChatColor());
return currentChannel.getChatColor();
case "channel_is_bungee":
return mcp.getCurrentChannel().getBungee() ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
return currentChannel.getBungee() ? PlaceholderAPIPlugin.booleanTrue() : PlaceholderAPIPlugin.booleanFalse();
case "channel_cooldown":
return mcp.getCurrentChannel().getCooldown() + "";
return currentChannel.getCooldown() + "";
case "channel_distance":
return mcp.getCurrentChannel().getDistance() + "";
return currentChannel.getDistance() + "";
case "channel_prefix":
return currentChannel.getPrefix();
}
}
return null;
}
private String textToHex(String color) {
if (color.equalsIgnoreCase("black")) return "0";
if (color.equalsIgnoreCase("dark_blue")) return "1";
if (color.equalsIgnoreCase("dark_green")) return "2";
if (color.equalsIgnoreCase("dark_aqua")) return "3";
if (color.equalsIgnoreCase("dark_red")) return "4";
if (color.equalsIgnoreCase("dark_purple")) return "5";
if (color.equalsIgnoreCase("gold")) return "6";
if (color.equalsIgnoreCase("gray")) return "7";
if (color.equalsIgnoreCase("dark_gray")) return "8";
if (color.equalsIgnoreCase("blue")) return "9";
if (color.equalsIgnoreCase("green")) return "a";
if (color.equalsIgnoreCase("aqua")) return "b";
if (color.equalsIgnoreCase("red")) return "c";
if (color.equalsIgnoreCase("light_purple")) return "d";
if (color.equalsIgnoreCase("yellow")) return "e";
if (color.equalsIgnoreCase("white")) return "f";
return "f";
}
@Override
public boolean persist() {
return true;

View File

@ -47,6 +47,7 @@ public class ChatChannel {
private boolean bungee;
private String format;
private int cooldown;
private String prefix;
/**
* Read chat channels from config file and initialize channel array.
@ -73,8 +74,9 @@ public class ChatChannel {
double distance = cs.getDouble(key + ".distance", (double) 0);
int cooldown = cs.getInt(key + ".cooldown", 0);
boolean autojoin = cs.getBoolean(key + ".autojoin", false);
String prefix = cs.getString(key + ".channel_prefix");
ChatChannel chatChannel = new ChatChannel(name, color, chatColor, permission, speakPermission, mutable,
filter, defaultChannel, alias, distance, autojoin, bungee, cooldown, format);
filter, defaultChannel, alias, distance, autojoin, bungee, cooldown, prefix, format);
channels[counter++] = chatChannel;
chatChannels.put(name.toLowerCase(), chatChannel);
chatChannels.put(alias.toLowerCase(), chatChannel);
@ -87,7 +89,7 @@ public class ChatChannel {
if(defaultChatChannel == null) {
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll("&8[&eVentureChat&8]&e - &cNo default channel found!"));
defaultChatChannel = new ChatChannel("MissingDefault", "red", "red", "None", "None", false,
true, true, "md", 0, true, false, 0, "&f[&cMissingDefault&f] {vault_prefix} {player_displayname}&c:");
true, true, "md", 0, true, false, 0, "&f[&cMissingDefault&f]", "{venturechat_channel_prefix} {vault_prefix}{player_displayname}&c:");
defaultColor = defaultChatChannel.getColor();
chatChannels.put("missingdefault", defaultChatChannel);
chatChannels.put("md", defaultChatChannel);
@ -192,7 +194,7 @@ public class ChatChannel {
*/
public ChatChannel(String name, String color, String chatColor, String permission, String speakPermission,
boolean mutable, boolean filter, boolean defaultChannel, String alias, double distance, boolean autojoin,
boolean bungee, int cooldown, String format) {
boolean bungee, int cooldown, String prefix, String format) {
this.name = name;
this.color = color;
this.chatColor = chatColor;
@ -207,6 +209,7 @@ public class ChatChannel {
this.bungee = bungee;
this.cooldown = cooldown;
this.format = format;
this.prefix = prefix;
}
/**
@ -274,6 +277,14 @@ public class ChatChannel {
return cooldown;
}
/**
* Get the prefix of the chat channel.
* @return String
*/
public String getPrefix() {
return prefix;
}
/**
* Check if the chat channel is BungeeCord enabled.
*

View File

@ -0,0 +1,33 @@
package mineverse.Aust1n46.chat.json;
import java.util.List;
public class JsonAttribute {
private String name;
private List<String> hoverText;
private String clickAction;
private String clickText;
public JsonAttribute(String name, List<String> hoverText, String clickAction, String clickText) {
this.name = name;
this.hoverText = hoverText;
this.clickAction = clickAction;
this.clickText = clickText;
}
public String getName() {
return name;
}
public List<String> getHoverText() {
return hoverText;
}
public String getClickAction() {
return clickAction;
}
public String getClickText() {
return clickText;
}
}

View File

@ -1,5 +1,6 @@
package mineverse.Aust1n46.chat.json;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
@ -12,48 +13,30 @@ public class JsonFormat {
private static MineverseChat plugin = MineverseChat.getInstance();
private static HashMap<String, JsonFormat> jsonFormats;
private List<String> hoverTextName;
private List<String> hoverTextPrefix;
private List<String> hoverTextSuffix;
private String clickName;
private String clickNameText;
private String clickPrefix;
private String clickPrefixText;
private String clickSuffix;
private String clickSuffixText;
private List<JsonAttribute> jsonAttributes;
private int priority;
private String name;
public JsonFormat(String name, int priority, List<String> hoverTextName, String clickName, String clickNameText, List<String> hoverTextPrefix, String clickPrefix, String clickPrefixText, String clickSuffix, String clickSuffixText, List<String> hoverTextSuffix) {
public JsonFormat(String name, int priority, List<JsonAttribute> jsonAttributes) {
this.name = name;
this.priority = priority;
this.hoverTextName = hoverTextName;
this.clickNameText = clickNameText;
this.hoverTextPrefix = hoverTextPrefix;
this.clickPrefix = clickPrefix;
this.clickPrefixText = clickPrefixText;
this.clickName = clickName;
this.clickSuffix = clickSuffix;
this.clickSuffixText = clickSuffixText;
this.hoverTextSuffix = hoverTextSuffix;
this.jsonAttributes = jsonAttributes;
}
public static void initialize() {
jsonFormats = new HashMap<String, JsonFormat>();
ConfigurationSection cs = plugin.getConfig().getConfigurationSection("jsonformatting");
for (String key : cs.getKeys(false)) {
String name = key;
int priority = cs.getInt(key + ".priority", 0);
List<String> hoverTextName = cs.getStringList(key + ".hover_name");
List<String> hoverTextPrefix = cs.getStringList(key + ".hover_prefix");
List<String> hoverTextSuffix = cs.getStringList(key + ".hover_suffix");
String clickPrefix = cs.getString(key + ".click_prefix");
String clickName = cs.getString(key + ".click_name");
String clickNameText = cs.getString(key + ".click_name_text");
String clickPrefixText = cs.getString(key + ".click_prefix_text");
String clickSuffixText = cs.getString(key + ".click_suffix_text");
String clickSuffix = cs.getString(key + ".click_suffix");
jsonFormats.put(name.toLowerCase(), new JsonFormat(name, priority, hoverTextName, clickName, clickNameText, hoverTextPrefix, clickPrefix, clickPrefixText, clickSuffix, clickSuffixText, hoverTextSuffix));
ConfigurationSection jsonFormatSection = plugin.getConfig().getConfigurationSection("jsonformatting");
for (String jsonFormat : jsonFormatSection.getKeys(false)) {
int priority = jsonFormatSection.getInt(jsonFormat + ".priority", 0);
List<JsonAttribute> jsonAttributes = new ArrayList<>();
ConfigurationSection jsonAttributeSection = jsonFormatSection.getConfigurationSection(jsonFormat + ".json_attributes");
for (String attribute : jsonAttributeSection.getKeys(false)) {
List<String> hoverText = jsonAttributeSection.getStringList(attribute + ".hover_text");
String clickAction = jsonAttributeSection.getString(attribute + ".click_action");
String clickText = jsonAttributeSection.getString(attribute + ".click_text");
jsonAttributes.add(new JsonAttribute(attribute, hoverText, clickAction, clickText));
}
jsonFormats.put(jsonFormat.toLowerCase(), new JsonFormat(jsonFormat, priority, jsonAttributes));
}
}
@ -69,43 +52,11 @@ public class JsonFormat {
return name;
}
public String getClickName() {
return clickName;
}
public String getClickNameText() {
return clickNameText;
}
public String getClickSuffix() {
return clickSuffix;
}
public String getClickSuffixText() {
return clickSuffixText;
}
public int getPriority() {
return priority;
}
public List<String> getHoverTextName() {
return hoverTextName;
}
public List<String> getHoverTextPrefix() {
return hoverTextPrefix;
}
public List<String> getHoverTextSuffix() {
return hoverTextSuffix;
}
public String getClickPrefix() {
return clickPrefix;
}
public String getClickPrefixText() {
return clickPrefixText;
public List<JsonAttribute> getJsonAttributes() {
return jsonAttributes;
}
}

View File

@ -343,11 +343,12 @@ public class ChatListener implements Listener {
chDistance = eventChannel.getDistance();
}
format = Format.FormatStringAll(PlaceholderAPI.setBracketPlaceholders(mcp.getPlayer(), Format.FormatStringAll(eventChannel.getFormat())));
if(plugin.getConfig().getBoolean("formatcleaner", false)) {
format = format.replace("[]", " ");
format = format.replace(" ", " ").replace(" ", " ").replace(" ", " ");
}
// format = Format.FormatStringAll(PlaceholderAPI.setBracketPlaceholders(mcp.getPlayer(), Format.FormatStringAll(eventChannel.getFormat())));
// if(plugin.getConfig().getBoolean("formatcleaner", false)) {
// format = format.replace("[]", " ");
// format = format.replace(" ", " ").replace(" ", " ").replace(" ", " ");
// }
format = Format.FormatStringAll(eventChannel.getFormat());
filterthis = eventChannel.isFiltered();
if(filterthis) {
@ -478,7 +479,6 @@ public class ChatListener implements Listener {
if(!mcp.isQuickChat()) {
chat = " " + chat;
}
mcp.setQuickChat(false);
if(curColor.equalsIgnoreCase("None")) {
chat = Format.getLastCode(format) + chat;
}
@ -497,6 +497,8 @@ public class ChatListener implements Listener {
Bukkit.getServer().getPluginManager().callEvent(ventureChatEvent);
//Call method to send the processed chat
handleVentureChatEvent(ventureChatEvent);
// Reset quick chat flag
mcp.setQuickChat(false);
}
public void handleVentureChatEvent(VentureChatEvent event) {

View File

@ -22,9 +22,9 @@ import com.comphenix.protocol.events.PacketContainer;
import com.comphenix.protocol.wrappers.WrappedChatComponent;
import me.clip.placeholderapi.PlaceholderAPI;
import mineverse.Aust1n46.chat.MineverseChat;
import mineverse.Aust1n46.chat.api.MineverseChatAPI;
import mineverse.Aust1n46.chat.api.MineverseChatPlayer;
import mineverse.Aust1n46.chat.json.JsonAttribute;
import mineverse.Aust1n46.chat.json.JsonFormat;
import mineverse.Aust1n46.chat.localization.LocalizedMessage;
import mineverse.Aust1n46.chat.versions.VersionHandler;
@ -67,32 +67,7 @@ public class Format {
String f = escapeJsonChars(format);
String c = escapeJsonChars(chat);
String json = "[\"\",{\"text\":\"\",\"extra\":[";
String prefix = "";
String suffix = "";
try {
prefix = FormatStringAll(MineverseChat.getVaultChat().getPlayerPrefix(sender.getPlayer()));
suffix = FormatStringAll(MineverseChat.getVaultChat().getPlayerSuffix(sender.getPlayer()));
// Don't apply JSON if the prefix or suffix is just a color code
if (suffix.isEmpty() || (suffix.length() == 2 && suffix.substring(1).matches("[0-9a-fA-F]"))) {
suffix = "venturechat_no_suffix_code";
}
if (prefix.isEmpty() || (prefix.length() == 2 && prefix.substring(1).matches("[0-9a-fA-F]"))) {
prefix = "venturechat_no_prefix_code";
}
} catch (Exception e) {
System.out.println("Exception?" + e.getLocalizedMessage());
if (getInstance().getConfig().getString("loglevel", "info").equals("debug")) {
Bukkit.getConsoleSender().sendMessage(Format.FormatStringAll(
"&8[&eVentureChat&8]&e - Prefix and / or suffix don't exist, setting to nothing."));
}
suffix = "venturechat_no_suffix_code";
prefix = "venturechat_no_prefix_code";
}
String nickname = "";
if (sender.getPlayer() != null) {
nickname = FormatStringAll(sender.getPlayer().getDisplayName());
}
json += convertPlaceholders(f, JSONformat, prefix, nickname, suffix, sender);
json += convertPlaceholders(f, JSONformat, sender);
json += "]}";
json += "," + convertLinks(c);
json += "]";
@ -118,61 +93,47 @@ public class Format {
* @param icp
* @return {@link String}
*/
private static String convertPlaceholders(String s, JsonFormat format, String prefix, String nickname,
String suffix, MineverseChatPlayer icp) {
private static String convertPlaceholders(String s, JsonFormat format, MineverseChatPlayer icp) {
String remaining = s;
String temp = "";
int indexStart = -1;
int indexEnd = -1;
String placeholder = "";
String formattedPlaceholder = "";
String lastCode = DEFAULT_COLOR_CODE;
do {
Pattern pattern = Pattern.compile(
"(" + escapeAllRegex(prefix) + "|" + escapeAllRegex(nickname) + "|" + escapeAllRegex(suffix) + ")");
Pattern pattern = Pattern.compile("(\\{[A-Za-z0-9-_]+\\})");
Matcher matcher = pattern.matcher(remaining);
if (matcher.find()) {
indexStart = matcher.start();
indexEnd = matcher.end();
placeholder = remaining.substring(indexStart, indexEnd);
formattedPlaceholder = PlaceholderAPI.setBracketPlaceholders(icp.getPlayer(), placeholder);
temp += convertToJsonColors(lastCode + remaining.substring(0, indexStart)) + ",";
lastCode = getLastCode(lastCode + remaining.substring(0, indexStart));
String action = "";
String text = "";
String hover = "";
if (placeholder.contains(prefix)) {
action = format.getClickPrefix();
for (JsonAttribute jsonAttribute : format.getJsonAttributes()) {
if (placeholder.contains(jsonAttribute.getName().replace("{", "").replace("}", ""))) {
action = jsonAttribute.getClickAction();
text = Format.FormatStringAll(
PlaceholderAPI.setBracketPlaceholders(icp.getPlayer(), format.getClickPrefixText()));
for (String st : format.getHoverTextPrefix()) {
PlaceholderAPI.setBracketPlaceholders(icp.getPlayer(), jsonAttribute.getClickText()));
for (String st : jsonAttribute.getHoverText()) {
hover += Format.FormatStringAll(st) + "\n";
}
}
if (placeholder.contains(nickname)) {
action = format.getClickName();
text = Format.FormatStringAll(
PlaceholderAPI.setBracketPlaceholders(icp.getPlayer(), format.getClickNameText()));
for (String st : format.getHoverTextName()) {
hover += Format.FormatStringAll(st) + "\n";
}
}
if (placeholder.contains(suffix)) {
action = format.getClickSuffix();
text = Format.FormatStringAll(
PlaceholderAPI.setBracketPlaceholders(icp.getPlayer(), format.getClickSuffixText()));
for (String st : format.getHoverTextSuffix()) {
hover += Format.FormatStringAll(st) + "\n";
}
}
if(!hover.isEmpty()) {
hover = Format.FormatStringAll(
PlaceholderAPI.setBracketPlaceholders(icp.getPlayer(), hover.substring(0, hover.length() - 1)));
}
temp += convertToJsonColors(lastCode + placeholder,
temp += convertToJsonColors(lastCode + formattedPlaceholder,
",\"clickEvent\":{\"action\":\"" + action + "\",\"value\":\"" + text
+ "\"},\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"\",\"extra\":["
+ convertToJsonColors(hover) + "]}}")
+ ",";
lastCode = getLastCode(lastCode + placeholder);
lastCode = getLastCode(lastCode + formattedPlaceholder);
remaining = remaining.substring(indexEnd);
} else {
temp += convertToJsonColors(lastCode + remaining);

View File

@ -140,33 +140,41 @@ messageremovertext: '&c&o<message removed>'
jsonformatting:
Default: # This default format is required! Do not delete or rename it!
priority: 2147483647 # Integer.MAX_VALUE
hover_name:
json_attributes:
player_displayname:
hover_text:
- '&6I have no rank!'
click_name: 'suggest_command'
click_name_text: '/msg {player_name}'
hover_prefix:
- '&dI am default!'
click_prefix: 'run_command'
click_prefix_text: '/help'
hover_suffix:
- '&dI am default suffix!'
click_suffix: 'suggest_command'
click_suffix_text: '/msg {player_name}'
click_action: 'suggest_command'
click_text: '/msg {player_name}'
vault_prefix:
hover_text:
- '&cI am default!'
click_action: 'run_command'
click_text: '/help'
venturechat_channel_prefix:
hover_text:
- '&fChannel: {venturechat_channel_color}{venturechat_channel_name}'
click_action: 'run_command'
click_text: '/chlist'
Owner:
priority: 1 # Lowest Priority
hover_name:
priority: 1
json_attributes:
player_displayname:
hover_text:
- '&cOwner of the server!'
- '&bMessage me for help!'
click_name: 'suggest_command'
click_name_text: '/msg {player_name}'
hover_prefix:
click_action: 'suggest_command'
click_text: '/msg {player_name}'
vault_prefix:
hover_text:
- '&dServer Owner'
click_prefix: 'run_command'
click_prefix_text: '/help'
hover_suffix:
- '&dI am Owner suffix!'
click_suffix: 'suggest_command'
click_suffix_text: '/msg {player_name}'
click_action: 'run_command'
click_text: '/help'
venturechat_channel_prefix:
hover_text:
- '&fChannel: {venturechat_channel_color}{venturechat_channel_name}'
click_action: 'run_command'
click_text: '/chlist'
# The icon is the block shown in the GUI
# The text is the display name of the block icon
@ -282,62 +290,6 @@ enable_factions_channel: false
# Important!!!
# If you delete a channel, restart the server! Do not use /chatreload!!!
channels:
GroupChatColorExample:
color: '#706C1E'
chatcolor: 'None'
mutable: true
filter: true
autojoin: true
default: false
distance: 0
cooldown: 0
bungeecord: false
alias: ge
permissions: None
speak_permissions: None
format: '&f[#706C1EGroupColorChat&f] {vault_prefix} {player_displayname}#706C1E:{vault_suffix}'
HexExample:
color: '#ff0000'
chatcolor: '#ff0000'
mutable: true
filter: true
autojoin: true
default: false
distance: 0
cooldown: 0
bungeecord: false
alias: he
permissions: None
speak_permissions: None
format: '&f[#ff0000Hex&f] {vault_prefix} {player_displayname}#ff0000:'
BungeeExample:
color: gold
chatcolor: gold
mutable: true
filter: true
autojoin: true
default: false
distance: 0
cooldown: 3
bungeecord: true
alias: be
permissions: None
speak_permissions: None
format: '&f[&6Network&f] {vault_prefix} {player_displayname}&6:'
AnnouncementExample:
color: red
chatcolor: red
mutable: false
filter: false
autojoin: true
default: false
distance: 0
cooldown: 0
bungeecord: true
alias: announce
permissions: None
speak_permissions: announcement
format: '&f[&aServer Announcement&f] {vault_prefix} {player_displayname}&c:'
Global:
color: dark_green
chatcolor: dark_green
@ -351,7 +303,38 @@ channels:
alias: g
permissions: None
speak_permissions: None
format: '&f[&2Global&f] {vault_prefix} {player_displayname}&2:'
channel_prefix: '&f[&2Global&f]'
format: '{venturechat_channel_prefix} {vault_prefix}{player_displayname}&2:'
Network:
color: gold
chatcolor: gold
mutable: true
filter: true
autojoin: true
default: false
distance: 0
cooldown: 3
bungeecord: true
alias: be
permissions: None
speak_permissions: None
channel_prefix: '&f[&6Network&f]'
format: '{venturechat_channel_prefix} {vault_prefix}{player_displayname}&6:'
Announcement:
color: red
chatcolor: red
mutable: false
filter: false
autojoin: true
default: false
distance: 0
cooldown: 0
bungeecord: true
alias: announce
permissions: None
speak_permissions: announcement
channel_prefix: '&f[&aServer Announcement&f]'
format: '{venturechat_channel_prefix} {vault_prefix}{player_displayname}&c:'
Staff:
color: green
chatcolor: green
@ -365,7 +348,8 @@ channels:
alias: st
permissions: staffchannel
speak_permissions: None
format: '&f[&aStaff&f] {vault_prefix} {player_displayname}&a:'
channel_prefix: '&f[&aStaff&f]'
format: '{venturechat_channel_prefix} {vault_prefix}{player_displayname}&a:'
Donator:
color: light_purple
chatcolor: light_purple
@ -379,7 +363,8 @@ channels:
alias: d
permissions: donatorchannel
speak_permissions: None
format: '&f[&dDonator&f] {vault_prefix} {player_displayname}&d:'
channel_prefix: '&f[&dDonator&f]'
format: '{venturechat_channel_prefix} {vault_prefix}{player_displayname}&d:'
Help:
color: aqua
chatcolor: aqua
@ -393,7 +378,8 @@ channels:
alias: h
permissions: None
speak_permissions: None
format: '&f[&bHelp&f] {vault_prefix} {player_displayname}&b:'
channel_prefix: '&f[&bHelp&f]'
format: '{venturechat_channel_prefix} {vault_prefix}{player_displayname}&b:'
Trade:
color: dark_aqua
chatcolor: dark_aqua
@ -407,7 +393,8 @@ channels:
alias: t
permissions: None
speak_permissions: None
format: '&f[&3Trade&f] {vault_prefix} {player_displayname}&3:'
channel_prefix: '&f[&3Trade&f]'
format: '{venturechat_channel_prefix} {vault_prefix}{player_displayname}&3:'
Local:
color: yellow
chatcolor: yellow
@ -421,4 +408,5 @@ channels:
alias: l
permissions: None
speak_permissions: None
format: '&f[&eLocal&f] {vault_prefix} {player_displayname}&e:'
channel_prefix: '&f[&eLocal&f]'
format: '{venturechat_channel_prefix} {vault_prefix}{player_displayname}&e:'