Added Spigot-side API
This commit is contained in:
@@ -15,16 +15,25 @@ import org.bukkit.event.server.ServerListPingEvent;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.plugin.messaging.PluginMessageListener;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import us.ajg0702.queue.api.AjQueueAPI;
|
||||
import us.ajg0702.queue.api.communication.ComResponse;
|
||||
import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI;
|
||||
import us.ajg0702.queue.spigot.api.SpigotAPI;
|
||||
import us.ajg0702.queue.spigot.communication.ResponseManager;
|
||||
import us.ajg0702.queue.spigot.placeholders.PlaceholderExpansion;
|
||||
import us.ajg0702.utils.common.ConfigFile;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.HashMap;
|
||||
import java.util.logging.Level;
|
||||
|
||||
@SuppressWarnings("UnstableApiUsage")
|
||||
public class SpigotMain extends JavaPlugin implements PluginMessageListener,Listener {
|
||||
|
||||
private boolean papi = false;
|
||||
private Placeholders placeholders;
|
||||
private PlaceholderExpansion placeholders;
|
||||
|
||||
private ResponseManager responseManager = new ResponseManager();
|
||||
|
||||
private ConfigFile config;
|
||||
|
||||
@@ -34,6 +43,9 @@ public class SpigotMain extends JavaPlugin implements PluginMessageListener,List
|
||||
public void onEnable() {
|
||||
getServer().getMessenger().registerIncomingPluginChannel(this, "ajqueue:tospigot", this);
|
||||
getServer().getMessenger().registerOutgoingPluginChannel(this, "ajqueue:toproxy");
|
||||
|
||||
AjQueueAPI.SPIGOT_INSTANCE = new SpigotAPI(responseManager, this);
|
||||
AjQueueSpigotAPI.INSTANCE = AjQueueAPI.SPIGOT_INSTANCE;
|
||||
|
||||
this.getCommand("move").setExecutor(new Commands(this));
|
||||
this.getCommand("leavequeue").setExecutor(new Commands(this));
|
||||
@@ -43,7 +55,7 @@ public class SpigotMain extends JavaPlugin implements PluginMessageListener,List
|
||||
papi = Bukkit.getPluginManager().getPlugin("PlaceholderAPI") != null;
|
||||
|
||||
if(papi) {
|
||||
placeholders = new Placeholders(this);
|
||||
placeholders = new PlaceholderExpansion(this);
|
||||
placeholders.register();
|
||||
getLogger().info("Registered PlaceholderAPI placeholders");
|
||||
}
|
||||
@@ -94,6 +106,7 @@ public class SpigotMain extends JavaPlugin implements PluginMessageListener,List
|
||||
|
||||
if(subchannel.equals("ack")) {
|
||||
hasProxy = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if(subchannel.equals("inqueueevent")) {
|
||||
@@ -102,95 +115,15 @@ public class SpigotMain extends JavaPlugin implements PluginMessageListener,List
|
||||
if(p == null) return;
|
||||
QueueScoreboardActivator e = new QueueScoreboardActivator(p);
|
||||
Bukkit.getPluginManager().callEvent(e);
|
||||
return;
|
||||
}
|
||||
if(subchannel.equals("queuename") && papi) {
|
||||
String playername = in.readUTF();
|
||||
Player p = Bukkit.getPlayer(playername);
|
||||
if(p == null) return;
|
||||
if(!p.isOnline()) return;
|
||||
|
||||
String data = in.readUTF();
|
||||
HashMap<String, String> phs = placeholders.responseCache.get(p);
|
||||
if(phs == null) phs = new HashMap<>();
|
||||
phs.put("queued", data);
|
||||
placeholders.responseCache.put(p, phs);
|
||||
}
|
||||
if(subchannel.equals("position") && papi) {
|
||||
String playername = in.readUTF();
|
||||
Player p = Bukkit.getPlayer(playername);
|
||||
if(p == null) return;
|
||||
if(!p.isOnline()) return;
|
||||
|
||||
String data = in.readUTF();
|
||||
HashMap<String, String> phs = placeholders.responseCache.get(p);
|
||||
if(phs == null) phs = new HashMap<>();
|
||||
phs.put("position", data);
|
||||
placeholders.responseCache.put(p, phs);
|
||||
}
|
||||
if(subchannel.equals("positionof") && papi) {
|
||||
String playername = in.readUTF();
|
||||
Player p = Bukkit.getPlayer(playername);
|
||||
if(p == null) return;
|
||||
if(!p.isOnline()) return;
|
||||
|
||||
String data = in.readUTF();
|
||||
HashMap<String, String> phs = placeholders.responseCache.get(p);
|
||||
if(phs == null) phs = new HashMap<>();
|
||||
phs.put("of", data);
|
||||
placeholders.responseCache.put(p, phs);
|
||||
}
|
||||
if(subchannel.equals("inqueue") && papi) {
|
||||
String playername = in.readUTF();
|
||||
Player p = Bukkit.getPlayer(playername);
|
||||
if(p == null) return;
|
||||
if(!p.isOnline()) return;
|
||||
|
||||
String data = in.readUTF();
|
||||
HashMap<String, String> phs = placeholders.responseCache.get(p);
|
||||
if(phs == null) phs = new HashMap<>();
|
||||
phs.put("inqueue", data);
|
||||
placeholders.responseCache.put(p, phs);
|
||||
}
|
||||
if(subchannel.equals("queuedfor")) {
|
||||
String playername = in.readUTF();
|
||||
String queuename = in.readUTF();
|
||||
|
||||
Player p = Bukkit.getPlayer(playername);
|
||||
if(p == null) return;
|
||||
if(!p.isOnline()) return;
|
||||
|
||||
int number = Integer.parseInt(in.readUTF());
|
||||
HashMap<String, String> phs = placeholders.responseCache.get(p);
|
||||
if(phs == null) phs = new HashMap<>();
|
||||
phs.put("queuedfor_"+queuename, number+"");
|
||||
placeholders.responseCache.put(p, phs);
|
||||
}
|
||||
if(subchannel.equals("estimated_time")) {
|
||||
String playername = in.readUTF();
|
||||
|
||||
Player p = Bukkit.getPlayer(playername);
|
||||
if(p == null) return;
|
||||
if(!p.isOnline()) return;
|
||||
try {
|
||||
ComResponse response = ComResponse.from(subchannel, in);
|
||||
|
||||
String time = in.readUTF();
|
||||
HashMap<String, String> phs = placeholders.responseCache.get(p);
|
||||
if(phs == null) phs = new HashMap<>();
|
||||
phs.put("estimated_time", time);
|
||||
placeholders.responseCache.put(p, phs);
|
||||
}
|
||||
if(subchannel.equals("status")) {
|
||||
String playername = in.readUTF();
|
||||
String server = in.readUTF();
|
||||
|
||||
Player p = Bukkit.getPlayer(playername);
|
||||
if(p == null) return;
|
||||
if(!p.isOnline()) return;
|
||||
|
||||
String status = in.readUTF();
|
||||
HashMap<String, String> phs = placeholders.responseCache.get(p);
|
||||
if(phs == null) phs = new HashMap<>();
|
||||
phs.put("status_"+server, status+"");
|
||||
placeholders.responseCache.put(p, phs);
|
||||
responseManager.executeResponse(response);
|
||||
} catch (Exception e) {
|
||||
getLogger().log(Level.SEVERE, "Error while processing proxy response " + subchannel + ": ", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -215,7 +148,7 @@ public class SpigotMain extends JavaPlugin implements PluginMessageListener,List
|
||||
@EventHandler
|
||||
public void onLeave(PlayerQuitEvent e) {
|
||||
if(!papi) return;
|
||||
placeholders.cleanCache();
|
||||
placeholders.cleanCache(e.getPlayer());
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
|
||||
@@ -0,0 +1,157 @@
|
||||
package us.ajg0702.queue.spigot.api;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI;
|
||||
import us.ajg0702.queue.api.spigot.MessagedResponse;
|
||||
import us.ajg0702.queue.spigot.SpigotMain;
|
||||
import us.ajg0702.queue.spigot.communication.ResponseManager;
|
||||
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.CompletableFuture;
|
||||
import java.util.concurrent.Future;
|
||||
|
||||
public class SpigotAPI extends AjQueueSpigotAPI {
|
||||
private final ResponseManager responseManager;
|
||||
private final SpigotMain main;
|
||||
|
||||
public SpigotAPI(ResponseManager responseManager, SpigotMain main) {
|
||||
this.responseManager = responseManager;
|
||||
this.main = main;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<Boolean> isInQueue(UUID player) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if(p == null) throw new IllegalArgumentException("Player must be online!");
|
||||
|
||||
CompletableFuture<Boolean> future = new CompletableFuture<>();
|
||||
|
||||
responseManager.awaitResponse(player.toString(), "inqueue", response -> {
|
||||
future.complete(Boolean.valueOf(response.getResponse()));
|
||||
});
|
||||
|
||||
main.sendMessage(p, "inqueue", "");
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<Boolean> addToQueue(UUID player, String queueName) {
|
||||
throw new UnsupportedOperationException("Not yet implemented!");
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<MessagedResponse<String>> getQueueName(UUID player) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if(p == null) throw new IllegalArgumentException("Player must be online!");
|
||||
|
||||
CompletableFuture<MessagedResponse<String>> future = new CompletableFuture<>();
|
||||
|
||||
responseManager.awaitResponse(player.toString(), "queuename", response -> {
|
||||
future.complete(new MessagedResponse<>(response.getResponse(), response.getNoneMessage()));
|
||||
});
|
||||
|
||||
main.sendMessage(p, "queuename", "");
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<MessagedResponse<Integer>> getPosition(UUID player) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if(p == null) throw new IllegalArgumentException("Player must be online!");
|
||||
|
||||
CompletableFuture<MessagedResponse<Integer>> future = new CompletableFuture<>();
|
||||
|
||||
responseManager.awaitResponse(player.toString(), "position", response -> {
|
||||
String r = response.getResponse();
|
||||
Integer i = r == null ? null : Integer.valueOf(r);
|
||||
future.complete(new MessagedResponse<>(i, response.getNoneMessage()));
|
||||
});
|
||||
|
||||
main.sendMessage(p, "position", "");
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<MessagedResponse<Integer>> getTotalPositions(UUID player) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if(p == null) throw new IllegalArgumentException("Player must be online!");
|
||||
|
||||
CompletableFuture<MessagedResponse<Integer>> future = new CompletableFuture<>();
|
||||
|
||||
responseManager.awaitResponse(player.toString(), "positionof", response -> {
|
||||
String r = response.getResponse();
|
||||
Integer i = r == null ? null : Integer.valueOf(r);
|
||||
future.complete(new MessagedResponse<>(i, response.getNoneMessage()));
|
||||
});
|
||||
|
||||
main.sendMessage(p, "positionof", "");
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<Integer> getPlayersInQueue(String queueName) {
|
||||
Player p = Bukkit.getOnlinePlayers().iterator().next();
|
||||
|
||||
CompletableFuture<Integer> future = new CompletableFuture<>();
|
||||
|
||||
responseManager.awaitResponse(queueName, "queuedfor", response -> {
|
||||
String responseString = response.getResponse();
|
||||
if(responseString.equals("invalid_server")) {
|
||||
future.completeExceptionally(new IllegalArgumentException(queueName + " does not exist!"));
|
||||
}
|
||||
future.complete(Integer.valueOf(responseString));
|
||||
});
|
||||
|
||||
main.sendMessage(p, "queuedfor", queueName);
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<String> getServerStatusString(String queueName) {
|
||||
return getServerStatusString(queueName, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<String> getServerStatusString(String queueName, UUID player) {
|
||||
Player p = player == null ? Bukkit.getOnlinePlayers().iterator().next() : Bukkit.getPlayer(player);
|
||||
if(p == null) throw new IllegalArgumentException("Player must be online!");
|
||||
|
||||
String channel = player == null ? "status" : "playerstatus";
|
||||
|
||||
CompletableFuture<String> future = new CompletableFuture<>();
|
||||
|
||||
responseManager.awaitResponse(queueName, channel, response -> {
|
||||
String responseString = response.getResponse();
|
||||
if(responseString.equals("invalid_server")) {
|
||||
future.completeExceptionally(new IllegalArgumentException(queueName + " does not exist!"));
|
||||
}
|
||||
future.complete(responseString);
|
||||
});
|
||||
|
||||
main.sendMessage(p, channel, queueName);
|
||||
|
||||
return future;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Future<MessagedResponse<String>> getEstimatedTime(UUID player) {
|
||||
Player p = Bukkit.getPlayer(player);
|
||||
if(p == null) throw new IllegalArgumentException("Player must be online!");
|
||||
|
||||
CompletableFuture<MessagedResponse<String>> future = new CompletableFuture<>();
|
||||
|
||||
responseManager.awaitResponse(player.toString(), "estimated_time", response -> {
|
||||
future.complete(new MessagedResponse<>(response.getResponse(), response.getNoneMessage()));
|
||||
});
|
||||
|
||||
main.sendMessage(p, "estimated_time", "");
|
||||
|
||||
return future;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
package us.ajg0702.queue.spigot.communication;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
public class ResponseKey {
|
||||
private final String id;
|
||||
private final String from;
|
||||
|
||||
public ResponseKey(String id, String from) {
|
||||
this.id = id;
|
||||
this.from = from;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
if (o == null || getClass() != o.getClass()) return false;
|
||||
ResponseKey that = (ResponseKey) o;
|
||||
return id.equals(that.id) && from.equals(that.from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, from);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ResponseKey{" +
|
||||
"id='" + id + '\'' +
|
||||
", from='" + from + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,31 @@
|
||||
package us.ajg0702.queue.spigot.communication;
|
||||
|
||||
import us.ajg0702.queue.api.communication.ComResponse;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
|
||||
public class ResponseManager {
|
||||
private final Map<ResponseKey, Consumer<ComResponse>> responseMap = new ConcurrentHashMap<>();
|
||||
|
||||
public synchronized void awaitResponse(String id, String from, Consumer<ComResponse> callback) {
|
||||
ResponseKey key = new ResponseKey(id, from);
|
||||
responseMap.merge(key, callback, (a, b) -> r -> {
|
||||
b.accept(r);
|
||||
a.accept(r);
|
||||
});
|
||||
}
|
||||
|
||||
public void executeResponse(ComResponse response) {
|
||||
ResponseKey key = new ResponseKey(response.getIdentifier(), response.getFrom());
|
||||
Consumer<ComResponse> callback = responseMap.get(key);
|
||||
if(callback == null) {
|
||||
System.out.println("[ajQueue] No callback for " + key + " with " + response);
|
||||
return;
|
||||
}
|
||||
responseMap.remove(key);
|
||||
callback.accept(response);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package us.ajg0702.queue.spigot.placeholders;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class CachedPlaceholder {
|
||||
private final Matcher matcher;
|
||||
private final Placeholder placeholder;
|
||||
|
||||
public CachedPlaceholder(Matcher matcher, Placeholder placeholder) {
|
||||
this.matcher = matcher;
|
||||
this.placeholder = placeholder;
|
||||
}
|
||||
|
||||
public Matcher getMatcher() {
|
||||
return matcher;
|
||||
}
|
||||
|
||||
public Placeholder getPlaceholder() {
|
||||
return placeholder;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package us.ajg0702.queue.spigot.placeholders;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.ajg0702.queue.spigot.SpigotMain;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public abstract class Placeholder {
|
||||
|
||||
protected final SpigotMain plugin;
|
||||
|
||||
private Pattern pattern;
|
||||
|
||||
public Placeholder(SpigotMain plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public abstract String getRegex();
|
||||
|
||||
public Pattern getPattern() {
|
||||
if(pattern == null) {
|
||||
pattern = Pattern.compile(getRegex());
|
||||
}
|
||||
return pattern;
|
||||
}
|
||||
|
||||
public abstract String parse(Matcher matcher, OfflinePlayer p);
|
||||
|
||||
public abstract void cleanCache(Player player);
|
||||
}
|
||||
@@ -0,0 +1,88 @@
|
||||
package us.ajg0702.queue.spigot.placeholders;
|
||||
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import us.ajg0702.queue.spigot.SpigotMain;
|
||||
import us.ajg0702.queue.spigot.placeholders.placeholders.*;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class PlaceholderExpansion extends me.clip.placeholderapi.expansion.PlaceholderExpansion {
|
||||
|
||||
private final List<Placeholder> placeholders = new ArrayList<>();
|
||||
|
||||
private final SpigotMain plugin;
|
||||
|
||||
@SuppressWarnings("deprecated")
|
||||
public PlaceholderExpansion(SpigotMain plugin) {
|
||||
|
||||
this.plugin = plugin;
|
||||
|
||||
placeholders.add(new EstimatedTime(plugin));
|
||||
placeholders.add(new InQueue(plugin));
|
||||
placeholders.add(new Position(plugin));
|
||||
placeholders.add(new PositionOf(plugin));
|
||||
placeholders.add(new Queued(plugin));
|
||||
placeholders.add(new QueuedFor(plugin));
|
||||
placeholders.add(new Status(plugin));
|
||||
|
||||
}
|
||||
|
||||
Map<String, CachedPlaceholder> placeholderCache = new HashMap<>();
|
||||
|
||||
@Override
|
||||
public String onRequest(OfflinePlayer p, @NotNull String params) {
|
||||
|
||||
if(p == null || !p.isOnline()) {
|
||||
return "No player";
|
||||
}
|
||||
|
||||
CachedPlaceholder cachedPlaceholder = placeholderCache.computeIfAbsent(params, s -> {
|
||||
for(Placeholder placeholder : placeholders) {
|
||||
Matcher matcher = placeholder.getPattern().matcher(params);
|
||||
if(!matcher.matches()) continue;
|
||||
return new CachedPlaceholder(matcher, placeholder);
|
||||
}
|
||||
return null;
|
||||
});
|
||||
if(cachedPlaceholder == null) return null;
|
||||
|
||||
return cachedPlaceholder.getPlaceholder().parse(cachedPlaceholder.getMatcher(), p);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@Override
|
||||
public @NotNull String getIdentifier() {
|
||||
return "ajqueue";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getAuthor() {
|
||||
return "ajgeiss0702";
|
||||
}
|
||||
|
||||
@Override
|
||||
public @NotNull String getVersion() {
|
||||
return plugin.getDescription().getVersion();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean persist(){
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canRegister() {
|
||||
return true;
|
||||
}
|
||||
|
||||
public void cleanCache(Player player) {
|
||||
placeholders.forEach(p -> p.cleanCache(player));
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package us.ajg0702.queue.spigot.placeholders.placeholders;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI;
|
||||
import us.ajg0702.queue.api.spigot.MessagedResponse;
|
||||
import us.ajg0702.queue.spigot.SpigotMain;
|
||||
import us.ajg0702.queue.spigot.placeholders.Placeholder;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class EstimatedTime extends Placeholder {
|
||||
public EstimatedTime(SpigotMain plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
private final Map<UUID, String> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public String getRegex() {
|
||||
return "estimated_time";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(Matcher matcher, OfflinePlayer p) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
MessagedResponse<String> response = AjQueueSpigotAPI.getInstance()
|
||||
.getEstimatedTime(p.getUniqueId())
|
||||
.get(30, TimeUnit.SECONDS);
|
||||
|
||||
cache.put(p.getUniqueId(), response.getEither());
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (TimeoutException e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Timed out while trying to get placeholder data from proxy: ", e);
|
||||
}
|
||||
});
|
||||
|
||||
return cache.getOrDefault(p.getUniqueId(), "...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache(Player player) {
|
||||
cache.remove(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package us.ajg0702.queue.spigot.placeholders.placeholders;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI;
|
||||
import us.ajg0702.queue.spigot.SpigotMain;
|
||||
import us.ajg0702.queue.spigot.placeholders.Placeholder;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class InQueue extends Placeholder {
|
||||
public InQueue(SpigotMain plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
private final Map<UUID, String> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public String getRegex() {
|
||||
return "inqueue";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(Matcher matcher, OfflinePlayer p) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
Boolean response = AjQueueSpigotAPI.getInstance()
|
||||
.isInQueue(p.getUniqueId())
|
||||
.get(30, TimeUnit.SECONDS);
|
||||
|
||||
cache.put(p.getUniqueId(), response + "");
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (TimeoutException e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Timed out while trying to get placeholder data from proxy: ", e);
|
||||
}
|
||||
});
|
||||
|
||||
return cache.getOrDefault(p.getUniqueId(), "...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache(Player player) {
|
||||
cache.remove(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package us.ajg0702.queue.spigot.placeholders.placeholders;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI;
|
||||
import us.ajg0702.queue.api.spigot.MessagedResponse;
|
||||
import us.ajg0702.queue.spigot.SpigotMain;
|
||||
import us.ajg0702.queue.spigot.placeholders.Placeholder;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class Position extends Placeholder {
|
||||
public Position(SpigotMain plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
private final Map<UUID, String> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public String getRegex() {
|
||||
return "position";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(Matcher matcher, OfflinePlayer p) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
MessagedResponse<Integer> response = AjQueueSpigotAPI.getInstance()
|
||||
.getPosition(p.getUniqueId())
|
||||
.get(30, TimeUnit.SECONDS);
|
||||
|
||||
cache.put(p.getUniqueId(), response.getEither());
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (TimeoutException e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Timed out while trying to get placeholder data from proxy: ", e);
|
||||
}
|
||||
});
|
||||
|
||||
return cache.getOrDefault(p.getUniqueId(), "...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache(Player player) {
|
||||
cache.remove(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
+55
@@ -0,0 +1,55 @@
|
||||
package us.ajg0702.queue.spigot.placeholders.placeholders;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI;
|
||||
import us.ajg0702.queue.api.spigot.MessagedResponse;
|
||||
import us.ajg0702.queue.spigot.SpigotMain;
|
||||
import us.ajg0702.queue.spigot.placeholders.Placeholder;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class PositionOf extends Placeholder {
|
||||
public PositionOf(SpigotMain plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
private final Map<UUID, String> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public String getRegex() {
|
||||
return "of";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(Matcher matcher, OfflinePlayer p) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
MessagedResponse<Integer> response = AjQueueSpigotAPI.getInstance()
|
||||
.getTotalPositions(p.getUniqueId())
|
||||
.get(30, TimeUnit.SECONDS);
|
||||
|
||||
cache.put(p.getUniqueId(), response.getEither());
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (TimeoutException e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Timed out while trying to get placeholder data from proxy: ", e);
|
||||
}
|
||||
});
|
||||
|
||||
return cache.getOrDefault(p.getUniqueId(), "...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache(Player player) {
|
||||
cache.remove(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package us.ajg0702.queue.spigot.placeholders.placeholders;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI;
|
||||
import us.ajg0702.queue.api.spigot.MessagedResponse;
|
||||
import us.ajg0702.queue.spigot.SpigotMain;
|
||||
import us.ajg0702.queue.spigot.placeholders.Placeholder;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.logging.Level;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class Queued extends Placeholder {
|
||||
public Queued(SpigotMain plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
private final Map<UUID, String> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public String getRegex() {
|
||||
return "queued";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(Matcher matcher, OfflinePlayer p) {
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
MessagedResponse<String> response = AjQueueSpigotAPI.getInstance()
|
||||
.getQueueName(p.getUniqueId())
|
||||
.get(30, TimeUnit.SECONDS);
|
||||
|
||||
cache.put(p.getUniqueId(), response.getEither());
|
||||
} catch (InterruptedException | ExecutionException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (TimeoutException e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Timed out while trying to get placeholder data from proxy: ", e);
|
||||
}
|
||||
});
|
||||
|
||||
return cache.getOrDefault(p.getUniqueId(), "...");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache(Player player) {
|
||||
cache.remove(player.getUniqueId());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package us.ajg0702.queue.spigot.placeholders.placeholders;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI;
|
||||
import us.ajg0702.queue.spigot.SpigotMain;
|
||||
import us.ajg0702.queue.spigot.placeholders.Placeholder;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class QueuedFor extends Placeholder {
|
||||
public QueuedFor(SpigotMain plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
private final String invalidMessage = "Invalid queue name";
|
||||
|
||||
private final Map<String, String> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public String getRegex() {
|
||||
return "queuedfor_(.*)";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(Matcher matcher, OfflinePlayer p) {
|
||||
String queue = matcher.group(1);
|
||||
String cached = cache.getOrDefault(queue, "...");
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
Integer response = AjQueueSpigotAPI.getInstance()
|
||||
.getPlayersInQueue(queue)
|
||||
.get(30, TimeUnit.SECONDS);
|
||||
|
||||
cache.put(queue, response + "");
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (TimeoutException e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Timed out while trying to get placeholder data from proxy: ", e);
|
||||
} catch (ExecutionException e) {
|
||||
if(e.getCause() instanceof IllegalArgumentException) {
|
||||
cache.put(queue, invalidMessage);
|
||||
} else {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return cached;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache(Player player) {}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
package us.ajg0702.queue.spigot.placeholders.placeholders;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.OfflinePlayer;
|
||||
import org.bukkit.entity.Player;
|
||||
import us.ajg0702.queue.api.spigot.AjQueueSpigotAPI;
|
||||
import us.ajg0702.queue.spigot.SpigotMain;
|
||||
import us.ajg0702.queue.spigot.placeholders.Placeholder;
|
||||
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.TimeoutException;
|
||||
import java.util.logging.Level;
|
||||
import java.util.regex.Matcher;
|
||||
|
||||
public class Status extends Placeholder {
|
||||
public Status(SpigotMain plugin) {
|
||||
super(plugin);
|
||||
}
|
||||
|
||||
private final String invalidMessage = "Invalid queue name";
|
||||
|
||||
private final Map<String, String> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public String getRegex() {
|
||||
return "status_(.*)";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String parse(Matcher matcher, OfflinePlayer p) {
|
||||
String queue = matcher.group(1);
|
||||
String cached = cache.getOrDefault(queue, "...");
|
||||
|
||||
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
|
||||
try {
|
||||
String response = AjQueueSpigotAPI.getInstance()
|
||||
.getServerStatusString(queue)
|
||||
.get(30, TimeUnit.SECONDS);
|
||||
|
||||
cache.put(queue, response);
|
||||
} catch (InterruptedException e) {
|
||||
throw new RuntimeException(e);
|
||||
} catch (TimeoutException e) {
|
||||
plugin.getLogger().log(Level.WARNING, "Timed out while trying to get placeholder data from proxy: ", e);
|
||||
} catch (ExecutionException e) {
|
||||
if(e.getCause() instanceof IllegalArgumentException) {
|
||||
cache.put(queue, invalidMessage);
|
||||
} else {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return cached;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void cleanCache(Player player) {}
|
||||
}
|
||||
Reference in New Issue
Block a user