-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPlayerFinder.java
More file actions
55 lines (48 loc) · 2.74 KB
/
PlayerFinder.java
File metadata and controls
55 lines (48 loc) · 2.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package me.imgalvin.playerfinder;
import com.mojang.brigadier.arguments.StringArgumentType;
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.registry.RegistryKey;
import net.minecraft.server.command.CommandManager;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;
public class PlayerFinder implements ModInitializer {
PlayerFinderUtils utils = new PlayerFinderUtils();
@Override
public void onInitialize() {
CommandRegistrationCallback.EVENT.register((dispatcher, registryAccess, environment) -> {
dispatcher.register(CommandManager.literal("findplayer")
.then(CommandManager.argument("player", StringArgumentType.string())
.suggests(new PlayerSuggestionProvider())
.executes(context -> {
String playerName = StringArgumentType.getString(context, "player");
PlayerEntity targetPlayer = context.getSource().getServer().getPlayerManager().getPlayer(playerName);
PlayerEntity sourcePlayer = context.getSource().getPlayer();
assert targetPlayer != null;
assert sourcePlayer != null;
BlockPos targetBlockPos = targetPlayer.getBlockPos();
BlockPos sourceBlockPos = sourcePlayer.getBlockPos();
RegistryKey<World> playerDimension = targetPlayer.getEntityWorld().getRegistryKey();
RegistryKey<World> sourceDimension = sourcePlayer.getEntityWorld().getRegistryKey();
boolean isSameDimension = sourceDimension == playerDimension;
context.getSource().sendFeedback(() -> (Text) Text.literal(playerName + " is at ")
.append(Text.literal(targetBlockPos.getX() + ", " + targetBlockPos.getY() + ", " + targetBlockPos.getZ())
.formatted(utils.getDimensionColor(playerDimension)))
.append(Text.literal(" in the ")
.formatted(Formatting.WHITE))
.append(Text.literal(utils.getDimensionText(playerDimension))
.formatted(utils.getDimensionColor(playerDimension)))
.append(Text.literal(isSameDimension
? " (" + utils.getDistance(sourceBlockPos, targetBlockPos) + " blocks away)"
: " (Player is in a different dimension)")
.formatted(isSameDimension ? Formatting.GREEN : Formatting.RED)), false);
return 1;
})
)
);
});
}
}