11package me .imgalvin .playerfinder ;
22
3- import com .mojang .brigadier .arguments .StringArgumentType ;
43import net .fabricmc .api .ModInitializer ;
54import net .fabricmc .fabric .api .command .v2 .CommandRegistrationCallback ;
6- import net .minecraft .entity .player .PlayerEntity ;
7- import net .minecraft .registry .RegistryKey ;
8- import net .minecraft .server .command .CommandManager ;
9- import net .minecraft .text .Text ;
10- import net .minecraft .util .Formatting ;
11- import net .minecraft .util .math .BlockPos ;
12- import net .minecraft .world .World ;
5+ import net .minecraft .ChatFormatting ;
6+ import net .minecraft .commands .Commands ;
7+ import net .minecraft .commands .arguments .EntityArgument ;
8+ import net .minecraft .core .BlockPos ;
9+ import net .minecraft .network .chat .Component ;
10+ import net .minecraft .resources .ResourceKey ;
11+ import net .minecraft .server .level .ServerPlayer ;
12+ import net .minecraft .world .level .Level ;
13+ import org .slf4j .Logger ;
14+ import org .slf4j .LoggerFactory ;
1315
1416public class PlayerFinder implements ModInitializer {
1517 PlayerFinderUtils utils = new PlayerFinderUtils ();
1618
19+ public static final Logger LOGGER = LoggerFactory .getLogger ("PlayerFinder" );
20+
1721 @ Override
1822 public void onInitialize () {
19- CommandRegistrationCallback .EVENT .register ((dispatcher , registryAccess , environment ) -> {
20- dispatcher .register (CommandManager .literal ("findplayer" )
21- .then (CommandManager .argument ("player" , StringArgumentType .string ())
22- .suggests (new PlayerSuggestionProvider ())
23- .executes (context -> {
24- String playerName = StringArgumentType .getString (context , "player" );
25- PlayerEntity targetPlayer = context .getSource ().getServer ().getPlayerManager ().getPlayer (playerName );
26- PlayerEntity sourcePlayer = context .getSource ().getPlayer ();
27-
28- assert targetPlayer != null ;
29- assert sourcePlayer != null ;
30-
31- BlockPos targetBlockPos = targetPlayer .getBlockPos ();
32- BlockPos sourceBlockPos = sourcePlayer .getBlockPos ();
33- RegistryKey <World > playerDimension = targetPlayer .getEntityWorld ().getRegistryKey ();
34- RegistryKey <World > sourceDimension = sourcePlayer .getEntityWorld ().getRegistryKey ();
35-
36- boolean isSameDimension = sourceDimension == playerDimension ;
37-
38- context .getSource ().sendFeedback (() -> (Text ) Text .literal (playerName + " is at " )
39- .append (Text .literal (targetBlockPos .getX () + ", " + targetBlockPos .getY () + ", " + targetBlockPos .getZ ())
40- .formatted (utils .getDimensionColor (playerDimension )))
41- .append (Text .literal (" in the " )
42- .formatted (Formatting .WHITE ))
43- .append (Text .literal (utils .getDimensionText (playerDimension ))
44- .formatted (utils .getDimensionColor (playerDimension )))
45- .append (Text .literal (isSameDimension
46- ? " (" + utils .getDistance (sourceBlockPos , targetBlockPos ) + " blocks away)"
47- : " (Player is in a different dimension)" )
48- .formatted (isSameDimension ? Formatting .GREEN : Formatting .RED )), false );
49- return 1 ;
50- })
51- )
52- );
53- });
23+ LOGGER .info ("PlayerFinder initialized!" );
24+ // _ previously registryAccess, environment
25+ CommandRegistrationCallback .EVENT .register ((dispatcher , _ , _ ) -> dispatcher .register (Commands .literal ("findplayer" )
26+ .then (Commands .argument ("player" , EntityArgument .player ())
27+ .executes (context -> {
28+ LOGGER .info ("Executing /findplayer command" );
29+
30+ ServerPlayer targetPlayerName = EntityArgument .getPlayer (context , "player" );
31+ String playerName = targetPlayerName .getGameProfile ().name ();
32+
33+ ServerPlayer targetPlayer = context .getSource ().getServer ().getPlayerList ().getPlayer (playerName );
34+ ServerPlayer sourcePlayer = context .getSource ().getServer ().getPlayerList ().getPlayer (context .getSource ().getTextName ());
35+
36+ if (targetPlayer == null ) {
37+ context .getSource ().sendSystemMessage (Component .literal ("[PlayerFinder ERROR] Player " + playerName + " not found" ).withStyle (ChatFormatting .RED ));
38+ return 0 ;
39+ }
40+ if (sourcePlayer == null ) {
41+ context .getSource ().sendSystemMessage (Component .literal ("[PlayerFinder ERROR] Could not determine command source player" ).withStyle (ChatFormatting .RED ));
42+ return 0 ;
43+ }
44+
45+ BlockPos targetBlockPos = targetPlayer .blockPosition ();
46+ BlockPos sourceBlockPos = sourcePlayer .blockPosition ();
47+
48+ LOGGER .info ("Target player position: {}" , targetBlockPos );
49+ LOGGER .info ("Source player position: {}" , sourceBlockPos );
50+
51+ ResourceKey <Level > playerDimension = targetPlayer .level ().getLevel ().dimension ();
52+ ResourceKey <Level > sourceDimension = sourcePlayer .level ().getLevel ().dimension ();
53+
54+ LOGGER .info ("Target player dimension: {}" , playerDimension );
55+ LOGGER .info ("Source player dimension: {}" , sourceDimension );
56+
57+ boolean isSameDimension = sourceDimension == playerDimension ;
58+
59+ Component message = Component .literal (playerName + " is at " )
60+ .append (Component .literal (targetBlockPos .getX () + ", " + targetBlockPos .getY () + ", " + targetBlockPos .getZ ())
61+ .withStyle (utils .getDimensionColor (playerDimension )))
62+ .append (Component .literal (" in the " ).withStyle (ChatFormatting .WHITE ))
63+ .append (Component .literal (utils .getDimensionText (playerDimension ))
64+ .withStyle (utils .getDimensionColor (playerDimension )))
65+ .append (Component .literal (isSameDimension
66+ ? " (" + utils .getDistance (sourceBlockPos , targetBlockPos ) + " blocks away)"
67+ : " (Player is in a different dimension)" )
68+ .withStyle (isSameDimension ? ChatFormatting .GREEN : ChatFormatting .RED ));
69+
70+ // Send it as a system message to the source
71+ context .getSource ().sendSystemMessage (message );
72+
73+ return 1 ;
74+ })
75+ )
76+ ));
5477 }
5578}
0 commit comments