HangingEntity .java

net.minecraft.world.entity.decoration.HangingEntity (class): members: calculateSupportBox, DEFAULT_DIRECTION, hasLevelCollision, setDirectionRaw, survives.

net.minecraft.world.entity.decorationclass162 linescommonextends BlockAttachedEntity5.3 KB

API Surface (14)

void defineSynchedData(final SynchedEntityData.Builder entityData)
void onSyncedDataUpdated(final EntityDataAccessor<?> accessor)
Direction getDirection()
void setDirectionRaw(final Direction direction)
void setDirection(final Direction direction)
void recalculateBoundingBox()
boolean survives()
AABB calculateSupportBox()
boolean canCoexist(final boolean allowIntersectingSameType)
boolean hasLevelCollision(final AABB popBox)
AABB getPopBox()
ItemEntity spawnAtLocation(final ServerLevel level, final ItemStack itemStack, final float yOffs)
float rotate(final Rotation rotation)
float mirror(final Mirror mirror)

Fields & Constants (2)

DATA_DIRECTION
DEFAULT_DIRECTION

Full Source

HangingEntity.java162 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.decoration;
2
3
import java.util.Objects;
4
import java.util.function.Predicate;
5
import net.minecraft.core.BlockPos;
6
import net.minecraft.core.Direction;
7
import net.minecraft.network.syncher.EntityDataAccessor;
8
import net.minecraft.network.syncher.EntityDataSerializers;
9
import net.minecraft.network.syncher.SynchedEntityData;
10
import net.minecraft.server.level.ServerLevel;
11
import net.minecraft.util.Mth;
12
import net.minecraft.world.entity.EntityType;
13
import net.minecraft.world.entity.item.ItemEntity;
14
import net.minecraft.world.item.ItemStack;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.block.DiodeBlock;
17
import net.minecraft.world.level.block.Mirror;
18
import net.minecraft.world.level.block.Rotation;
19
import net.minecraft.world.level.block.state.BlockState;
20
import net.minecraft.world.level.entity.EntityTypeTest;
21
import net.minecraft.world.phys.AABB;
22
import net.minecraft.world.phys.Vec3;
23
import org.apache.commons.lang3.Validate;
24
25
public abstract class HangingEntity extends BlockAttachedEntity {
26
	private static final EntityDataAccessor<Direction> DATA_DIRECTION = SynchedEntityData.defineId(HangingEntity.class, EntityDataSerializers.DIRECTION);
27
	private static final Direction DEFAULT_DIRECTION = Direction.SOUTH;
28
29
	protected HangingEntity(final EntityType<? extends HangingEntity> type, final Level level) {
30
		super(type, level);
31
	}
32
33
	protected HangingEntity(final EntityType<? extends HangingEntity> type, final Level level, final BlockPos pos) {
34
		this(type, level);
35
		this.pos = pos;
36
	}
37
38
	@Override
39
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
40
		entityData.define(DATA_DIRECTION, DEFAULT_DIRECTION);
41
	}
42
43
	@Override
44
	public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
45
		super.onSyncedDataUpdated(accessor);
46
		if (accessor.equals(DATA_DIRECTION)) {
47
			this.setDirection(this.getDirection());
48
		}
49
	}
50
51
	@Override
52
	public Direction getDirection() {
53
		return this.entityData.get(DATA_DIRECTION);
54
	}
55
56
	protected void setDirectionRaw(final Direction direction) {
57
		this.entityData.set(DATA_DIRECTION, direction);
58
	}
59
60
	protected void setDirection(final Direction direction) {
61
		Objects.requireNonNull(direction);
62
		Validate.isTrue(direction.getAxis().isHorizontal());
63
		this.setDirectionRaw(direction);
64
		this.setYRot(direction.get2DDataValue() * 90);
65
		this.yRotO = this.getYRot();
66
		this.recalculateBoundingBox();
67
	}
68
69
	@Override
70
	protected void recalculateBoundingBox() {
71
		if (this.getDirection() != null) {
72
			AABB aabb = this.calculateBoundingBox(this.pos, this.getDirection());
73
			Vec3 center = aabb.getCenter();
74
			this.setPosRaw(center.x, center.y, center.z);
75
			this.setBoundingBox(aabb);
76
		}
77
	}
78
79
	protected abstract AABB calculateBoundingBox(BlockPos pos, Direction direction);
80
81
	@Override
82
	public boolean survives() {
83
		if (this.hasLevelCollision(this.getPopBox())) {
84
			return false;
85
		}
86
87
		boolean isSupported = this.level().findBlocksIn(this.calculateSupportBox()).filterState(HangingEntity::isSupportingBlock).allMatched();
88
		return isSupported && this.canCoexist(false);
89
	}
90
91
	private static boolean isSupportingBlock(final BlockState state) {
92
		return state.isSolid() || DiodeBlock.isDiode(state);
93
	}
94
95
	protected AABB calculateSupportBox() {
96
		return this.getBoundingBox().move(this.getDirection().step().mul(-0.5F)).deflate(1.0E-7);
97
	}
98
99
	protected boolean canCoexist(final boolean allowIntersectingSameType) {
100
		Predicate<HangingEntity> nonIntersectable = hangingEntity -> {
101
			boolean intersectsSameType = !allowIntersectingSameType && hangingEntity.getType() == this.getType();
102
			boolean isSameDirection = hangingEntity.getDirection() == this.getDirection();
103
			return hangingEntity != this && (intersectsSameType || isSameDirection);
104
		};
105
		return !this.level().hasEntities(EntityTypeTest.forClass(HangingEntity.class), this.getPopBox(), nonIntersectable);
106
	}
107
108
	protected boolean hasLevelCollision(final AABB popBox) {
109
		Level level = this.level();
110
		return !level.noBlockCollision(this, popBox) || !level.noBorderCollision(this, popBox);
111
	}
112
113
	protected AABB getPopBox() {
114
		return this.getBoundingBox();
115
	}
116
117
	public abstract void playPlacementSound();
118
119
	@Override
120
	public ItemEntity spawnAtLocation(final ServerLevel level, final ItemStack itemStack, final float yOffs) {
121
		ItemEntity entity = new ItemEntity(
122
			this.level(), this.getX() + this.getDirection().getStepX() * 0.15F, this.getY() + yOffs, this.getZ() + this.getDirection().getStepZ() * 0.15F, itemStack
123
		);
124
		entity.setDefaultPickUpDelay();
125
		this.level().addFreshEntity(entity);
126
		return entity;
127
	}
128
129
	@Override
130
	public float rotate(final Rotation rotation) {
131
		Direction direction = this.getDirection();
132
		if (direction.getAxis() != Direction.Axis.Y) {
133
			switch (rotation) {
134
				case CLOCKWISE_180:
135
					direction = direction.getOpposite();
136
					break;
137
				case COUNTERCLOCKWISE_90:
138
					direction = direction.getCounterClockWise();
139
					break;
140
				case CLOCKWISE_90:
141
					direction = direction.getClockWise();
142
			}
143
144
			this.setDirection(direction);
145
		}
146
147
		float angle = Mth.wrapDegrees(this.getYRot());
148
149
		return switch (rotation) {
150
			case CLOCKWISE_180 -> angle + 180.0F;
151
			case COUNTERCLOCKWISE_90 -> angle + 90.0F;
152
			case CLOCKWISE_90 -> angle + 270.0F;
153
			default -> angle;
154
		};
155
	}
156
157
	@Override
158
	public float mirror(final Mirror mirror) {
159
		return this.rotate(mirror.getRotation(this.getDirection()));
160
	}
161
}
162

Decompiled Minecraft 26.3 source of HangingEntity (net/minecraft/world/entity/decoration/HangingEntity.java) with Mojang official mappings, stored verbatim on the site.