| 1 | package net.minecraft.client.entity;
|
| 2 |
|
| 3 | import com.mojang.logging.LogUtils;
|
| 4 | import java.util.Optional;
|
| 5 | import java.util.concurrent.CompletableFuture;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.multiplayer.ClientLevel;
|
| 9 | import net.minecraft.client.renderer.PlayerSkinRenderCache;
|
| 10 | import net.minecraft.client.resources.DefaultPlayerSkin;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 13 | import net.minecraft.world.entity.animal.parrot.Parrot.Variant;
|
| 14 | import net.minecraft.world.entity.decoration.Mannequin;
|
| 15 | import net.minecraft.world.entity.player.PlayerSkin;
|
| 16 | import net.minecraft.world.level.Level;
|
| 17 | import org.jspecify.annotations.Nullable;
|
| 18 | import org.slf4j.Logger;
|
| 19 |
|
| 20 | @Environment(EnvType.CLIENT)
|
| 21 | public class ClientMannequin extends Mannequin implements ClientAvatarEntity {
|
| 22 | private static final Logger LOGGER = LogUtils.getLogger();
|
| 23 | public static final PlayerSkin DEFAULT_SKIN = DefaultPlayerSkin.get(Mannequin.DEFAULT_PROFILE.partialProfile());
|
| 24 | private final ClientAvatarState avatarState = new ClientAvatarState();
|
| 25 | private @Nullable CompletableFuture<Optional<PlayerSkin>> skinLookup;
|
| 26 | private PlayerSkin skin = DEFAULT_SKIN;
|
| 27 | private final PlayerSkinRenderCache skinRenderCache;
|
| 28 |
|
| 29 | public static void registerOverrides(final PlayerSkinRenderCache cache) {
|
| 30 | Mannequin.constructor = (type, level) -> level instanceof ClientLevel ? new ClientMannequin(level, cache) : new Mannequin(type, level);
|
| 31 | }
|
| 32 |
|
| 33 | public ClientMannequin(final Level level, final PlayerSkinRenderCache skinRenderCache) {
|
| 34 | super(level);
|
| 35 | this.skinRenderCache = skinRenderCache;
|
| 36 | }
|
| 37 |
|
| 38 | @Override
|
| 39 | public void tick() {
|
| 40 | super.tick();
|
| 41 | this.avatarState.tick(this.position(), this.getDeltaMovement());
|
| 42 | if (this.skinLookup != null && this.skinLookup.isDone()) {
|
| 43 | try {
|
| 44 | this.skinLookup.get().ifPresent(this::setSkin);
|
| 45 | this.skinLookup = null;
|
| 46 | } catch (Exception e) {
|
| 47 | LOGGER.error("Error when trying to look up skin", e);
|
| 48 | }
|
| 49 | }
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 54 | super.onSyncedDataUpdated(accessor);
|
| 55 | if (accessor.equals(DATA_PROFILE)) {
|
| 56 | this.updateSkin();
|
| 57 | }
|
| 58 | }
|
| 59 |
|
| 60 | private void updateSkin() {
|
| 61 | if (this.skinLookup != null) {
|
| 62 | CompletableFuture<Optional<PlayerSkin>> future = this.skinLookup;
|
| 63 | this.skinLookup = null;
|
| 64 | future.cancel(false);
|
| 65 | }
|
| 66 |
|
| 67 | this.skinLookup = this.skinRenderCache.lookup(this.getProfile()).thenApply(info -> info.map(PlayerSkinRenderCache.RenderInfo::playerSkin));
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public ClientAvatarState avatarState() {
|
| 72 | return this.avatarState;
|
| 73 | }
|
| 74 |
|
| 75 | @Override
|
| 76 | public PlayerSkin getSkin() {
|
| 77 | return this.skin;
|
| 78 | }
|
| 79 |
|
| 80 | private void setSkin(final PlayerSkin skin) {
|
| 81 | this.skin = skin;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public @Nullable Component belowNameDisplay() {
|
| 86 | return this.getDescription();
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | public @Nullable Variant getParrotVariantOnShoulder(final boolean left) {
|
| 91 | return null;
|
| 92 | }
|
| 93 |
|
| 94 | @Override
|
| 95 | public boolean showExtraEars() {
|
| 96 | return false;
|
| 97 | }
|
| 98 | }
|
| 99 |
|