Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added %ajqueue_status_<server>_player%
- Loading branch information
1 parent
429eb35
commit a99bd73
Showing
4 changed files
with
102 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package us.ajg0702.queue.api; | ||
|
||
public interface QueueHolder { | ||
boolean isAvailable(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/StatusPlayer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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())); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
spigot/src/main/java/us/ajg0702/queue/spigot/utils/UUIDStringKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |