Cushion .java

net.minecraft.world.entity.decoration.Cushion (class): members: LIGHTNING_DROP_INVULNERABLE_TICKS, destroyIfInFire, wouldSurviveAt, DATA_COLOR, survives.

net.minecraft.world.entity.decorationclass264 linescommonextends BlockAttachedEntity9.3 KB

API Surface (21)

DyeColor getColor()
void setColor(final DyeColor color)
void dropItem(final ServerLevel level, final @Nullable Entity causedBy)
boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage)
boolean hurtClient(final DamageSource source)
InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location)
void removePassenger(final Entity passenger)
ItemStack getPickResult()
void tickAtCheckInterval()
void destroyIfInFire(final ServerLevel level)
void thunderHit(final ServerLevel level, final LightningBolt lightningBolt)
void setPos(final double x, final double y, final double z)
boolean canBePlacedAt(final Level level, final AABB boundingBox)
boolean wouldSurviveAt(final Level level, final AABB boundingBox)
boolean survives()
void recalculateBoundingBox()
void defineSynchedData(final SynchedEntityData.Builder entityData)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
void applyImplicitComponents(final DataComponentGetter components)
<T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value)

Fields & Constants (3)

DEFAULT_COLOR
LIGHTNING_DROP_INVULNERABLE_TICKS
DATA_COLOR

String literals (1)

"color"

Full Source

Cushion.java264 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.decoration;
2
3
import net.minecraft.core.BlockPos;
4
import net.minecraft.core.component.DataComponentGetter;
5
import net.minecraft.core.component.DataComponentType;
6
import net.minecraft.core.component.DataComponents;
7
import net.minecraft.core.particles.BlockParticleOption;
8
import net.minecraft.core.particles.ParticleTypes;
9
import net.minecraft.network.syncher.EntityDataAccessor;
10
import net.minecraft.network.syncher.EntityDataSerializers;
11
import net.minecraft.network.syncher.SynchedEntityData;
12
import net.minecraft.server.level.ServerLevel;
13
import net.minecraft.sounds.SoundEvents;
14
import net.minecraft.tags.BlockTags;
15
import net.minecraft.util.Continuation;
16
import net.minecraft.world.InteractionHand;
17
import net.minecraft.world.InteractionResult;
18
import net.minecraft.world.damagesource.DamageSource;
19
import net.minecraft.world.entity.Entity;
20
import net.minecraft.world.entity.EntityType;
21
import net.minecraft.world.entity.LightningBolt;
22
import net.minecraft.world.entity.item.ItemEntity;
23
import net.minecraft.world.entity.player.Player;
24
import net.minecraft.world.item.DyeColor;
25
import net.minecraft.world.item.ItemStack;
26
import net.minecraft.world.item.Items;
27
import net.minecraft.world.level.Level;
28
import net.minecraft.world.level.block.Blocks;
29
import net.minecraft.world.level.gamerules.GameRules;
30
import net.minecraft.world.level.material.FluidState;
31
import net.minecraft.world.level.storage.ValueInput;
32
import net.minecraft.world.level.storage.ValueOutput;
33
import net.minecraft.world.phys.AABB;
34
import net.minecraft.world.phys.Vec3;
35
import net.minecraft.world.phys.shapes.BooleanOp;
36
import net.minecraft.world.phys.shapes.Shapes;
37
import net.minecraft.world.phys.shapes.VoxelShape;
38
import org.jspecify.annotations.Nullable;
39
40
public class Cushion extends BlockAttachedEntity {
41
	private static final DyeColor DEFAULT_COLOR = DyeColor.WHITE;
42
	private static final int LIGHTNING_DROP_INVULNERABLE_TICKS = 20;
43
	private static final EntityDataAccessor<DyeColor> DATA_COLOR = SynchedEntityData.defineId(Cushion.class, EntityDataSerializers.DYE_COLOR);
44
45
	public Cushion(final EntityType<Cushion> type, final Level level) {
46
		super(type, level);
47
	}
48
49
	public DyeColor getColor() {
50
		return this.entityData.get(DATA_COLOR);
51
	}
52
53
	public void setColor(final DyeColor color) {
54
		this.entityData.set(DATA_COLOR, color);
55
	}
56
57
	@Override
58
	public void dropItem(final ServerLevel level, final @Nullable Entity causedBy) {
59
		this.playSound(SoundEvents.CUSHION_BREAK, 1.0F, 1.0F);
60
		this.showBreakingParticles();
61
		if (level.getGameRules().get(GameRules.ENTITY_DROPS)) {
62
			if (!(causedBy instanceof Player player && player.hasInfiniteMaterials())) {
63
				ItemEntity itemEntity = this.spawnAtLocation(level, this.getCushionItemStackWithData());
64
				if (itemEntity != null && causedBy instanceof LightningBolt) {
65
					itemEntity.setInvulnerableTime(20);
66
				}
67
			}
68
		}
69
	}
70
71
	private static boolean isBreakingDeniedFor(final DamageSource source) {
72
		return source.getEntity() instanceof Player player && !player.mayBuild();
73
	}
74
75
	private boolean isBreakingDeniedAtPosFor(final ServerLevel level, final DamageSource source) {
76
		return source.getEntity() instanceof Player player && !level.mayInteract(player, this.pos);
77
	}
78
79
	@Override
80
	public boolean hurtServer(final ServerLevel level, final DamageSource source, final float damage) {
81
		return !isBreakingDeniedFor(source) && !this.isBreakingDeniedAtPosFor(level, source) ? super.hurtServer(level, source, damage) : false;
82
	}
83
84
	@Override
85
	public boolean hurtClient(final DamageSource source) {
86
		return isBreakingDeniedFor(source) ? false : super.hurtClient(source);
87
	}
88
89
	@Override
90
	public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
91
		if (player.isSecondaryUseActive() || this.isVehicle()) {
92
			return InteractionResult.PASS;
93
		} else if (!this.level().isClientSide() && player.startRiding(this)) {
94
			this.playSound(SoundEvents.CUSHION_SIT, 1.0F, 1.0F);
95
			return InteractionResult.SUCCESS_SERVER;
96
		} else {
97
			return InteractionResult.CONSUME;
98
		}
99
	}
100
101
	@Override
102
	protected void removePassenger(final Entity passenger) {
103
		super.removePassenger(passenger);
104
		if (!this.level().isClientSide() && this.getRemovalReason() == null) {
105
			this.playSound(SoundEvents.CUSHION_GET_UP, 1.0F, 1.0F);
106
		}
107
	}
108
109
	@Override
110
	public ItemStack getPickResult() {
111
		return new ItemStack(Items.CUSHION.pick(this.getColor()));
112
	}
113
114
	@Override
115
	protected void tickAtCheckInterval() {
116
		if (this.level() instanceof ServerLevel level) {
117
			BlockPos blockPos = this.blockPosition();
118
			FluidState fluidState = level.getBlockState(blockPos).getFluidState();
119
			if (this.collidedWithFluid(fluidState, blockPos, this.position(), this.position())) {
120
				fluidState.entityInside(level, blockPos, this, this.insideEffectCollector);
121
				this.insideEffectCollector.applyAndClear(this);
122
			}
123
124
			this.destroyIfInFire(level);
125
		}
126
	}
127
128
	public void destroyIfInFire(final ServerLevel level) {
129
		if (!this.isRemoved()) {
130
			level.findBlocksIn(this.getBoundingBox().nextDeflated()).filterState(state -> state.is(BlockTags.FIRE)).forEachUntil((var2, var3) -> {
131
				this.hurtServer(level, this.damageSources().inFire(), 1.0F);
132
				return Continuation.ABORT;
133
			});
134
		}
135
	}
136
137
	@Override
138
	public void thunderHit(final ServerLevel level, final LightningBolt lightningBolt) {
139
		if (!this.isRemoved()) {
140
			this.kill(level, lightningBolt);
141
			this.dropItem(level, lightningBolt);
142
		}
143
	}
144
145
	@Override
146
	public void setPos(final double x, final double y, final double z) {
147
		this.setPosRaw(x, y, z);
148
		super.setPos(x, y, z);
149
	}
150
151
	private void showBreakingParticles() {
152
		if (this.level() instanceof ServerLevel level) {
153
			level.sendParticles(
154
				new BlockParticleOption(ParticleTypes.BLOCK, Blocks.WOOL.pick(this.getColor()).defaultBlockState()),
155
				this.getX(),
156
				this.getY(0.6666666666666666),
157
				this.getZ(),
158
				10,
159
				this.getBbWidth() / 4.0F,
160
				this.getBbHeight() / 4.0F,
161
				this.getBbWidth() / 4.0F,
162
				0.05
163
			);
164
		}
165
	}
166
167
	public static boolean canBePlacedAt(final Level level, final AABB boundingBox) {
168
		return wouldSurviveAt(level, boundingBox) && !isAnchorBuried(level, boundingBox);
169
	}
170
171
	public static boolean wouldSurviveAt(final Level level, final AABB boundingBox) {
172
		return hasAnchorBelow(level, boundingBox) && !isCoveredBySuffocatingBlocks(level, boundingBox);
173
	}
174
175
	private static boolean hasAnchorBelow(final Level level, final AABB boundingBox) {
176
		AABB anchorBox = new AABB(
177
			boundingBox.minX, boundingBox.minY - 0.015625, boundingBox.minZ, Math.nextDown(boundingBox.maxX), boundingBox.minY, Math.nextDown(boundingBox.maxZ)
178
		);
179
		return level.findBlocksIn(anchorBox.expandTowards(0.0, -0.125, 0.0)).forEachUntil((blockPos, blockState) -> {
180
			VoxelShape shape = blockState.getShape(level, blockPos);
181
			return !shape.isEmpty() && shape.bounds().move(blockPos).intersects(anchorBox) ? Continuation.ABORT : Continuation.CONTINUE;
182
		});
183
	}
184
185
	private static boolean isAnchorBuried(final Level level, final AABB boundingBox) {
186
		AABB restingSlice = new AABB(boundingBox.minX, boundingBox.minY, boundingBox.minZ, boundingBox.maxX, boundingBox.minY + 0.015625, boundingBox.maxZ)
187
			.nextDeflated();
188
		VoxelShape exposedSurface = Shapes.create(restingSlice);
189
190
		for (VoxelShape collider : level.getBlockCollisions(null, restingSlice)) {
191
			exposedSurface = Shapes.join(exposedSurface, collider, BooleanOp.ONLY_FIRST);
192
			if (exposedSurface.isEmpty()) {
193
				return true;
194
			}
195
		}
196
197
		return false;
198
	}
199
200
	private static boolean isCoveredBySuffocatingBlocks(final Level level, final AABB boundingBox) {
201
		for (BlockPos blockPos : BlockPos.betweenClosed(boundingBox.nextDeflated())) {
202
			if (!level.getBlockState(blockPos).isSuffocating(level, blockPos)) {
203
				return false;
204
			}
205
		}
206
207
		return true;
208
	}
209
210
	@Override
211
	public boolean survives() {
212
		return wouldSurviveAt(this.level(), this.getBoundingBox());
213
	}
214
215
	@Override
216
	protected void recalculateBoundingBox() {
217
		this.setBoundingBox(this.makeBoundingBox());
218
	}
219
220
	@Override
221
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
222
		entityData.define(DATA_COLOR, DEFAULT_COLOR);
223
	}
224
225
	@Override
226
	protected void addAdditionalSaveData(final ValueOutput output) {
227
		super.addAdditionalSaveData(output);
228
		output.store("color", DyeColor.CODEC, this.getColor());
229
	}
230
231
	@Override
232
	protected void readAdditionalSaveData(final ValueInput input) {
233
		super.readAdditionalSaveData(input);
234
		this.setColor(input.read("color", DyeColor.CODEC).orElse(DEFAULT_COLOR));
235
	}
236
237
	@Override
238
	public <T> @Nullable T get(final DataComponentType<? extends T> type) {
239
		return type == DataComponents.CUSHION_COLOR ? castComponentValue((DataComponentType<T>)type, this.getColor()) : super.get(type);
240
	}
241
242
	@Override
243
	protected void applyImplicitComponents(final DataComponentGetter components) {
244
		this.applyImplicitComponentIfPresent(components, DataComponents.CUSHION_COLOR);
245
		super.applyImplicitComponents(components);
246
	}
247
248
	@Override
249
	protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
250
		if (type == DataComponents.CUSHION_COLOR) {
251
			this.setColor(castComponentValue(DataComponents.CUSHION_COLOR, value));
252
			return true;
253
		} else {
254
			return super.applyImplicitComponent(type, value);
255
		}
256
	}
257
258
	private ItemStack getCushionItemStackWithData() {
259
		ItemStack itemStack = new ItemStack(Items.CUSHION.pick(this.getColor()));
260
		itemStack.set(DataComponents.CUSTOM_NAME, this.getCustomName());
261
		return itemStack;
262
	}
263
}
264

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