Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Associate bukkit player with user object earlier #1096

Open
wants to merge 2 commits into
base: 2.0
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,21 +42,41 @@ 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) {
System.out.println("First factual: " + event.getPlayer().getName());
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) {
System.out.println("Second factual: " + event.getPlayer().getName());
if (PacketEvents.getAPI().getServerManager().getVersion().isOlderThan(ServerVersion.V_1_20_2)) {
this.onLogin(event.getPlayer());
}
}

private void onLogin(Player player) {
System.out.println("PLAYER LOGGED ON: " + player.getName());
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) -> {
FoliaScheduler.getEntityScheduler().runDelayed(player, plugin, (o) -> {
player.kickPlayer("PacketEvents 2.0 failed to inject");
}, 0);
}, null, 0);
}
return;
}
Expand Down
Loading