|
| 1 | +package me.drex.essentials.command.impl.misc; |
| 2 | + |
| 3 | +import com.mojang.authlib.GameProfile; |
| 4 | +import com.mojang.brigadier.builder.LiteralArgumentBuilder; |
| 5 | +import com.mojang.brigadier.exceptions.CommandSyntaxException; |
| 6 | +import com.mojang.serialization.Dynamic; |
| 7 | +import eu.pb4.placeholders.api.PlaceholderContext; |
| 8 | +import me.drex.essentials.command.Command; |
| 9 | +import me.drex.essentials.command.CommandProperties; |
| 10 | +import net.fabricmc.fabric.impl.event.interaction.FakePlayerNetworkHandler; |
| 11 | +import net.minecraft.commands.CommandBuildContext; |
| 12 | +import net.minecraft.commands.CommandSourceStack; |
| 13 | +import net.minecraft.commands.Commands; |
| 14 | +import net.minecraft.commands.arguments.EntityArgument; |
| 15 | +import net.minecraft.commands.arguments.GameProfileArgument; |
| 16 | +import net.minecraft.nbt.CompoundTag; |
| 17 | +import net.minecraft.nbt.NbtOps; |
| 18 | +import net.minecraft.resources.ResourceKey; |
| 19 | +import net.minecraft.server.MinecraftServer; |
| 20 | +import net.minecraft.server.level.ClientInformation; |
| 21 | +import net.minecraft.server.level.ServerLevel; |
| 22 | +import net.minecraft.server.level.ServerPlayer; |
| 23 | +//? if >= 1.21.9 { |
| 24 | +import net.minecraft.server.players.NameAndId; |
| 25 | +import net.minecraft.world.level.storage.TagValueInput; |
| 26 | +//?} |
| 27 | +import net.minecraft.server.players.PlayerList; |
| 28 | +import net.minecraft.world.level.Level; |
| 29 | +import net.minecraft.world.level.dimension.DimensionType; |
| 30 | +//? if >= 1.21.6 { |
| 31 | +import net.minecraft.util.ProblemReporter; |
| 32 | +import net.minecraft.world.level.storage.ValueInput; |
| 33 | +//?} |
| 34 | + |
| 35 | +import java.util.Collection; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.Optional; |
| 38 | + |
| 39 | +import static me.drex.message.api.LocalizedMessage.localized; |
| 40 | + |
| 41 | +public class WhoIsCommand extends Command { |
| 42 | + public WhoIsCommand() { |
| 43 | + super(CommandProperties.create("whois", 2)); |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + protected void registerArguments(LiteralArgumentBuilder<CommandSourceStack> literal, CommandBuildContext commandBuildContext) { |
| 48 | + literal.then( |
| 49 | + Commands.argument("target", GameProfileArgument.gameProfile()) |
| 50 | + .executes(ctx -> whois(ctx.getSource(), GameProfileArgument.getGameProfiles(ctx, "target"))) |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + private int whois(CommandSourceStack source, Collection</*? if >= 1.21.9 {*/ NameAndId /*?} else {*/ /*GameProfile *//*?}*/> targets) throws CommandSyntaxException { |
| 55 | + if (targets.isEmpty()) throw EntityArgument.NO_PLAYERS_FOUND.create(); |
| 56 | + if (targets.size() > 1) throw EntityArgument.ERROR_NOT_SINGLE_PLAYER.create(); |
| 57 | + var target = targets.iterator().next(); |
| 58 | + MinecraftServer server = source.getServer(); |
| 59 | + |
| 60 | + ServerPlayer serverPlayer = loadPlayer(server, target); |
| 61 | + source.sendSuccess(() -> localized("fabric-essentials.commands.whois", Collections.emptyMap(), PlaceholderContext.of(serverPlayer)), false); |
| 62 | + return 1; |
| 63 | + } |
| 64 | + |
| 65 | + private ServerPlayer loadPlayer(MinecraftServer server, /*? if >= 1.21.9 {*/ NameAndId /*?} else {*/ /*GameProfile *//*?}*/ target) { |
| 66 | + PlayerList playerList = server.getPlayerList(); |
| 67 | + ServerPlayer player = playerList.getPlayer(/*? if >= 1.21.9 {*/ target.id()/*?} else {*/ /*target.getId()*/ /*?}*/); |
| 68 | + if (player != null) return player; |
| 69 | + |
| 70 | + ServerPlayer serverPlayer = new ServerPlayer(server, server.overworld(), /*? if >= 1.21.9 {*/ new GameProfile(target.id(), target.name()) /*?} else {*/ /*target *//*?}*/, ClientInformation.createDefault()); |
| 71 | + new FakePlayerNetworkHandler(serverPlayer); |
| 72 | + //? if >= 1.21.6 { |
| 73 | + try (ProblemReporter.ScopedCollector scopedCollector = new ProblemReporter.ScopedCollector(serverPlayer.problemPath(), LOGGER)) { |
| 74 | + //? if >= 1.21.9 { |
| 75 | + Optional<ValueInput> optional = playerList.loadPlayerData(target) |
| 76 | + .map(compoundTag -> TagValueInput.create(scopedCollector, server.registryAccess(), compoundTag)); |
| 77 | + optional.ifPresent(serverPlayer::load); |
| 78 | + //?} else { |
| 79 | + /*Optional<ValueInput> optional = playerList.load(serverPlayer, scopedCollector); |
| 80 | + ResourceKey<Level> resourceKey = optional.flatMap(valueInput -> valueInput.read("Dimension", Level.RESOURCE_KEY_CODEC)) |
| 81 | + .orElse(Level.OVERWORLD); |
| 82 | + ServerLevel serverLevel = server.getLevel(resourceKey); |
| 83 | + if (serverLevel == null) { |
| 84 | + serverLevel = server.overworld(); |
| 85 | + } |
| 86 | + serverPlayer.setServerLevel(serverLevel); |
| 87 | + *///?} |
| 88 | + } |
| 89 | + //?} else { |
| 90 | + /*Optional<CompoundTag> optional = playerList.load(serverPlayer); |
| 91 | + ResourceKey<Level> resourceKey = optional.flatMap(compoundTag -> DimensionType.parseLegacy(new Dynamic<>(NbtOps.INSTANCE, compoundTag.get("Dimension"))).resultOrPartial(LOGGER::error)).orElse(Level.OVERWORLD); |
| 92 | + ServerLevel serverLevel = server.getLevel(resourceKey); |
| 93 | + if (serverLevel == null) { |
| 94 | + serverLevel = server.overworld(); |
| 95 | + } |
| 96 | + serverPlayer.setServerLevel(serverLevel); |
| 97 | + *///?} |
| 98 | + return serverPlayer; |
| 99 | + } |
| 100 | +} |
0 commit comments