mirror of
https://github.com/ryandw11/UltraChat.git
synced 2025-01-03 06:38:05 +01:00
API Addons
- Placeholder addons for developers to add placeholders not via placeholderapi. - Added some more useful API methods.
This commit is contained in:
parent
9ad76611e5
commit
3b6479df40
@ -8,6 +8,7 @@ import java.util.UUID;
|
||||
|
||||
import me.ryandw11.ultrachat.api.ChatType;
|
||||
import me.ryandw11.ultrachat.api.Lang;
|
||||
import me.ryandw11.ultrachat.api.managers.AddonManager;
|
||||
import me.ryandw11.ultrachat.commands.ChannelCmd;
|
||||
import me.ryandw11.ultrachat.commands.ChatCommand;
|
||||
import me.ryandw11.ultrachat.commands.CommandTabCompleter;
|
||||
@ -80,6 +81,7 @@ public class UltraChat extends JavaPlugin{
|
||||
public static File LANG_FILE;
|
||||
|
||||
private ColorGUI colorGUI;
|
||||
public AddonManager addonManager;
|
||||
|
||||
|
||||
|
||||
@ -127,6 +129,7 @@ public class UltraChat extends JavaPlugin{
|
||||
@SuppressWarnings("unused")
|
||||
Metrics m = new Metrics(this);
|
||||
}
|
||||
addonManager = new AddonManager();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -3,11 +3,15 @@ package me.ryandw11.ultrachat.api;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.ryandw11.ultrachat.UltraChat;
|
||||
import me.ryandw11.ultrachat.api.channels.ChannelBuilder;
|
||||
import me.ryandw11.ultrachat.api.channels.ChatChannel;
|
||||
import me.ryandw11.ultrachat.api.managers.AddonManager;
|
||||
import me.ryandw11.ultrachat.formatting.PlayerFormatting;
|
||||
/**
|
||||
* UltraChatAPI
|
||||
@ -48,6 +52,16 @@ public class UltraChatAPI{
|
||||
plugin.getConfig().set("Custom_Chat." + number + ".JSON", json);
|
||||
plugin.saveConfig();
|
||||
}
|
||||
|
||||
public ChatChannel getPlayerCurrentChannel(UUID player) {
|
||||
return new ChannelBuilder(plugin.data.getString(player + ".channel")).build();
|
||||
}
|
||||
|
||||
public AddonManager getAddonManager() {
|
||||
return plugin.addonManager;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get the current chat type.
|
||||
* @return chat type
|
||||
|
30
src/me/ryandw11/ultrachat/api/managers/AddonManager.java
Normal file
30
src/me/ryandw11/ultrachat/api/managers/AddonManager.java
Normal file
@ -0,0 +1,30 @@
|
||||
package me.ryandw11.ultrachat.api.managers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import me.ryandw11.ultrachat.api.placeholders.PlaceholderAddon;
|
||||
|
||||
public class AddonManager {
|
||||
private List<PlaceholderAddon> pa;
|
||||
public AddonManager() {
|
||||
pa = new ArrayList<>();
|
||||
}
|
||||
|
||||
public void addAddon(PlaceholderAddon pa) {
|
||||
this.pa.add(pa);
|
||||
}
|
||||
|
||||
public List<PlaceholderAddon> getAddons(){
|
||||
return pa;
|
||||
}
|
||||
|
||||
public String replacePlaceholders(String s, UUID p) {
|
||||
String output = s;
|
||||
for(PlaceholderAddon fs : pa) {
|
||||
output.replace(fs.getName(), fs.getString(p));
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
@ -2,6 +2,7 @@ package me.ryandw11.ultrachat.api.managers;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.configuration.ConfigurationSection;
|
||||
@ -166,8 +167,103 @@ public class JComponentManager {
|
||||
}
|
||||
// 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"));
|
||||
if(cs.contains("Events.Hover.Show_Text")) {
|
||||
List<String> finals = new ArrayList<String>();
|
||||
for(String st : cs.getStringList("Events.Hover.Show_Text")) {
|
||||
finals.add(UltraChat.plugin.addonManager.replacePlaceholders(st, p.getUniqueId()));
|
||||
}
|
||||
jsb.setHoverShowText(finals);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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, UUID p) {
|
||||
/*
|
||||
* Parse the message for JSON components
|
||||
*/
|
||||
List<String> msgs = new ArrayList<String>();
|
||||
List<String> compName = new ArrayList<String>();
|
||||
|
||||
List<String> fin = new ArrayList<String>();
|
||||
|
||||
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")) {
|
||||
List<String> finals = new ArrayList<String>();
|
||||
for(String st : cs.getStringList("Events.Hover.Show_Text")) {
|
||||
finals.add(UltraChat.plugin.addonManager.replacePlaceholders(st, p));
|
||||
}
|
||||
jsb.setHoverShowText(finals);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
mb.addJSON(jsb);
|
||||
|
@ -0,0 +1,10 @@
|
||||
package me.ryandw11.ultrachat.api.placeholders;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
public interface PlaceholderAddon {
|
||||
|
||||
public String getName();
|
||||
public String getString(UUID p);
|
||||
|
||||
}
|
@ -8,7 +8,9 @@ import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import me.ryandw11.ultrachat.UltraChat;
|
||||
import me.ryandw11.ultrachat.api.ChatType;
|
||||
import me.ryandw11.ultrachat.api.Lang;
|
||||
import me.ryandw11.ultrachat.api.UltraChatAPI;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
import net.md_5.bungee.api.chat.ClickEvent;
|
||||
import net.md_5.bungee.api.chat.ComponentBuilder;
|
||||
@ -33,7 +35,8 @@ public class ChannelCmd implements CommandExecutor {
|
||||
p.sendMessage(ChatColor.translateAlternateColorCodes('&', Lang.NO_PERM.toString()));
|
||||
return true;
|
||||
}
|
||||
if(!plugin.channelEnabled){
|
||||
UltraChatAPI uapi = new UltraChatAPI();
|
||||
if(uapi.getChatType() != ChatType.CHANNEL){
|
||||
p.sendMessage(ChatColor.translateAlternateColorCodes('&', Lang.ERROR_CHANNEL_ENABLED.toString()));
|
||||
return true;
|
||||
}
|
||||
|
@ -6,7 +6,10 @@ import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
|
||||
import me.ryandw11.ultrachat.UltraChat;
|
||||
import me.ryandw11.ultrachat.api.ChatType;
|
||||
import me.ryandw11.ultrachat.api.UltraChatAPI;
|
||||
import me.ryandw11.ultrachat.api.Util;
|
||||
import me.ryandw11.ultrachat.api.channels.ChatChannel;
|
||||
import net.md_5.bungee.api.ChatColor;
|
||||
|
||||
/**
|
||||
@ -29,13 +32,21 @@ private UltraChat plugin;
|
||||
OfflinePlayer op = Bukkit.getOfflinePlayer(ud);
|
||||
|
||||
color = ChatColor.translateAlternateColorCodes('&', plugin.data.getString(ud + ".color"));
|
||||
prefix = ChatColor.translateAlternateColorCodes('&', plugin.chat.getPlayerPrefix(world, op));
|
||||
suffix = ChatColor.translateAlternateColorCodes('&', plugin.chat.getPlayerSuffix(world, op));
|
||||
try {
|
||||
prefix = ChatColor.translateAlternateColorCodes('&', plugin.chat.getPlayerPrefix(world, op));
|
||||
suffix = ChatColor.translateAlternateColorCodes('&', plugin.chat.getPlayerSuffix(world, op));
|
||||
}
|
||||
catch(NullPointerException ex) {
|
||||
prefix = plugin.chat.getPlayerPrefix(world, op);
|
||||
suffix = plugin.chat.getPlayerSuffix(world, op);
|
||||
}
|
||||
formatOp = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Custom_Chat.Op_Chat.Format"));
|
||||
defaults = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Custom_Chat.Default_Chat.Format"));
|
||||
global = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Global.format"));
|
||||
this.world = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("World.format"));
|
||||
local = ChatColor.translateAlternateColorCodes('&', plugin.getConfig().getString("Local.format"));
|
||||
this.op = op;
|
||||
worldName = world;
|
||||
}
|
||||
|
||||
private String prefix;
|
||||
@ -46,6 +57,8 @@ private UltraChat plugin;
|
||||
private String global;
|
||||
private String world;
|
||||
private String local;
|
||||
private OfflinePlayer op;
|
||||
private String worldName;
|
||||
|
||||
public String getGlobal(){
|
||||
return global;
|
||||
@ -78,5 +91,39 @@ private UltraChat plugin;
|
||||
public String getCustomFormat(int num) {
|
||||
return plugin.getConfig().getString("Custom_Chat." + num + ".Format");
|
||||
}
|
||||
|
||||
public OfflinePlayer getOfflinePlayer() {
|
||||
return op;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the active format for a player.
|
||||
* @return The active format
|
||||
* @since 2.4
|
||||
*/
|
||||
public String getActiveFormat() {
|
||||
UltraChatAPI uapi = new UltraChatAPI();
|
||||
if(uapi.getChatType() == ChatType.NORMAL) {
|
||||
if(op.isOp()) return this.getOpFormat();
|
||||
|
||||
int i = 1;
|
||||
while (i <= plugin.getConfig().getInt("Custom_Chat.Chat_Count")) {
|
||||
if (plugin.perms.playerHas(worldName, op, plugin.getConfig().getString("Custom_Chat." + i + ".Permission"))) {
|
||||
return this.getCustomFormat(i);
|
||||
}
|
||||
i++;
|
||||
}
|
||||
return this.getDefaultFormat();
|
||||
}
|
||||
else if(uapi.getChatType() == ChatType.CHANNEL) {
|
||||
ChatChannel cc = uapi.getPlayerCurrentChannel(op.getUniqueId());
|
||||
return cc.getFormat();
|
||||
}
|
||||
else if(uapi.getChatType() == ChatType.RANGE) {
|
||||
return plugin.getConfig().getString("Local.format");
|
||||
}
|
||||
|
||||
return "ERROR: COULD NOT GET ACTIVE TYPE";
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user