Skip to content

Commit

Permalink
Dont update spigot placeholder too often
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgeiss0702 committed Jun 3, 2024
1 parent 2f297f4 commit ec9b055
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 104 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public EstimatedTime(SpigotMain plugin) {
}

private final Map<UUID, String> cache = new ConcurrentHashMap<>();
private final Map<UUID, Long> lastFetch = new ConcurrentHashMap<>();

@Override
public String getRegex() {
Expand All @@ -31,24 +32,29 @@ public String getRegex() {

@Override
public String parse(Matcher matcher, OfflinePlayer p) {
plugin.getScheduler().runTaskAsynchronously(() -> {
if(!p.isOnline()) return;
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 | 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<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 | IllegalArgumentException ignored) {}
});
}

return cache.getOrDefault(p.getUniqueId(), "...");
}

@Override
public void cleanCache(Player player) {
cache.remove(player.getUniqueId());
lastFetch.remove(player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public InQueue(SpigotMain plugin) {
}

private final Map<UUID, String> cache = new ConcurrentHashMap<>();
private final Map<UUID, Long> lastFetch = new ConcurrentHashMap<>();

@Override
public String getRegex() {
Expand All @@ -30,24 +31,29 @@ 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(), "...");
}

@Override
public void cleanCache(Player player) {
cache.remove(player.getUniqueId());
lastFetch.remove(player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Position(SpigotMain plugin) {
}

private final Map<UUID, String> cache = new ConcurrentHashMap<>();
private final Map<UUID, Long> lastFetch = new ConcurrentHashMap<>();

@Override
public String getRegex() {
Expand All @@ -31,24 +32,29 @@ public String getRegex() {

@Override
public String parse(Matcher matcher, OfflinePlayer p) {
plugin.getScheduler().runTaskAsynchronously(() -> {
if(!p.isOnline()) return;
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 | 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<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 | IllegalArgumentException ignored) {}
});
}

return cache.getOrDefault(p.getUniqueId(), "...");
}

@Override
public void cleanCache(Player player) {
cache.remove(player.getUniqueId());
lastFetch.remove(player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public PositionOf(SpigotMain plugin) {
}

private final Map<UUID, String> cache = new ConcurrentHashMap<>();
private final Map<UUID, Long> lastFetch = new ConcurrentHashMap<>();

@Override
public String getRegex() {
Expand All @@ -31,24 +32,29 @@ public String getRegex() {

@Override
public String parse(Matcher matcher, OfflinePlayer p) {
plugin.getScheduler().runTaskAsynchronously(() -> {
if(!p.isOnline()) return;
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 | 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<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 | IllegalArgumentException ignored) {}
});
}

return cache.getOrDefault(p.getUniqueId(), "...");
}

@Override
public void cleanCache(Player player) {
cache.remove(player.getUniqueId());
cache.remove(player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Queued(SpigotMain plugin) {
}

private final Map<UUID, String> cache = new ConcurrentHashMap<>();
private final Map<UUID, Long> lastFetch = new ConcurrentHashMap<>();

@Override
public String getRegex() {
Expand All @@ -31,24 +32,29 @@ public String getRegex() {

@Override
public String parse(Matcher matcher, OfflinePlayer p) {
plugin.getScheduler().runTaskAsynchronously(() -> {
if(!p.isOnline()) return;
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 | 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<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 | IllegalArgumentException ignored) {}
});
}

return cache.getOrDefault(p.getUniqueId(), "...");
}

@Override
public void cleanCache(Player player) {
cache.remove(player.getUniqueId());
cache.remove(player.getUniqueId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public QueuedFor(SpigotMain plugin) {
private final String invalidMessage = "Invalid queue name";

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

@Override
public String getRegex() {
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public Status(SpigotMain plugin) {
private final String invalidMessage = "Invalid queue name";

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

@Override
public String getRegex() {
Expand All @@ -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;
}
Expand Down
Loading

0 comments on commit ec9b055

Please sign in to comment.