BlockAttachedEntity .java

net.minecraft.world.entity.decoration.BlockAttachedEntity (class, 180 lines): members: ticksSinceLastCheck, CHECK_INTERVAL, TAG_BLOCK_POS, onKilled, kill.

net.minecraft.world.entity.decorationclass180 linescommonextends Entity4.9 KB

API Surface (19)

void tick()
void tickAtCheckInterval()
boolean isPickable()
boolean skipAttackInteraction(final Entity source)
void kill(final ServerLevel level)
void kill(final ServerLevel level, final @Nullable Entity attributedTo)
void onKilled()
boolean hurtClient(final DamageSource source)
boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage)
boolean ignoreExplosion(final Explosion explosion)
void move(final MoverType moverType, final Vec3 delta)
void push(final double xa, final double ya, final double za)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
boolean repositionEntityAfterLoad()
void setPos(final double x, final double y, final double z)
BlockPos getPos()
void thunderHit(final ServerLevel level, final LightningBolt lightningBolt)
void refreshDimensions()

Fields & Constants (5)

TAG_BLOCK_POS
LOGGER
CHECK_INTERVAL
ticksSinceLastCheck
pos

String literals (1)

"block_pos"

Full Source

BlockAttachedEntity.java180 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.decoration;
2
3
import com.mojang.logging.LogUtils;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.server.level.ServerLevel;
6
import net.minecraft.world.damagesource.DamageSource;
7
import net.minecraft.world.entity.Entity;
8
import net.minecraft.world.entity.EntityType;
9
import net.minecraft.world.entity.LightningBolt;
10
import net.minecraft.world.entity.Mob;
11
import net.minecraft.world.entity.MoverType;
12
import net.minecraft.world.entity.player.Player;
13
import net.minecraft.world.level.Explosion;
14
import net.minecraft.world.level.Level;
15
import net.minecraft.world.level.gameevent.GameEvent;
16
import net.minecraft.world.level.gamerules.GameRules;
17
import net.minecraft.world.level.storage.ValueInput;
18
import net.minecraft.world.level.storage.ValueOutput;
19
import net.minecraft.world.phys.Vec3;
20
import org.jspecify.annotations.Nullable;
21
import org.slf4j.Logger;
22
23
public abstract class BlockAttachedEntity extends Entity {
24
	public static final String TAG_BLOCK_POS = "block_pos";
25
	private static final Logger LOGGER = LogUtils.getLogger();
26
	private static final int CHECK_INTERVAL = 100;
27
	private int ticksSinceLastCheck;
28
	protected BlockPos pos;
29
30
	protected BlockAttachedEntity(final EntityType<? extends BlockAttachedEntity> type, final Level level) {
31
		super(type, level);
32
	}
33
34
	protected BlockAttachedEntity(final EntityType<? extends BlockAttachedEntity> type, final Level level, final BlockPos pos) {
35
		this(type, level);
36
		this.pos = pos;
37
	}
38
39
	protected abstract void recalculateBoundingBox();
40
41
	@Override
42
	public void tick() {
43
		if (this.level() instanceof ServerLevel level) {
44
			this.checkBelowWorld();
45
			if (this.ticksSinceLastCheck++ >= 100) {
46
				this.ticksSinceLastCheck = 0;
47
				this.tickAtCheckInterval();
48
				if (!this.isRemoved() && !this.survives()) {
49
					this.discard();
50
					this.dropItem(level, null);
51
				}
52
			}
53
		}
54
	}
55
56
	protected void tickAtCheckInterval() {
57
	}
58
59
	public abstract boolean survives();
60
61
	@Override
62
	public boolean isPickable() {
63
		return true;
64
	}
65
66
	@Override
67
	public boolean skipAttackInteraction(final Entity source) {
68
		if (source instanceof Player player) {
69
			return !this.level().mayInteract(player, this.pos) ? true : this.hurtOrSimulate(this.damageSources().playerAttack(player), 0.0F);
70
		} else {
71
			return false;
72
		}
73
	}
74
75
	@Override
76
	public void kill(final ServerLevel level) {
77
		this.kill(level, this);
78
	}
79
80
	public void kill(final ServerLevel level, final @Nullable Entity attributedTo) {
81
		this.onKilled();
82
		this.remove(Entity.RemovalReason.KILLED);
83
		this.gameEvent(GameEvent.ENTITY_DIE, attributedTo != null ? attributedTo : this);
84
	}
85
86
	protected void onKilled() {
87
	}
88
89
	@Override
90
	public boolean hurtClient(final DamageSource source) {
91
		return !this.isInvulnerableToBase(source);
92
	}
93
94
	@Override
95
	public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
96
		if (this.isInvulnerableToBase(source)) {
97
			return false;
98
		}
99
100
		if (!level.getGameRules().get(GameRules.MOB_GRIEFING) && source.getEntity() instanceof Mob) {
101
			return false;
102
		}
103
104
		if (!this.isRemoved()) {
105
			this.kill(level, source.getEntity());
106
			this.markHurt();
107
			this.dropItem(level, source.getEntity());
108
		}
109
110
		return true;
111
	}
112
113
	@Override
114
	public boolean ignoreExplosion(final Explosion explosion) {
115
		Entity directEntity = explosion.getDirectSourceEntity();
116
		if (directEntity != null && directEntity.isInWater()) {
117
			return true;
118
		} else {
119
			return explosion.shouldAffectBlocklikeEntities() ? super.ignoreExplosion(explosion) : true;
120
		}
121
	}
122
123
	@Override
124
	public void move(final MoverType moverType, final Vec3 delta) {
125
		if (this.level() instanceof ServerLevel level && !this.isRemoved() && delta.lengthSqr() > 0.0) {
126
			this.kill(level);
127
			this.dropItem(level, null);
128
		}
129
	}
130
131
	@Override
132
	public void push(final double xa, final double ya, final double za) {
133
		if (this.level() instanceof ServerLevel level && !this.isRemoved() && xa * xa + ya * ya + za * za > 0.0) {
134
			this.kill(level);
135
			this.dropItem(level, null);
136
		}
137
	}
138
139
	@Override
140
	protected void addAdditionalSaveData(final ValueOutput output) {
141
		output.store("block_pos", BlockPos.CODEC, this.getPos());
142
	}
143
144
	@Override
145
	protected void readAdditionalSaveData(final ValueInput input) {
146
		BlockPos storedPos = input.read("block_pos", BlockPos.CODEC).orElse(null);
147
		if (storedPos != null && storedPos.closerThan(this.blockPosition(), 16.0)) {
148
			this.pos = storedPos;
149
		} else {
150
			LOGGER.error("Block-attached entity at invalid position: {}", storedPos);
151
		}
152
	}
153
154
	public abstract void dropItem(ServerLevel level, @Nullable Entity causedBy);
155
156
	@Override
157
	protected boolean repositionEntityAfterLoad() {
158
		return false;
159
	}
160
161
	@Override
162
	public void setPos(final double x, final double y, final double z) {
163
		this.pos = BlockPos.containing(x, y, z);
164
		this.recalculateBoundingBox();
165
		this.needsSync = true;
166
	}
167
168
	public BlockPos getPos() {
169
		return this.pos;
170
	}
171
172
	@Override
173
	public void thunderHit(final ServerLevel level, final LightningBolt lightningBolt) {
174
	}
175
176
	@Override
177
	public void refreshDimensions() {
178
	}
179
}
180

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