diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/EstimatedTime.java b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/EstimatedTime.java index 42362164..02117f14 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/EstimatedTime.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/EstimatedTime.java @@ -23,6 +23,7 @@ public EstimatedTime(SpigotMain plugin) { } private final Map cache = new ConcurrentHashMap<>(); + private final Map lastFetch = new ConcurrentHashMap<>(); @Override public String getRegex() { @@ -31,18 +32,22 @@ public String getRegex() { @Override public String parse(Matcher matcher, OfflinePlayer p) { - plugin.getScheduler().runTaskAsynchronously(() -> { - if(!p.isOnline()) return; - try { - MessagedResponse 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 | IllegalArgumentException ignored) {} - }); + + if(System.currentTimeMillis() - lastFetch.getOrDefault(p.getUniqueId(), 0L) > 2000) { + lastFetch.put(p.getUniqueId(), System.currentTimeMillis()); + plugin.getScheduler().runTaskAsynchronously(() -> { + if(!p.isOnline()) return; + try { + MessagedResponse 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 | IllegalArgumentException ignored) {} + }); + } return cache.getOrDefault(p.getUniqueId(), "..."); } @@ -50,5 +55,6 @@ public String parse(Matcher matcher, OfflinePlayer p) { @Override public void cleanCache(Player player) { cache.remove(player.getUniqueId()); + lastFetch.remove(player.getUniqueId()); } } diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/InQueue.java b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/InQueue.java index 7d4cb34b..034d8144 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/InQueue.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/InQueue.java @@ -22,6 +22,7 @@ public InQueue(SpigotMain plugin) { } private final Map cache = new ConcurrentHashMap<>(); + private final Map lastFetch = new ConcurrentHashMap<>(); @Override public String getRegex() { @@ -30,18 +31,22 @@ public String getRegex() { @Override public String parse(Matcher matcher, OfflinePlayer p) { - plugin.getScheduler().runTaskAsynchronously(() -> { - if(!p.isOnline()) return; - 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 | IllegalArgumentException ignored) {} - }); + + if(System.currentTimeMillis() - lastFetch.getOrDefault(p.getUniqueId(), 0L) > 2000) { + lastFetch.put(p.getUniqueId(), System.currentTimeMillis()); + plugin.getScheduler().runTaskAsynchronously(() -> { + if(!p.isOnline()) return; + 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 | IllegalArgumentException ignored) {} + }); + } return cache.getOrDefault(p.getUniqueId(), "..."); } @@ -49,5 +54,6 @@ public String parse(Matcher matcher, OfflinePlayer p) { @Override public void cleanCache(Player player) { cache.remove(player.getUniqueId()); + lastFetch.remove(player.getUniqueId()); } } diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Position.java b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Position.java index 47f8b59a..58ab6cef 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Position.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Position.java @@ -23,6 +23,7 @@ public Position(SpigotMain plugin) { } private final Map cache = new ConcurrentHashMap<>(); + private final Map lastFetch = new ConcurrentHashMap<>(); @Override public String getRegex() { @@ -31,18 +32,22 @@ public String getRegex() { @Override public String parse(Matcher matcher, OfflinePlayer p) { - plugin.getScheduler().runTaskAsynchronously(() -> { - if(!p.isOnline()) return; - try { - MessagedResponse 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 | IllegalArgumentException ignored) {} - }); + + if(System.currentTimeMillis() - lastFetch.getOrDefault(p.getUniqueId(), 0L) > 2000) { + lastFetch.put(p.getUniqueId(), System.currentTimeMillis()); + plugin.getScheduler().runTaskAsynchronously(() -> { + if(!p.isOnline()) return; + try { + MessagedResponse 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 | IllegalArgumentException ignored) {} + }); + } return cache.getOrDefault(p.getUniqueId(), "..."); } @@ -50,5 +55,6 @@ public String parse(Matcher matcher, OfflinePlayer p) { @Override public void cleanCache(Player player) { cache.remove(player.getUniqueId()); + lastFetch.remove(player.getUniqueId()); } } diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/PositionOf.java b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/PositionOf.java index f7a2a9ce..868ee183 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/PositionOf.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/PositionOf.java @@ -23,6 +23,7 @@ public PositionOf(SpigotMain plugin) { } private final Map cache = new ConcurrentHashMap<>(); + private final Map lastFetch = new ConcurrentHashMap<>(); @Override public String getRegex() { @@ -31,18 +32,22 @@ public String getRegex() { @Override public String parse(Matcher matcher, OfflinePlayer p) { - plugin.getScheduler().runTaskAsynchronously(() -> { - if(!p.isOnline()) return; - try { - MessagedResponse 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 | IllegalArgumentException ignored) {} - }); + + if(System.currentTimeMillis() - lastFetch.getOrDefault(p.getUniqueId(), 0L) > 2000) { + lastFetch.put(p.getUniqueId(), System.currentTimeMillis()); + plugin.getScheduler().runTaskAsynchronously(() -> { + if(!p.isOnline()) return; + try { + MessagedResponse 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 | IllegalArgumentException ignored) {} + }); + } return cache.getOrDefault(p.getUniqueId(), "..."); } @@ -50,5 +55,6 @@ public String parse(Matcher matcher, OfflinePlayer p) { @Override public void cleanCache(Player player) { cache.remove(player.getUniqueId()); + cache.remove(player.getUniqueId()); } } diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Queued.java b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Queued.java index 734297f2..856bac76 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Queued.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Queued.java @@ -23,6 +23,7 @@ public Queued(SpigotMain plugin) { } private final Map cache = new ConcurrentHashMap<>(); + private final Map lastFetch = new ConcurrentHashMap<>(); @Override public String getRegex() { @@ -31,18 +32,22 @@ public String getRegex() { @Override public String parse(Matcher matcher, OfflinePlayer p) { - plugin.getScheduler().runTaskAsynchronously(() -> { - if(!p.isOnline()) return; - try { - MessagedResponse 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 | IllegalArgumentException ignored) {} - }); + + if(System.currentTimeMillis() - lastFetch.getOrDefault(p.getUniqueId(), 0L) > 2000) { + lastFetch.put(p.getUniqueId(), System.currentTimeMillis()); + plugin.getScheduler().runTaskAsynchronously(() -> { + if(!p.isOnline()) return; + try { + MessagedResponse 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 | IllegalArgumentException ignored) {} + }); + } return cache.getOrDefault(p.getUniqueId(), "..."); } @@ -50,5 +55,6 @@ public String parse(Matcher matcher, OfflinePlayer p) { @Override public void cleanCache(Player player) { cache.remove(player.getUniqueId()); + cache.remove(player.getUniqueId()); } } diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/QueuedFor.java b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/QueuedFor.java index 9afe22fb..785b0a82 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/QueuedFor.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/QueuedFor.java @@ -23,6 +23,7 @@ public QueuedFor(SpigotMain plugin) { private final String invalidMessage = "Invalid queue name"; private final Map cache = new ConcurrentHashMap<>(); + private final Map lastFetch = new ConcurrentHashMap<>(); @Override public String getRegex() { @@ -34,24 +35,28 @@ public String parse(Matcher matcher, OfflinePlayer p) { String queue = matcher.group(1); String cached = cache.getOrDefault(queue, "..."); - plugin.getScheduler().runTaskAsynchronously(() -> { - if(!p.isOnline()) return; - try { - Integer response = AjQueueSpigotAPI.getInstance() - .getPlayersInQueue(queue) - .get(30, TimeUnit.SECONDS); - - cache.put(queue, response + ""); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } catch (ExecutionException e) { - if(e.getCause() instanceof IllegalArgumentException) { - cache.put(queue, invalidMessage); - } else { + if(System.currentTimeMillis() - lastFetch.getOrDefault(queue, 0L) > 2000) { + lastFetch.put(queue, System.currentTimeMillis()); + plugin.getScheduler().runTaskAsynchronously(() -> { + if (!p.isOnline()) return; + try { + Integer response = AjQueueSpigotAPI.getInstance() + .getPlayersInQueue(queue) + .get(30, TimeUnit.SECONDS); + + cache.put(queue, response + ""); + } catch (InterruptedException e) { throw new RuntimeException(e); + } catch (ExecutionException e) { + if (e.getCause() instanceof IllegalArgumentException) { + cache.put(queue, invalidMessage); + } else { + throw new RuntimeException(e); + } + } catch (TimeoutException ignored) { } - } catch (TimeoutException ignored) {} - }); + }); + } return cached; } diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Status.java b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Status.java index d075b908..5fc5ca1a 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Status.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/Status.java @@ -23,6 +23,7 @@ public Status(SpigotMain plugin) { private final String invalidMessage = "Invalid queue name"; private final Map cache = new ConcurrentHashMap<>(); + private final Map lastFetch = new ConcurrentHashMap<>(); @Override public String getRegex() { @@ -34,24 +35,29 @@ public String parse(Matcher matcher, OfflinePlayer p) { String queue = matcher.group(1); String cached = cache.getOrDefault(queue, "..."); - plugin.getScheduler().runTaskAsynchronously(() -> { - if(!p.isOnline()) return; - try { - String response = AjQueueSpigotAPI.getInstance() - .getServerStatusString(queue) - .get(30, TimeUnit.SECONDS); - - cache.put(queue, response); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } catch (ExecutionException e) { - if(e.getCause() instanceof IllegalArgumentException) { - cache.put(queue, invalidMessage); - } else { + if(System.currentTimeMillis() - lastFetch.getOrDefault(queue, 0L) > 2000) { + lastFetch.put(queue, System.currentTimeMillis()); + + plugin.getScheduler().runTaskAsynchronously(() -> { + if (!p.isOnline()) return; + try { + String response = AjQueueSpigotAPI.getInstance() + .getServerStatusString(queue) + .get(30, TimeUnit.SECONDS); + + cache.put(queue, response); + } catch (InterruptedException e) { throw new RuntimeException(e); + } catch (ExecutionException e) { + if (e.getCause() instanceof IllegalArgumentException) { + cache.put(queue, invalidMessage); + } else { + throw new RuntimeException(e); + } + } catch (TimeoutException | IllegalArgumentException ignored) { } - } catch (TimeoutException | IllegalArgumentException ignored) {} - }); + }); + } return cached; } diff --git a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/StatusPlayer.java b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/StatusPlayer.java index b2b74b5d..d371ad64 100644 --- a/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/StatusPlayer.java +++ b/spigot/src/main/java/us/ajg0702/queue/spigot/placeholders/placeholders/StatusPlayer.java @@ -24,6 +24,7 @@ public StatusPlayer(SpigotMain plugin) { } private final Map cache = new ConcurrentHashMap<>(); + private final Map lastFetch = new ConcurrentHashMap<>(); @Override public String getRegex() { @@ -35,20 +36,25 @@ public String parse(Matcher matcher, OfflinePlayer p) { String queue = matcher.group(1); UUIDStringKey key = new UUIDStringKey(p.getUniqueId(), queue); - if(!p.isOnline()) return "You aren't online!"; + if(!p.isOnline()) return "You aren't online!?!"; - plugin.getScheduler().runTaskAsynchronously(() -> { - if(!p.isOnline()) return; - try { - String response = AjQueueSpigotAPI.getInstance() - .getServerStatusString(queue, p.getUniqueId()) - .get(30, TimeUnit.SECONDS); + if(System.currentTimeMillis() - lastFetch.getOrDefault(key, 0L) > 2000) { + lastFetch.put(key, System.currentTimeMillis()); - cache.put(key, response); - } catch (InterruptedException | ExecutionException e) { - throw new RuntimeException(e); - } catch (TimeoutException | IllegalArgumentException ignored) {} - }); + plugin.getScheduler().runTaskAsynchronously(() -> { + if (!p.isOnline()) return; + 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 | IllegalArgumentException ignored) { + } + }); + } return cache.getOrDefault(key, "..."); } @@ -56,5 +62,6 @@ public String parse(Matcher matcher, OfflinePlayer p) { @Override public void cleanCache(Player player) { cache.entrySet().removeIf(entry -> entry.getKey().getUuid().equals(player.getUniqueId())); + lastFetch.entrySet().removeIf(entry -> entry.getKey().getUuid().equals(player.getUniqueId())); } }