summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLogan Fick <logaldeveloper@protonmail.com>2019-02-08 15:47:42 -0500
committerLogan Fick <logaldeveloper@protonmail.com>2019-02-08 15:47:42 -0500
commit83d1019290cd5b10f353a83b8595a326dd5e1a60 (patch)
tree33bdf8deec04bcf7b6559f8d66137b54b18194b5
parentf257d60fc76434f018e7c84551a99962f1a48362 (diff)
Reformatted code.HEADv1master
-rw-r--r--.idea/modules/ChatAPI.main.iml4
-rw-r--r--src/main/java/net/nemez/chatapi/ChatAPI.java186
-rw-r--r--src/main/java/net/nemez/chatapi/click/CallbackCommand.java5
-rw-r--r--src/main/java/net/nemez/chatapi/click/CallbackMap.java10
-rw-r--r--src/main/java/net/nemez/chatapi/click/ClickCallback.java21
-rw-r--r--src/main/java/net/nemez/chatapi/click/Message.java184
-rw-r--r--src/main/java/net/nemez/chatapi/click/RunnableCallback.java2
7 files changed, 183 insertions, 229 deletions
diff --git a/.idea/modules/ChatAPI.main.iml b/.idea/modules/ChatAPI.main.iml
index 85aa9bc..f5bac21 100644
--- a/.idea/modules/ChatAPI.main.iml
+++ b/.idea/modules/ChatAPI.main.iml
@@ -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.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.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: 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: junit:junit:4.10" level="project" />
</component>
</module> \ No newline at end of file
diff --git a/src/main/java/net/nemez/chatapi/ChatAPI.java b/src/main/java/net/nemez/chatapi/ChatAPI.java
index 00f97de..0e8b8bb 100644
--- a/src/main/java/net/nemez/chatapi/ChatAPI.java
+++ b/src/main/java/net/nemez/chatapi/ChatAPI.java
@@ -1,56 +1,49 @@
package net.nemez.chatapi;
-import java.lang.reflect.Field;
-import java.util.Random;
-
+import net.md_5.bungee.api.ChatMessageType;
+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.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;
-import net.md_5.bungee.api.chat.TextComponent;
-import net.md_5.bungee.api.ChatMessageType;
-import net.nemez.chatapi.click.CallbackCommand;
-import net.nemez.chatapi.click.Message;
-import net.nemez.chatapi.click.PlayerQuitListener;
+import java.lang.reflect.Field;
+import java.util.Random;
+
+public class ChatAPI {
-public class ChatAPI
-{
-
/* message coloring permission */
- public static final String PERMISSION_CHAT_COLOR = "chat.color";
+ public static final String PERMISSION_CHAT_COLOR = "chat.color";
/* message formatting permission */
- public static final String PERMISSION_CHAT_FORMAT = "chat.format";
+ public static final String PERMISSION_CHAT_FORMAT = "chat.format";
/* message magic formatting permission */
- public static final String PERMISSION_CHAT_MAGIC = "chat.magic";
+ public static final String PERMISSION_CHAT_MAGIC = "chat.magic";
/* permission to send messages in chat */
- public static final String PERMISSION_CHAT_USE = "chat.use";
+ public static final String PERMISSION_CHAT_USE = "chat.use";
/* message to send when the internal command is not ran correctly (ran by user) */
- public static final String MESSAGE_HELP_CLICK_CALLBACK = "&cThis is an internal command for ChatAPI and should not be ran by players manually.";
+ public static final String MESSAGE_HELP_CLICK_CALLBACK = "&cThis is an internal command for ChatAPI and should not be ran by players manually.";
/* message to send when the internal command is not ran by a player */
public static final String MESSAGE_PLAYER_CLICK_CALLBACK = "&cThis command can only be run by a player";
/* the actual command name for use in click callbacks */
- private static String internalCommandName;
-
+ private static String internalCommandName;
+
/** Initializes ChatAPI and registers the required commands for clickable chat to function. */
- public static void initialize(JavaPlugin plugin)
- {
- if (internalCommandName != null)
- {
+ public static void initialize(JavaPlugin plugin) {
+ if (internalCommandName != null) {
return;
}
Random rand = new Random(System.currentTimeMillis());
internalCommandName = "chatapi-exec-" + Integer.toHexString(rand.nextInt(0xEFFF) + 0x1000);
- try
- {
+ try {
final Field cmdMap = Bukkit.getServer().getClass().getDeclaredField("commandMap");
cmdMap.setAccessible(true);
CommandMap map = (CommandMap) cmdMap.get(Bukkit.getServer());
map.register("net/nemez/chatapi", new CallbackCommand(internalCommandName));
- }
- catch (Exception e)
- {
+ } catch (Exception e) {
plugin.getLogger().severe("Failed to register internal command '" + internalCommandName + "'");
e.printStackTrace();
}
@@ -59,97 +52,100 @@ public class ChatAPI
plugin.getServer().getPluginManager().registerEvents(new PlayerQuitListener(), plugin);
plugin.getLogger().info("ChatAPI initialized");
}
-
- /** Colorifies a message using &format codes. Respects permissions.
- *
- * @param sender the command sender whose permissions to use. null if permissions are to be ignored.
+
+ /**
+ * 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 message the message to color
- * @return colored message */
- public static String colorify(CommandSender sender, String message)
- {
- if (sender == null || sender.hasPermission(PERMISSION_CHAT_COLOR))
- {
+ *
+ * @return colored message
+ */
+ public static String colorify(CommandSender sender, String message) {
+ if (sender == null || sender.hasPermission(PERMISSION_CHAT_COLOR)) {
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");
}
- if (sender == null || sender.hasPermission(PERMISSION_CHAT_MAGIC))
- {
+ if (sender == null || sender.hasPermission(PERMISSION_CHAT_MAGIC)) {
message = message.replaceAll("&([kKrR])", "§$1");
}
message = message.replace("&§", "&");
return message;
}
-
- /** 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));
- }
-
- /** Sends a colorified action bar message to the command sender.
- *
- * @param sender the command sender to whom to send the action bar message.
- * @param message the message to send. */
- public static void sendActionBar(CommandSender sender, String message)
- {
- if (sender instanceof Player)
- {
+
+ /**
+ * Sends a colorified action bar message to the command sender.
+ *
+ * @param sender the command sender to whom to send the action bar message.
+ * @param message the message to send.
+ */
+ public static void sendActionBar(CommandSender sender, String message) {
+ if (sender instanceof Player) {
((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.
- * @return true/false if sender can chat or is null. */
- public static boolean canChat(CommandSender sender)
- {
- if (sender == null)
- {
+ *
+ * @return true/false if sender can chat or is null.
+ */
+ public static boolean canChat(CommandSender sender) {
+ if (sender == null) {
return true;
- }
- else
- {
+ } else {
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.
+ *
+ * @return message object.
+ */
+ public static Message createMessage(CommandSender sender) {
+ return createMessage(sender, null);
+ }
+
+ /**
+ * 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 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. */
- public static Message createMessage(CommandSender sender)
- {
- return createMessage(sender, null);
- }
-
- /** Gets the name of the internal ChatAPI command used for click callbacks.
+
+ /**
+ * 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.
- *
- * @return callback command name */
- public static String getInternalCallbackCommand()
- {
+ *
+ * @return callback command name
+ */
+ public static String getInternalCallbackCommand() {
return internalCommandName;
}
}
diff --git a/src/main/java/net/nemez/chatapi/click/CallbackCommand.java b/src/main/java/net/nemez/chatapi/click/CallbackCommand.java
index 8685108..1319b4c 100644
--- a/src/main/java/net/nemez/chatapi/click/CallbackCommand.java
+++ b/src/main/java/net/nemez/chatapi/click/CallbackCommand.java
@@ -1,12 +1,11 @@
package net.nemez.chatapi.click;
-import java.util.UUID;
-
+import net.nemez.chatapi.ChatAPI;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
-import net.nemez.chatapi.ChatAPI;
+import java.util.UUID;
public class CallbackCommand extends Command {
diff --git a/src/main/java/net/nemez/chatapi/click/CallbackMap.java b/src/main/java/net/nemez/chatapi/click/CallbackMap.java
index 6735ae9..dd377e3 100644
--- a/src/main/java/net/nemez/chatapi/click/CallbackMap.java
+++ b/src/main/java/net/nemez/chatapi/click/CallbackMap.java
@@ -1,10 +1,10 @@
package net.nemez.chatapi.click;
+import org.bukkit.command.CommandSender;
+
import java.util.HashMap;
import java.util.UUID;
-import org.bukkit.command.CommandSender;
-
public class CallbackMap {
private static HashMap<String, HashMap<Integer, ClickCallback>> map = new HashMap<String, HashMap<Integer, ClickCallback>>();
@@ -25,7 +25,7 @@ public class CallbackMap {
playerMap.put(id, callback);
return id;
}
-
+
protected static void execute(CommandSender sender, UUID uuid, int id) {
HashMap<Integer, ClickCallback> playerMap = map.get(uuid.toString());
if (playerMap == null) {
@@ -42,11 +42,11 @@ public class CallbackMap {
}
};
t.start();
- }else{
+ } else {
cb.execute(sender);
}
}
-
+
protected static void discard(UUID uuid) {
map.remove(uuid.toString());
}
diff --git a/src/main/java/net/nemez/chatapi/click/ClickCallback.java b/src/main/java/net/nemez/chatapi/click/ClickCallback.java
index 10e8f00..f2e078e 100644
--- a/src/main/java/net/nemez/chatapi/click/ClickCallback.java
+++ b/src/main/java/net/nemez/chatapi/click/ClickCallback.java
@@ -1,28 +1,25 @@
package net.nemez.chatapi.click;
-import org.bukkit.command.CommandSender;
-
import net.nemez.chatapi.ChatAPI;
+import org.bukkit.command.CommandSender;
public abstract class ClickCallback {
private boolean repeatable, async;
- private String expiredMessage;
+ private String expiredMessage;
private boolean expired;
-
+
public ClickCallback(boolean repeatable, boolean async, String expiredMessage) {
this.repeatable = repeatable;
this.async = async;
this.expiredMessage = expiredMessage;
this.expired = false;
}
-
- public abstract void run(CommandSender sender);
-
+
public final void execute(CommandSender sender) {
if (!expired) {
run(sender);
- }else{
+ } else {
if (sender != null) {
sender.sendMessage(ChatAPI.colorify(null, expiredMessage));
}
@@ -31,15 +28,17 @@ public abstract class ClickCallback {
expired = true;
}
}
-
+
+ public abstract void run(CommandSender sender);
+
public boolean isRepeatable() {
return repeatable;
}
-
+
public boolean isAsynchronous() {
return async;
}
-
+
public String getExpiredMessage() {
return expiredMessage;
}
diff --git a/src/main/java/net/nemez/chatapi/click/Message.java b/src/main/java/net/nemez/chatapi/click/Message.java
index cb887b5..735fa8b 100644
--- a/src/main/java/net/nemez/chatapi/click/Message.java
+++ b/src/main/java/net/nemez/chatapi/click/Message.java
@@ -1,129 +1,121 @@
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.entity.Player;
-import net.md_5.bungee.api.ChatMessageType;
-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 {
-public class Message
-{
-
private CommandSender sender;
private CommandSender permission;
private TextComponent message;
- private String rawMessage;
-
- public Message(CommandSender sender, CommandSender permission)
- {
+ private String rawMessage;
+
+ public Message(CommandSender sender, CommandSender permission) {
this.sender = sender;
this.permission = permission;
message = new TextComponent("");
rawMessage = "";
}
-
- public Message appendText(String text)
- {
+
+ public Message appendLink(String text, String url) {
text = ChatAPI.colorify(permission, text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
- for (BaseComponent component : components)
- {
+ for (BaseComponent component : components) {
+ component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
message.addExtra(component);
}
rawMessage += text;
return this;
}
-
- public Message appendLink(String text, String url)
- {
+
+ 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.OPEN_URL, url));
+ for (BaseComponent component : components) {
+ component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestion));
message.addExtra(component);
}
rawMessage += text;
return this;
}
-
- public Message appendSendChat(String text, String msg)
- {
+
+ public Message appendCallback(String text, ClickCallback callback) {
+ if (sender instanceof Player) {
+ int id = CallbackMap.register(((Player) sender).getUniqueId(), callback);
+ return appendSendChat(text, "/" + ChatAPI.getInternalCallbackCommand() + " " + id);
+ } else {
+ return appendText(text);
+ }
+ }
+
+ public Message appendSendChat(String text, String msg) {
text = ChatAPI.colorify(permission, 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)
- {
+
+ public Message appendText(String text) {
text = ChatAPI.colorify(permission, text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
- for (BaseComponent component : components)
- {
- component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestion));
+ for (BaseComponent component : components) {
message.addExtra(component);
}
rawMessage += text;
return this;
}
-
- public Message appendCallback(String text, ClickCallback callback)
- {
- if (sender instanceof Player)
- {
- int id = CallbackMap.register(((Player) sender).getUniqueId(), callback);
- return appendSendChat(text, "/" + ChatAPI.getInternalCallbackCommand() + " " + id);
- }
- else
- {
- return appendText(text);
- }
- }
-
- public Message appendTextHover(String text, String hover)
- {
+
+ public Message appendLinkHover(String text, String url, String hover) {
text = ChatAPI.colorify(permission, text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
- for (BaseComponent component : components)
- {
+ for (BaseComponent component : components) {
+ component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
addHoverText(component, hover);
message.addExtra(component);
}
rawMessage += text;
return this;
}
-
- public Message appendLinkHover(String text, String url, String hover)
- {
+
+ private void addHoverText(BaseComponent comp, String text) {
+ comp.setHoverEvent(new HoverEvent(
+ HoverEvent.Action.SHOW_TEXT,
+ new ComponentBuilder(ChatAPI.colorify(permission, text)).create()
+ ));
+ }
+
+ public Message appendSuggestHover(String text, String suggestion, 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));
+ for (BaseComponent component : components) {
+ component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestion));
addHoverText(component, hover);
message.addExtra(component);
}
rawMessage += text;
return this;
}
-
- public Message appendSendChatHover(String text, String msg, String hover)
- {
+
+ public Message appendCallbackHover(String text, ClickCallback callback, String hover) {
+ if (sender instanceof Player) {
+ int id = CallbackMap.register(((Player) sender).getUniqueId(), callback);
+ return appendSendChatHover(text, "/" + ChatAPI.getInternalCallbackCommand() + " " + id, hover);
+ } else {
+ 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)
- {
+ for (BaseComponent component : components) {
component.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, msg));
addHoverText(component, hover);
message.addExtra(component);
@@ -131,79 +123,47 @@ public class Message
rawMessage += text;
return this;
}
-
- public Message appendSuggestHover(String text, String suggestion, String hover)
- {
+
+ public Message appendTextHover(String text, String hover) {
text = ChatAPI.colorify(permission, text);
BaseComponent[] components = TextComponent.fromLegacyText(text);
- for (BaseComponent component : components)
- {
- component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestion));
+ for (BaseComponent component : components) {
addHoverText(component, hover);
message.addExtra(component);
}
rawMessage += text;
return this;
}
-
- public Message appendCallbackHover(String text, ClickCallback callback, String hover)
- {
- if (sender instanceof Player)
- {
- int id = CallbackMap.register(((Player) sender).getUniqueId(), callback);
- return appendSendChatHover(text, "/" + ChatAPI.getInternalCallbackCommand() + " " + id, hover);
- }
- else
- {
- return appendTextHover(text, hover);
- }
- }
-
+
public Message appendMessage(Message msg) {
message.addExtra(msg.message);
rawMessage += msg.rawMessage;
return this;
}
-
- public void send()
- {
- if (sender == null || !ChatAPI.canChat(this.permission))
- {
+
+ public void send() {
+ if (sender == null || !ChatAPI.canChat(this.permission)) {
return;
}
- if (sender instanceof Player)
- {
+ if (sender instanceof Player) {
((Player) sender).spigot().sendMessage(message);
- }
- else
- {
+ } else {
sender.sendMessage(rawMessage);
}
}
-
- public void sendAsActionBar()
- {
- if (sender == null)
- {
+
+ public void sendAsActionBar() {
+ if (sender == null) {
return;
}
- if (sender instanceof Player)
- {
+ if (sender instanceof Player) {
((Player) sender).spigot().sendMessage(ChatMessageType.ACTION_BAR, message);
- }
- else
- {
+ } else {
sender.sendMessage(rawMessage);
}
}
-
+
public String getRawMessage() {
return rawMessage;
}
-
- private void addHoverText(BaseComponent comp, String text)
- {
- comp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT,
- new ComponentBuilder(ChatAPI.colorify(permission, text)).create()));
- }
}
diff --git a/src/main/java/net/nemez/chatapi/click/RunnableCallback.java b/src/main/java/net/nemez/chatapi/click/RunnableCallback.java
index 2ee16da..5299fa2 100644
--- a/src/main/java/net/nemez/chatapi/click/RunnableCallback.java
+++ b/src/main/java/net/nemez/chatapi/click/RunnableCallback.java
@@ -5,7 +5,7 @@ import org.bukkit.command.CommandSender;
public class RunnableCallback extends ClickCallback {
private Runnable runnable;
-
+
public RunnableCallback(Runnable runnable, boolean repeatable, boolean async, String expiredMessage) {
super(repeatable, async, expiredMessage);
this.runnable = runnable;