Skip to content

Commit 82b75af

Browse files
committed
Make vanish use futures. Also adding the missing Vanish response in https://github.com/Fernthedev/AskPlaceHolder
1 parent 38a7cb4 commit 82b75af

File tree

8 files changed

+91
-42
lines changed

8 files changed

+91
-42
lines changed

core/src/main/java/com/github/fernthedev/fernapi/universal/api/IFPlayer.java

Lines changed: 26 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.github.fernthedev.fernapi.universal.api;
22

33
import com.github.fernthedev.fernapi.universal.data.network.IServerInfo;
4-
import com.github.fernthedev.fernapi.universal.exceptions.FernRuntimeException;
54
import com.github.fernthedev.fernapi.universal.exceptions.network.PluginTimeoutException;
65
import com.github.fernthedev.fernapi.universal.util.UUIDFetcher;
76
import com.github.fernthedev.fernapi.universal.util.network.vanish.VanishProxyCheck;
@@ -15,6 +14,8 @@
1514
import java.util.Arrays;
1615
import java.util.List;
1716
import java.util.UUID;
17+
import java.util.concurrent.CompletableFuture;
18+
import java.util.concurrent.TimeUnit;
1819

1920
@Getter
2021
public abstract class IFPlayer<T> implements FernCommandIssuer {
@@ -111,33 +112,40 @@ public boolean isPlayerNull() {
111112
return isPlayer || isDataNull;
112113
}
113114

114-
public boolean isVanished() {
115-
final boolean[] vanished = new boolean[1];
116-
try {
117-
new VanishProxyCheck(this, (player, isVanished, timedOut) -> {
118-
if (timedOut) throw new PluginTimeoutException("The vanish check timed out. The server must have FernAPI enabled and registered");
115+
public CompletableFuture<Boolean> isVanished() {
116+
return isVanished(10, TimeUnit.SECONDS);
117+
}
119118

120-
vanished[0] = isVanished;
121-
}).awaitVanishResponse(20);
122-
} catch (InterruptedException e) {
123-
throw new FernRuntimeException("Interrupted", e);
124-
}
119+
public CompletableFuture<Boolean> isVanished(int amount, TimeUnit timeUnit) {
120+
CompletableFuture<Boolean> completableFuture = new CompletableFuture<>();
121+
122+
new VanishProxyCheck(this, (player, isVanished, timedOut) -> {
123+
if (timedOut) throw new PluginTimeoutException("The vanish check timed out. The server must have FernAPI enabled and registered");
124+
125+
completableFuture.complete(isVanished);
126+
}).setTimeout(amount, timeUnit);
125127

126-
return vanished[0];
128+
return completableFuture;
127129
}
128130

129-
public boolean canSee(IFPlayer<?> player) {
130-
return player.isVanished() && hasVanishPermission("acf.seevanish");
131+
public CompletableFuture<Boolean> canSee(IFPlayer<?> player) {
132+
if (hasVanishPermission()) return CompletableFuture.completedFuture(true);
133+
134+
return player.isVanished().thenApply(aBoolean -> !aBoolean || hasVanishPermission());
131135
}
132136

133137

134138

135-
public static boolean canSee(FernCommandIssuer commandIssuer, IFPlayer<?> player) {
136-
if (!commandIssuer.isPlayer()) return true;
139+
public static CompletableFuture<Boolean> canSee(FernCommandIssuer commandIssuer, IFPlayer<?> player) {
140+
if (!commandIssuer.isPlayer()) return CompletableFuture.completedFuture(true);
141+
if (commandIssuer.hasVanishPermission("acf.seevanish")) return CompletableFuture.completedFuture(true);
142+
143+
144+
CompletableFuture<Boolean> vanished = player.isVanished();
137145

138-
boolean vanished = player.isVanished();
139146

140-
return !vanished || commandIssuer.hasVanishPermission();
147+
// Invert to see if it can be seen
148+
return vanished.thenApply(v -> !v);
141149
}
142150

143151
/**

core/src/main/java/com/github/fernthedev/fernapi/universal/api/OfflineFPlayer.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.net.InetSocketAddress;
1111
import java.util.UUID;
12+
import java.util.concurrent.CompletableFuture;
1213

1314
public class OfflineFPlayer<P> extends IFPlayer<IFPlayer<P>> {
1415

@@ -104,12 +105,12 @@ public String getCurrentServerName() {
104105
}
105106

106107
@Override
107-
public boolean isVanished() {
108+
public CompletableFuture<Boolean> isVanished() {
108109
return player.isVanished();
109110
}
110111

111112
@Override
112-
public boolean canSee(IFPlayer<?> player) {
113+
public CompletableFuture<Boolean> canSee(IFPlayer<?> player) {
113114
return player.canSee(player);
114115
}
115116

core/src/main/java/com/github/fernthedev/fernapi/universal/util/UniversalContextResolvers.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.github.fernthedev.fernapi.universal.api.IFConsole;
1010
import com.github.fernthedev.fernapi.universal.api.IFPlayer;
1111
import com.github.fernthedev.fernapi.universal.api.OfflineFPlayer;
12+
import lombok.SneakyThrows;
1213
import org.jetbrains.annotations.Nullable;
1314

1415
import java.util.*;
@@ -292,13 +293,14 @@ public static IFPlayer<?> findPlayerSmart(CommandIssuer issuer, String search) {
292293
return Universal.getMethods().getPlayerFromName(name);
293294
}
294295

296+
@SneakyThrows
295297
private static void findMatches(String search, CommandIssuer requester, List<? extends IFPlayer<?>> matches, List<IFPlayer<?>> confirmList) {
296298
// Remove vanished players from smart matching.
297299
Iterator<? extends IFPlayer<?>> iter = matches.iterator();
298300
//noinspection Duplicates
299301
while (iter.hasNext()) {
300302
IFPlayer<?> player = iter.next();
301-
if (requester instanceof IFPlayer<?> && !((IFPlayer<?>) requester).canSee(player)) {
303+
if (requester instanceof IFPlayer<?> && !((IFPlayer<?>) requester).canSee(player).get()) {
302304
if (requester.hasPermission("acf.seevanish")) {
303305
if (!search.endsWith(":confirm")) {
304306
confirmList.add(player);

core/src/main/java/com/github/fernthedev/fernapi/universal/util/network/vanish/VanishProxyCheck.java

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,20 @@
1313

1414
import java.io.ByteArrayOutputStream;
1515
import java.util.*;
16+
import java.util.concurrent.CompletableFuture;
1617
import java.util.concurrent.TimeUnit;
1718

1819
public class VanishProxyCheck extends PluginMessageHandler {
1920

2021
private static Object o;
2122

22-
private VanishFunction vanishFunction;
23-
private IFPlayer<?> player;
23+
private final CompletableFuture<VanishProxyResponse> completableFuture;
24+
private final VanishFunction vanishFunction;
25+
private final IFPlayer<?> player;
2426

25-
private static Map<UUID, VanishProxyCheck> instances = new HashMap<>();
27+
private static final Map<UUID, VanishProxyCheck> instances = new HashMap<>();
2628

27-
private UUID uuid;
29+
private final UUID uuid;
2830

2931
/**
3032
* Internal use
@@ -38,16 +40,19 @@ public class VanishProxyCheck extends PluginMessageHandler {
3840
* @param timeout The amount of time to wait
3941
* @param timeUnit The unit of time
4042
*/
41-
public void setTimeout(long timeout, TimeUnit timeUnit) {
43+
public VanishProxyCheck setTimeout(long timeout, TimeUnit timeUnit) {
4244
VanishProxyCheck vanishProxyCheck = this;
4345

4446
Universal.getScheduler().runSchedule(() -> {
4547
if (instances.containsKey(vanishProxyCheck.uuid)) {
48+
completableFuture.complete(new VanishProxyResponse(player, false, true));
4649
vanishFunction.run(player, false, true);
4750
instances.remove(vanishProxyCheck.uuid);
4851

4952
}
5053
}, timeout, timeUnit);
54+
55+
return this;
5156
}
5257

5358

@@ -60,6 +65,10 @@ private VanishProxyCheck() {
6065

6166
o = new Object();
6267
Universal.getMethods().getAbstractLogger().info("Registered VanishProxyCheck Listener");
68+
completableFuture = null;
69+
player = null;
70+
vanishFunction = null;
71+
uuid = null;
6372
}
6473

6574
/**
@@ -72,6 +81,7 @@ public VanishProxyCheck(@NonNull IFPlayer<?> player, @NonNull VanishFunction van
7281
this.vanishFunction = vanishFunction;
7382
this.player = player;
7483
uuid = UUID.randomUUID();
84+
completableFuture = new CompletableFuture<>();
7585

7686
instances.put(uuid, this);
7787

@@ -118,11 +128,12 @@ public void onMessageReceived(PluginMessageData data, Channel channel) {
118128
if (Universal.getMethods().getServerType().isProxy()) {
119129
boolean vanished = Boolean.parseBoolean(dataQueue.remove());
120130

121-
System.out.println(vanished + " vanished");
131+
Universal.debug(vanished + " vanished");
122132

123133

124134
VanishProxyCheck vanishProxyCheck = instances.get(uuidCheck);
125135
if (vanishProxyCheck != null) {
136+
completableFuture.complete(new VanishProxyResponse(vanishProxyCheck.player, vanished, false));
126137
Universal.getScheduler().runAsync(() -> vanishFunction.run(vanishProxyCheck.player, vanished, false));
127138

128139
instances.remove(vanishProxyCheck.uuid);
@@ -134,6 +145,9 @@ public void onMessageReceived(PluginMessageData data, Channel channel) {
134145
* Synchronise to await message response
135146
*
136147
* @param millis The amount of time to wait
148+
*
149+
* @deprecated Use {@link CompletableFuture#join()} with {@link #completableFuture}
150+
*
137151
* @throws InterruptedException
138152
*/
139153
public void awaitVanishResponse(int millis) throws InterruptedException {
@@ -142,5 +156,7 @@ public void awaitVanishResponse(int millis) throws InterruptedException {
142156
}
143157
}
144158

145-
159+
public CompletableFuture<VanishProxyResponse> getCompletableFuture() {
160+
return completableFuture;
161+
}
146162
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package com.github.fernthedev.fernapi.universal.util.network.vanish;
2+
3+
import com.github.fernthedev.fernapi.universal.api.IFPlayer;
4+
5+
public class VanishProxyResponse {
6+
private final IFPlayer<?> player;
7+
private final boolean isVanished;
8+
private final boolean timedOut;
9+
10+
public VanishProxyResponse(IFPlayer<?> player, boolean isVanished, boolean timedOut) {
11+
this.player = player;
12+
this.isVanished = isVanished;
13+
this.timedOut = timedOut;
14+
}
15+
16+
public IFPlayer<?> getPlayer() {
17+
return player;
18+
}
19+
20+
public boolean isVanished() {
21+
return isVanished;
22+
}
23+
24+
public boolean isTimedOut() {
25+
return timedOut;
26+
}
27+
}

spigot/build.gradle

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,6 @@ plugins {
66
sourceCompatibility = 1.8
77

88
repositories {
9-
maven {
10-
name = 'placeholderapi'
11-
url = 'http://repo.extendedclip.com/content/repositories/placeholderapi/'
12-
}
139
maven {
1410
name = 'spigotmc-repo'
1511
url = 'https://hub.spigotmc.org/nexus/content/groups/public/'
@@ -29,7 +25,6 @@ dependencies {
2925
compile "net.kyori:adventure-platform-bukkit:$adventureAPIPlatform"
3026
api'fr.minuskube.inv:smart-invs:1.2.7'
3127
api ("me.lucko:helper:5.6.5")
32-
compileOnly group: 'me.clip', name: 'placeholderapi', version: '2.9.2'
3328

3429
compileOnly 'org.spigotmc:spigot-api:1.14.4-R0.1-SNAPSHOT' // The Spigot API with no shadowing. Requires the OSS repo.
3530
compileOnly "com.github.MilkBowl:VaultAPI:1.7"

spigot/src/main/java/com/github/fernthedev/fernapi/server/spigot/player/SpigotFPlayer.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@
44
import com.github.fernthedev.fernapi.universal.api.IFPlayer;
55
import com.github.fernthedev.fernapi.universal.data.chat.BaseMessage;
66
import com.github.fernthedev.fernapi.universal.data.network.IServerInfo;
7-
import com.github.fernthedev.fernapi.universal.handlers.NetworkHandler;
87
import net.kyori.adventure.audience.Audience;
98
import net.md_5.bungee.api.chat.BaseComponent;
109
import net.md_5.bungee.api.chat.ComponentBuilder;
1110
import org.bukkit.ChatColor;
12-
import org.bukkit.Server;
1311
import org.bukkit.entity.Player;
1412
import org.bukkit.metadata.MetadataValue;
1513
import org.jetbrains.annotations.Nullable;
1614

1715
import java.lang.reflect.InvocationTargetException;
1816
import java.net.InetSocketAddress;
1917
import java.util.UUID;
18+
import java.util.concurrent.CompletableFuture;
2019

2120
public class SpigotFPlayer extends IFPlayer<Player> {
2221

@@ -66,24 +65,24 @@ public void sendMessage(BaseMessage baseMessage) {
6665
public IServerInfo getServerInfo() {
6766
if (player == null) return null;
6867

69-
return ((NetworkHandler<Server>) Universal.getNetworkHandler()).toServer(player.getServer());
68+
return Universal.getNetworkHandler().toServer(player.getServer());
7069
}
7170

7271
private BaseComponent[] message(String text) {
7372
return new ComponentBuilder(ChatColor.translateAlternateColorCodes('&',text)).create();
7473
}
7574

7675
@Override
77-
public boolean isVanished() {
76+
public CompletableFuture<Boolean> isVanished() {
7877
for (MetadataValue meta : player.getMetadata("vanished")) {
79-
if (meta.asBoolean()) return true;
78+
if (meta.asBoolean()) return CompletableFuture.completedFuture(true);
8079
}
81-
return false;
80+
return CompletableFuture.completedFuture(false);
8281
}
8382

8483

8584
@Override
86-
public boolean canSee(IFPlayer<?> player) {
87-
return this.player.canSee((Player) player.getPlayer());
85+
public CompletableFuture<Boolean> canSee(IFPlayer<?> player) {
86+
return CompletableFuture.completedFuture(this.player.canSee((Player) player.getPlayer()));
8887
}
8988
}

sponge/src/main/java/com/github/fernthedev/fernapi/server/sponge/player/SpongeFPlayer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import java.net.InetSocketAddress;
1515
import java.util.UUID;
16+
import java.util.concurrent.CompletableFuture;
1617

1718
public class SpongeFPlayer extends IFPlayer<Player> {
1819

@@ -88,7 +89,7 @@ public IServerInfo getServerInfo() {
8889
}
8990

9091
@Override
91-
public boolean isVanished() {
92+
public CompletableFuture<Boolean> isVanished() {
9293
throw new FernRuntimeException("This method does not work on Sponge yet. Use Universal.getMethods().getServerType() to check for Sponge");
9394
}
9495

0 commit comments

Comments
 (0)