| 1 | package net.minecraft.world.entity.decoration;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.DataResult;
|
| 5 | import java.util.Arrays;
|
| 6 | import java.util.Optional;
|
| 7 | import java.util.Set;
|
| 8 | import net.minecraft.core.component.DataComponentGetter;
|
| 9 | import net.minecraft.core.component.DataComponentType;
|
| 10 | import net.minecraft.core.component.DataComponents;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.network.chat.ComponentSerialization;
|
| 13 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 14 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 15 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 16 | import net.minecraft.world.entity.Avatar;
|
| 17 | import net.minecraft.world.entity.EntityType;
|
| 18 | import net.minecraft.world.entity.EntityTypes;
|
| 19 | import net.minecraft.world.entity.HumanoidArm;
|
| 20 | import net.minecraft.world.entity.Pose;
|
| 21 | import net.minecraft.world.entity.player.PlayerModelPart;
|
| 22 | import net.minecraft.world.item.component.ResolvableProfile;
|
| 23 | import net.minecraft.world.level.Level;
|
| 24 | import net.minecraft.world.level.storage.ValueInput;
|
| 25 | import net.minecraft.world.level.storage.ValueOutput;
|
| 26 | import org.jspecify.annotations.Nullable;
|
| 27 |
|
| 28 | public class Mannequin extends Avatar {
|
| 29 | protected static final EntityDataAccessor<ResolvableProfile> DATA_PROFILE = SynchedEntityData.defineId(
|
| 30 | Mannequin.class, EntityDataSerializers.RESOLVABLE_PROFILE
|
| 31 | );
|
| 32 | private static final EntityDataAccessor<Boolean> DATA_IMMOVABLE = SynchedEntityData.defineId(Mannequin.class, EntityDataSerializers.BOOLEAN);
|
| 33 | private static final EntityDataAccessor<Optional<Component>> DATA_DESCRIPTION = SynchedEntityData.defineId(
|
| 34 | Mannequin.class, EntityDataSerializers.OPTIONAL_COMPONENT
|
| 35 | );
|
| 36 | private static final byte ALL_LAYERS = (byte)Arrays.stream(PlayerModelPart.values()).mapToInt(PlayerModelPart::getMask).reduce(0, (a, b) -> a | b);
|
| 37 | private static final Set<Pose> VALID_POSES = Set.of(Pose.STANDING, Pose.CROUCHING, Pose.SWIMMING, Pose.FALL_FLYING, Pose.SLEEPING);
|
| 38 | public static final Codec<Pose> POSE_CODEC = Pose.CODEC
|
| 39 | .validate(pose -> VALID_POSES.contains(pose) ? DataResult.success(pose) : DataResult.error(() -> "Invalid pose: " + pose.getSerializedName()));
|
| 40 | private static final Codec<Byte> LAYERS_CODEC = PlayerModelPart.CODEC
|
| 41 | .listOf()
|
| 42 | .xmap(
|
| 43 | list -> (byte)list.stream().mapToInt(PlayerModelPart::getMask).reduce(ALL_LAYERS, (a, b) -> a & ~b),
|
| 44 | mask -> Arrays.stream(PlayerModelPart.values()).filter(part -> (mask & part.getMask()) == 0).toList()
|
| 45 | );
|
| 46 | public static final ResolvableProfile DEFAULT_PROFILE = ResolvableProfile.Static.EMPTY;
|
| 47 | private static final Component DEFAULT_DESCRIPTION = Component.translatable("entity.minecraft.mannequin.label");
|
| 48 | protected static EntityType.EntityFactory<Mannequin> constructor = Mannequin::new;
|
| 49 | private static final String PROFILE_FIELD = "profile";
|
| 50 | private static final String HIDDEN_LAYERS_FIELD = "hidden_layers";
|
| 51 | private static final String MAIN_HAND_FIELD = "main_hand";
|
| 52 | private static final String POSE_FIELD = "pose";
|
| 53 | private static final String IMMOVABLE_FIELD = "immovable";
|
| 54 | private static final String DESCRIPTION_FIELD = "description";
|
| 55 | private static final String HIDE_DESCRIPTION_FIELD = "hide_description";
|
| 56 | private Component description = DEFAULT_DESCRIPTION;
|
| 57 | private boolean hideDescription = false;
|
| 58 |
|
| 59 | public Mannequin(final EntityType<Mannequin> type, final Level level) {
|
| 60 | super(type, level);
|
| 61 | this.entityData.set(DATA_PLAYER_MODE_CUSTOMISATION, ALL_LAYERS);
|
| 62 | }
|
| 63 |
|
| 64 | protected Mannequin(final Level level) {
|
| 65 | this(EntityTypes.MANNEQUIN, level);
|
| 66 | }
|
| 67 |
|
| 68 | public static @Nullable Mannequin create(final EntityType<Mannequin> type, final Level level) {
|
| 69 | return constructor.create(type, level);
|
| 70 | }
|
| 71 |
|
| 72 | @Override
|
| 73 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 74 | super.defineSynchedData(entityData);
|
| 75 | entityData.define(DATA_PROFILE, DEFAULT_PROFILE);
|
| 76 | entityData.define(DATA_IMMOVABLE, false);
|
| 77 | entityData.define(DATA_DESCRIPTION, Optional.of(DEFAULT_DESCRIPTION));
|
| 78 | }
|
| 79 |
|
| 80 | @Override
|
| 81 | public ResolvableProfile getProfile() {
|
| 82 | return this.entityData.get(DATA_PROFILE);
|
| 83 | }
|
| 84 |
|
| 85 | private void setProfile(final ResolvableProfile profile) {
|
| 86 | this.entityData.set(DATA_PROFILE, profile);
|
| 87 | }
|
| 88 |
|
| 89 | private boolean getImmovable() {
|
| 90 | return this.entityData.get(DATA_IMMOVABLE);
|
| 91 | }
|
| 92 |
|
| 93 | private void setImmovable(final boolean immovable) {
|
| 94 | this.entityData.set(DATA_IMMOVABLE, immovable);
|
| 95 | }
|
| 96 |
|
| 97 | protected @Nullable Component getDescription() {
|
| 98 | return this.entityData.get(DATA_DESCRIPTION).orElse(null);
|
| 99 | }
|
| 100 |
|
| 101 | private void setDescription(final Component description) {
|
| 102 | this.description = description;
|
| 103 | this.updateDescription();
|
| 104 | }
|
| 105 |
|
| 106 | private void setHideDescription(final boolean hideDescription) {
|
| 107 | this.hideDescription = hideDescription;
|
| 108 | this.updateDescription();
|
| 109 | }
|
| 110 |
|
| 111 | private void updateDescription() {
|
| 112 | this.entityData.set(DATA_DESCRIPTION, this.hideDescription ? Optional.empty() : Optional.of(this.description));
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | protected boolean isImmobile() {
|
| 117 | return this.getImmovable() || super.isImmobile();
|
| 118 | }
|
| 119 |
|
| 120 | @Override
|
| 121 | public boolean isEffectiveAi() {
|
| 122 | return !this.getImmovable() && super.isEffectiveAi();
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 127 | super.addAdditionalSaveData(output);
|
| 128 | output.store("profile", ResolvableProfile.CODEC, this.getProfile());
|
| 129 | output.store("hidden_layers", LAYERS_CODEC, this.entityData.get(DATA_PLAYER_MODE_CUSTOMISATION));
|
| 130 | output.store("main_hand", HumanoidArm.CODEC, this.getMainArm());
|
| 131 | output.store("pose", POSE_CODEC, this.getPose());
|
| 132 | output.putBoolean("immovable", this.getImmovable());
|
| 133 | Component description = this.getDescription();
|
| 134 | if (description != null) {
|
| 135 | if (!description.equals(DEFAULT_DESCRIPTION)) {
|
| 136 | output.store("description", ComponentSerialization.CODEC, description);
|
| 137 | }
|
| 138 | } else {
|
| 139 | output.putBoolean("hide_description", true);
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 145 | super.readAdditionalSaveData(input);
|
| 146 | input.read("profile", ResolvableProfile.CODEC).ifPresent(this::setProfile);
|
| 147 | this.entityData.set(DATA_PLAYER_MODE_CUSTOMISATION, input.read("hidden_layers", LAYERS_CODEC).orElse(ALL_LAYERS));
|
| 148 | this.setMainArm(input.read("main_hand", HumanoidArm.CODEC).orElse(DEFAULT_MAIN_HAND));
|
| 149 | this.setPose(input.read("pose", POSE_CODEC).orElse(Pose.STANDING));
|
| 150 | this.setImmovable(input.getBooleanOr("immovable", false));
|
| 151 | this.setHideDescription(input.getBooleanOr("hide_description", false));
|
| 152 | this.setDescription(input.read("description", ComponentSerialization.CODEC).orElse(DEFAULT_DESCRIPTION));
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public <T> @Nullable T get(final DataComponentType<? extends T> type) {
|
| 157 | return type == DataComponents.PROFILE ? castComponentValue((DataComponentType<T>)type, this.getProfile()) : super.get(type);
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | protected void applyImplicitComponents(final DataComponentGetter components) {
|
| 162 | this.applyImplicitComponentIfPresent(components, DataComponents.PROFILE);
|
| 163 | super.applyImplicitComponents(components);
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
|
| 168 | if (type == DataComponents.PROFILE) {
|
| 169 | this.setProfile(castComponentValue(DataComponents.PROFILE, value));
|
| 170 | return true;
|
| 171 | } else {
|
| 172 | return super.applyImplicitComponent(type, value);
|
| 173 | }
|
| 174 | }
|
| 175 | }
|
| 176 |
|