Skip to content

Commit

Permalink
Added %ajqueue_status_<server>_player%
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgeiss0702 committed May 29, 2023
1 parent 429eb35 commit a99bd73
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 0 deletions.
6 changes: 6 additions & 0 deletions api/src/main/java/us/ajg0702/queue/api/QueueHolder.java
@@ -0,0 +1,6 @@
package us.ajg0702.queue.api;

public interface QueueHolder {
boolean isAvailable();

}
Expand Up @@ -30,6 +30,7 @@ public PlaceholderExpansion(SpigotMain plugin) {
placeholders.add(new Queued(plugin));
placeholders.add(new QueuedFor(plugin));
placeholders.add(new Status(plugin));
placeholders.add(new StatusPlayer(plugin));

}

Expand Down
@@ -0,0 +1,59 @@
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 us.ajg0702.queue.spigot.utils.UUIDStringKey;

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 StatusPlayer extends Placeholder {
public StatusPlayer(SpigotMain plugin) {
super(plugin);
}

private final Map<UUIDStringKey, String> cache = new ConcurrentHashMap<>();

@Override
public String getRegex() {
return "status_(.*)_player";
}

@Override
public String parse(Matcher matcher, OfflinePlayer p) {
String queue = matcher.group(1);
UUIDStringKey key = new UUIDStringKey(p.getUniqueId(), queue);

Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
try {
String response = AjQueueSpigotAPI.getInstance()
.getServerStatusString(queue, p.getUniqueId())
.get(30, TimeUnit.SECONDS);

cache.put(key, 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(key, "...");
}

@Override
public void cleanCache(Player player) {
cache.entrySet().removeIf(entry -> entry.getKey().getUuid().equals(player.getUniqueId()));
}
}
@@ -0,0 +1,36 @@
package us.ajg0702.queue.spigot.utils;

import java.util.Objects;
import java.util.UUID;

public class UUIDStringKey {

private final UUID uuid;
private final String string;

public UUIDStringKey(UUID uuid, String string) {
this.uuid = uuid;
this.string = string;
}

public UUID getUuid() {
return uuid;
}

public String getString() {
return string;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof UUIDStringKey)) return false;
UUIDStringKey that = (UUIDStringKey) o;
return uuid.equals(that.uuid) && string.equals(that.string);
}

@Override
public int hashCode() {
return Objects.hash(uuid, string);
}
}

0 comments on commit a99bd73

Please sign in to comment.