| 1 | package net.minecraft.world.entity.decoration;
|
| 2 |
|
| 3 | import com.mojang.serialization.Codec;
|
| 4 | import com.mojang.serialization.codecs.RecordCodecBuilder;
|
| 5 | import java.util.function.Predicate;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.Rotations;
|
| 8 | import net.minecraft.core.component.DataComponents;
|
| 9 | import net.minecraft.core.particles.BlockParticleOption;
|
| 10 | import net.minecraft.core.particles.ParticleTypes;
|
| 11 | import net.minecraft.network.syncher.EntityDataAccessor;
|
| 12 | import net.minecraft.network.syncher.EntityDataSerializers;
|
| 13 | import net.minecraft.network.syncher.SynchedEntityData;
|
| 14 | import net.minecraft.server.level.ServerLevel;
|
| 15 | import net.minecraft.sounds.SoundEvent;
|
| 16 | import net.minecraft.sounds.SoundEvents;
|
| 17 | import net.minecraft.tags.DamageTypeTags;
|
| 18 | import net.minecraft.world.InteractionHand;
|
| 19 | import net.minecraft.world.InteractionResult;
|
| 20 | import net.minecraft.world.damagesource.DamageSource;
|
| 21 | import net.minecraft.world.entity.Entity;
|
| 22 | import net.minecraft.world.entity.EntityDimensions;
|
| 23 | import net.minecraft.world.entity.EntityEvent;
|
| 24 | import net.minecraft.world.entity.EntityType;
|
| 25 | import net.minecraft.world.entity.EntityTypes;
|
| 26 | import net.minecraft.world.entity.EquipmentSlot;
|
| 27 | import net.minecraft.world.entity.HumanoidArm;
|
| 28 | import net.minecraft.world.entity.LightningBolt;
|
| 29 | import net.minecraft.world.entity.LivingEntity;
|
| 30 | import net.minecraft.world.entity.Mob;
|
| 31 | import net.minecraft.world.entity.Pose;
|
| 32 | import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
|
| 33 | import net.minecraft.world.entity.ai.attributes.Attributes;
|
| 34 | import net.minecraft.world.entity.player.Player;
|
| 35 | import net.minecraft.world.entity.vehicle.minecart.AbstractMinecart;
|
| 36 | import net.minecraft.world.item.ItemStack;
|
| 37 | import net.minecraft.world.item.Items;
|
| 38 | import net.minecraft.world.item.enchantment.EnchantmentEffectComponents;
|
| 39 | import net.minecraft.world.item.enchantment.EnchantmentHelper;
|
| 40 | import net.minecraft.world.level.Explosion;
|
| 41 | import net.minecraft.world.level.Level;
|
| 42 | import net.minecraft.world.level.LightLayer;
|
| 43 | import net.minecraft.world.level.block.Block;
|
| 44 | import net.minecraft.world.level.block.Blocks;
|
| 45 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 46 | import net.minecraft.world.level.gamerules.GameRules;
|
| 47 | import net.minecraft.world.level.material.PushReaction;
|
| 48 | import net.minecraft.world.level.storage.ValueInput;
|
| 49 | import net.minecraft.world.level.storage.ValueOutput;
|
| 50 | import net.minecraft.world.phys.AABB;
|
| 51 | import net.minecraft.world.phys.Vec3;
|
| 52 | import org.jspecify.annotations.Nullable;
|
| 53 |
|
| 54 | public class ArmorStand extends LivingEntity {
|
| 55 | public static final int WOBBLE_TIME = 5;
|
| 56 | private static final boolean ENABLE_ARMS = true;
|
| 57 | public static final Rotations DEFAULT_HEAD_POSE = new Rotations(0.0F, 0.0F, 0.0F);
|
| 58 | public static final Rotations DEFAULT_BODY_POSE = new Rotations(0.0F, 0.0F, 0.0F);
|
| 59 | public static final Rotations DEFAULT_LEFT_ARM_POSE = new Rotations(-10.0F, 0.0F, -10.0F);
|
| 60 | public static final Rotations DEFAULT_RIGHT_ARM_POSE = new Rotations(-15.0F, 0.0F, 10.0F);
|
| 61 | public static final Rotations DEFAULT_LEFT_LEG_POSE = new Rotations(-1.0F, 0.0F, -1.0F);
|
| 62 | public static final Rotations DEFAULT_RIGHT_LEG_POSE = new Rotations(1.0F, 0.0F, 1.0F);
|
| 63 | private static final EntityDimensions MARKER_DIMENSIONS = EntityDimensions.fixed(0.0F, 0.0F);
|
| 64 | private static final EntityDimensions BABY_DIMENSIONS = EntityTypes.ARMOR_STAND.getDimensions().scale(0.5F).withEyeHeight(0.9875F);
|
| 65 | private static final double FEET_OFFSET = 0.1;
|
| 66 | private static final double CHEST_OFFSET = 0.9;
|
| 67 | private static final double LEGS_OFFSET = 0.4;
|
| 68 | private static final double HEAD_OFFSET = 1.6;
|
| 69 | public static final int DISABLE_TAKING_OFFSET = 8;
|
| 70 | public static final int DISABLE_PUTTING_OFFSET = 16;
|
| 71 | public static final int CLIENT_FLAG_SMALL = 1;
|
| 72 | public static final int CLIENT_FLAG_SHOW_ARMS = 4;
|
| 73 | public static final int CLIENT_FLAG_NO_BASEPLATE = 8;
|
| 74 | public static final int CLIENT_FLAG_MARKER = 16;
|
| 75 | public static final EntityDataAccessor<Byte> DATA_CLIENT_FLAGS = SynchedEntityData.defineId(ArmorStand.class, EntityDataSerializers.BYTE);
|
| 76 | public static final EntityDataAccessor<Rotations> DATA_HEAD_POSE = SynchedEntityData.defineId(ArmorStand.class, EntityDataSerializers.ROTATIONS);
|
| 77 | public static final EntityDataAccessor<Rotations> DATA_BODY_POSE = SynchedEntityData.defineId(ArmorStand.class, EntityDataSerializers.ROTATIONS);
|
| 78 | public static final EntityDataAccessor<Rotations> DATA_LEFT_ARM_POSE = SynchedEntityData.defineId(ArmorStand.class, EntityDataSerializers.ROTATIONS);
|
| 79 | public static final EntityDataAccessor<Rotations> DATA_RIGHT_ARM_POSE = SynchedEntityData.defineId(ArmorStand.class, EntityDataSerializers.ROTATIONS);
|
| 80 | public static final EntityDataAccessor<Rotations> DATA_LEFT_LEG_POSE = SynchedEntityData.defineId(ArmorStand.class, EntityDataSerializers.ROTATIONS);
|
| 81 | public static final EntityDataAccessor<Rotations> DATA_RIGHT_LEG_POSE = SynchedEntityData.defineId(ArmorStand.class, EntityDataSerializers.ROTATIONS);
|
| 82 | private static final Predicate<Entity> RIDABLE_MINECARTS = entity -> entity instanceof AbstractMinecart minecart && minecart.isRideable();
|
| 83 | private static final boolean DEFAULT_INVISIBLE = false;
|
| 84 | private static final int DEFAULT_DISABLED_SLOTS = 0;
|
| 85 | private static final boolean DEFAULT_SMALL = false;
|
| 86 | private static final boolean DEFAULT_SHOW_ARMS = false;
|
| 87 | private static final boolean DEFAULT_NO_BASE_PLATE = false;
|
| 88 | private static final boolean DEFAULT_MARKER = false;
|
| 89 | private boolean invisible = false;
|
| 90 | public long lastHit;
|
| 91 | private int disabledSlots = 0;
|
| 92 |
|
| 93 | public ArmorStand(final EntityType<? extends ArmorStand> type, final Level level) {
|
| 94 | super(type, level);
|
| 95 | }
|
| 96 |
|
| 97 | public ArmorStand(final Level level, final double x, final double y, final double z) {
|
| 98 | this(EntityTypes.ARMOR_STAND, level);
|
| 99 | this.setPos(x, y, z);
|
| 100 | }
|
| 101 |
|
| 102 | public static AttributeSupplier.Builder createAttributes() {
|
| 103 | return createLivingAttributes().add(Attributes.STEP_HEIGHT, 0.0);
|
| 104 | }
|
| 105 |
|
| 106 | @Override
|
| 107 | public void refreshDimensions() {
|
| 108 | double oldX = this.getX();
|
| 109 | double oldY = this.getY();
|
| 110 | double oldZ = this.getZ();
|
| 111 | super.refreshDimensions();
|
| 112 | this.setPos(oldX, oldY, oldZ);
|
| 113 | }
|
| 114 |
|
| 115 | private boolean hasPhysics() {
|
| 116 | return !this.isMarker() && !this.isNoGravity();
|
| 117 | }
|
| 118 |
|
| 119 | @Override
|
| 120 | public boolean isEffectiveAi() {
|
| 121 | return super.isEffectiveAi() && this.hasPhysics();
|
| 122 | }
|
| 123 |
|
| 124 | @Override
|
| 125 | protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
|
| 126 | super.defineSynchedData(entityData);
|
| 127 | entityData.define(DATA_CLIENT_FLAGS, (byte)0);
|
| 128 | entityData.define(DATA_HEAD_POSE, DEFAULT_HEAD_POSE);
|
| 129 | entityData.define(DATA_BODY_POSE, DEFAULT_BODY_POSE);
|
| 130 | entityData.define(DATA_LEFT_ARM_POSE, DEFAULT_LEFT_ARM_POSE);
|
| 131 | entityData.define(DATA_RIGHT_ARM_POSE, DEFAULT_RIGHT_ARM_POSE);
|
| 132 | entityData.define(DATA_LEFT_LEG_POSE, DEFAULT_LEFT_LEG_POSE);
|
| 133 | entityData.define(DATA_RIGHT_LEG_POSE, DEFAULT_RIGHT_LEG_POSE);
|
| 134 | }
|
| 135 |
|
| 136 | @Override
|
| 137 | public boolean canUseSlot(final EquipmentSlot slot) {
|
| 138 | return slot != EquipmentSlot.BODY && slot != EquipmentSlot.SADDLE && !this.isDisabled(slot);
|
| 139 | }
|
| 140 |
|
| 141 | @Override
|
| 142 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 143 | super.addAdditionalSaveData(output);
|
| 144 | output.putBoolean("Invisible", this.isInvisible());
|
| 145 | output.putBoolean("Small", this.isSmall());
|
| 146 | output.putBoolean("ShowArms", this.showArms());
|
| 147 | output.putInt("DisabledSlots", this.disabledSlots);
|
| 148 | output.putBoolean("NoBasePlate", !this.showBasePlate());
|
| 149 | if (this.isMarker()) {
|
| 150 | output.putBoolean("Marker", this.isMarker());
|
| 151 | }
|
| 152 |
|
| 153 | output.store("Pose", ArmorStand.ArmorStandPose.CODEC, this.getArmorStandPose());
|
| 154 | }
|
| 155 |
|
| 156 | @Override
|
| 157 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 158 | super.readAdditionalSaveData(input);
|
| 159 | this.setInvisible(input.getBooleanOr("Invisible", false));
|
| 160 | this.setSmall(input.getBooleanOr("Small", false));
|
| 161 | this.setShowArms(input.getBooleanOr("ShowArms", false));
|
| 162 | this.disabledSlots = input.getIntOr("DisabledSlots", 0);
|
| 163 | this.setNoBasePlate(input.getBooleanOr("NoBasePlate", false));
|
| 164 | this.setMarker(input.getBooleanOr("Marker", false));
|
| 165 | this.noPhysics = !this.hasPhysics();
|
| 166 | input.read("Pose", ArmorStand.ArmorStandPose.CODEC).ifPresent(this::setArmorStandPose);
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | public boolean isPushable() {
|
| 171 | return false;
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | protected void doPush(final Entity entity) {
|
| 176 | }
|
| 177 |
|
| 178 | @Override
|
| 179 | protected void pushEntities() {
|
| 180 | for (Entity entity : this.level().getEntities(this, this.getBoundingBox(), RIDABLE_MINECARTS)) {
|
| 181 | if (this.distanceToSqr(entity) <= 0.2) {
|
| 182 | entity.push(this);
|
| 183 | }
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | @Override
|
| 188 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 189 | ItemStack itemStack = player.getItemInHand(hand);
|
| 190 | if (this.isMarker() || itemStack.is(Items.NAME_TAG)) {
|
| 191 | return super.interact(player, hand, location);
|
| 192 | }
|
| 193 |
|
| 194 | if (player.isSpectator()) {
|
| 195 | return InteractionResult.SUCCESS;
|
| 196 | }
|
| 197 |
|
| 198 | if (player.level().isClientSide()) {
|
| 199 | return InteractionResult.SUCCESS_SERVER;
|
| 200 | }
|
| 201 |
|
| 202 | EquipmentSlot itemInHandSlot = this.getEquipmentSlotForItem(itemStack);
|
| 203 | if (itemStack.isEmpty()) {
|
| 204 | EquipmentSlot clickedSlot = this.getClickedSlot(location);
|
| 205 | EquipmentSlot targetSlot = this.isDisabled(clickedSlot) ? itemInHandSlot : clickedSlot;
|
| 206 | if (this.hasItemInSlot(targetSlot) && this.swapItem(player, targetSlot, itemStack, hand)) {
|
| 207 | return InteractionResult.SUCCESS_SERVER;
|
| 208 | }
|
| 209 | } else {
|
| 210 | if (this.isDisabled(itemInHandSlot)) {
|
| 211 | return InteractionResult.FAIL;
|
| 212 | }
|
| 213 |
|
| 214 | if (itemInHandSlot.getType() == EquipmentSlot.Type.HAND && !this.showArms()) {
|
| 215 | return InteractionResult.FAIL;
|
| 216 | }
|
| 217 |
|
| 218 | if (this.swapItem(player, itemInHandSlot, itemStack, hand)) {
|
| 219 | return InteractionResult.SUCCESS_SERVER;
|
| 220 | }
|
| 221 | }
|
| 222 |
|
| 223 | return super.interact(player, hand, location);
|
| 224 | }
|
| 225 |
|
| 226 | private EquipmentSlot getClickedSlot(final Vec3 location) {
|
| 227 | EquipmentSlot slotClicked = EquipmentSlot.MAINHAND;
|
| 228 | boolean small = this.isSmall();
|
| 229 | double clickYPosition = location.y / (this.getScale() * this.getAgeScale());
|
| 230 | EquipmentSlot feet = EquipmentSlot.FEET;
|
| 231 | if (clickYPosition >= 0.1 && clickYPosition < 0.1 + (small ? 0.8 : 0.45) && this.hasItemInSlot(feet)) {
|
| 232 | slotClicked = EquipmentSlot.FEET;
|
| 233 | } else if (clickYPosition >= 0.9 + (small ? 0.3 : 0.0) && clickYPosition < 0.9 + (small ? 1.0 : 0.7) && this.hasItemInSlot(EquipmentSlot.CHEST)) {
|
| 234 | slotClicked = EquipmentSlot.CHEST;
|
| 235 | } else if (clickYPosition >= 0.4 && clickYPosition < 0.4 + (small ? 1.0 : 0.8) && this.hasItemInSlot(EquipmentSlot.LEGS)) {
|
| 236 | slotClicked = EquipmentSlot.LEGS;
|
| 237 | } else if (clickYPosition >= 1.6 && this.hasItemInSlot(EquipmentSlot.HEAD)) {
|
| 238 | slotClicked = EquipmentSlot.HEAD;
|
| 239 | } else if (!this.hasItemInSlot(EquipmentSlot.MAINHAND) && this.hasItemInSlot(EquipmentSlot.OFFHAND)) {
|
| 240 | slotClicked = EquipmentSlot.OFFHAND;
|
| 241 | }
|
| 242 |
|
| 243 | return slotClicked;
|
| 244 | }
|
| 245 |
|
| 246 | private boolean isDisabled(final EquipmentSlot slot) {
|
| 247 | return (this.disabledSlots & 1 << slot.getFilterBit(0)) != 0 || slot.getType() == EquipmentSlot.Type.HAND && !this.showArms();
|
| 248 | }
|
| 249 |
|
| 250 | private boolean swapItem(final Player player, final EquipmentSlot slot, final ItemStack playerItemStack, final InteractionHand hand) {
|
| 251 | ItemStack itemStack = this.getItemBySlot(slot);
|
| 252 | if (!itemStack.isEmpty() && (this.disabledSlots & 1 << slot.getFilterBit(8)) != 0) {
|
| 253 | return false;
|
| 254 | }
|
| 255 |
|
| 256 | if (itemStack.isEmpty() && (this.disabledSlots & 1 << slot.getFilterBit(16)) != 0) {
|
| 257 | return false;
|
| 258 | }
|
| 259 |
|
| 260 | if (player.hasInfiniteMaterials() && itemStack.isEmpty() && !playerItemStack.isEmpty()) {
|
| 261 | this.setItemSlot(slot, playerItemStack.copyWithCount(1));
|
| 262 | return true;
|
| 263 | }
|
| 264 |
|
| 265 | if (playerItemStack.isEmpty() || playerItemStack.getCount() <= 1) {
|
| 266 | this.setItemSlot(slot, playerItemStack);
|
| 267 | player.setItemInHand(hand, itemStack);
|
| 268 | return true;
|
| 269 | }
|
| 270 |
|
| 271 | if (!itemStack.isEmpty()) {
|
| 272 | return false;
|
| 273 | }
|
| 274 |
|
| 275 | this.setItemSlot(slot, playerItemStack.split(1));
|
| 276 | return true;
|
| 277 | }
|
| 278 |
|
| 279 | @Override
|
| 280 | public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
|
| 281 | if (this.isRemoved()) {
|
| 282 | return false;
|
| 283 | }
|
| 284 |
|
| 285 | if (!level.getGameRules().get(GameRules.MOB_GRIEFING) && source.getEntity() instanceof Mob) {
|
| 286 | return false;
|
| 287 | }
|
| 288 |
|
| 289 | if (source.is(DamageTypeTags.BYPASSES_INVULNERABILITY)) {
|
| 290 | this.kill(level, source.getEntity());
|
| 291 | return false;
|
| 292 | }
|
| 293 |
|
| 294 | if (this.isInvulnerableTo(level, source) || this.invisible || this.isMarker()) {
|
| 295 | return false;
|
| 296 | }
|
| 297 |
|
| 298 | if (source.is(DamageTypeTags.IS_EXPLOSION)) {
|
| 299 | this.brokenByAnything(level, source);
|
| 300 | this.kill(level, source.getEntity());
|
| 301 | return false;
|
| 302 | }
|
| 303 |
|
| 304 | if (source.is(DamageTypeTags.IGNITES_ARMOR_STANDS)) {
|
| 305 | if (this.isOnFire()) {
|
| 306 | this.causeDamage(level, source, 0.15F);
|
| 307 | } else {
|
| 308 | this.igniteForSeconds(5.0F);
|
| 309 | }
|
| 310 |
|
| 311 | return false;
|
| 312 | } else if (source.is(DamageTypeTags.BURNS_ARMOR_STANDS) && this.getHealth() > 0.5F) {
|
| 313 | this.causeDamage(level, source, 4.0F);
|
| 314 | return false;
|
| 315 | } else {
|
| 316 | boolean allowIncrementalBreaking = source.is(DamageTypeTags.CAN_BREAK_ARMOR_STAND);
|
| 317 | boolean shouldKill = source.is(DamageTypeTags.ALWAYS_KILLS_ARMOR_STANDS);
|
| 318 | if (!allowIncrementalBreaking && !shouldKill) {
|
| 319 | return false;
|
| 320 | } else if (source.getEntity() instanceof Player player && !player.getAbilities().mayBuild) {
|
| 321 | return false;
|
| 322 | } else {
|
| 323 | if (source.isCreativePlayer()) {
|
| 324 | this.playBrokenSound();
|
| 325 | this.showBreakingParticles();
|
| 326 | this.kill(level, source.getEntity());
|
| 327 | return true;
|
| 328 | }
|
| 329 |
|
| 330 | long time = level.getGameTime();
|
| 331 | if (time - this.lastHit > 5L && !shouldKill) {
|
| 332 | level.broadcastEntityEvent(this, (byte)32);
|
| 333 | this.gameEvent(GameEvent.ENTITY_DAMAGE, source.getEntity());
|
| 334 | this.lastHit = time;
|
| 335 | } else {
|
| 336 | this.brokenByPlayer(level, source);
|
| 337 | this.showBreakingParticles();
|
| 338 | this.kill(level, source.getEntity());
|
| 339 | }
|
| 340 |
|
| 341 | return true;
|
| 342 | }
|
| 343 | }
|
| 344 | }
|
| 345 |
|
| 346 | @Override
|
| 347 | public void handleEntityEvent(final @EntityEvent.Value byte id) {
|
| 348 | if (id == 32) {
|
| 349 | if (this.level().isClientSide()) {
|
| 350 | this.level().playLocalSound(this.getX(), this.getY(), this.getZ(), SoundEvents.ARMOR_STAND_HIT, this.getSoundSource(), 0.3F, 1.0F, false);
|
| 351 | this.lastHit = this.level().getGameTime();
|
| 352 | }
|
| 353 | } else {
|
| 354 | super.handleEntityEvent(id);
|
| 355 | }
|
| 356 | }
|
| 357 |
|
| 358 | @Override
|
| 359 | public boolean shouldRenderAtSqrDistance(final double distance) {
|
| 360 | double size = this.getBoundingBox().getSize() * 4.0;
|
| 361 | if (Double.isNaN(size) || size == 0.0) {
|
| 362 | size = 4.0;
|
| 363 | }
|
| 364 |
|
| 365 | size *= 64.0;
|
| 366 | return distance < size * size;
|
| 367 | }
|
| 368 |
|
| 369 | private void showBreakingParticles() {
|
| 370 | if (this.level() instanceof ServerLevel) {
|
| 371 | ((ServerLevel)this.level())
|
| 372 | .sendParticles(
|
| 373 | new BlockParticleOption(ParticleTypes.BLOCK, Blocks.OAK_PLANKS.defaultBlockState()),
|
| 374 | this.getX(),
|
| 375 | this.getY(0.6666666666666666),
|
| 376 | this.getZ(),
|
| 377 | 10,
|
| 378 | this.getBbWidth() / 4.0F,
|
| 379 | this.getBbHeight() / 4.0F,
|
| 380 | this.getBbWidth() / 4.0F,
|
| 381 | 0.05
|
| 382 | );
|
| 383 | }
|
| 384 | }
|
| 385 |
|
| 386 | private void causeDamage(final ServerLevel level, final DamageSource source, final float dmg) {
|
| 387 | float health = this.getHealth();
|
| 388 | health -= dmg;
|
| 389 | if (health <= 0.5F) {
|
| 390 | this.brokenByAnything(level, source);
|
| 391 | this.kill(level, source.getEntity());
|
| 392 | } else {
|
| 393 | this.setHealth(health);
|
| 394 | this.gameEvent(GameEvent.ENTITY_DAMAGE, source.getEntity());
|
| 395 | }
|
| 396 | }
|
| 397 |
|
| 398 | private void brokenByPlayer(final ServerLevel level, final DamageSource source) {
|
| 399 | ItemStack result = new ItemStack(Items.ARMOR_STAND);
|
| 400 | result.set(DataComponents.CUSTOM_NAME, this.getCustomName());
|
| 401 | Block.popResource(this.level(), this.blockPosition(), result);
|
| 402 | this.brokenByAnything(level, source);
|
| 403 | }
|
| 404 |
|
| 405 | private void brokenByAnything(final ServerLevel level, final DamageSource source) {
|
| 406 | this.playBrokenSound();
|
| 407 | this.dropAllDeathLoot(level, source);
|
| 408 |
|
| 409 | for (EquipmentSlot slot : EquipmentSlot.VALUES) {
|
| 410 | ItemStack itemStack = this.equipment.set(slot, ItemStack.EMPTY);
|
| 411 | if (!itemStack.isEmpty() && !EnchantmentHelper.has(itemStack, EnchantmentEffectComponents.PREVENT_EQUIPMENT_DROP)) {
|
| 412 | Block.popResource(this.level(), this.blockPosition().above(), itemStack);
|
| 413 | }
|
| 414 | }
|
| 415 | }
|
| 416 |
|
| 417 | private void playBrokenSound() {
|
| 418 | this.level().playSound(null, this.getX(), this.getY(), this.getZ(), SoundEvents.ARMOR_STAND_BREAK, this.getSoundSource(), 1.0F, 1.0F);
|
| 419 | }
|
| 420 |
|
| 421 | @Override
|
| 422 | protected void tickHeadTurn(final float yBodyRotT) {
|
| 423 | this.yBodyRotO = this.yRotO;
|
| 424 | this.yBodyRot = this.getYRot();
|
| 425 | }
|
| 426 |
|
| 427 | @Override
|
| 428 | public void travel(final Vec3 input) {
|
| 429 | if (this.hasPhysics()) {
|
| 430 | super.travel(input);
|
| 431 | }
|
| 432 | }
|
| 433 |
|
| 434 | @Override
|
| 435 | public void setYBodyRot(final float yBodyRot) {
|
| 436 | this.yBodyRotO = this.yRotO = yBodyRot;
|
| 437 | this.yHeadRotO = this.yHeadRot = yBodyRot;
|
| 438 | }
|
| 439 |
|
| 440 | @Override
|
| 441 | public void setYHeadRot(final float yHeadRot) {
|
| 442 | this.yBodyRotO = this.yRotO = yHeadRot;
|
| 443 | this.yHeadRotO = this.yHeadRot = yHeadRot;
|
| 444 | }
|
| 445 |
|
| 446 | @Override
|
| 447 | protected void updateInvisibilityStatus() {
|
| 448 | this.setInvisible(this.invisible);
|
| 449 | }
|
| 450 |
|
| 451 | @Override
|
| 452 | public void setInvisible(final boolean invisible) {
|
| 453 | this.invisible = invisible;
|
| 454 | super.setInvisible(invisible);
|
| 455 | }
|
| 456 |
|
| 457 | @Override
|
| 458 | public boolean isBaby() {
|
| 459 | return this.isSmall();
|
| 460 | }
|
| 461 |
|
| 462 | public void kill(final ServerLevel level, final @Nullable Entity attributedTo) {
|
| 463 | this.remove(Entity.RemovalReason.KILLED);
|
| 464 | this.gameEvent(GameEvent.ENTITY_DIE, attributedTo != null ? attributedTo : this);
|
| 465 | }
|
| 466 |
|
| 467 | @Override
|
| 468 | public boolean ignoreExplosion(final Explosion explosion) {
|
| 469 | return explosion.shouldAffectBlocklikeEntities() ? this.isInvisible() : true;
|
| 470 | }
|
| 471 |
|
| 472 | @Override
|
| 473 | public PushReaction getPistonPushReaction() {
|
| 474 | return this.isMarker() ? PushReaction.IGNORE_ENTITY : super.getPistonPushReaction();
|
| 475 | }
|
| 476 |
|
| 477 | @Override
|
| 478 | public boolean isIgnoringBlockTriggers() {
|
| 479 | return this.isMarker();
|
| 480 | }
|
| 481 |
|
| 482 | private void setSmall(final boolean value) {
|
| 483 | this.entityData.set(DATA_CLIENT_FLAGS, this.setBit(this.entityData.get(DATA_CLIENT_FLAGS), 1, value));
|
| 484 | }
|
| 485 |
|
| 486 | public boolean isSmall() {
|
| 487 | return (this.entityData.get(DATA_CLIENT_FLAGS) & 1) != 0;
|
| 488 | }
|
| 489 |
|
| 490 | public void setShowArms(final boolean value) {
|
| 491 | this.entityData.set(DATA_CLIENT_FLAGS, this.setBit(this.entityData.get(DATA_CLIENT_FLAGS), 4, value));
|
| 492 | }
|
| 493 |
|
| 494 | public boolean showArms() {
|
| 495 | return (this.entityData.get(DATA_CLIENT_FLAGS) & 4) != 0;
|
| 496 | }
|
| 497 |
|
| 498 | public void setNoBasePlate(final boolean value) {
|
| 499 | this.entityData.set(DATA_CLIENT_FLAGS, this.setBit(this.entityData.get(DATA_CLIENT_FLAGS), 8, value));
|
| 500 | }
|
| 501 |
|
| 502 | public boolean showBasePlate() {
|
| 503 | return (this.entityData.get(DATA_CLIENT_FLAGS) & 8) == 0;
|
| 504 | }
|
| 505 |
|
| 506 | private void setMarker(final boolean value) {
|
| 507 | this.entityData.set(DATA_CLIENT_FLAGS, this.setBit(this.entityData.get(DATA_CLIENT_FLAGS), 16, value));
|
| 508 | }
|
| 509 |
|
| 510 | public boolean isMarker() {
|
| 511 | return (this.entityData.get(DATA_CLIENT_FLAGS) & 16) != 0;
|
| 512 | }
|
| 513 |
|
| 514 | private byte setBit(byte data, final int bit, final boolean value) {
|
| 515 | if (value) {
|
| 516 | data = (byte)(data | bit);
|
| 517 | } else {
|
| 518 | data = (byte)(data & ~bit);
|
| 519 | }
|
| 520 |
|
| 521 | return data;
|
| 522 | }
|
| 523 |
|
| 524 | public void setHeadPose(final Rotations headPose) {
|
| 525 | this.entityData.set(DATA_HEAD_POSE, headPose);
|
| 526 | }
|
| 527 |
|
| 528 | public void setBodyPose(final Rotations bodyPose) {
|
| 529 | this.entityData.set(DATA_BODY_POSE, bodyPose);
|
| 530 | }
|
| 531 |
|
| 532 | public void setLeftArmPose(final Rotations leftArmPose) {
|
| 533 | this.entityData.set(DATA_LEFT_ARM_POSE, leftArmPose);
|
| 534 | }
|
| 535 |
|
| 536 | public void setRightArmPose(final Rotations rightArmPose) {
|
| 537 | this.entityData.set(DATA_RIGHT_ARM_POSE, rightArmPose);
|
| 538 | }
|
| 539 |
|
| 540 | public void setLeftLegPose(final Rotations leftLegPose) {
|
| 541 | this.entityData.set(DATA_LEFT_LEG_POSE, leftLegPose);
|
| 542 | }
|
| 543 |
|
| 544 | public void setRightLegPose(final Rotations rightLegPose) {
|
| 545 | this.entityData.set(DATA_RIGHT_LEG_POSE, rightLegPose);
|
| 546 | }
|
| 547 |
|
| 548 | public Rotations getHeadPose() {
|
| 549 | return this.entityData.get(DATA_HEAD_POSE);
|
| 550 | }
|
| 551 |
|
| 552 | public Rotations getBodyPose() {
|
| 553 | return this.entityData.get(DATA_BODY_POSE);
|
| 554 | }
|
| 555 |
|
| 556 | public Rotations getLeftArmPose() {
|
| 557 | return this.entityData.get(DATA_LEFT_ARM_POSE);
|
| 558 | }
|
| 559 |
|
| 560 | public Rotations getRightArmPose() {
|
| 561 | return this.entityData.get(DATA_RIGHT_ARM_POSE);
|
| 562 | }
|
| 563 |
|
| 564 | public Rotations getLeftLegPose() {
|
| 565 | return this.entityData.get(DATA_LEFT_LEG_POSE);
|
| 566 | }
|
| 567 |
|
| 568 | public Rotations getRightLegPose() {
|
| 569 | return this.entityData.get(DATA_RIGHT_LEG_POSE);
|
| 570 | }
|
| 571 |
|
| 572 | @Override
|
| 573 | public boolean isPickable() {
|
| 574 | return super.isPickable() && !this.isMarker();
|
| 575 | }
|
| 576 |
|
| 577 | @Override
|
| 578 | public boolean skipAttackInteraction(final Entity source) {
|
| 579 | return source instanceof Player playerSource && !this.level().mayInteract(playerSource, this.blockPosition());
|
| 580 | }
|
| 581 |
|
| 582 | @Override
|
| 583 | public HumanoidArm getMainArm() {
|
| 584 | return HumanoidArm.RIGHT;
|
| 585 | }
|
| 586 |
|
| 587 | @Override
|
| 588 | public LivingEntity.Fallsounds getFallSounds() {
|
| 589 | return new LivingEntity.Fallsounds(SoundEvents.ARMOR_STAND_FALL, SoundEvents.ARMOR_STAND_FALL);
|
| 590 | }
|
| 591 |
|
| 592 | @Override
|
| 593 | protected @Nullable SoundEvent getHurtSound(final DamageSource source) {
|
| 594 | return SoundEvents.ARMOR_STAND_HIT;
|
| 595 | }
|
| 596 |
|
| 597 | @Override
|
| 598 | protected @Nullable SoundEvent getDeathSound() {
|
| 599 | return SoundEvents.ARMOR_STAND_BREAK;
|
| 600 | }
|
| 601 |
|
| 602 | @Override
|
| 603 | public void thunderHit(final ServerLevel level, final LightningBolt lightningBolt) {
|
| 604 | }
|
| 605 |
|
| 606 | @Override
|
| 607 | public boolean isAffectedByPotions() {
|
| 608 | return false;
|
| 609 | }
|
| 610 |
|
| 611 | @Override
|
| 612 | public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
|
| 613 | if (DATA_CLIENT_FLAGS.equals(accessor)) {
|
| 614 | this.refreshDimensions();
|
| 615 | this.blocksBuilding = !this.isMarker();
|
| 616 | }
|
| 617 |
|
| 618 | super.onSyncedDataUpdated(accessor);
|
| 619 | }
|
| 620 |
|
| 621 | @Override
|
| 622 | public boolean attackable() {
|
| 623 | return false;
|
| 624 | }
|
| 625 |
|
| 626 | @Override
|
| 627 | public EntityDimensions getDefaultDimensions(final Pose pose) {
|
| 628 | return this.getDimensionsMarker(this.isMarker());
|
| 629 | }
|
| 630 |
|
| 631 | private EntityDimensions getDimensionsMarker(final boolean isMarker) {
|
| 632 | if (isMarker) {
|
| 633 | return MARKER_DIMENSIONS;
|
| 634 | } else {
|
| 635 | return this.isBaby() ? BABY_DIMENSIONS : this.getType().getDimensions();
|
| 636 | }
|
| 637 | }
|
| 638 |
|
| 639 | @Override
|
| 640 | public Vec3 getLightProbePosition(final float partialTickTime) {
|
| 641 | if (this.isMarker()) {
|
| 642 | AABB box = this.getDimensionsMarker(false).makeBoundingBox(this.position());
|
| 643 | BlockPos probePos = this.blockPosition();
|
| 644 | int brightestLight = Integer.MIN_VALUE;
|
| 645 |
|
| 646 | for (BlockPos pos : BlockPos.betweenClosed(box)) {
|
| 647 | int blockBrightness = Math.max(this.level().getBrightness(LightLayer.BLOCK, pos), this.level().getBrightness(LightLayer.SKY, pos));
|
| 648 | if (blockBrightness == 15) {
|
| 649 | return Vec3.atCenterOf(pos);
|
| 650 | }
|
| 651 |
|
| 652 | if (blockBrightness > brightestLight) {
|
| 653 | brightestLight = blockBrightness;
|
| 654 | probePos = pos.immutable();
|
| 655 | }
|
| 656 | }
|
| 657 |
|
| 658 | return Vec3.atCenterOf(probePos);
|
| 659 | } else {
|
| 660 | return super.getLightProbePosition(partialTickTime);
|
| 661 | }
|
| 662 | }
|
| 663 |
|
| 664 | @Override
|
| 665 | public ItemStack getPickResult() {
|
| 666 | return new ItemStack(Items.ARMOR_STAND);
|
| 667 | }
|
| 668 |
|
| 669 | @Override
|
| 670 | public boolean canBeSeenByAnyone() {
|
| 671 | return !this.isInvisible() && !this.isMarker();
|
| 672 | }
|
| 673 |
|
| 674 | public void setArmorStandPose(final ArmorStand.ArmorStandPose pose) {
|
| 675 | this.setHeadPose(pose.head());
|
| 676 | this.setBodyPose(pose.body());
|
| 677 | this.setLeftArmPose(pose.leftArm());
|
| 678 | this.setRightArmPose(pose.rightArm());
|
| 679 | this.setLeftLegPose(pose.leftLeg());
|
| 680 | this.setRightLegPose(pose.rightLeg());
|
| 681 | }
|
| 682 |
|
| 683 | public ArmorStand.ArmorStandPose getArmorStandPose() {
|
| 684 | return new ArmorStand.ArmorStandPose(
|
| 685 | this.getHeadPose(), this.getBodyPose(), this.getLeftArmPose(), this.getRightArmPose(), this.getLeftLegPose(), this.getRightLegPose()
|
| 686 | );
|
| 687 | }
|
| 688 |
|
| 689 | public record ArmorStandPose(Rotations head, Rotations body, Rotations leftArm, Rotations rightArm, Rotations leftLeg, Rotations rightLeg) {
|
| 690 | public static final ArmorStand.ArmorStandPose DEFAULT = new ArmorStand.ArmorStandPose(
|
| 691 | ArmorStand.DEFAULT_HEAD_POSE,
|
| 692 | ArmorStand.DEFAULT_BODY_POSE,
|
| 693 | ArmorStand.DEFAULT_LEFT_ARM_POSE,
|
| 694 | ArmorStand.DEFAULT_RIGHT_ARM_POSE,
|
| 695 | ArmorStand.DEFAULT_LEFT_LEG_POSE,
|
| 696 | ArmorStand.DEFAULT_RIGHT_LEG_POSE
|
| 697 | );
|
| 698 | public static final Codec<ArmorStand.ArmorStandPose> CODEC = RecordCodecBuilder.create(
|
| 699 | i -> i.group(
|
| 700 | Rotations.CODEC.optionalFieldOf("Head", ArmorStand.DEFAULT_HEAD_POSE).forGetter(ArmorStand.ArmorStandPose::head),
|
| 701 | Rotations.CODEC.optionalFieldOf("Body", ArmorStand.DEFAULT_BODY_POSE).forGetter(ArmorStand.ArmorStandPose::body),
|
| 702 | Rotations.CODEC.optionalFieldOf("LeftArm", ArmorStand.DEFAULT_LEFT_ARM_POSE).forGetter(ArmorStand.ArmorStandPose::leftArm),
|
| 703 | Rotations.CODEC.optionalFieldOf("RightArm", ArmorStand.DEFAULT_RIGHT_ARM_POSE).forGetter(ArmorStand.ArmorStandPose::rightArm),
|
| 704 | Rotations.CODEC.optionalFieldOf("LeftLeg", ArmorStand.DEFAULT_LEFT_LEG_POSE).forGetter(ArmorStand.ArmorStandPose::leftLeg),
|
| 705 | Rotations.CODEC.optionalFieldOf("RightLeg", ArmorStand.DEFAULT_RIGHT_LEG_POSE).forGetter(ArmorStand.ArmorStandPose::rightLeg)
|
| 706 | )
|
| 707 | .apply(i, ArmorStand.ArmorStandPose::new)
|
| 708 | );
|
| 709 | }
|
| 710 | }
|
| 711 |
|