WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content
This repository was archived by the owner on Apr 9, 2024. It is now read-only.

Commit 07c1893

Browse files
authored
Merge pull request #115 from Andre601/feature/papiproxybridge-support
PAPIProxyBridge support
2 parents 0b21958 + eed3c37 commit 07c1893

File tree

15 files changed

+249
-30
lines changed

15 files changed

+249
-30
lines changed

bukkit/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,13 @@
8282
<dependency>
8383
<groupId>io.papermc.paper</groupId>
8484
<artifactId>paper-api</artifactId>
85-
<version>1.19.3-R0.1-SNAPSHOT</version>
85+
<version>1.20-R0.1-SNAPSHOT</version>
8686
<scope>provided</scope>
8787
</dependency>
8888
<dependency>
8989
<groupId>org.spigotmc</groupId>
9090
<artifactId>spigot-api</artifactId>
91-
<version>1.19-R0.1-SNAPSHOT</version>
91+
<version>1.20-R0.1-SNAPSHOT</version>
9292
<scope>provided</scope>
9393
</dependency>
9494
<dependency>

bungeecord/src/main/java/ch/andre601/advancedserverlist/bungeecord/listeners/BungeeEventWrapper.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import ch.andre601.advancedserverlist.bungeecord.objects.BungeePlayerImpl;
3232
import ch.andre601.advancedserverlist.api.events.GenericServerListEvent;
3333
import ch.andre601.advancedserverlist.bungeecord.objects.BungeeProxyImpl;
34+
import ch.andre601.advancedserverlist.core.events.PingEventHandler;
3435
import ch.andre601.advancedserverlist.core.interfaces.core.PluginCore;
3536
import ch.andre601.advancedserverlist.core.interfaces.events.GenericEventWrapper;
3637
import ch.andre601.advancedserverlist.core.objects.CachedPlayer;
@@ -42,6 +43,7 @@
4243
import net.md_5.bungee.api.ServerPing;
4344
import net.md_5.bungee.api.chat.TextComponent;
4445
import net.md_5.bungee.api.config.ServerInfo;
46+
import net.md_5.bungee.api.connection.ProxiedPlayer;
4547
import net.md_5.bungee.api.event.ProxyPingEvent;
4648

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

154156
@Override
155157
public String parsePAPIPlaceholders(String text, BungeePlayerImpl player){
156-
return text;
158+
if(plugin.getProxy().getPluginManager().getPlugin("PAPIProxyBridge") == null)
159+
return text;
160+
161+
if(!PingEventHandler.getPAPIUtil().isCompatible())
162+
return text;
163+
164+
String server = PingEventHandler.getPAPIUtil().getServer();
165+
if(server == null || server.isEmpty())
166+
return text;
167+
168+
ServerInfo serverInfo = plugin.getProxy().getServerInfo(server);
169+
if(serverInfo == null || serverInfo.getPlayers().isEmpty())
170+
return text;
171+
172+
ProxiedPlayer carrier = PingEventHandler.getPAPIUtil().getPlayer(serverInfo.getPlayers());
173+
if(carrier == null)
174+
return text;
175+
176+
return PingEventHandler.getPAPIUtil().parse(text, carrier.getUniqueId(), player.getUUID());
157177
}
158178

159179
@Override

bungeecord/src/main/resources/bungee.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ author: 'Andre_601'
33
version: '${plugin.version}'
44
description: '${plugin.description}'
55

6+
softDepends:
7+
- PAPIProxyBridge
8+
69
main: 'ch.andre601.advancedserverlist.bungeecord.BungeeCordCore'

core/pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@
4848
<id>flexver-repo</id>
4949
<url>https://repo.sleeping.town/</url>
5050
</repository>
51+
<repository>
52+
<id>papiproxybridge-repo</id>
53+
<url>https://repo.william278.net/releases/</url>
54+
</repository>
5155
</repositories>
5256

5357
<dependencies>
@@ -105,6 +109,12 @@
105109
<version>32.1.1-jre</version>
106110
<scope>provided</scope>
107111
</dependency>
112+
<dependency>
113+
<groupId>net.william278</groupId>
114+
<artifactId>papiproxybridge</artifactId>
115+
<version>1.3</version>
116+
<scope>provided</scope>
117+
</dependency>
108118

109119
<!-- Test dependencies -->
110120
<dependency>

core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateCheckThread.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
public class UpdateCheckThread implements ThreadFactory{
3333
@Override
3434
public Thread newThread(@NotNull Runnable r){
35-
return new Thread(r, "AdvancedServerList Update-Thread");
35+
Thread t = new Thread(r, "AdvancedServerList-UpdateThread");
36+
t.setDaemon(true);
37+
return t;
3638
}
3739
}

core/src/main/java/ch/andre601/advancedserverlist/core/check/UpdateChecker.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public class UpdateChecker{
5252
private final String url = "https://api.modrinth.com/v2/project/advancedserverlist/version?loaders=[\"%s\"]";
5353
private final OkHttpClient client = new OkHttpClient();
5454

55-
private final AdvancedServerList core;
55+
private final AdvancedServerList<?> core;
5656
private final PluginLogger logger;
5757
private final String loader;
5858

@@ -65,7 +65,7 @@ public class UpdateChecker{
6565

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

68-
public UpdateChecker(AdvancedServerList core){
68+
public UpdateChecker(AdvancedServerList<?> core){
6969
this.core = core;
7070
this.logger = core.getPlugin().getPluginLogger();
7171
this.loader = core.getPlugin().getLoader();

core/src/main/java/ch/andre601/advancedserverlist/core/commands/CommandHandler.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public class CommandHandler{
3636

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

39-
public CommandHandler(AdvancedServerList core){
39+
public CommandHandler(AdvancedServerList<?> core){
4040
subCommands.add(new Help());
4141
subCommands.add(new Reload(core));
4242
subCommands.add(new ClearCache(core));
@@ -86,9 +86,9 @@ public void handle(CmdSender sender){
8686

8787
private static class Reload extends PluginCommand{
8888

89-
private final AdvancedServerList core;
89+
private final AdvancedServerList<?> core;
9090

91-
public Reload(AdvancedServerList core){
91+
public Reload(AdvancedServerList<?> core){
9292
super("reload");
9393

9494
this.core = core;
@@ -122,9 +122,9 @@ public void handle(CmdSender sender){
122122

123123
private static class ClearCache extends PluginCommand{
124124

125-
private final AdvancedServerList core;
125+
private final AdvancedServerList<?> core;
126126

127-
public ClearCache(AdvancedServerList core){
127+
public ClearCache(AdvancedServerList<?> core){
128128
super("clearCache");
129129

130130
this.core = core;

core/src/main/java/ch/andre601/advancedserverlist/core/events/PingEventHandler.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,16 @@
3131
import ch.andre601.advancedserverlist.api.profiles.ProfileEntry;
3232
import ch.andre601.advancedserverlist.core.interfaces.core.PluginCore;
3333
import ch.andre601.advancedserverlist.core.interfaces.events.GenericEventWrapper;
34-
import ch.andre601.advancedserverlist.core.objects.GenericServerImpl;
34+
import ch.andre601.advancedserverlist.core.papi.PAPIUtil;
3535
import ch.andre601.advancedserverlist.core.parsing.ComponentParser;
3636
import ch.andre601.advancedserverlist.core.profiles.ServerListProfile;
3737
import ch.andre601.advancedserverlist.core.profiles.profile.ProfileManager;
3838
import ch.andre601.advancedserverlist.core.profiles.replacer.StringReplacer;
3939

4040
public class PingEventHandler{
4141

42+
private static PAPIUtil papiUtil = null;
43+
4244
public static <F, P extends GenericPlayer> void handleEvent(GenericEventWrapper<F, P> event){
4345
if(event.isInvalidProtocol())
4446
return;
@@ -124,4 +126,11 @@ public static <F, P extends GenericPlayer> void handleEvent(GenericEventWrapper<
124126

125127
event.updateEvent();
126128
}
129+
130+
public static PAPIUtil getPAPIUtil(){
131+
if(papiUtil != null)
132+
return papiUtil;
133+
134+
return (papiUtil = new PAPIUtil());
135+
}
127136
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2022-2023 Andre_601
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
26+
package ch.andre601.advancedserverlist.core.papi;
27+
28+
import java.util.function.Supplier;
29+
30+
public class PAPICache{
31+
32+
private CacheValue cache = null;
33+
34+
public PAPICache(){}
35+
36+
public String get(Supplier<String> supplier){
37+
if(cache == null || cache.isExpired())
38+
cache = new CacheValue(supplier.get(), System.currentTimeMillis());
39+
40+
return cache.server();
41+
}
42+
43+
private record CacheValue(String server, long timestamp){
44+
// Consider the cache expired if it is older than 5 seconds.
45+
public boolean isExpired(){
46+
return (timestamp < 0L) || System.currentTimeMillis() - timestamp >= 5000L;
47+
}
48+
}
49+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2022-2023 Andre_601
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*
24+
*/
25+
26+
package ch.andre601.advancedserverlist.core.papi;
27+
28+
import net.william278.papiproxybridge.api.PlaceholderAPI;
29+
30+
import java.util.*;
31+
import java.util.concurrent.CancellationException;
32+
import java.util.concurrent.CompletableFuture;
33+
import java.util.concurrent.CompletionException;
34+
35+
public class PAPIUtil{
36+
37+
private final PlaceholderAPI papi;
38+
39+
private final PAPICache cache = new PAPICache();
40+
41+
public PAPIUtil(){
42+
this.papi = PlaceholderAPI.createInstance();
43+
}
44+
45+
// Make sure the version of PAPIProxyBridge we get has the required methods we need...
46+
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
47+
public boolean isCompatible(){
48+
try{
49+
papi.getClass().getMethod("findServers");
50+
return true;
51+
}catch(NoSuchMethodException ex){
52+
return false;
53+
}
54+
}
55+
56+
public String getServer(){
57+
return cache.get(() -> {
58+
List<String> servers;
59+
try{
60+
servers = papi.findServers().getNow(Collections.emptyList());
61+
}catch(CancellationException | CompletionException ex){
62+
return null;
63+
}
64+
65+
if(servers == null || servers.isEmpty())
66+
return null;
67+
68+
return servers.get(0);
69+
});
70+
}
71+
72+
public <P> P getPlayer(Collection<P> players){
73+
if(players.isEmpty())
74+
return null;
75+
76+
return List.copyOf(players).get(0);
77+
}
78+
79+
public String parse(String text, UUID carrier, UUID player){
80+
try{
81+
CompletableFuture<String> future = papi.formatPlaceholders(text, carrier, player);
82+
return future.getNow(text);
83+
}catch(IllegalArgumentException | CancellationException | CompletionException ex){
84+
return text;
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)