Files
src
build.gradle
gradle.properties
settings.gradle
Code
6 147
src/client/java/com/playerlocator/PlayerLocatorClient.java
package com.playerlocator;
import com.mojang.brigadier.Command;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.network.ClientPlayerEntity;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.text.Text;
public class PlayerLocatorClient implements ClientModInitializer {
@Override
public void onInitializeClient() {
ClientCommandRegistrationCallback.EVENT.register((dispatcher, registryAccess) ->
dispatcher.register(
ClientCommandManager.literal("findplayer")
.then(ClientCommandManager.argument("username", StringArgumentType.word())
.executes(context -> findPlayer(
StringArgumentType.getString(context, "username")
))
)
)
);
}
private static int findPlayer(String username) {
MinecraftClient client = MinecraftClient.getInstance();
if (client.world == null || client.player == null) {
return sendLocalMessage(client, "You must be connected to a world to use /findplayer.");
}
for (PlayerEntity player : client.world.getPlayers()) {
if (player.getName().getString().equalsIgnoreCase(username)) {
String coordinates = "X: " + Double.toString(player.getX())
+ ", Y: " + Double.toString(player.getY())
+ ", Z: " + Double.toString(player.getZ());
client.player.sendMessage(
Text.literal(player.getName().getString() + " is at " + coordinates),
false
);
return Command.SINGLE_SUCCESS;
}
}
return sendLocalMessage(client, "Player '" + username + "' is not currently visible to your client.");
}
private static int sendLocalMessage(MinecraftClient client, String message) {
if (client.player != null) {
client.player.sendMessage(Text.literal(message), false);
}
return 0;
}
}