EndCrystalItem .java

net.minecraft.world.item.EndCrystalItem (class, 59 lines): members: useOn. imports: EnderDragonFight, EndCrystal, UseOnContext, GameEvent, AABB.

net.minecraft.world.itemclass59 linescommonextends Item1.9 KB

API Surface (1)

InteractionResult useOn(final UseOnContext context)

Full Source

EndCrystalItem.java59 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import java.util.List;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.server.level.ServerLevel;
6
import net.minecraft.world.InteractionResult;
7
import net.minecraft.world.entity.Entity;
8
import net.minecraft.world.entity.boss.enderdragon.EndCrystal;
9
import net.minecraft.world.item.context.UseOnContext;
10
import net.minecraft.world.level.Level;
11
import net.minecraft.world.level.block.Blocks;
12
import net.minecraft.world.level.block.state.BlockState;
13
import net.minecraft.world.level.dimension.end.EnderDragonFight;
14
import net.minecraft.world.level.gameevent.GameEvent;
15
import net.minecraft.world.phys.AABB;
16
17
public class EndCrystalItem extends Item {
18
	public EndCrystalItem(final Item.Properties properties) {
19
		super(properties);
20
	}
21
22
	@Override
23
	public InteractionResult useOn(final UseOnContext context) {
24
		Level level = context.getLevel();
25
		BlockPos pos = context.getClickedPos();
26
		BlockState blockState = level.getBlockState(pos);
27
		if (!blockState.is(Blocks.OBSIDIAN) && !blockState.is(Blocks.BEDROCK)) {
28
			return InteractionResult.FAIL;
29
		}
30
31
		BlockPos above = pos.above();
32
		if (!level.isEmptyBlock(above)) {
33
			return InteractionResult.FAIL;
34
		}
35
36
		double x = above.getX();
37
		double y = above.getY();
38
		double z = above.getZ();
39
		List<Entity> entities = level.getEntities(null, new AABB(x, y, z, x + 1.0, y + 2.0, z + 1.0));
40
		if (!entities.isEmpty()) {
41
			return InteractionResult.FAIL;
42
		}
43
44
		if (level instanceof ServerLevel serverLevel) {
45
			EndCrystal crystal = new EndCrystal(level, x + 0.5, y, z + 0.5);
46
			crystal.setShowBottom(false);
47
			level.addFreshEntity(crystal);
48
			level.gameEvent(context.getPlayer(), GameEvent.ENTITY_PLACE, above);
49
			EnderDragonFight fight = serverLevel.getDragonFight();
50
			if (fight != null) {
51
				fight.tryRespawn();
52
			}
53
		}
54
55
		context.getItemInHand().shrink(1);
56
		return InteractionResult.SUCCESS;
57
	}
58
}
59

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