CarvedPumpkinBlock .java

net.minecraft.world.level.block.CarvedPumpkinBlock (class): members: replaceCopperBlockWithChest, updatePatternBlocks, PUMPKINS_PREDICATE, copperGolemBase.

net.minecraft.world.level.blockclass223 linescommonextends HorizontalDirectionalBlock8.9 KB

API Surface (7)

void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston)
boolean canSpawnGolem(final LevelReader level, final BlockPos topPos)
void clearPatternBlocks(final Level level, final BlockPattern.BlockPatternMatch match)
void updatePatternBlocks(final Level level, final BlockPattern.BlockPatternMatch match)
BlockState getStateForPlacement(final BlockPlaceContext context)
void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder)
void replaceCopperBlockWithChest(final Level level, final BlockPattern.BlockPatternMatch match)

Fields & Constants (8)

FACING
PUMPKINS_PREDICATE
snowGolemBase
snowGolemFull
ironGolemBase
ironGolemFull
copperGolemBase
copperGolemFull

Full Source

CarvedPumpkinBlock.java223 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import java.util.Optional;
4
import java.util.function.Predicate;
5
import net.minecraft.advancements.triggers.CriteriaTriggers;
6
import net.minecraft.core.BlockPos;
7
import net.minecraft.core.Direction;
8
import net.minecraft.server.level.ServerPlayer;
9
import net.minecraft.tags.BlockTags;
10
import net.minecraft.world.entity.Entity;
11
import net.minecraft.world.entity.EntitySpawnReason;
12
import net.minecraft.world.entity.EntityTypes;
13
import net.minecraft.world.entity.animal.golem.CopperGolem;
14
import net.minecraft.world.entity.animal.golem.IronGolem;
15
import net.minecraft.world.entity.animal.golem.SnowGolem;
16
import net.minecraft.world.item.HoneycombItem;
17
import net.minecraft.world.item.context.BlockPlaceContext;
18
import net.minecraft.world.level.Level;
19
import net.minecraft.world.level.LevelReader;
20
import net.minecraft.world.level.block.state.BlockBehaviour;
21
import net.minecraft.world.level.block.state.BlockState;
22
import net.minecraft.world.level.block.state.StateDefinition;
23
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
24
import net.minecraft.world.level.block.state.pattern.BlockPattern;
25
import net.minecraft.world.level.block.state.pattern.BlockPatternBuilder;
26
import net.minecraft.world.level.block.state.predicate.BlockStatePredicate;
27
import net.minecraft.world.level.block.state.properties.EnumProperty;
28
import org.jspecify.annotations.Nullable;
29
30
/**
31
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
32
 */
33
public class CarvedPumpkinBlock extends HorizontalDirectionalBlock {
34
	public static final EnumProperty<Direction> FACING = HorizontalDirectionalBlock.FACING;
35
	private @Nullable BlockPattern snowGolemBase;
36
	private @Nullable BlockPattern snowGolemFull;
37
	private @Nullable BlockPattern ironGolemBase;
38
	private @Nullable BlockPattern ironGolemFull;
39
	private @Nullable BlockPattern copperGolemBase;
40
	private @Nullable BlockPattern copperGolemFull;
41
	private static final Predicate<BlockState> PUMPKINS_PREDICATE = input -> input.is(Blocks.CARVED_PUMPKIN) || input.is(Blocks.JACK_O_LANTERN);
42
43
	/**
44
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
45
	 */
46
	public CarvedPumpkinBlock(final BlockBehaviour.Properties properties) {
47
		super(properties);
48
		this.registerDefaultState(this.stateDefinition.any().setValue(FACING, Direction.NORTH));
49
	}
50
51
	@Override
52
	protected void onPlace(final BlockState state, final Level level, final BlockPos pos, final BlockState oldState, final boolean movedByPiston) {
53
		if (!oldState.is(state.getBlock())) {
54
			this.trySpawnGolem(level, pos);
55
		}
56
	}
57
58
	public boolean canSpawnGolem(final LevelReader level, final BlockPos topPos) {
59
		return this.getOrCreateSnowGolemBase().find(level, topPos) != null
60
			|| this.getOrCreateIronGolemBase().find(level, topPos) != null
61
			|| this.getOrCreateCopperGolemBase().find(level, topPos) != null;
62
	}
63
64
	private void trySpawnGolem(final Level level, final BlockPos topPos) {
65
		BlockPattern.BlockPatternMatch snowGolemMatch = this.getOrCreateSnowGolemFull().find(level, topPos);
66
		if (snowGolemMatch != null) {
67
			SnowGolem snowGolem = EntityTypes.SNOW_GOLEM.create(level, EntitySpawnReason.TRIGGERED);
68
			if (snowGolem != null) {
69
				spawnGolemInWorld(level, snowGolemMatch, snowGolem, snowGolemMatch.getBlock(0, 2, 0).getPos());
70
				return;
71
			}
72
		}
73
74
		BlockPattern.BlockPatternMatch ironGolemMatch = this.getOrCreateIronGolemFull().find(level, topPos);
75
		if (ironGolemMatch != null) {
76
			IronGolem ironGolem = EntityTypes.IRON_GOLEM.create(level, EntitySpawnReason.TRIGGERED);
77
			if (ironGolem != null) {
78
				ironGolem.setPlayerCreated(true);
79
				spawnGolemInWorld(level, ironGolemMatch, ironGolem, ironGolemMatch.getBlock(1, 2, 0).getPos());
80
				return;
81
			}
82
		}
83
84
		BlockPattern.BlockPatternMatch copperGolemMatch = this.getOrCreateCopperGolemFull().find(level, topPos);
85
		if (copperGolemMatch != null) {
86
			CopperGolem copperGolem = EntityTypes.COPPER_GOLEM.create(level, EntitySpawnReason.TRIGGERED);
87
			if (copperGolem != null) {
88
				spawnGolemInWorld(level, copperGolemMatch, copperGolem, copperGolemMatch.getBlock(0, 0, 0).getPos());
89
				this.replaceCopperBlockWithChest(level, copperGolemMatch);
90
				copperGolem.spawn(this.getWeatherStateFromPattern(copperGolemMatch));
91
			}
92
		}
93
	}
94
95
	private WeatheringCopper.WeatherState getWeatherStateFromPattern(final BlockPattern.BlockPatternMatch copperGolemMatch) {
96
		BlockState state = copperGolemMatch.getBlock(0, 1, 0).getState();
97
		return state.getBlock() instanceof WeatheringCopper copper
98
			? copper.getAge()
99
			: Optional.ofNullable(HoneycombItem.WAX_OFF_BY_BLOCK.get().get(state.getBlock()))
100
				.filter(weatheringCopper -> weatheringCopper instanceof WeatheringCopper)
101
				.map(weatheringCopper -> (WeatheringCopper)weatheringCopper)
102
				.orElse((WeatheringCopper)Blocks.COPPER_BLOCK.weathering().unaffected())
103
				.getAge();
104
	}
105
106
	private static void spawnGolemInWorld(final Level level, final BlockPattern.BlockPatternMatch match, final Entity golem, final BlockPos spawnPos) {
107
		clearPatternBlocks(level, match);
108
		golem.snapTo(spawnPos.getX() + 0.5, spawnPos.getY() + 0.05, spawnPos.getZ() + 0.5, 0.0F, 0.0F);
109
		level.addFreshEntity(golem);
110
111
		for (ServerPlayer player : level.getEntitiesOfClass(ServerPlayer.class, golem.getBoundingBox().inflate(5.0))) {
112
			CriteriaTriggers.SUMMONED_ENTITY.trigger(player, golem);
113
		}
114
115
		updatePatternBlocks(level, match);
116
	}
117
118
	public static void clearPatternBlocks(final Level level, final BlockPattern.BlockPatternMatch match) {
119
		for (int x = 0; x < match.getWidth(); x++) {
120
			for (int y = 0; y < match.getHeight(); y++) {
121
				BlockInWorld block = match.getBlock(x, y, 0);
122
				level.setBlock(block.getPos(), Blocks.AIR.defaultBlockState(), 2);
123
				level.levelEvent(2001, block.getPos(), Block.getId(block.getState()));
124
			}
125
		}
126
	}
127
128
	public static void updatePatternBlocks(final Level level, final BlockPattern.BlockPatternMatch match) {
129
		for (int x = 0; x < match.getWidth(); x++) {
130
			for (int y = 0; y < match.getHeight(); y++) {
131
				BlockInWorld block = match.getBlock(x, y, 0);
132
				level.updateNeighborsAt(block.getPos(), Blocks.AIR);
133
			}
134
		}
135
	}
136
137
	@Override
138
	public BlockState getStateForPlacement(final BlockPlaceContext context) {
139
		return this.defaultBlockState().setValue(FACING, context.getHorizontalDirection().getOpposite());
140
	}
141
142
	@Override
143
	protected void createBlockStateDefinition(final StateDefinition.Builder<Block, BlockState> builder) {
144
		builder.add(FACING);
145
	}
146
147
	private BlockPattern getOrCreateSnowGolemBase() {
148
		if (this.snowGolemBase == null) {
149
			this.snowGolemBase = BlockPatternBuilder.start()
150
				.aisle(" ", "#", "#")
151
				.where('#', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.SNOW_BLOCK)))
152
				.build();
153
		}
154
155
		return this.snowGolemBase;
156
	}
157
158
	private BlockPattern getOrCreateSnowGolemFull() {
159
		if (this.snowGolemFull == null) {
160
			this.snowGolemFull = BlockPatternBuilder.start()
161
				.aisle("^", "#", "#")
162
				.where('^', BlockInWorld.hasState(PUMPKINS_PREDICATE))
163
				.where('#', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.SNOW_BLOCK)))
164
				.build();
165
		}
166
167
		return this.snowGolemFull;
168
	}
169
170
	private BlockPattern getOrCreateIronGolemBase() {
171
		if (this.ironGolemBase == null) {
172
			this.ironGolemBase = BlockPatternBuilder.start()
173
				.aisle("~ ~", "###", "~#~")
174
				.where('#', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.IRON_BLOCK)))
175
				.where('~', BlockInWorld.hasState(BlockBehaviour.BlockStateBase::isAir))
176
				.build();
177
		}
178
179
		return this.ironGolemBase;
180
	}
181
182
	private BlockPattern getOrCreateIronGolemFull() {
183
		if (this.ironGolemFull == null) {
184
			this.ironGolemFull = BlockPatternBuilder.start()
185
				.aisle("~^~", "###", "~#~")
186
				.where('^', BlockInWorld.hasState(PUMPKINS_PREDICATE))
187
				.where('#', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.IRON_BLOCK)))
188
				.where('~', BlockInWorld.hasState(BlockBehaviour.BlockStateBase::isAir))
189
				.build();
190
		}
191
192
		return this.ironGolemFull;
193
	}
194
195
	private BlockPattern getOrCreateCopperGolemBase() {
196
		if (this.copperGolemBase == null) {
197
			this.copperGolemBase = BlockPatternBuilder.start().aisle(" ", "#").where('#', BlockInWorld.hasState(block -> block.is(BlockTags.COPPER))).build();
198
		}
199
200
		return this.copperGolemBase;
201
	}
202
203
	private BlockPattern getOrCreateCopperGolemFull() {
204
		if (this.copperGolemFull == null) {
205
			this.copperGolemFull = BlockPatternBuilder.start()
206
				.aisle("^", "#")
207
				.where('^', BlockInWorld.hasState(PUMPKINS_PREDICATE))
208
				.where('#', BlockInWorld.hasState(block -> block.is(BlockTags.COPPER)))
209
				.build();
210
		}
211
212
		return this.copperGolemFull;
213
	}
214
215
	public void replaceCopperBlockWithChest(final Level level, final BlockPattern.BlockPatternMatch match) {
216
		BlockInWorld copperBlock = match.getBlock(0, 1, 0);
217
		BlockInWorld pumpkinBlock = match.getBlock(0, 0, 0);
218
		Direction facing = pumpkinBlock.getState().getValue(FACING);
219
		BlockState blockState = CopperChestBlock.getFromCopperBlock(copperBlock.getState().getBlock(), facing, level, copperBlock.getPos());
220
		level.setBlock(copperBlock.getPos(), blockState, 2);
221
	}
222
}
223

Decompiled Minecraft 26.3 source of CarvedPumpkinBlock (net/minecraft/world/level/block/CarvedPumpkinBlock.java) with Mojang official mappings, stored verbatim on the site.