Painting .java

net.minecraft.world.entity.decoration.painting.Painting (class, 214 lines): members: DATA_PAINTING_VARIANT_ID, trackingPosition, snapTo, DEPTH, dropItem.

net.minecraft.world.entity.decoration.paintingclass214 linescommonextends HangingEntity7.9 KB

API Surface (16)

void defineSynchedData(final SynchedEntityData.Builder entityData)
void onSyncedDataUpdated(final EntityDataAccessor<?> accessor)
Holder<PaintingVariant> getVariant()
void applyImplicitComponents(final DataComponentGetter components)
<T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value)
Optional<Painting> create(final Level level, final BlockPos pos, final Direction direction)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
AABB calculateBoundingBox(final BlockPos pos, final Direction direction)
void dropItem(final ServerLevel level, final @Nullable Entity causedBy)
void playPlacementSound()
void snapTo(final double x, final double y, final double z, final float yRot, final float xRot)
Vec3 trackingPosition()
Packet<ClientGamePacketListener> getAddEntityPacket(final ServerEntity serverEntity)
void recreateFromPacket(final ClientboundAddEntityPacket packet)
ItemStack getPickResult()

Fields & Constants (2)

DATA_PAINTING_VARIANT_ID
DEPTH

String literals (1)

"facing"

Full Source

Painting.java214 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.decoration.painting;
2
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Optional;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.core.Holder;
9
import net.minecraft.core.component.DataComponentGetter;
10
import net.minecraft.core.component.DataComponentType;
11
import net.minecraft.core.component.DataComponents;
12
import net.minecraft.core.registries.Registries;
13
import net.minecraft.network.protocol.Packet;
14
import net.minecraft.network.protocol.game.ClientGamePacketListener;
15
import net.minecraft.network.protocol.game.ClientboundAddEntityPacket;
16
import net.minecraft.network.syncher.EntityDataAccessor;
17
import net.minecraft.network.syncher.EntityDataSerializers;
18
import net.minecraft.network.syncher.SynchedEntityData;
19
import net.minecraft.server.level.ServerEntity;
20
import net.minecraft.server.level.ServerLevel;
21
import net.minecraft.sounds.SoundEvents;
22
import net.minecraft.tags.PaintingVariantTags;
23
import net.minecraft.util.Util;
24
import net.minecraft.world.entity.Entity;
25
import net.minecraft.world.entity.EntityType;
26
import net.minecraft.world.entity.EntityTypes;
27
import net.minecraft.world.entity.decoration.HangingEntity;
28
import net.minecraft.world.entity.player.Player;
29
import net.minecraft.world.entity.variant.VariantUtils;
30
import net.minecraft.world.item.ItemStack;
31
import net.minecraft.world.item.Items;
32
import net.minecraft.world.level.Level;
33
import net.minecraft.world.level.gamerules.GameRules;
34
import net.minecraft.world.level.storage.ValueInput;
35
import net.minecraft.world.level.storage.ValueOutput;
36
import net.minecraft.world.phys.AABB;
37
import net.minecraft.world.phys.Vec3;
38
import org.jspecify.annotations.Nullable;
39
40
public class Painting extends HangingEntity {
41
	private static final EntityDataAccessor<Holder<PaintingVariant>> DATA_PAINTING_VARIANT_ID = SynchedEntityData.defineId(
42
		Painting.class, EntityDataSerializers.PAINTING_VARIANT
43
	);
44
	public static final float DEPTH = 0.0625F;
45
46
	public Painting(final EntityType<? extends Painting> type, final Level level) {
47
		super(type, level);
48
	}
49
50
	@Override
51
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
52
		super.defineSynchedData(entityData);
53
		entityData.define(DATA_PAINTING_VARIANT_ID, VariantUtils.getAny(this.registryAccess(), Registries.PAINTING_VARIANT));
54
	}
55
56
	@Override
57
	public void onSyncedDataUpdated(final EntityDataAccessor<?> accessor) {
58
		super.onSyncedDataUpdated(accessor);
59
		if (DATA_PAINTING_VARIANT_ID.equals(accessor)) {
60
			this.recalculateBoundingBox();
61
		}
62
	}
63
64
	private void setVariant(final Holder<PaintingVariant> variant) {
65
		this.entityData.set(DATA_PAINTING_VARIANT_ID, variant);
66
	}
67
68
	public Holder<PaintingVariant> getVariant() {
69
		return this.entityData.get(DATA_PAINTING_VARIANT_ID);
70
	}
71
72
	@Override
73
	public <T> @Nullable T get(final DataComponentType<? extends T> type) {
74
		return type == DataComponents.PAINTING_VARIANT ? castComponentValue((DataComponentType<T>)type, this.getVariant()) : super.get(type);
75
	}
76
77
	@Override
78
	protected void applyImplicitComponents(final DataComponentGetter components) {
79
		this.applyImplicitComponentIfPresent(components, DataComponents.PAINTING_VARIANT);
80
		super.applyImplicitComponents(components);
81
	}
82
83
	@Override
84
	protected <T> boolean applyImplicitComponent(final DataComponentType<T> type, final T value) {
85
		if (type == DataComponents.PAINTING_VARIANT) {
86
			this.setVariant(castComponentValue(DataComponents.PAINTING_VARIANT, value));
87
			return true;
88
		} else {
89
			return super.applyImplicitComponent(type, value);
90
		}
91
	}
92
93
	public static Optional<Painting> create(final Level level, final BlockPos pos, final Direction direction) {
94
		Painting candidate = new Painting(level, pos);
95
		List<Holder<PaintingVariant>> potentialVariants = new ArrayList<>();
96
		level.registryAccess().lookupOrThrow(Registries.PAINTING_VARIANT).getTagOrEmpty(PaintingVariantTags.PLACEABLE).forEach(potentialVariants::add);
97
		if (potentialVariants.isEmpty()) {
98
			return Optional.empty();
99
		}
100
101
		candidate.setDirection(direction);
102
		potentialVariants.removeIf(variant -> {
103
			candidate.setVariant((Holder<PaintingVariant>)variant);
104
			return !candidate.survives();
105
		});
106
		if (potentialVariants.isEmpty()) {
107
			return Optional.empty();
108
		}
109
110
		int largestPaintingAreaSize = potentialVariants.stream().mapToInt(Painting::variantArea).max().orElse(0);
111
		potentialVariants.removeIf(variant -> variantArea((Holder<PaintingVariant>)variant) < largestPaintingAreaSize);
112
		Optional<Holder<PaintingVariant>> selectedVariant = Util.getRandomSafe(potentialVariants, candidate.random);
113
		if (selectedVariant.isEmpty()) {
114
			return Optional.empty();
115
		}
116
117
		candidate.setVariant(selectedVariant.get());
118
		candidate.setDirection(direction);
119
		return Optional.of(candidate);
120
	}
121
122
	private static int variantArea(final Holder<PaintingVariant> variant) {
123
		return variant.value().area();
124
	}
125
126
	private Painting(final Level level, final BlockPos blockPos) {
127
		super(EntityTypes.PAINTING, level, blockPos);
128
	}
129
130
	public Painting(final Level level, final BlockPos blockPos, final Direction direction, final Holder<PaintingVariant> variant) {
131
		this(level, blockPos);
132
		this.setVariant(variant);
133
		this.setDirection(direction);
134
	}
135
136
	@Override
137
	protected void addAdditionalSaveData(final ValueOutput output) {
138
		output.store("facing", Direction.LEGACY_ID_CODEC_2D, this.getDirection());
139
		super.addAdditionalSaveData(output);
140
		VariantUtils.writeVariant(output, this.getVariant());
141
	}
142
143
	@Override
144
	protected void readAdditionalSaveData(final ValueInput input) {
145
		Direction direction = input.read("facing", Direction.LEGACY_ID_CODEC_2D).orElse(Direction.SOUTH);
146
		super.readAdditionalSaveData(input);
147
		this.setDirection(direction);
148
		VariantUtils.readVariant(input, Registries.PAINTING_VARIANT).ifPresent(this::setVariant);
149
	}
150
151
	@Override
152
	protected AABB calculateBoundingBox(final BlockPos pos, final Direction direction) {
153
		float shiftToBlockWall = 0.46875F;
154
		Vec3 attachedToWall = Vec3.atCenterOf(pos).relative(direction, -0.46875);
155
		PaintingVariant variant = this.getVariant().value();
156
		double horizontalOffset = this.offsetForPaintingSize(variant.width());
157
		double verticalOffset = this.offsetForPaintingSize(variant.height());
158
		Direction left = direction.getCounterClockWise();
159
		Vec3 position = attachedToWall.relative(left, horizontalOffset).relative(Direction.UP, verticalOffset);
160
		Direction.Axis axis = direction.getAxis();
161
		double xSize = axis == Direction.Axis.X ? 0.0625 : variant.width();
162
		double ySize = variant.height();
163
		double zSize = axis == Direction.Axis.Z ? 0.0625 : variant.width();
164
		return AABB.ofSize(position, xSize, ySize, zSize);
165
	}
166
167
	private double offsetForPaintingSize(final int size) {
168
		return size % 2 == 0 ? 0.5 : 0.0;
169
	}
170
171
	@Override
172
	public void dropItem(final ServerLevel level, final @Nullable Entity causedBy) {
173
		if (level.getGameRules().get(GameRules.ENTITY_DROPS)) {
174
			this.playSound(SoundEvents.PAINTING_BREAK, 1.0F, 1.0F);
175
			if (!(causedBy instanceof Player player && player.hasInfiniteMaterials())) {
176
				ItemStack itemStack = new ItemStack(Items.PAINTING);
177
				itemStack.set(DataComponents.CUSTOM_NAME, this.getCustomName());
178
				this.spawnAtLocation(level, itemStack);
179
			}
180
		}
181
	}
182
183
	@Override
184
	public void playPlacementSound() {
185
		this.playSound(SoundEvents.PAINTING_PLACE, 1.0F, 1.0F);
186
	}
187
188
	@Override
189
	public void snapTo(final double x, final double y, final double z, final float yRot, final float xRot) {
190
		this.setPos(x, y, z);
191
	}
192
193
	@Override
194
	public Vec3 trackingPosition() {
195
		return Vec3.atLowerCornerOf(this.pos);
196
	}
197
198
	@Override
199
	public Packet<ClientGamePacketListener> getAddEntityPacket(final ServerEntity serverEntity) {
200
		return new ClientboundAddEntityPacket(this, this.getDirection().get3DDataValue(), this.getPos());
201
	}
202
203
	@Override
204
	public void recreateFromPacket(final ClientboundAddEntityPacket packet) {
205
		super.recreateFromPacket(packet);
206
		this.setDirection(Direction.from3DDataValue(packet.getData()));
207
	}
208
209
	@Override
210
	public ItemStack getPickResult() {
211
		return new ItemStack(Items.PAINTING);
212
	}
213
}
214

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