DyeItem .java

net.minecraft.world.item.DyeItem (class): members: interactLivingEntity, tryApplyToSign. imports: Sheep, SignTextSlot, SignBlockEntity, InteractionHand.

net.minecraft.world.itemclass49 linescommonextends Item1.8 KB

Implements

API Surface (2)

InteractionResult interactLivingEntity(final ItemStack itemStack, final Player player, final LivingEntity target, final InteractionHand type)
boolean tryApplyToSign(final Level level, final SignBlockEntity sign, final SignTextSlot slot, final ItemStack item, final Player player)

Full Source

DyeItem.java49 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item;
2
3
import net.minecraft.core.component.DataComponents;
4
import net.minecraft.sounds.SoundEvents;
5
import net.minecraft.sounds.SoundSource;
6
import net.minecraft.world.InteractionHand;
7
import net.minecraft.world.InteractionResult;
8
import net.minecraft.world.entity.LivingEntity;
9
import net.minecraft.world.entity.animal.sheep.Sheep;
10
import net.minecraft.world.entity.player.Player;
11
import net.minecraft.world.level.Level;
12
import net.minecraft.world.level.block.entity.SignBlockEntity;
13
import net.minecraft.world.level.block.entity.SignTextSlot;
14
15
public class DyeItem extends Item implements SignApplicator {
16
	public DyeItem(final Item.Properties properties) {
17
		super(properties);
18
	}
19
20
	@Override
21
	public InteractionResult interactLivingEntity(final ItemStack itemStack, final Player player, final LivingEntity target, final InteractionHand type) {
22
		if (target instanceof Sheep sheep && sheep.isAlive() && !sheep.isSheared()) {
23
			DyeColor dyeColor = itemStack.get(DataComponents.DYE);
24
			if (dyeColor != null && sheep.getColor() != dyeColor) {
25
				sheep.level().playSound(player, sheep, SoundEvents.DYE_USE, SoundSource.PLAYERS, 1.0F, 1.0F);
26
				if (!player.level().isClientSide()) {
27
					sheep.setColor(dyeColor);
28
					itemStack.shrink(1);
29
				}
30
31
				return InteractionResult.SUCCESS;
32
			}
33
		}
34
35
		return InteractionResult.PASS;
36
	}
37
38
	@Override
39
	public boolean tryApplyToSign(final Level level, final SignBlockEntity sign, final SignTextSlot slot, final ItemStack item, final Player player) {
40
		DyeColor dye = item.get(DataComponents.DYE);
41
		if (dye != null && sign.updateText(text -> text.withColor(dye), slot)) {
42
			level.playSound(null, sign.getBlockPos(), SoundEvents.DYE_USE, SoundSource.BLOCKS, 1.0F, 1.0F);
43
			return true;
44
		} else {
45
			return false;
46
		}
47
	}
48
}
49

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