Equippable .java

net.minecraft.world.item.equipment.Equippable (record): members: slot, equipSound, assetId, cameraOverlay, allowedEntities, dispensable, swappable, saddle.

net.minecraft.world.item.equipmentrecord268 linescommon9.9 KB

API Surface (20)

record Equippable(EquipmentSlot slot, Holder<SoundEvent> equipSound, Optional<ResourceKey<EquipmentAsset>> assetId, Optional<Identifier> cameraOverlay, Optional<HolderSet<EntityType<?>>> allowedEntities, boolean dispensable, boolean swappable, boolean damageOnHurt, boolean equipOnInteract, boolean canBeSheared, Holder<SoundEvent> shearingSound)
Equippable llamaSwag(final DyeColor color)
Equippable saddle()
Equippable harness(final DyeColor color)
Equippable.Builder builder(final EquipmentSlot slot)
InteractionResult swapWithEquipmentSlot(final ItemStack inHand, final Player player)
InteractionResult equipOnTarget(final Player player, final LivingEntity target, final ItemStack itemStack)
boolean canBeEquippedBy(final Holder<EntityType<?>> type)
Equippable.Builder setEquipSound(final Holder<SoundEvent> equipSound)
Equippable.Builder setAsset(final ResourceKey<EquipmentAsset> assetId)
Equippable.Builder setCameraOverlay(final Identifier cameraOverlay)
Equippable.Builder setAllowedEntities(final EntityType<?>... allowedEntities)
Equippable.Builder setAllowedEntities(final HolderSet<EntityType<?>> allowedEntities)
Equippable.Builder setDispensable(final boolean dispensable)
Equippable.Builder setSwappable(final boolean swappable)
Equippable.Builder setDamageOnHurt(final boolean damageOnHurt)
Equippable.Builder setEquipOnInteract(final boolean equipOnInteract)
Equippable.Builder setCanBeSheared(final boolean canBeSheared)
Equippable.Builder setShearingSound(final Holder<SoundEvent> shearingSound)
Equippable build()

Fields & Constants (2)

CODEC
STREAM_CODEC

String literals (11)

"slot"
"equip_sound"
"asset_id"
"camera_overlay"
"allowed_entities"
"dispensable"
"swappable"
"damage_on_hurt"
"equip_on_interact"
"can_be_sheared"
"shearing_sound"

Full Source

Equippable.java268 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.item.equipment;
2
3
import com.mojang.serialization.Codec;
4
import com.mojang.serialization.codecs.RecordCodecBuilder;
5
import java.util.Optional;
6
import net.minecraft.core.Holder;
7
import net.minecraft.core.HolderGetter;
8
import net.minecraft.core.HolderSet;
9
import net.minecraft.core.registries.BuiltInRegistries;
10
import net.minecraft.core.registries.Registries;
11
import net.minecraft.core.registries.codec.RegistryCodecs;
12
import net.minecraft.network.RegistryFriendlyByteBuf;
13
import net.minecraft.network.codec.ByteBufCodecs;
14
import net.minecraft.network.codec.StreamCodec;
15
import net.minecraft.resources.Identifier;
16
import net.minecraft.resources.ResourceKey;
17
import net.minecraft.sounds.SoundEvent;
18
import net.minecraft.sounds.SoundEvents;
19
import net.minecraft.stats.Stats;
20
import net.minecraft.tags.EntityTypeTags;
21
import net.minecraft.util.Prediction;
22
import net.minecraft.world.InteractionResult;
23
import net.minecraft.world.entity.EntityType;
24
import net.minecraft.world.entity.EntityTypes;
25
import net.minecraft.world.entity.EquipmentSlot;
26
import net.minecraft.world.entity.LivingEntity;
27
import net.minecraft.world.entity.Mob;
28
import net.minecraft.world.entity.player.Player;
29
import net.minecraft.world.item.DyeColor;
30
import net.minecraft.world.item.ItemStack;
31
import net.minecraft.world.item.enchantment.EnchantmentEffectComponents;
32
import net.minecraft.world.item.enchantment.EnchantmentHelper;
33
34
public record Equippable(
35
	EquipmentSlot slot,
36
	Holder<SoundEvent> equipSound,
37
	Optional<ResourceKey<EquipmentAsset>> assetId,
38
	Optional<Identifier> cameraOverlay,
39
	Optional<HolderSet<EntityType<?>>> allowedEntities,
40
	boolean dispensable,
41
	boolean swappable,
42
	boolean damageOnHurt,
43
	boolean equipOnInteract,
44
	boolean canBeSheared,
45
	Holder<SoundEvent> shearingSound
46
) {
47
	public static final Codec<Equippable> CODEC = RecordCodecBuilder.create(
48
		i -> i.group(
49
				EquipmentSlot.CODEC.fieldOf("slot").forGetter(Equippable::slot),
50
				SoundEvent.CODEC.optionalFieldOf("equip_sound", SoundEvents.ARMOR_EQUIP_GENERIC).forGetter(Equippable::equipSound),
51
				ResourceKey.codec(EquipmentAssets.ROOT_ID).optionalFieldOf("asset_id").forGetter(Equippable::assetId),
52
				Identifier.CODEC.optionalFieldOf("camera_overlay").forGetter(Equippable::cameraOverlay),
53
				RegistryCodecs.holderSet(Registries.ENTITY_TYPE).optionalFieldOf("allowed_entities").forGetter(Equippable::allowedEntities),
54
				Codec.BOOL.optionalFieldOf("dispensable", true).forGetter(Equippable::dispensable),
55
				Codec.BOOL.optionalFieldOf("swappable", true).forGetter(Equippable::swappable),
56
				Codec.BOOL.optionalFieldOf("damage_on_hurt", true).forGetter(Equippable::damageOnHurt),
57
				Codec.BOOL.optionalFieldOf("equip_on_interact", false).forGetter(Equippable::equipOnInteract),
58
				Codec.BOOL.optionalFieldOf("can_be_sheared", false).forGetter(Equippable::canBeSheared),
59
				SoundEvent.CODEC
60
					.optionalFieldOf("shearing_sound", BuiltInRegistries.SOUND_EVENT.wrapAsHolder(SoundEvents.SHEARS_SNIP))
61
					.forGetter(Equippable::shearingSound)
62
			)
63
			.apply(i, Equippable::new)
64
	);
65
	public static final StreamCodec<RegistryFriendlyByteBuf, Equippable> STREAM_CODEC = StreamCodec.composite(
66
		EquipmentSlot.STREAM_CODEC,
67
		Equippable::slot,
68
		SoundEvent.STREAM_CODEC,
69
		Equippable::equipSound,
70
		ResourceKey.streamCodec(EquipmentAssets.ROOT_ID).apply(ByteBufCodecs::optional),
71
		Equippable::assetId,
72
		Identifier.STREAM_CODEC.apply(ByteBufCodecs::optional),
73
		Equippable::cameraOverlay,
74
		ByteBufCodecs.holderSet(Registries.ENTITY_TYPE).apply(ByteBufCodecs::optional),
75
		Equippable::allowedEntities,
76
		ByteBufCodecs.BOOL,
77
		Equippable::dispensable,
78
		ByteBufCodecs.BOOL,
79
		Equippable::swappable,
80
		ByteBufCodecs.BOOL,
81
		Equippable::damageOnHurt,
82
		ByteBufCodecs.BOOL,
83
		Equippable::equipOnInteract,
84
		ByteBufCodecs.BOOL,
85
		Equippable::canBeSheared,
86
		SoundEvent.STREAM_CODEC,
87
		Equippable::shearingSound,
88
		Equippable::new
89
	);
90
91
	public static Equippable llamaSwag(final DyeColor color) {
92
		return builder(EquipmentSlot.BODY)
93
			.setEquipSound(SoundEvents.LLAMA_SWAG)
94
			.setAsset(EquipmentAssets.CARPETS.get(color))
95
			.setAllowedEntities(EntityTypes.LLAMA, EntityTypes.TRADER_LLAMA)
96
			.setCanBeSheared(true)
97
			.setShearingSound(SoundEvents.LLAMA_CARPET_UNEQUIP)
98
			.build();
99
	}
100
101
	public static Equippable saddle() {
102
		HolderGetter<EntityType<?>> entityGetter = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.ENTITY_TYPE);
103
		return builder(EquipmentSlot.SADDLE)
104
			.setEquipSound(SoundEvents.HORSE_SADDLE)
105
			.setAsset(EquipmentAssets.SADDLE)
106
			.setAllowedEntities(entityGetter.getOrThrow(EntityTypeTags.CAN_EQUIP_SADDLE))
107
			.setEquipOnInteract(true)
108
			.setCanBeSheared(true)
109
			.setShearingSound(SoundEvents.SADDLE_UNEQUIP)
110
			.build();
111
	}
112
113
	public static Equippable harness(final DyeColor color) {
114
		HolderGetter<EntityType<?>> entityGetter = BuiltInRegistries.acquireBootstrapRegistrationLookup(BuiltInRegistries.ENTITY_TYPE);
115
		return builder(EquipmentSlot.BODY)
116
			.setEquipSound(SoundEvents.HARNESS_EQUIP)
117
			.setAsset(EquipmentAssets.HARNESSES.get(color))
118
			.setAllowedEntities(entityGetter.getOrThrow(EntityTypeTags.CAN_EQUIP_HARNESS))
119
			.setEquipOnInteract(true)
120
			.setCanBeSheared(true)
121
			.setShearingSound(BuiltInRegistries.SOUND_EVENT.wrapAsHolder(SoundEvents.HARNESS_UNEQUIP))
122
			.build();
123
	}
124
125
	public static Equippable.Builder builder(final EquipmentSlot slot) {
126
		return new Equippable.Builder(slot);
127
	}
128
129
	public InteractionResult swapWithEquipmentSlot(final ItemStack inHand, final Player player) {
130
		if (player.canUseSlot(this.slot) && this.canBeEquippedBy(player.typeHolder())) {
131
			ItemStack inEquipmentSlot = player.getItemBySlot(this.slot);
132
			if ((!EnchantmentHelper.has(inEquipmentSlot, EnchantmentEffectComponents.PREVENT_ARMOR_CHANGE) || player.isCreative())
133
				&& !ItemStack.isSameItemSameComponents(inHand, inEquipmentSlot)) {
134
				if (!player.level().isClientSide()) {
135
					player.awardStat(Stats.ITEM_USED.get(inHand.getItem()));
136
				}
137
138
				if (inHand.getCount() <= 1) {
139
					ItemStack swappedToHand = inEquipmentSlot.isEmpty() ? inHand : inEquipmentSlot.copyAndClear();
140
					ItemStack swappedToEquipment = player.isCreative() ? inHand.copy() : inHand.copyAndClear();
141
					player.setItemSlot(this.slot, swappedToEquipment);
142
					return InteractionResult.SUCCESS.heldItemTransformedTo(swappedToHand);
143
				}
144
145
				ItemStack swappedToInventory = inEquipmentSlot.copyAndClear();
146
				ItemStack swappedToEquipment = inHand.consumeAndReturn(1, player);
147
				player.setItemSlot(this.slot, swappedToEquipment);
148
				if (!player.getInventory().add(swappedToInventory)) {
149
					player.drop(swappedToInventory, false, Prediction.PREDICTED);
150
				}
151
152
				return InteractionResult.SUCCESS.heldItemTransformedTo(inHand);
153
			} else {
154
				return InteractionResult.FAIL;
155
			}
156
		} else {
157
			return InteractionResult.PASS;
158
		}
159
	}
160
161
	public InteractionResult equipOnTarget(final Player player, final LivingEntity target, final ItemStack itemStack) {
162
		if (target.isEquippableInSlot(itemStack, this.slot) && !target.hasItemInSlot(this.slot) && target.isAlive()) {
163
			if (!player.level().isClientSide()) {
164
				target.setItemSlot(this.slot, itemStack.split(1));
165
				if (target instanceof Mob mob) {
166
					mob.setGuaranteedDrop(this.slot);
167
				}
168
			}
169
170
			return InteractionResult.SUCCESS;
171
		} else {
172
			return InteractionResult.PASS;
173
		}
174
	}
175
176
	public boolean canBeEquippedBy(final Holder<EntityType<?>> type) {
177
		return this.allowedEntities.isEmpty() || this.allowedEntities.get().contains(type);
178
	}
179
180
	public static class Builder {
181
		private final EquipmentSlot slot;
182
		private Holder<SoundEvent> equipSound = SoundEvents.ARMOR_EQUIP_GENERIC;
183
		private Optional<ResourceKey<EquipmentAsset>> assetId = Optional.empty();
184
		private Optional<Identifier> cameraOverlay = Optional.empty();
185
		private Optional<HolderSet<EntityType<?>>> allowedEntities = Optional.empty();
186
		private boolean dispensable = true;
187
		private boolean swappable = true;
188
		private boolean damageOnHurt = true;
189
		private boolean equipOnInteract;
190
		private boolean canBeSheared;
191
		private Holder<SoundEvent> shearingSound = BuiltInRegistries.SOUND_EVENT.wrapAsHolder(SoundEvents.SHEARS_SNIP);
192
193
		private Builder(final EquipmentSlot slot) {
194
			this.slot = slot;
195
		}
196
197
		public Equippable.Builder setEquipSound(final Holder<SoundEvent> equipSound) {
198
			this.equipSound = equipSound;
199
			return this;
200
		}
201
202
		public Equippable.Builder setAsset(final ResourceKey<EquipmentAsset> assetId) {
203
			this.assetId = Optional.of(assetId);
204
			return this;
205
		}
206
207
		public Equippable.Builder setCameraOverlay(final Identifier cameraOverlay) {
208
			this.cameraOverlay = Optional.of(cameraOverlay);
209
			return this;
210
		}
211
212
		public Equippable.Builder setAllowedEntities(final EntityType<?>... allowedEntities) {
213
			return this.setAllowedEntities(HolderSet.direct(EntityType::builtInRegistryHolder, allowedEntities));
214
		}
215
216
		public Equippable.Builder setAllowedEntities(final HolderSet<EntityType<?>> allowedEntities) {
217
			this.allowedEntities = Optional.of(allowedEntities);
218
			return this;
219
		}
220
221
		public Equippable.Builder setDispensable(final boolean dispensable) {
222
			this.dispensable = dispensable;
223
			return this;
224
		}
225
226
		public Equippable.Builder setSwappable(final boolean swappable) {
227
			this.swappable = swappable;
228
			return this;
229
		}
230
231
		public Equippable.Builder setDamageOnHurt(final boolean damageOnHurt) {
232
			this.damageOnHurt = damageOnHurt;
233
			return this;
234
		}
235
236
		public Equippable.Builder setEquipOnInteract(final boolean equipOnInteract) {
237
			this.equipOnInteract = equipOnInteract;
238
			return this;
239
		}
240
241
		public Equippable.Builder setCanBeSheared(final boolean canBeSheared) {
242
			this.canBeSheared = canBeSheared;
243
			return this;
244
		}
245
246
		public Equippable.Builder setShearingSound(final Holder<SoundEvent> shearingSound) {
247
			this.shearingSound = shearingSound;
248
			return this;
249
		}
250
251
		public Equippable build() {
252
			return new Equippable(
253
				this.slot,
254
				this.equipSound,
255
				this.assetId,
256
				this.cameraOverlay,
257
				this.allowedEntities,
258
				this.dispensable,
259
				this.swappable,
260
				this.damageOnHurt,
261
				this.equipOnInteract,
262
				this.canBeSheared,
263
				this.shearingSound
264
			);
265
		}
266
	}
267
}
268

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