DebugStickItem .java

net.minecraft.world.item.DebugStickItem (class): members: canDestroyBlock, cycle, useOn. strings: ".select", ".update", ".empty". imports: DebugStickState.

net.minecraft.world.itemclass105 linescommonextends Item4.0 KB

API Surface (2)

boolean canDestroyBlock(final ItemStack itemStack, final BlockState state, final Level level, final BlockPos pos, final LivingEntity user)
InteractionResult useOn(final UseOnContext context)

Fields & Constants (1)

cycle

String literals (3)

".empty"
".update"
".select"

Full Source

DebugStickItem.java105 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import java.util.Collection;
4
import net.minecraft.core.BlockPos;
5
import net.minecraft.core.Holder;
6
import net.minecraft.core.component.DataComponents;
7
import net.minecraft.network.chat.Component;
8
import net.minecraft.server.level.ServerPlayer;
9
import net.minecraft.util.Util;
10
import net.minecraft.world.InteractionResult;
11
import net.minecraft.world.entity.LivingEntity;
12
import net.minecraft.world.entity.player.Player;
13
import net.minecraft.world.item.component.DebugStickState;
14
import net.minecraft.world.item.context.UseOnContext;
15
import net.minecraft.world.level.Level;
16
import net.minecraft.world.level.LevelAccessor;
17
import net.minecraft.world.level.block.Block;
18
import net.minecraft.world.level.block.state.BlockState;
19
import net.minecraft.world.level.block.state.StateDefinition;
20
import net.minecraft.world.level.block.state.properties.Property;
21
import org.jspecify.annotations.Nullable;
22
23
public class DebugStickItem extends Item {
24
	public DebugStickItem(final Item.Properties properties) {
25
		super(properties);
26
	}
27
28
	@Override
29
	public boolean canDestroyBlock(final ItemStack itemStack, final BlockState state, final Level level, final BlockPos pos, final LivingEntity user) {
30
		if (user instanceof ServerPlayer player) {
31
			this.handleInteraction(player, state, level, pos, false, itemStack);
32
		}
33
34
		return false;
35
	}
36
37
	@Override
38
	public InteractionResult useOn(final UseOnContext context) {
39
		Player player = context.getPlayer();
40
		Level level = context.getLevel();
41
		if (player instanceof ServerPlayer serverPlayer) {
42
			BlockPos pos = context.getClickedPos();
43
			if (!this.handleInteraction(serverPlayer, level.getBlockState(pos), level, pos, true, context.getItemInHand())) {
44
				return InteractionResult.FAIL;
45
			}
46
		}
47
48
		return InteractionResult.SUCCESS;
49
	}
50
51
	private boolean handleInteraction(
52
		final ServerPlayer player, final BlockState state, final LevelAccessor level, final BlockPos pos, final boolean cycle, final ItemStack itemStackInHand
53
	) {
54
		if (!player.canUseGameMasterBlocks()) {
55
			return false;
56
		}
57
58
		Holder<Block> block = state.typeHolder();
59
		StateDefinition<Block, BlockState> definition = block.value().getStateDefinition();
60
		Collection<Property<?>> properties = definition.getProperties();
61
		if (properties.isEmpty()) {
62
			message(player, Component.translatable(this.descriptionId + ".empty", block.getRegisteredName()));
63
			return false;
64
		}
65
66
		DebugStickState debugStickState = itemStackInHand.get(DataComponents.DEBUG_STICK_STATE);
67
		if (debugStickState == null) {
68
			return false;
69
		}
70
71
		Property<?> property = debugStickState.properties().get(block);
72
		if (cycle) {
73
			if (property == null) {
74
				property = properties.iterator().next();
75
			}
76
77
			BlockState newState = cycleState(state, property, player.isSecondaryUseActive());
78
			level.setBlock(pos, newState, 18);
79
			message(player, Component.translatable(this.descriptionId + ".update", property.getName(), getNameHelper(newState, property)));
80
		} else {
81
			property = getRelative(properties, property, player.isSecondaryUseActive());
82
			itemStackInHand.set(DataComponents.DEBUG_STICK_STATE, debugStickState.withProperty(block, property));
83
			message(player, Component.translatable(this.descriptionId + ".select", property.getName(), getNameHelper(state, property)));
84
		}
85
86
		return true;
87
	}
88
89
	private static <T extends Comparable<T>> BlockState cycleState(final BlockState state, final Property<T> property, final boolean backward) {
90
		return state.setValue(property, getRelative(property.getPossibleValues(), state.getValue(property), backward));
91
	}
92
93
	private static <T> T getRelative(final Iterable<T> collection, final @Nullable T current, final boolean backward) {
94
		return backward ? Util.findPreviousInIterable(collection, current) : Util.findNextInIterable(collection, current);
95
	}
96
97
	private static void message(final ServerPlayer player, final Component message) {
98
		player.sendOverlayMessage(message);
99
	}
100
101
	private static <T extends Comparable<T>> String getNameHelper(final BlockState state, final Property<T> property) {
102
		return property.getName(state.getValue(property));
103
	}
104
}
105

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