| 1 | package net.minecraft.client.multiplayer;
|
| 2 |
|
| 3 | import com.mojang.authlib.GameProfile;
|
| 4 | import java.util.function.Supplier;
|
| 5 | import net.fabricmc.api.EnvType;
|
| 6 | import net.fabricmc.api.Environment;
|
| 7 | import net.minecraft.client.Minecraft;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.network.chat.RemoteChatSession;
|
| 10 | import net.minecraft.network.chat.SignedMessageValidator;
|
| 11 | import net.minecraft.world.entity.player.PlayerSkin;
|
| 12 | import net.minecraft.world.entity.player.ProfilePublicKey;
|
| 13 | import net.minecraft.world.level.GameType;
|
| 14 | import net.minecraft.world.scores.PlayerTeam;
|
| 15 | import org.jspecify.annotations.Nullable;
|
| 16 |
|
| 17 | @Environment(EnvType.CLIENT)
|
| 18 | public class PlayerInfo {
|
| 19 | private final GameProfile profile;
|
| 20 | private @Nullable Supplier<PlayerSkin> skinLookup;
|
| 21 | private GameType gameMode = GameType.DEFAULT_MODE;
|
| 22 | private int latency;
|
| 23 | private @Nullable Component tabListDisplayName;
|
| 24 | private boolean showHat = true;
|
| 25 | private @Nullable RemoteChatSession chatSession;
|
| 26 | private SignedMessageValidator messageValidator;
|
| 27 | private int tabListOrder;
|
| 28 |
|
| 29 | public PlayerInfo(final GameProfile profile, final boolean enforcesSecureChat) {
|
| 30 | this.profile = profile;
|
| 31 | this.messageValidator = fallbackMessageValidator(enforcesSecureChat);
|
| 32 | }
|
| 33 |
|
| 34 | private static Supplier<PlayerSkin> createSkinLookup(final GameProfile profile) {
|
| 35 | Minecraft minecraft = Minecraft.getInstance();
|
| 36 | boolean requireSecure = !minecraft.isLocalPlayer(profile.id());
|
| 37 | return minecraft.getSkinManager().createLookup(profile, requireSecure);
|
| 38 | }
|
| 39 |
|
| 40 | public GameProfile getProfile() {
|
| 41 | return this.profile;
|
| 42 | }
|
| 43 |
|
| 44 | public @Nullable RemoteChatSession getChatSession() {
|
| 45 | return this.chatSession;
|
| 46 | }
|
| 47 |
|
| 48 | public SignedMessageValidator getMessageValidator() {
|
| 49 | return this.messageValidator;
|
| 50 | }
|
| 51 |
|
| 52 | public boolean hasVerifiableChat() {
|
| 53 | return this.chatSession != null;
|
| 54 | }
|
| 55 |
|
| 56 | protected void setChatSession(final RemoteChatSession chatSession) {
|
| 57 | this.chatSession = chatSession;
|
| 58 | this.messageValidator = chatSession.createMessageValidator(ProfilePublicKey.EXPIRY_GRACE_PERIOD);
|
| 59 | }
|
| 60 |
|
| 61 | protected void clearChatSession(final boolean enforcesSecureChat) {
|
| 62 | this.chatSession = null;
|
| 63 | this.messageValidator = fallbackMessageValidator(enforcesSecureChat);
|
| 64 | }
|
| 65 |
|
| 66 | private static SignedMessageValidator fallbackMessageValidator(final boolean enforcesSecureChat) {
|
| 67 | return enforcesSecureChat ? SignedMessageValidator.REJECT_ALL : SignedMessageValidator.ACCEPT_UNSIGNED;
|
| 68 | }
|
| 69 |
|
| 70 | public GameType getGameMode() {
|
| 71 | return this.gameMode;
|
| 72 | }
|
| 73 |
|
| 74 | protected void setGameMode(final GameType gameMode) {
|
| 75 | this.gameMode = gameMode;
|
| 76 | }
|
| 77 |
|
| 78 | public int getLatency() {
|
| 79 | return this.latency;
|
| 80 | }
|
| 81 |
|
| 82 | protected void setLatency(final int latency) {
|
| 83 | this.latency = latency;
|
| 84 | }
|
| 85 |
|
| 86 | public PlayerSkin getSkin() {
|
| 87 | if (this.skinLookup == null) {
|
| 88 | this.skinLookup = createSkinLookup(this.profile);
|
| 89 | }
|
| 90 |
|
| 91 | return this.skinLookup.get();
|
| 92 | }
|
| 93 |
|
| 94 | public @Nullable PlayerTeam getTeam() {
|
| 95 | return Minecraft.getInstance().level.getScoreboard().getPlayersTeam(this.getProfile().name());
|
| 96 | }
|
| 97 |
|
| 98 | public void setTabListDisplayName(final @Nullable Component tabListDisplayName) {
|
| 99 | this.tabListDisplayName = tabListDisplayName;
|
| 100 | }
|
| 101 |
|
| 102 | public @Nullable Component getTabListDisplayName() {
|
| 103 | return this.tabListDisplayName;
|
| 104 | }
|
| 105 |
|
| 106 | public void setShowHat(final boolean showHat) {
|
| 107 | this.showHat = showHat;
|
| 108 | }
|
| 109 |
|
| 110 | public boolean showHat() {
|
| 111 | return this.showHat;
|
| 112 | }
|
| 113 |
|
| 114 | public void setTabListOrder(final int tabListOrder) {
|
| 115 | this.tabListOrder = tabListOrder;
|
| 116 | }
|
| 117 |
|
| 118 | public int getTabListOrder() {
|
| 119 | return this.tabListOrder;
|
| 120 | }
|
| 121 | }
|
| 122 |
|