Archived
0

Reformatted code.

This commit is contained in:
Logan Fick
2019-02-08 15:47:42 -05:00
parent f257d60fc7
commit 83d1019290
7 changed files with 194 additions and 240 deletions

View File

@@ -13,9 +13,9 @@
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.google.guava:guava:21.0" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Gradle: com.google.guava:guava:21.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: com.google.code.gson:gson:2.8.0" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Gradle: com.google.code.gson:gson:2.8.0" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.yaml:snakeyaml:1.23" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Gradle: org.yaml:snakeyaml:1.23" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: net.md-5:bungeecord-chat:1.13-SNAPSHOT" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Gradle: net.md-5:bungeecord-chat:1.13-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: junit:junit:4.10" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Gradle: org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: org.hamcrest:hamcrest-core:1.1" level="project" /> <orderEntry type="library" scope="PROVIDED" name="Gradle: org.hamcrest:hamcrest-core:1.1" level="project" />
<orderEntry type="library" scope="PROVIDED" name="Gradle: junit:junit:4.10" level="project" />
</component> </component>
</module> </module>

View File

@@ -1,22 +1,20 @@
package net.nemez.chatapi; package net.nemez.chatapi;
import java.lang.reflect.Field; import net.md_5.bungee.api.ChatMessageType;
import java.util.Random; import net.md_5.bungee.api.chat.TextComponent;
import net.nemez.chatapi.click.CallbackCommand;
import net.nemez.chatapi.click.Message;
import net.nemez.chatapi.click.PlayerQuitListener;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.CommandMap; import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
import net.md_5.bungee.api.chat.TextComponent; import java.lang.reflect.Field;
import net.md_5.bungee.api.ChatMessageType; import java.util.Random;
import net.nemez.chatapi.click.CallbackCommand;
import net.nemez.chatapi.click.Message;
import net.nemez.chatapi.click.PlayerQuitListener;
public class ChatAPI public class ChatAPI {
{
/* message coloring permission */ /* message coloring permission */
public static final String PERMISSION_CHAT_COLOR = "chat.color"; public static final String PERMISSION_CHAT_COLOR = "chat.color";
@@ -34,23 +32,18 @@ public class ChatAPI
private static String internalCommandName; private static String internalCommandName;
/** Initializes ChatAPI and registers the required commands for clickable chat to function. */ /** Initializes ChatAPI and registers the required commands for clickable chat to function. */
public static void initialize(JavaPlugin plugin) public static void initialize(JavaPlugin plugin) {
{ if (internalCommandName != null) {
if (internalCommandName != null)
{
return; return;
} }
Random rand = new Random(System.currentTimeMillis()); Random rand = new Random(System.currentTimeMillis());
internalCommandName = "chatapi-exec-" + Integer.toHexString(rand.nextInt(0xEFFF) + 0x1000); internalCommandName = "chatapi-exec-" + Integer.toHexString(rand.nextInt(0xEFFF) + 0x1000);
try try {
{
final Field cmdMap = Bukkit.getServer().getClass().getDeclaredField("commandMap"); final Field cmdMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
cmdMap.setAccessible(true); cmdMap.setAccessible(true);
CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer()); CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer());
map.register("net/nemez/chatapi", new CallbackCommand(internalCommandName)); map.register("net/nemez/chatapi", new CallbackCommand(internalCommandName));
} } catch (Exception e) {
catch (Exception e)
{
plugin.getLogger().severe("Failed to register internal command '" + internalCommandName + "'"); plugin.getLogger().severe("Failed to register internal command '" + internalCommandName + "'");
e.printStackTrace(); e.printStackTrace();
} }
@@ -60,96 +53,99 @@ public class ChatAPI
plugin.getLogger().info("ChatAPI initialized"); plugin.getLogger().info("ChatAPI initialized");
} }
/** Colorifies a message using &format codes. Respects permissions. /**
* Sends a colorified message to the command sender.
*
* @param sender the command sender to whom to send the message.
* @param message the message to send.
*/
public static void send(CommandSender sender, String message) {
if (sender == null) {
return;
}
sender.sendMessage(colorify(null, message));
}
/**
* Colorifies a message using &format codes. Respects permissions.
* *
* @param sender the command sender whose permissions to use. null if permissions are to be ignored. * @param sender the command sender whose permissions to use. null if permissions are to be ignored.
* @param message the message to color * @param message the message to color
* @return colored message */ *
public static String colorify(CommandSender sender, String message) * @return colored message
{ */
if (sender == null || sender.hasPermission(PERMISSION_CHAT_COLOR)) public static String colorify(CommandSender sender, String message) {
{ if (sender == null || sender.hasPermission(PERMISSION_CHAT_COLOR)) {
message = message.replaceAll("&([0-9a-fA-FrR])", "§$1"); message = message.replaceAll("&([0-9a-fA-FrR])", "§$1");
} }
if (sender == null || sender.hasPermission(PERMISSION_CHAT_FORMAT)) if (sender == null || sender.hasPermission(PERMISSION_CHAT_FORMAT)) {
{
message = message.replaceAll("&([l-oL-OrR])", "§$1"); message = message.replaceAll("&([l-oL-OrR])", "§$1");
} }
if (sender == null || sender.hasPermission(PERMISSION_CHAT_MAGIC)) if (sender == null || sender.hasPermission(PERMISSION_CHAT_MAGIC)) {
{
message = message.replaceAll("&([kKrR])", "§$1"); message = message.replaceAll("&([kKrR])", "§$1");
} }
message = message.replace("", "&"); message = message.replace("", "&");
return message; return message;
} }
/** Sends a colorified message to the command sender. /**
* * Sends a colorified action bar message to the command sender.
* @param sender the command sender to whom to send the message.
* @param message the message to send. */
public static void send(CommandSender sender, String message)
{
if (sender == null)
{
return;
}
sender.sendMessage(colorify(null, message));
}
/** Sends a colorified action bar message to the command sender.
* *
* @param sender the command sender to whom to send the action bar message. * @param sender the command sender to whom to send the action bar message.
* @param message the message to send. */ * @param message the message to send.
public static void sendActionBar(CommandSender sender, String message) */
{ public static void sendActionBar(CommandSender sender, String message) {
if (sender instanceof Player) if (sender instanceof Player) {
{
((Player) sender).spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(colorify(null, message))); ((Player) sender).spigot().sendMessage(ChatMessageType.ACTION_BAR, new TextComponent(colorify(null, message)));
} }
} }
/** Checks if a command sender has the permission node required to send chat messages. /**
* Checks if a command sender has the permission node required to send chat messages.
* *
* @param sender the command sender to check. * @param sender the command sender to check.
* @return true/false if sender can chat or is null. */ *
public static boolean canChat(CommandSender sender) * @return true/false if sender can chat or is null.
{ */
if (sender == null) public static boolean canChat(CommandSender sender) {
{ if (sender == null) {
return true; return true;
} } else {
else
{
return sender.hasPermission(PERMISSION_CHAT_USE); return sender.hasPermission(PERMISSION_CHAT_USE);
} }
} }
/** Creates a new message object that will be sent to the given command sender with regards to the second command sender's permissions. /**
* Creates a new message object that will be sent to the given command sender.
* *
* @param sender the command sender to whom to send the message. * @param sender the command sender to whom to send the message.
* @param permissionSender the command sender whose permissions to use.
* @return message object */
public static Message createMessage(CommandSender sender, CommandSender permissionSender)
{
return new Message(sender, permissionSender);
}
/** Creates a new message object that will be sent to the given command sender.
* *
* @param sender the command sender to whom to send the message. * @return message object.
* @return message object. */ */
public static Message createMessage(CommandSender sender) public static Message createMessage(CommandSender sender) {
{
return createMessage(sender, null); return createMessage(sender, null);
} }
/** Gets the name of the internal ChatAPI command used for click callbacks. /**
* Creates a new message object that will be sent to the given command sender with regards to the second command sender's permissions.
*
* @param sender the command sender to whom to send the message.
* @param permissionSender the command sender whose permissions to use.
*
* @return message object
*/
public static Message createMessage(CommandSender sender, CommandSender permissionSender) {
return new Message(sender, permissionSender);
}
/**
* Gets the name of the internal ChatAPI command used for click callbacks.
* This function is used internally and you don't need to worry about it. * This function is used internally and you don't need to worry about it.
* *
* @return callback command name */ * @return callback command name
public static String getInternalCallbackCommand() */
{ public static String getInternalCallbackCommand() {
return internalCommandName; return internalCommandName;
} }
} }

View File

@@ -1,12 +1,11 @@
package net.nemez.chatapi.click; package net.nemez.chatapi.click;
import java.util.UUID; import net.nemez.chatapi.ChatAPI;
import org.bukkit.command.Command; import org.bukkit.command.Command;
import org.bukkit.command.CommandSender; import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import net.nemez.chatapi.ChatAPI; import java.util.UUID;
public class CallbackCommand extends Command { public class CallbackCommand extends Command {

View File

@@ -1,10 +1,10 @@
package net.nemez.chatapi.click; package net.nemez.chatapi.click;
import org.bukkit.command.CommandSender;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
import org.bukkit.command.CommandSender;
public class CallbackMap { public class CallbackMap {
private static HashMap<String, HashMap<Integer, ClickCallback>> map = new HashMap<String, HashMap<Integer, ClickCallback>>(); private static HashMap<String, HashMap<Integer, ClickCallback>> map = new HashMap<String, HashMap<Integer, ClickCallback>>();
@@ -42,7 +42,7 @@ public class CallbackMap {
} }
}; };
t.start(); t.start();
}else{ } else {
cb.execute(sender); cb.execute(sender);
} }
} }

View File

@@ -1,8 +1,7 @@
package net.nemez.chatapi.click; package net.nemez.chatapi.click;
import org.bukkit.command.CommandSender;
import net.nemez.chatapi.ChatAPI; import net.nemez.chatapi.ChatAPI;
import org.bukkit.command.CommandSender;
public abstract class ClickCallback { public abstract class ClickCallback {
@@ -17,12 +16,10 @@ public abstract class ClickCallback {
this.expired = false; this.expired = false;
} }
public abstract void run(CommandSender sender);
public final void execute(CommandSender sender) { public final void execute(CommandSender sender) {
if (!expired) { if (!expired) {
run(sender); run(sender);
}else{ } else {
if (sender != null) { if (sender != null) {
sender.sendMessage(ChatAPI.colorify(null, expiredMessage)); sender.sendMessage(ChatAPI.colorify(null, expiredMessage));
} }
@@ -32,6 +29,8 @@ public abstract class ClickCallback {
} }
} }
public abstract void run(CommandSender sender);
public boolean isRepeatable() { public boolean isRepeatable() {
return repeatable; return repeatable;
} }

View File

@@ -1,50 +1,29 @@
package net.nemez.chatapi.click; package net.nemez.chatapi.click;
import net.md_5.bungee.api.ChatMessageType;
import net.md_5.bungee.api.chat.*;
import net.nemez.chatapi.ChatAPI;
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.ChatMessageType; public class Message {
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.api.chat.ClickEvent;
import net.md_5.bungee.api.chat.ComponentBuilder;
import net.md_5.bungee.api.chat.HoverEvent;
import net.md_5.bungee.api.chat.TextComponent;
import net.nemez.chatapi.ChatAPI;
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 appendLink(String text, String url) {
{
text = ChatAPI.colorify(permission, text); text = ChatAPI.colorify(permission, text);
BaseComponent[] components = TextComponent.fromLegacyText(text); BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components) for (BaseComponent component : components) {
{
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendLink(String text, String url)
{
text = ChatAPI.colorify(permission, 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);
} }
@@ -52,25 +31,10 @@ public class Message
return this; return this;
} }
public Message appendSendChat(String text, String msg) public Message appendSuggest(String text, String suggestion) {
{
text = ChatAPI.colorify(permission, text); text = ChatAPI.colorify(permission, text);
BaseComponent[] components = TextComponent.fromLegacyText(text); BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components) for (BaseComponent component : components) {
{
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, msg));
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendSuggest(String text, String suggestion)
{
text = ChatAPI.colorify(permission, 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);
} }
@@ -78,38 +42,40 @@ public class Message
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 appendSendChat(String text, String msg) {
{
text = ChatAPI.colorify(permission, text); text = ChatAPI.colorify(permission, text);
BaseComponent[] components = TextComponent.fromLegacyText(text); BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components) for (BaseComponent component : components) {
{ component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, msg));
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 appendText(String text) {
{
text = ChatAPI.colorify(permission, text); text = ChatAPI.colorify(permission, text);
BaseComponent[] components = TextComponent.fromLegacyText(text); BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components) for (BaseComponent component : components) {
{ message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendLinkHover(String text, String url, String hover) {
text = ChatAPI.colorify(permission, 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);
@@ -118,26 +84,17 @@ public class Message
return this; return this;
} }
public Message appendSendChatHover(String text, String msg, String hover) private void addHoverText(BaseComponent comp, String text) {
{ comp.setHoverEvent(new HoverEvent(
text = ChatAPI.colorify(permission, text); HoverEvent.Action.SHOW_TEXT,
BaseComponent[] components = TextComponent.fromLegacyText(text); new ComponentBuilder(ChatAPI.colorify(permission, text)).create()
for (BaseComponent component : components) ));
{
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, msg));
addHoverText(component, hover);
message.addExtra(component);
}
rawMessage += text;
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);
BaseComponent[] components = TextComponent.fromLegacyText(text); BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components) 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);
@@ -146,53 +103,62 @@ public class Message
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 Message appendSendChatHover(String text, String msg, String hover) {
text = ChatAPI.colorify(permission, text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components) {
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, msg));
addHoverText(component, hover);
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendTextHover(String text, String hover) {
text = ChatAPI.colorify(permission, text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
for (BaseComponent component : components) {
addHoverText(component, hover);
message.addExtra(component);
}
rawMessage += text;
return this;
}
public Message appendMessage(Message msg) { public Message appendMessage(Message msg) {
message.addExtra(msg.message); message.addExtra(msg.message);
rawMessage += msg.rawMessage; rawMessage += msg.rawMessage;
return this; return this;
} }
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); ((Player) sender).spigot().sendMessage(message);
} } else {
else
{
sender.sendMessage(rawMessage); sender.sendMessage(rawMessage);
} }
} }
public void sendAsActionBar() public void sendAsActionBar() {
{ if (sender == null) {
if (sender == null)
{
return; return;
} }
if (sender instanceof Player) if (sender instanceof Player) {
{
((Player) sender).spigot().sendMessage(ChatMessageType.ACTION_BAR, message); ((Player) sender).spigot().sendMessage(ChatMessageType.ACTION_BAR, message);
} } else {
else
{
sender.sendMessage(rawMessage); sender.sendMessage(rawMessage);
} }
} }
@@ -200,10 +166,4 @@ public class Message
public String getRawMessage() { public String getRawMessage() {
return rawMessage; return rawMessage;
} }
private void addHoverText(BaseComponent comp, String text)
{
comp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
new ComponentBuilder(ChatAPI.colorify(permission, text)).create()));
}
} }