19
19
package io .github .retrooper .packetevents .mixin ;
20
20
21
21
import com .github .retrooper .packetevents .PacketEvents ;
22
+ import com .github .retrooper .packetevents .PacketEventsAPI ;
23
+ import com .github .retrooper .packetevents .event .UserLoginEvent ;
24
+ import com .github .retrooper .packetevents .protocol .player .User ;
25
+ import com .github .retrooper .packetevents .util .FakeChannelUtil ;
22
26
import io .netty .channel .Channel ;
23
27
import net .minecraft .network .Connection ;
28
+ import net .minecraft .network .chat .Component ;
24
29
import net .minecraft .server .level .ServerPlayer ;
25
30
import net .minecraft .server .network .CommonListenerCookie ;
26
31
import net .minecraft .server .players .PlayerList ;
@@ -47,6 +52,39 @@ private void preNewPlayerPlace(
47
52
PacketEvents .getAPI ().getInjector ().setPlayer (connection .channel , player );
48
53
}
49
54
55
+ /**
56
+ * @reason Associate connection instance with player instance and handle login event
57
+ */
58
+ @ Inject (
59
+ method = "placeNewPlayer" ,
60
+ at = @ At (
61
+ value = "INVOKE" ,
62
+ target = "Lnet/minecraft/server/players/PlayerList;broadcastAll(Lnet/minecraft/network/protocol/Packet;)V" ,
63
+ shift = At .Shift .AFTER
64
+ )
65
+ )
66
+ private void onPlayerLogin (
67
+ Connection connection , ServerPlayer player ,
68
+ CommonListenerCookie cookie , CallbackInfo ci
69
+ ) {
70
+ PacketEventsAPI <?> api = PacketEvents .getAPI ();
71
+
72
+ User user = api .getPlayerManager ().getUser (player );
73
+ if (user == null ) {
74
+ Object channelObj = api .getPlayerManager ().getChannel (player );
75
+
76
+ // Check if it's a fake connection
77
+ if (!FakeChannelUtil .isFakeChannel (channelObj ) &&
78
+ (!api .isTerminated () || api .getSettings ().isKickIfTerminated ())) {
79
+ // Kick the player if they're not a fake player
80
+ player .connection .disconnect (Component .literal ("PacketEvents 2.0 failed to inject" ));
81
+ }
82
+ return ;
83
+ }
84
+
85
+ api .getEventManager ().callEvent (new UserLoginEvent (user , player ));
86
+ }
87
+
50
88
/**
51
89
* @reason Minecraft creates a new player instance on respawn
52
90
*/
0 commit comments