diff --git a/config.yml b/config.yml index ad85733..01df505 100644 --- a/config.yml +++ b/config.yml @@ -45,18 +45,19 @@ Blocked_Words: # This is a system to create JSON pockets with in a message. # It uses a placerholder system to use. #The name of the component: -example: +JSON_Components: + example: #The base message of the component - Message: 'Example Message' - #The component events - Events: + Message: 'Example Message' + #The component events + Events: #A click event {Optional} - Click: + Click: # One and only one of the Click Operations. Full list includes: {Open_URL, Run_Command, Suggest_Command} - Open_URL: 'https://www.spigotmc.org/' - # When the player hovers over the base text. - Hover: - Show_Text: + Open_URL: 'https://www.spigotmc.org/' + # When the player hovers over the base text. + Hover: + Show_Text: - 'This is the first line' - '&cThis is the second line' ############################################## diff --git a/src/me/ryandw11/ultrachat/UltraChat.java b/src/me/ryandw11/ultrachat/UltraChat.java index b7d52b3..e50d3c2 100644 --- a/src/me/ryandw11/ultrachat/UltraChat.java +++ b/src/me/ryandw11/ultrachat/UltraChat.java @@ -34,6 +34,9 @@ import me.ryandw11.ultrachat.listner.StopChat; import me.ryandw11.ultrachat.pluginhooks.AdvancedBanMute; import me.ryandw11.ultrachat.pluginhooks.EssentialsMute; import me.ryandw11.ultrachat.util.Metrics; +import me.ryandw11.ultrachat.util.papi.PAPIDisabled; +import me.ryandw11.ultrachat.util.papi.PAPIEnabled; +import me.ryandw11.ultrachat.util.papi.PlaceHolderAPIHook; import net.milkbowl.vault.chat.Chat; import net.milkbowl.vault.permission.Permission; @@ -46,7 +49,7 @@ import org.bukkit.plugin.java.JavaPlugin; /** * Main Class * @author Ryandw11 - * @version 2.3.1 + * @version 2.4 * Updated for 1.14. * (Very few API methods here) */ @@ -63,6 +66,8 @@ public class UltraChat extends JavaPlugin{ public String defaultChannel; public ArrayList stafftoggle = new ArrayList<>(); public ArrayList spytoggle = new ArrayList<>(); + + public PlaceHolderAPIHook papi; public File datafile = new File(getDataFolder() + "/data/players.yml"); public FileConfiguration data = YamlConfiguration.loadConfiguration(datafile); @@ -90,15 +95,13 @@ public class UltraChat extends JavaPlugin{ Bukkit.getPluginManager().disablePlugin(this); return; } - if (getServer().getPluginManager().getPlugin("PlaceholderAPI") == null) { - getLogger().severe("§cWarning: You do not have PlaceholderAPI installed! This plugin is now disabled!"); - Bukkit.getPluginManager().disablePlugin(this); - return; - } - else{ - getLogger().info(String.format("UltraChat is enabled and running fine! V: %s", getDescription().getVersion())); + if (getServer().getPluginManager().getPlugin("PlaceholderAPI") != null) { getLogger().info("Hooked into PlaceholderAPI! You can use the place holders!"); + papi = new PAPIEnabled(); + }else { + papi = new PAPIDisabled(); } + getLogger().info(String.format("UltraChat is enabled and running fine! V: %s", getDescription().getVersion())); if(getServer().getPluginManager().getPlugin("AdvancedBan") != null && getConfig().getBoolean("pluginhooks.AdvancedBan")){ getLogger().info("AdvancedBan detected! Activating hook!"); getLogger().info("Mutes will now work with the chat types."); diff --git a/src/me/ryandw11/ultrachat/api/JSONChatBuilder.java b/src/me/ryandw11/ultrachat/api/JSONChatBuilder.java index 25fd76c..6f3be30 100644 --- a/src/me/ryandw11/ultrachat/api/JSONChatBuilder.java +++ b/src/me/ryandw11/ultrachat/api/JSONChatBuilder.java @@ -2,6 +2,9 @@ package me.ryandw11.ultrachat.api; import java.util.List; +import org.bukkit.entity.Player; + +import me.ryandw11.ultrachat.UltraChat; import net.md_5.bungee.api.ChatColor; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ClickEvent; @@ -18,19 +21,42 @@ import net.md_5.bungee.api.chat.HoverEvent; public class JSONChatBuilder { private ComponentBuilder displayMessage; + private Player p; + /** + * Create a JSON Message + * @param displayMessage The base string that you would hover or click on. + */ public JSONChatBuilder(String displayMessage) { this.displayMessage = new ComponentBuilder(displayMessage); + p = null; } /** - * Add a hove message. + * Create a JSON Message + * @param displayMessage The base string that you would hover or click on. + * @param p The player to use when doing PlaceHolders. + */ + public JSONChatBuilder(String displayMessage, Player p) { + this.displayMessage = new ComponentBuilder(displayMessage); + this.p = p; + } + + /** + * Add a hove message. (ChatColors are translated) * @param lore Lore */ public JSONChatBuilder setHoverShowText(List lore) { ComponentBuilder lores = new ComponentBuilder(""); + int i = 0; for(String s : lore) { - lores.append(ChatColor.translateAlternateColorCodes('&', s)); + if(p != null) + lores.append(UltraChat.plugin.papi.translatePlaceholders(ChatColor.translateAlternateColorCodes('&', s), p)); + else + lores.append(ChatColor.translateAlternateColorCodes('&', s)); + if(i < lore.size() - 1) + lores.append("\n"); + i++; } this.displayMessage.event(new HoverEvent(HoverEvent.Action.SHOW_TEXT, lores.create())); return this; diff --git a/src/me/ryandw11/ultrachat/api/MessageBuilder.java b/src/me/ryandw11/ultrachat/api/MessageBuilder.java index 68af6c3..1a49d63 100644 --- a/src/me/ryandw11/ultrachat/api/MessageBuilder.java +++ b/src/me/ryandw11/ultrachat/api/MessageBuilder.java @@ -2,6 +2,8 @@ package me.ryandw11.ultrachat.api; import net.md_5.bungee.api.chat.BaseComponent; import net.md_5.bungee.api.chat.ComponentBuilder; +import net.md_5.bungee.api.chat.ComponentBuilder.FormatRetention; +import net.md_5.bungee.api.chat.TextComponent; /** * Easy Builder to build several JSON sections into a single message. @@ -23,7 +25,7 @@ public class MessageBuilder { * @return The MessageBuilder to chain. */ public MessageBuilder addJSON(JSONChatBuilder json) { - compon.append(json.build()); + compon.append(json.build(), FormatRetention.FORMATTING); return this; } @@ -33,7 +35,8 @@ public class MessageBuilder { * @return the builder to chain */ public MessageBuilder addString(String s) { - compon.append(s); + TextComponent tc = new TextComponent(s); + compon.append(tc, FormatRetention.FORMATTING); return this; } @@ -43,7 +46,7 @@ public class MessageBuilder { * @return The Buider to chain */ public MessageBuilder addBaseComponent(BaseComponent[] bc) { - compon.append(bc); + compon.append(bc, FormatRetention.FORMATTING); return this; } diff --git a/src/me/ryandw11/ultrachat/api/managers/JComponentManager.java b/src/me/ryandw11/ultrachat/api/managers/JComponentManager.java new file mode 100644 index 0000000..891841f --- /dev/null +++ b/src/me/ryandw11/ultrachat/api/managers/JComponentManager.java @@ -0,0 +1,182 @@ +package me.ryandw11.ultrachat.api.managers; + +import java.util.ArrayList; +import java.util.List; + +import org.bukkit.Bukkit; +import org.bukkit.configuration.ConfigurationSection; +import org.bukkit.entity.Player; + +import me.ryandw11.ultrachat.UltraChat; +import me.ryandw11.ultrachat.api.JSONChatBuilder; +import me.ryandw11.ultrachat.api.MessageBuilder; +import net.md_5.bungee.api.chat.BaseComponent; + +public class JComponentManager { + + /** + * Get the final basecomponent from a message. + * @param message The string message + * @return The final basecomponent + */ + public static BaseComponent[] formatComponents(String message) { + /* + * Parse the message for JSON components + */ + List msgs = new ArrayList(); + List compName = new ArrayList(); + + List fin = new ArrayList(); + + String temps = ""; + for(int i = 0; i < message.length(); i++) { + char c = message.charAt(i); + if(c == '{') { + msgs.add(temps); + fin.add(temps); + temps = ""; + } + else if(c == '}') { + compName.add(temps); + fin.add(temps); + temps = ""; + } + else { + temps += c; + } + } + msgs.add(temps); + fin.add(temps); + /* + * Generate the final BaseComponent + */ + MessageBuilder mb = new MessageBuilder(); + + // Loops through the final array. + for(String s : fin) { + // If the message is a normal message. + if(msgs.contains(s)) { + mb.addString(s); + } + // If the message is a component + else if(compName.contains(s)) { + ConfigurationSection cs = null; + for(String st : UltraChat.plugin.getConfig().getConfigurationSection("JSON_Components").getKeys(false)) { + if(st.equals(s)) { + cs = UltraChat.plugin.getConfig().getConfigurationSection("JSON_Components." + st); + break; + } + } + + if(cs == null) { + Bukkit.getLogger().warning("Error: Can not find JSON Component named: " + s); + }else { + JSONChatBuilder jsb = new JSONChatBuilder(cs.getString("Message")); + // If the component contains the click event. + if(cs.contains("Events.Click")) { + if(cs.contains("Events.Click.Open_URL")) + jsb.setClickOpenUrl(cs.getString("Events.Click.Open_URL")); + else if(cs.contains("Events.Click.Run_Command")) + jsb.setClickRunCommand(cs.getString("Events.Click.Run_Command")); + else if(cs.contains("Events.Click.Suggest_Command")) + jsb.setClickSuggestCommand(cs.getString("Events.Click.Suggest_Command")); + } + // If the component contains the hover event. + if(cs.contains("Events.Hover")) { + if(cs.contains("Events.Hover.Show_Text")) + jsb.setHoverShowText(cs.getStringList("Events.Hover.Show_Text")); + } + + mb.addJSON(jsb); + } + } + } + + // Build the message builder and return it + return mb.build(); + } + + /** + * Get the final basecomponent from a message. + * @param message The string message + * @param p The player to translate PAPI for. + * @return The final basecomponent + */ + public static BaseComponent[] formatComponents(String message, Player p) { + /* + * Parse the message for JSON components + */ + List msgs = new ArrayList(); + List compName = new ArrayList(); + + List fin = new ArrayList(); + + String temps = ""; + for(int i = 0; i < message.length(); i++) { + char c = message.charAt(i); + if(c == '{') { + msgs.add(temps); + fin.add(temps); + temps = ""; + } + else if(c == '}') { + compName.add(temps); + fin.add(temps); + temps = ""; + } + else { + temps += c; + } + } + msgs.add(temps); + fin.add(temps); + /* + * Generate the final BaseComponent + */ + MessageBuilder mb = new MessageBuilder(); + + // Loops through the final array. + for(String s : fin) { + // If the message is a normal message. + if(msgs.contains(s)) { + mb.addString(s); + } + // If the message is a component + else if(compName.contains(s)) { + ConfigurationSection cs = null; + for(String st : UltraChat.plugin.getConfig().getConfigurationSection("JSON_Components").getKeys(false)) { + if(st.equals(s)) { + cs = UltraChat.plugin.getConfig().getConfigurationSection("JSON_Components." + st); + break; + } + } + + if(cs == null) { + Bukkit.getLogger().warning("Error: Can not find JSON Component named: " + s); + }else { + JSONChatBuilder jsb = new JSONChatBuilder(cs.getString("Message"), p); + // If the component contains the click event. + if(cs.contains("Events.Click")) { + if(cs.contains("Events.Click.Open_URL")) + jsb.setClickOpenUrl(cs.getString("Events.Click.Open_URL")); + else if(cs.contains("Events.Click.Run_Command")) + jsb.setClickRunCommand(cs.getString("Events.Click.Run_Command")); + else if(cs.contains("Events.Click.Suggest_Command")) + jsb.setClickSuggestCommand(cs.getString("Events.Click.Suggest_Command")); + } + // If the component contains the hover event. + if(cs.contains("Events.Hover")) { + if(cs.contains("Events.Hover.Show_Text")) + jsb.setHoverShowText(cs.getStringList("Events.Hover.Show_Text")); + } + + mb.addJSON(jsb); + } + } + } + + // Build the message builder and return it + return mb.build(); + } + +} diff --git a/src/me/ryandw11/ultrachat/listner/JoinListner.java b/src/me/ryandw11/ultrachat/listner/JoinListner.java index c43d80e..3bbe342 100644 --- a/src/me/ryandw11/ultrachat/listner/JoinListner.java +++ b/src/me/ryandw11/ultrachat/listner/JoinListner.java @@ -2,9 +2,9 @@ package me.ryandw11.ultrachat.listner; import java.util.List; -import me.clip.placeholderapi.PlaceholderAPI; import me.ryandw11.ultrachat.UltraChat; import me.ryandw11.ultrachat.api.Lang; +import me.ryandw11.ultrachat.api.managers.JComponentManager; import org.bukkit.Bukkit; import org.bukkit.ChatColor; @@ -99,8 +99,8 @@ public class JoinListner implements Listener { List motd = plugin.getConfig().getStringList("Motd"); for(String OutPut : motd){ String message = OutPut; - message = PlaceholderAPI.setPlaceholders(p, message); - p.sendMessage(ChatColor.translateAlternateColorCodes('&', message)); + message = plugin.papi.translatePlaceholders(message, p); + p.spigot().sendMessage(JComponentManager.formatComponents(ChatColor.translateAlternateColorCodes('&', message), p)); } } @@ -115,7 +115,10 @@ public class JoinListner implements Listener { public void NewPlayer(PlayerJoinEvent event){ Player p = event.getPlayer(); if(!(p.hasPlayedBefore()) && !(plugin.getConfig().getString("New_Player").equalsIgnoreCase("none"))){ - Bukkit.getServer().broadcastMessage(ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("New_Player").replace("%player%", p.getDisplayName()))); + String msg = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("New_Player").replace("%player%", p.getDisplayName())); + for(Player pl : Bukkit.getOnlinePlayers()) { + pl.spigot().sendMessage(JComponentManager.formatComponents(msg, p)); + } } } diff --git a/src/me/ryandw11/ultrachat/util/papi/PAPIDisabled.java b/src/me/ryandw11/ultrachat/util/papi/PAPIDisabled.java new file mode 100644 index 0000000..bc8b62e --- /dev/null +++ b/src/me/ryandw11/ultrachat/util/papi/PAPIDisabled.java @@ -0,0 +1,12 @@ +package me.ryandw11.ultrachat.util.papi; + +import org.bukkit.entity.Player; + +public class PAPIDisabled implements PlaceHolderAPIHook { + + @Override + public String translatePlaceholders(String s, Player p) { + return s; + } + +} diff --git a/src/me/ryandw11/ultrachat/util/papi/PAPIEnabled.java b/src/me/ryandw11/ultrachat/util/papi/PAPIEnabled.java new file mode 100644 index 0000000..80b31cf --- /dev/null +++ b/src/me/ryandw11/ultrachat/util/papi/PAPIEnabled.java @@ -0,0 +1,15 @@ +package me.ryandw11.ultrachat.util.papi; + +import org.bukkit.entity.Player; + +import me.clip.placeholderapi.PlaceholderAPI; + +public class PAPIEnabled implements PlaceHolderAPIHook{ + + @Override + public String translatePlaceholders(String s, Player p) { + return PlaceholderAPI.setPlaceholders(p, s); + } + + +} diff --git a/src/me/ryandw11/ultrachat/util/papi/PlaceHolderAPIHook.java b/src/me/ryandw11/ultrachat/util/papi/PlaceHolderAPIHook.java new file mode 100644 index 0000000..d24f378 --- /dev/null +++ b/src/me/ryandw11/ultrachat/util/papi/PlaceHolderAPIHook.java @@ -0,0 +1,7 @@ +package me.ryandw11.ultrachat.util.papi; + +import org.bukkit.entity.Player; + +public interface PlaceHolderAPIHook { + public String translatePlaceholders(String s, Player p); +}