WitherSkullBlock .java

net.minecraft.world.level.block.WitherSkullBlock (class, 115 lines): members: witherPatternBase, witherPatternFull, canSpawnMob, checkSpawn, setPlacedBy.

net.minecraft.world.level.blockclass115 linescommonextends SkullBlock4.5 KB

API Surface (4)

void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack)
void checkSpawn(final Level level, final BlockPos pos)
void checkSpawn(final Level level, final BlockPos pos, final SkullBlockEntity placedSkull)
boolean canSpawnMob(final Level level, final BlockPos pos, final ItemStack itemStack)

Fields & Constants (2)

witherPatternFull
witherPatternBase

Full Source

WitherSkullBlock.java115 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.level.block;
2
3
import net.minecraft.advancements.triggers.CriteriaTriggers;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Direction;
6
import net.minecraft.server.level.ServerPlayer;
7
import net.minecraft.tags.BlockTags;
8
import net.minecraft.world.Difficulty;
9
import net.minecraft.world.entity.EntitySpawnReason;
10
import net.minecraft.world.entity.EntityTypes;
11
import net.minecraft.world.entity.LivingEntity;
12
import net.minecraft.world.entity.boss.wither.WitherBoss;
13
import net.minecraft.world.item.ItemStack;
14
import net.minecraft.world.item.Items;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.block.entity.SkullBlockEntity;
17
import net.minecraft.world.level.block.state.BlockBehaviour;
18
import net.minecraft.world.level.block.state.BlockState;
19
import net.minecraft.world.level.block.state.pattern.BlockInWorld;
20
import net.minecraft.world.level.block.state.pattern.BlockPattern;
21
import net.minecraft.world.level.block.state.pattern.BlockPatternBuilder;
22
import net.minecraft.world.level.block.state.predicate.BlockStatePredicate;
23
import org.jspecify.annotations.Nullable;
24
25
/**
26
 * Access widened by fabric-transitive-access-wideners-v1 to accessible
27
 */
28
public class WitherSkullBlock extends SkullBlock {
29
	private static @Nullable BlockPattern witherPatternFull;
30
	private static @Nullable BlockPattern witherPatternBase;
31
32
	/**
33
	 * Access widened by fabric-transitive-access-wideners-v1 to accessible
34
	 */
35
	public WitherSkullBlock(final BlockBehaviour.Properties properties) {
36
		super(SkullBlock.Types.WITHER_SKELETON, properties);
37
	}
38
39
	@Override
40
	public void setPlacedBy(final Level level, final BlockPos pos, final BlockState state, final @Nullable LivingEntity by, final ItemStack itemStack) {
41
		checkSpawn(level, pos);
42
	}
43
44
	public static void checkSpawn(final Level level, final BlockPos pos) {
45
		if (level.getBlockEntity(pos) instanceof SkullBlockEntity placedSkull) {
46
			checkSpawn(level, pos, placedSkull);
47
		}
48
	}
49
50
	public static void checkSpawn(final Level level, final BlockPos pos, final SkullBlockEntity placedSkull) {
51
		if (!level.isClientSide()) {
52
			BlockState blockState = placedSkull.getBlockState();
53
			boolean correctBlock = blockState.is(Blocks.WITHER_SKELETON_SKULL) || blockState.is(Blocks.WITHER_SKELETON_WALL_SKULL);
54
			if (correctBlock && pos.getY() >= level.getMinY() && level.getDifficulty() != Difficulty.PEACEFUL) {
55
				BlockPattern.BlockPatternMatch match = getOrCreateWitherFull().find(level, pos);
56
				if (match != null) {
57
					WitherBoss witherBoss = EntityTypes.WITHER.create(level, EntitySpawnReason.TRIGGERED);
58
					if (witherBoss != null) {
59
						CarvedPumpkinBlock.clearPatternBlocks(level, match);
60
						BlockPos spawnPos = match.getBlock(1, 2, 0).getPos();
61
						witherBoss.snapTo(
62
							spawnPos.getX() + 0.5, spawnPos.getY() + 0.55, spawnPos.getZ() + 0.5, match.getForwards().getAxis() == Direction.Axis.X ? 0.0F : 90.0F, 0.0F
63
						);
64
						witherBoss.yBodyRot = match.getForwards().getAxis() == Direction.Axis.X ? 0.0F : 90.0F;
65
						witherBoss.makeInvulnerable();
66
67
						for (ServerPlayer player : level.getEntitiesOfClass(ServerPlayer.class, witherBoss.getBoundingBox().inflate(50.0))) {
68
							CriteriaTriggers.SUMMONED_ENTITY.trigger(player, witherBoss);
69
						}
70
71
						level.addFreshEntity(witherBoss);
72
						CarvedPumpkinBlock.updatePatternBlocks(level, match);
73
					}
74
				}
75
			}
76
		}
77
	}
78
79
	public static boolean canSpawnMob(final Level level, final BlockPos pos, final ItemStack itemStack) {
80
		return itemStack.is(Items.WITHER_SKELETON_SKULL)
81
				&& pos.getY() >= level.getMinY() + 2
82
				&& level.getDifficulty() != Difficulty.PEACEFUL
83
				&& !level.isClientSide()
84
			? getOrCreateWitherBase().find(level, pos) != null
85
			: false;
86
	}
87
88
	private static BlockPattern getOrCreateWitherFull() {
89
		if (witherPatternFull == null) {
90
			witherPatternFull = BlockPatternBuilder.start()
91
				.aisle("^^^", "###", "~#~")
92
				.where('#', block -> block.getState().is(BlockTags.WITHER_SUMMON_BASE_BLOCKS))
93
				.where(
94
					'^', BlockInWorld.hasState(BlockStatePredicate.forBlock(Blocks.WITHER_SKELETON_SKULL).or(BlockStatePredicate.forBlock(Blocks.WITHER_SKELETON_WALL_SKULL)))
95
				)
96
				.where('~', block -> block.getState().isAir())
97
				.build();
98
		}
99
100
		return witherPatternFull;
101
	}
102
103
	private static BlockPattern getOrCreateWitherBase() {
104
		if (witherPatternBase == null) {
105
			witherPatternBase = BlockPatternBuilder.start()
106
				.aisle("   ", "###", "~#~")
107
				.where('#', block -> block.getState().is(BlockTags.WITHER_SUMMON_BASE_BLOCKS))
108
				.where('~', block -> block.getState().isAir())
109
				.build();
110
		}
111
112
		return witherPatternBase;
113
	}
114
}
115

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