Skip to content

Commit 73e98c2

Browse files
committed
refactor: use paper's new configuration phase events
1 parent 58ac014 commit 73e98c2

File tree

3 files changed

+59
-29
lines changed

3 files changed

+59
-29
lines changed

src/main/java/pw/kaboom/extras/modules/player/PlayerConnection.java

Lines changed: 34 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,18 @@
33
import com.destroystokyo.paper.event.profile.PreLookupProfileEvent;
44
import com.destroystokyo.paper.profile.ProfileProperty;
55
import com.google.common.base.Charsets;
6+
import io.papermc.paper.event.connection.PlayerConnectionValidateLoginEvent;
7+
import io.papermc.paper.event.player.PlayerServerFullCheckEvent;
68
import net.kyori.adventure.text.Component;
79
import net.kyori.adventure.text.serializer.legacy.LegacyComponentSerializer;
810
import net.kyori.adventure.title.Title;
9-
import org.bukkit.Bukkit;
1011
import org.bukkit.Location;
11-
import org.bukkit.Server;
1212
import org.bukkit.World;
1313
import org.bukkit.configuration.file.FileConfiguration;
1414
import org.bukkit.entity.Player;
1515
import org.bukkit.event.EventHandler;
1616
import org.bukkit.event.Listener;
1717
import org.bukkit.event.player.*;
18-
import org.bukkit.event.player.PlayerLoginEvent.Result;
1918
import org.bukkit.plugin.java.JavaPlugin;
2019
import org.spigotmc.event.player.PlayerSpawnLocationEvent;
2120
import pw.kaboom.extras.Main;
@@ -25,6 +24,7 @@
2524

2625
import java.time.Duration;
2726
import java.util.HashSet;
27+
import java.util.Set;
2828
import java.util.UUID;
2929
import java.util.concurrent.ThreadLocalRandom;
3030

@@ -57,6 +57,7 @@ public final class PlayerConnection implements Listener {
5757
"allowJoinOnFullServer");
5858
private static final boolean OP_ON_JOIN = CONFIG.getBoolean("opOnJoin");
5959
private static final boolean RANDOMIZE_SPAWN = CONFIG.getBoolean("randomizeSpawn");
60+
private final Set<UUID> disallowedLogins = new HashSet<>(5);
6061

6162
@EventHandler
6263
void onAsyncPlayerPreLogin(final AsyncPlayerPreLoginEvent event) {
@@ -85,13 +86,21 @@ void onAsyncPlayerPreLogin(final AsyncPlayerPreLoginEvent event) {
8586
void onPlayerJoin(final PlayerJoinEvent event) {
8687
final Player player = event.getPlayer();
8788

89+
if (OP_ON_JOIN && !player.isOp()) {
90+
player.setOp(true);
91+
}
92+
8893
player.showTitle(Title.title(
8994
TITLE,
9095
SUBTITLE,
9196
Title.Times.times(FADE_IN, STAY, FADE_OUT)
9297
));
9398

9499
ServerTabComplete.getLoginNameList().put(player.getUniqueId(), player.getName());
100+
101+
if (!player.getPlayerProfile().hasTextures()) {
102+
SkinManager.applySkin(player, player.getName(), false);
103+
}
95104
}
96105

97106
@EventHandler
@@ -102,34 +111,32 @@ void onPlayerKick(final PlayerKickEvent event) {
102111
}
103112

104113
@EventHandler
105-
void onPlayerLogin(final PlayerLoginEvent event) {
106-
// #312 - If allow join on full server is off,
107-
// but join restrictions are disabled,
108-
// player can still join on full server
109-
110-
// Full server kicks should be handled differently from other join restrictions
111-
// since we have a separate configuration value for it
112-
113-
if (!ENABLE_JOIN_RESTRICTIONS && !Result.KICK_FULL.equals(event.getResult())) {
114-
event.allow();
114+
void onPlayerServerFullCheck(final PlayerServerFullCheckEvent event) {
115+
if (ALLOW_JOIN_ON_FULL_SERVER) {
116+
event.allow(true);
117+
} else if (!event.isAllowed()) {
118+
this.disallowedLogins.add(event.getPlayerProfile().getId());
115119
}
120+
}
116121

117-
if (Result.KICK_FULL.equals(event.getResult()) && ALLOW_JOIN_ON_FULL_SERVER) {
122+
// Note that this event gets fired even if FullCheckEvent returns disallowed, meaning we need
123+
// to keep track of the player's allowed state across events. Yuck.
124+
@SuppressWarnings("UnstableApiUsage")
125+
@EventHandler
126+
void onPlayerConnectionValidate(final PlayerConnectionValidateLoginEvent event) {
127+
// #312 - If allow join on full server is off, but join restrictions are disabled, player
128+
// can still join on full server
129+
130+
// Full server kicks should be handled differently from other join restrictions since we
131+
// have a separate configuration value for it
132+
final UUID uuid = Utility.getConnectionUuid(event.getConnection());
133+
final boolean disallowed = this.disallowedLogins.remove(uuid);
134+
135+
// If uuid is null, disallowedLogins will never contain it. So we always let connections
136+
// without a UUID through if join restrictions are disabled.
137+
if (!ENABLE_JOIN_RESTRICTIONS && !disallowed) {
118138
event.allow();
119139
}
120-
121-
final Player player = event.getPlayer();
122-
123-
if (OP_ON_JOIN && !player.isOp()) {
124-
player.setOp(true);
125-
}
126-
127-
final Server server = Bukkit.getServer();
128-
129-
130-
if (!server.getOnlineMode()) {
131-
SkinManager.applySkin(player, player.getName(), false);
132-
}
133140
}
134141

135142
@EventHandler

src/main/java/pw/kaboom/extras/modules/player/PlayerPrefix.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import org.bukkit.event.EventHandler;
1010
import org.bukkit.event.EventPriority;
1111
import org.bukkit.event.Listener;
12-
import org.bukkit.event.player.PlayerLoginEvent;
12+
import org.bukkit.event.player.PlayerJoinEvent;
1313
import org.bukkit.event.player.PlayerQuitEvent;
1414
import org.bukkit.plugin.java.JavaPlugin;
1515
import org.bukkit.scheduler.BukkitScheduler;
@@ -110,7 +110,7 @@ private static void onUpdate(Player player) throws IOException {
110110
}
111111

112112
@EventHandler(priority = EventPriority.MONITOR)
113-
public void onPlayerLoginEvent(PlayerLoginEvent event) throws IOException {
113+
public void onPlayerJoinEvent(PlayerJoinEvent event) throws IOException {
114114
final Player player = event.getPlayer();
115115
final boolean isOp = player.isOp();
116116

src/main/java/pw/kaboom/extras/util/Utility.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package pw.kaboom.extras.util;
22

3+
import com.destroystokyo.paper.profile.PlayerProfile;
4+
import io.papermc.paper.connection.PlayerConfigurationConnection;
5+
import io.papermc.paper.connection.PlayerConnection;
6+
import io.papermc.paper.connection.PlayerLoginConnection;
37
import org.bukkit.Bukkit;
48
import org.bukkit.attribute.Attributable;
59
import org.bukkit.attribute.Attribute;
@@ -10,10 +14,29 @@
1014

1115
import javax.annotation.Nonnull;
1216
import javax.annotation.Nullable;
17+
import java.util.UUID;
1318
import java.util.concurrent.Callable;
1419
import java.util.function.Function;
1520

1621
public final class Utility {
22+
@SuppressWarnings("UnstableApiUsage")
23+
public static @Nullable UUID getConnectionUuid(final PlayerConnection connection) {
24+
// https://discord.com/channels/289587909051416579/555462289851940864/1391545447495237637
25+
// Thanks, Paper!
26+
final PlayerProfile profile;
27+
if ((connection instanceof final PlayerLoginConnection loginConnection)) {
28+
profile = loginConnection.getAuthenticatedProfile() != null
29+
? loginConnection.getAuthenticatedProfile()
30+
: loginConnection.getUnsafeProfile();
31+
} else if ((connection instanceof final PlayerConfigurationConnection configConnection)) {
32+
profile = configConnection.getProfile();
33+
} else {
34+
profile = null;
35+
}
36+
37+
return profile != null ? profile.getId() : null;
38+
}
39+
1740
public static @Nullable Player getPlayerExactIgnoreCase(final String username) {
1841
return Bukkit.getOnlinePlayers()
1942
.stream()

0 commit comments

Comments
 (0)