Skip to content

Commit

Permalink
Associate bukkit player with user object earlier
Browse files Browse the repository at this point in the history
  • Loading branch information
booky10 committed Dec 22, 2024
1 parent 7cc9251 commit 0c02c4d
Showing 1 changed file with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package io.github.retrooper.packetevents.bukkit;

import com.github.retrooper.packetevents.PacketEvents;
import com.github.retrooper.packetevents.manager.server.ServerVersion;
import com.github.retrooper.packetevents.protocol.player.User;
import com.github.retrooper.packetevents.util.FakeChannelUtil;
import io.github.retrooper.packetevents.injector.SpigotChannelInjector;
Expand All @@ -29,7 +30,10 @@
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.ApiStatus;
import org.spigotmc.event.player.PlayerSpawnLocationEvent;

@ApiStatus.Internal
public class InternalBukkitListener implements Listener {

private final Plugin plugin;
Expand All @@ -38,17 +42,34 @@ public InternalBukkitListener(Plugin plugin) {
this.plugin = plugin;
}

// this is the first event executed after the player has
// finished logging in and has a channel reference set, for 1.20.2+
@EventHandler(priority = EventPriority.LOWEST)
public void onLogin(PlayerSpawnLocationEvent event) {
if (PacketEvents.getAPI().getServerManager().getVersion().isNewerThanOrEquals(ServerVersion.V_1_20_2)) {
this.onLogin(event.getPlayer());
}
}

// packetevents may handle a few packets without a proper player
// reference here, but I'm pretty sure there is nothing we can do about that
// (and according to tests this doesn't really happen much in versions older than 1.20.2)
@EventHandler(priority = EventPriority.HIGH)
public void onJoin(PlayerJoinEvent e) {
Player player = e.getPlayer();
public void onJoin(PlayerJoinEvent event) {
if (PacketEvents.getAPI().getServerManager().getVersion().isOlderThan(ServerVersion.V_1_20_2)) {
this.onLogin(event.getPlayer());
}
}

private void onLogin(Player player) {
SpigotChannelInjector injector = (SpigotChannelInjector) PacketEvents.getAPI().getInjector();

User user = PacketEvents.getAPI().getPlayerManager().getUser(player);
if (user == null) {
//We did not inject this user
Object channel = PacketEvents.getAPI().getPlayerManager().getChannel(player);
//Check if it is a fake connection...
if (!FakeChannelUtil.isFakeChannel(channel) && (!PacketEvents.getAPI().isTerminated() || PacketEvents.getAPI().getSettings().isKickIfTerminated())) {
if (channel == null || !FakeChannelUtil.isFakeChannel(channel) && (!PacketEvents.getAPI().isTerminated() || PacketEvents.getAPI().getSettings().isKickIfTerminated())) {
//Kick them, if they are not a fake player.
FoliaScheduler.getRegionScheduler().runDelayed(plugin, player.getLocation(), (o) -> {
player.kickPlayer("PacketEvents 2.0 failed to inject");
Expand Down

0 comments on commit 0c02c4d

Please sign in to comment.