Skip to content
This repository has been archived by the owner on Apr 9, 2024. It is now read-only.

Commit

Permalink
Merge pull request #115 from Andre601/feature/papiproxybridge-support
Browse files Browse the repository at this point in the history
PAPIProxyBridge support
  • Loading branch information
Andre601 authored Jul 28, 2023
2 parents 0b21958 + eed3c37 commit 07c1893
Show file tree
Hide file tree
Showing 15 changed files with 249 additions and 30 deletions.
4 changes: 2 additions & 2 deletions bukkit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,13 +82,13 @@
<dependency>
<groupId>io.papermc.paper</groupId>
<artifactId>paper-api</artifactId>
<version>1.19.3-R0.1-SNAPSHOT</version>
<version>1.20-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.spigotmc</groupId>
<artifactId>spigot-api</artifactId>
<version>1.19-R0.1-SNAPSHOT</version>
<version>1.20-R0.1-SNAPSHOT</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import ch.andre601.advancedserverlist.bungeecord.objects.BungeePlayerImpl;
import ch.andre601.advancedserverlist.api.events.GenericServerListEvent;
import ch.andre601.advancedserverlist.bungeecord.objects.BungeeProxyImpl;
import ch.andre601.advancedserverlist.core.events.PingEventHandler;
import ch.andre601.advancedserverlist.core.interfaces.core.PluginCore;
import ch.andre601.advancedserverlist.core.interfaces.events.GenericEventWrapper;
import ch.andre601.advancedserverlist.core.objects.CachedPlayer;
Expand All @@ -42,6 +43,7 @@
import net.md_5.bungee.api.ServerPing;
import net.md_5.bungee.api.chat.TextComponent;
import net.md_5.bungee.api.config.ServerInfo;
import net.md_5.bungee.api.connection.ProxiedPlayer;
import net.md_5.bungee.api.event.ProxyPingEvent;

import java.awt.image.BufferedImage;
Expand Down Expand Up @@ -153,7 +155,25 @@ public String getPlayerIP(){

@Override
public String parsePAPIPlaceholders(String text, BungeePlayerImpl player){
return text;
if(plugin.getProxy().getPluginManager().getPlugin("PAPIProxyBridge") == null)
return text;

if(!PingEventHandler.getPAPIUtil().isCompatible())
return text;

String server = PingEventHandler.getPAPIUtil().getServer();
if(server == null || server.isEmpty())
return text;

ServerInfo serverInfo = plugin.getProxy().getServerInfo(server);
if(serverInfo == null || serverInfo.getPlayers().isEmpty())
return text;

ProxiedPlayer carrier = PingEventHandler.getPAPIUtil().getPlayer(serverInfo.getPlayers());
if(carrier == null)
return text;

return PingEventHandler.getPAPIUtil().parse(text, carrier.getUniqueId(), player.getUUID());
}

@Override
Expand Down
3 changes: 3 additions & 0 deletions bungeecord/src/main/resources/bungee.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ author: 'Andre_601'
version: '${plugin.version}'
description: '${plugin.description}'

softDepends:
- PAPIProxyBridge

main: 'ch.andre601.advancedserverlist.bungeecord.BungeeCordCore'
10 changes: 10 additions & 0 deletions core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@
<id>flexver-repo</id>
<url>https://repo.sleeping.town/</url>
</repository>
<repository>
<id>papiproxybridge-repo</id>
<url>https://repo.william278.net/releases/</url>
</repository>
</repositories>

<dependencies>
Expand Down Expand Up @@ -105,6 +109,12 @@
<version>32.1.1-jre</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>net.william278</groupId>
<artifactId>papiproxybridge</artifactId>
<version>1.3</version>
<scope>provided</scope>
</dependency>

<!-- Test dependencies -->
<dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
public class UpdateCheckThread implements ThreadFactory{
@Override
public Thread newThread(@NotNull Runnable r){
return new Thread(r, "AdvancedServerList Update-Thread");
Thread t = new Thread(r, "AdvancedServerList-UpdateThread");
t.setDaemon(true);
return t;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public class UpdateChecker{
private final String url = "https://api.modrinth.com/v2/project/advancedserverlist/version?loaders=[\"%s\"]";
private final OkHttpClient client = new OkHttpClient();

private final AdvancedServerList core;
private final AdvancedServerList<?> core;
private final PluginLogger logger;
private final String loader;

Expand All @@ -65,7 +65,7 @@ public class UpdateChecker{

private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(new UpdateCheckThread());

public UpdateChecker(AdvancedServerList core){
public UpdateChecker(AdvancedServerList<?> core){
this.core = core;
this.logger = core.getPlugin().getPluginLogger();
this.loader = core.getPlugin().getLoader();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class CommandHandler{

private final List<PluginCommand> subCommands = new ArrayList<>();

public CommandHandler(AdvancedServerList core){
public CommandHandler(AdvancedServerList<?> core){
subCommands.add(new Help());
subCommands.add(new Reload(core));
subCommands.add(new ClearCache(core));
Expand Down Expand Up @@ -86,9 +86,9 @@ public void handle(CmdSender sender){

private static class Reload extends PluginCommand{

private final AdvancedServerList core;
private final AdvancedServerList<?> core;

public Reload(AdvancedServerList core){
public Reload(AdvancedServerList<?> core){
super("reload");

this.core = core;
Expand Down Expand Up @@ -122,9 +122,9 @@ public void handle(CmdSender sender){

private static class ClearCache extends PluginCommand{

private final AdvancedServerList core;
private final AdvancedServerList<?> core;

public ClearCache(AdvancedServerList core){
public ClearCache(AdvancedServerList<?> core){
super("clearCache");

this.core = core;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@
import ch.andre601.advancedserverlist.api.profiles.ProfileEntry;
import ch.andre601.advancedserverlist.core.interfaces.core.PluginCore;
import ch.andre601.advancedserverlist.core.interfaces.events.GenericEventWrapper;
import ch.andre601.advancedserverlist.core.objects.GenericServerImpl;
import ch.andre601.advancedserverlist.core.papi.PAPIUtil;
import ch.andre601.advancedserverlist.core.parsing.ComponentParser;
import ch.andre601.advancedserverlist.core.profiles.ServerListProfile;
import ch.andre601.advancedserverlist.core.profiles.profile.ProfileManager;
import ch.andre601.advancedserverlist.core.profiles.replacer.StringReplacer;

public class PingEventHandler{

private static PAPIUtil papiUtil = null;

public static <F, P extends GenericPlayer> void handleEvent(GenericEventWrapper<F, P> event){
if(event.isInvalidProtocol())
return;
Expand Down Expand Up @@ -124,4 +126,11 @@ public static <F, P extends GenericPlayer> void handleEvent(GenericEventWrapper<

event.updateEvent();
}

public static PAPIUtil getPAPIUtil(){
if(papiUtil != null)
return papiUtil;

return (papiUtil = new PAPIUtil());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Andre_601
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package ch.andre601.advancedserverlist.core.papi;

import java.util.function.Supplier;

public class PAPICache{

private CacheValue cache = null;

public PAPICache(){}

public String get(Supplier<String> supplier){
if(cache == null || cache.isExpired())
cache = new CacheValue(supplier.get(), System.currentTimeMillis());

return cache.server();
}

private record CacheValue(String server, long timestamp){
// Consider the cache expired if it is older than 5 seconds.
public boolean isExpired(){
return (timestamp < 0L) || System.currentTimeMillis() - timestamp >= 5000L;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* MIT License
*
* Copyright (c) 2022-2023 Andre_601
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/

package ch.andre601.advancedserverlist.core.papi;

import net.william278.papiproxybridge.api.PlaceholderAPI;

import java.util.*;
import java.util.concurrent.CancellationException;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionException;

public class PAPIUtil{

private final PlaceholderAPI papi;

private final PAPICache cache = new PAPICache();

public PAPIUtil(){
this.papi = PlaceholderAPI.createInstance();
}

// Make sure the version of PAPIProxyBridge we get has the required methods we need...
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
public boolean isCompatible(){
try{
papi.getClass().getMethod("findServers");
return true;
}catch(NoSuchMethodException ex){
return false;
}
}

public String getServer(){
return cache.get(() -> {
List<String> servers;
try{
servers = papi.findServers().getNow(Collections.emptyList());
}catch(CancellationException | CompletionException ex){
return null;
}

if(servers == null || servers.isEmpty())
return null;

return servers.get(0);
});
}

public <P> P getPlayer(Collection<P> players){
if(players.isEmpty())
return null;

return List.copyOf(players).get(0);
}

public String parse(String text, UUID carrier, UUID player){
try{
CompletableFuture<String> future = papi.formatPlaceholders(text, carrier, player);
return future.getNow(text);
}catch(IllegalArgumentException | CancellationException | CompletionException ex){
return text;
}
}
}
Loading

0 comments on commit 07c1893

Please sign in to comment.