Initial commit
This commit is contained in:
38
net/nemez/chatapi/click/CallbackCommand.java
Normal file
38
net/nemez/chatapi/click/CallbackCommand.java
Normal file
@@ -0,0 +1,38 @@
|
||||
package net.nemez.chatapi.click;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import net.nemez.chatapi.ChatAPI;
|
||||
|
||||
public class CallbackCommand extends Command {
|
||||
|
||||
public CallbackCommand(String internalCommandName) {
|
||||
super(internalCommandName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean execute(CommandSender sender, String label, String[] args) {
|
||||
if (!(sender instanceof Player)) {
|
||||
ChatAPI.send(sender, ChatAPI.MESSAGE_PLAYER_CLICK_CALLBACK);
|
||||
return true;
|
||||
}
|
||||
if ((args.length == 1 && args[0].equals("help")) || args.length != 1) {
|
||||
ChatAPI.send(sender, ChatAPI.MESSAGE_HELP_CLICK_CALLBACK);
|
||||
return true;
|
||||
}
|
||||
int id;
|
||||
try {
|
||||
id = Integer.parseInt(args[0]);
|
||||
} catch (NumberFormatException e) {
|
||||
ChatAPI.send(sender, ChatAPI.MESSAGE_HELP_CLICK_CALLBACK);
|
||||
return true;
|
||||
}
|
||||
UUID uuid = ((Player) sender).getUniqueId();
|
||||
CallbackMap.execute(sender, uuid, id);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
53
net/nemez/chatapi/click/CallbackMap.java
Normal file
53
net/nemez/chatapi/click/CallbackMap.java
Normal file
@@ -0,0 +1,53 @@
|
||||
package net.nemez.chatapi.click;
|
||||
|
||||
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>>();
|
||||
|
||||
protected static int register(UUID uuid, ClickCallback callback) {
|
||||
HashMap<Integer, ClickCallback> playerMap = map.get(uuid.toString());
|
||||
if (playerMap == null) {
|
||||
playerMap = new HashMap<Integer, ClickCallback>();
|
||||
map.put(uuid.toString(), playerMap);
|
||||
}
|
||||
int largestId = 0;
|
||||
for (int i : playerMap.keySet()) {
|
||||
if (i > largestId) {
|
||||
largestId = i;
|
||||
}
|
||||
}
|
||||
int id = largestId + 1;
|
||||
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) {
|
||||
return;
|
||||
}
|
||||
ClickCallback cb = playerMap.get(id);
|
||||
if (cb == null) {
|
||||
return;
|
||||
}
|
||||
if (cb.isAsynchronous()) {
|
||||
Thread t = new Thread() {
|
||||
public void run() {
|
||||
cb.execute(sender);
|
||||
}
|
||||
};
|
||||
t.start();
|
||||
}else{
|
||||
cb.execute(sender);
|
||||
}
|
||||
}
|
||||
|
||||
protected static void discard(UUID uuid) {
|
||||
map.remove(uuid.toString());
|
||||
}
|
||||
}
|
||||
40
net/nemez/chatapi/click/ClickCallback.java
Normal file
40
net/nemez/chatapi/click/ClickCallback.java
Normal file
@@ -0,0 +1,40 @@
|
||||
package net.nemez.chatapi.click;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public abstract class ClickCallback {
|
||||
|
||||
private boolean repeatable, async;
|
||||
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);
|
||||
}
|
||||
if (!repeatable) {
|
||||
expired = true;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isRepeatable() {
|
||||
return repeatable;
|
||||
}
|
||||
|
||||
public boolean isAsynchronous() {
|
||||
return async;
|
||||
}
|
||||
|
||||
public String getExpiredMessage() {
|
||||
return expiredMessage;
|
||||
}
|
||||
}
|
||||
131
net/nemez/chatapi/click/Message.java
Normal file
131
net/nemez/chatapi/click/Message.java
Normal file
@@ -0,0 +1,131 @@
|
||||
package net.nemez.chatapi.click;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
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 permission;
|
||||
private TextComponent message;
|
||||
private String rawMessage;
|
||||
|
||||
public Message(CommandSender sender, CommandSender permission) {
|
||||
this.sender = sender;
|
||||
this.permission = permission;
|
||||
message = new TextComponent("");
|
||||
rawMessage = "";
|
||||
}
|
||||
|
||||
public Message appendText(String text) {
|
||||
text = ChatAPI.colorify(permission, text);
|
||||
message.addExtra(text);
|
||||
rawMessage += text;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Message appendLink(String text, String url) {
|
||||
text = ChatAPI.colorify(permission, text);
|
||||
TextComponent component = new TextComponent(text);
|
||||
component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
|
||||
message.addExtra(component);
|
||||
rawMessage += text;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Message appendSendChat(String text, String msg) {
|
||||
text = ChatAPI.colorify(permission, text);
|
||||
TextComponent component = new TextComponent(text);
|
||||
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);
|
||||
TextComponent component = new TextComponent(text);
|
||||
component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestion));
|
||||
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) {
|
||||
text = ChatAPI.colorify(permission, text);
|
||||
TextComponent component = new TextComponent(text);
|
||||
addHoverText(component, hover);
|
||||
message.addExtra(component);
|
||||
rawMessage += text;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Message appendLinkHover(String text, String url, String hover) {
|
||||
text = ChatAPI.colorify(permission, text);
|
||||
TextComponent component = new TextComponent(text);
|
||||
component.setClickEvent(new ClickEvent(ClickEvent.Action.OPEN_URL, url));
|
||||
addHoverText(component, hover);
|
||||
message.addExtra(component);
|
||||
rawMessage += text;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Message appendSendChatHover(String text, String msg, String hover) {
|
||||
text = ChatAPI.colorify(permission, text);
|
||||
TextComponent component = new TextComponent(text);
|
||||
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) {
|
||||
text = ChatAPI.colorify(permission, text);
|
||||
TextComponent component = new TextComponent(text);
|
||||
component.setClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, suggestion));
|
||||
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 void send() {
|
||||
if (sender == null || !ChatAPI.canChat(this.permission)) {
|
||||
return;
|
||||
}
|
||||
if (sender instanceof Player) {
|
||||
((Player)sender).spigot().sendMessage(message);
|
||||
}else{
|
||||
sender.sendMessage(rawMessage);
|
||||
}
|
||||
}
|
||||
|
||||
private void addHoverText(TextComponent comp, String text) {
|
||||
comp.setHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, new ComponentBuilder(ChatAPI.colorify(permission, text)).create()));
|
||||
}
|
||||
}
|
||||
13
net/nemez/chatapi/click/PlayerQuitListener.java
Normal file
13
net/nemez/chatapi/click/PlayerQuitListener.java
Normal file
@@ -0,0 +1,13 @@
|
||||
package net.nemez.chatapi.click;
|
||||
|
||||
import org.bukkit.event.EventHandler;
|
||||
import org.bukkit.event.Listener;
|
||||
import org.bukkit.event.player.PlayerQuitEvent;
|
||||
|
||||
public class PlayerQuitListener implements Listener {
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
CallbackMap.discard(event.getPlayer().getUniqueId());
|
||||
}
|
||||
}
|
||||
17
net/nemez/chatapi/click/RunnableCallback.java
Normal file
17
net/nemez/chatapi/click/RunnableCallback.java
Normal file
@@ -0,0 +1,17 @@
|
||||
package net.nemez.chatapi.click;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(CommandSender sender) {
|
||||
runnable.run();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user