AbstractChestedHorse .java

net.minecraft.world.entity.animal.equine.AbstractChestedHorse (class, 190 lines): members: createBaseChestedHorseAttributes, DEFAULT_HAS_CHEST, setChest.

net.minecraft.world.entity.animal.equineclass190 linescommonextends AbstractHorse6.1 KB

API Surface (15)

void randomizeAttributes(final RandomSource random)
void defineSynchedData(final SynchedEntityData.Builder entityData)
AttributeSupplier.Builder createBaseChestedHorseAttributes()
boolean hasChest()
void setChest(final boolean flag)
EntityDimensions getDefaultDimensions(final Pose pose)
void dropEquipment(final ServerLevel level)
void addAdditionalSaveData(final ValueOutput output)
void readAdditionalSaveData(final ValueInput input)
ItemStack get()
boolean set(final ItemStack itemStack)
InteractionResult mobInteract(final Player player, final InteractionHand hand)
Vec3[] getQuadLeashOffsets()
void playChestEquipsSound()
int getInventoryColumns()

Fields & Constants (3)

DATA_ID_CHEST
DEFAULT_HAS_CHEST
babyDimensions

String literals (2)

"ChestedHorse"
"Items"

Full Source

AbstractChestedHorse.java190 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.world.entity.animal.equine;
2
3
import net.minecraft.network.syncher.EntityDataAccessor;
4
import net.minecraft.network.syncher.EntityDataSerializers;
5
import net.minecraft.network.syncher.SynchedEntityData;
6
import net.minecraft.server.level.ServerLevel;
7
import net.minecraft.sounds.SoundEvents;
8
import net.minecraft.util.RandomSource;
9
import net.minecraft.world.InteractionHand;
10
import net.minecraft.world.InteractionResult;
11
import net.minecraft.world.ItemStackWithSlot;
12
import net.minecraft.world.entity.EntityAttachment;
13
import net.minecraft.world.entity.EntityAttachments;
14
import net.minecraft.world.entity.EntityDimensions;
15
import net.minecraft.world.entity.EntityType;
16
import net.minecraft.world.entity.Leashable;
17
import net.minecraft.world.entity.Pose;
18
import net.minecraft.world.entity.SlotAccess;
19
import net.minecraft.world.entity.ai.attributes.AttributeSupplier;
20
import net.minecraft.world.entity.ai.attributes.Attributes;
21
import net.minecraft.world.entity.player.Player;
22
import net.minecraft.world.item.ItemStack;
23
import net.minecraft.world.item.Items;
24
import net.minecraft.world.level.Level;
25
import net.minecraft.world.level.block.Blocks;
26
import net.minecraft.world.level.storage.ValueInput;
27
import net.minecraft.world.level.storage.ValueOutput;
28
import net.minecraft.world.phys.Vec3;
29
import org.jspecify.annotations.Nullable;
30
31
public abstract class AbstractChestedHorse extends AbstractHorse {
32
	private static final EntityDataAccessor<Boolean> DATA_ID_CHEST = SynchedEntityData.defineId(AbstractChestedHorse.class, EntityDataSerializers.BOOLEAN);
33
	private static final boolean DEFAULT_HAS_CHEST = false;
34
	private final EntityDimensions babyDimensions;
35
36
	protected AbstractChestedHorse(final EntityType<? extends AbstractChestedHorse> type, final Level level) {
37
		super(type, level);
38
		this.canGallop = false;
39
		this.babyDimensions = type.getDimensions()
40
			.withAttachments(EntityAttachments.builder().attach(EntityAttachment.PASSENGER, 0.0F, type.getHeight() + 0.03125F, -0.3125F))
41
			.scale(0.5F);
42
	}
43
44
	@Override
45
	protected void randomizeAttributes(final RandomSource random) {
46
		this.getAttribute(Attributes.MAX_HEALTH).setBaseValue(generateMaxHealth(random::nextInt));
47
	}
48
49
	@Override
50
	protected void defineSynchedData(final SynchedEntityData.Builder entityData) {
51
		super.defineSynchedData(entityData);
52
		entityData.define(DATA_ID_CHEST, false);
53
	}
54
55
	public static AttributeSupplier.Builder createBaseChestedHorseAttributes() {
56
		return createBaseHorseAttributes().add(Attributes.MOVEMENT_SPEED, 0.175F).add(Attributes.JUMP_STRENGTH, 0.5);
57
	}
58
59
	public boolean hasChest() {
60
		return this.entityData.get(DATA_ID_CHEST);
61
	}
62
63
	public void setChest(final boolean flag) {
64
		this.entityData.set(DATA_ID_CHEST, flag);
65
	}
66
67
	@Override
68
	public EntityDimensions getDefaultDimensions(final Pose pose) {
69
		return this.isBaby() ? this.babyDimensions : super.getDefaultDimensions(pose);
70
	}
71
72
	@Override
73
	protected void dropEquipment(final ServerLevel level) {
74
		super.dropEquipment(level);
75
		if (this.hasChest()) {
76
			this.spawnAtLocation(level, Blocks.CHEST);
77
			this.setChest(false);
78
		}
79
	}
80
81
	@Override
82
	protected void addAdditionalSaveData(final ValueOutput output) {
83
		super.addAdditionalSaveData(output);
84
		output.putBoolean("ChestedHorse", this.hasChest());
85
		if (this.hasChest()) {
86
			ValueOutput.TypedOutputList<ItemStackWithSlot> items = output.list("Items", ItemStackWithSlot.CODEC);
87
88
			for (int i = 0; i < this.inventory.getContainerSize(); i++) {
89
				ItemStack stack = this.inventory.getItem(i);
90
				if (!stack.isEmpty()) {
91
					items.add(new ItemStackWithSlot(i, stack));
92
				}
93
			}
94
		}
95
	}
96
97
	@Override
98
	protected void readAdditionalSaveData(final ValueInput input) {
99
		super.readAdditionalSaveData(input);
100
		this.setChest(input.getBooleanOr("ChestedHorse", false));
101
		this.createInventory();
102
		if (this.hasChest()) {
103
			for (ItemStackWithSlot item : input.listOrEmpty("Items", ItemStackWithSlot.CODEC)) {
104
				if (item.isValidInContainer(this.inventory.getContainerSize())) {
105
					this.inventory.setItem(item.slot(), item.stack());
106
				}
107
			}
108
		}
109
	}
110
111
	@Override
112
	public @Nullable SlotAccess getSlot(final int slot) {
113
		return slot == 499 ? new SlotAccess() {
114
			@Override
115
			public ItemStack get() {
116
				return AbstractChestedHorse.this.hasChest() ? new ItemStack(Items.CHEST) : ItemStack.EMPTY;
117
			}
118
119
			@Override
120
			public boolean set(final ItemStack itemStack) {
121
				if (itemStack.isEmpty()) {
122
					if (AbstractChestedHorse.this.hasChest()) {
123
						AbstractChestedHorse.this.setChest(false);
124
						AbstractChestedHorse.this.createInventory();
125
					}
126
127
					return true;
128
				} else if (itemStack.is(Items.CHEST)) {
129
					if (!AbstractChestedHorse.this.hasChest()) {
130
						AbstractChestedHorse.this.setChest(true);
131
						AbstractChestedHorse.this.createInventory();
132
					}
133
134
					return true;
135
				} else {
136
					return false;
137
				}
138
			}
139
		} : super.getSlot(slot);
140
	}
141
142
	@Override
143
	public InteractionResult mobInteract(final Player player, final InteractionHand hand) {
144
		boolean shouldOpenInventory = !this.isBaby() && this.isTamed() && player.isSecondaryUseActive();
145
		if (!this.isVehicle() && !shouldOpenInventory && (!this.isBaby() || !player.isHolding(Items.GOLDEN_DANDELION))) {
146
			ItemStack itemStack = player.getItemInHand(hand);
147
			if (!itemStack.isEmpty()) {
148
				if (this.isFood(itemStack)) {
149
					return this.fedFood(player, itemStack);
150
				}
151
152
				if (!this.isTamed()) {
153
					this.makeMad();
154
					return InteractionResult.SUCCESS;
155
				}
156
157
				if (!this.hasChest() && itemStack.is(Items.CHEST)) {
158
					this.equipChest(player, itemStack);
159
					return InteractionResult.SUCCESS;
160
				}
161
			}
162
163
			return super.mobInteract(player, hand);
164
		} else {
165
			return super.mobInteract(player, hand);
166
		}
167
	}
168
169
	private void equipChest(final Player player, final ItemStack itemStack) {
170
		this.setChest(true);
171
		this.playChestEquipsSound();
172
		itemStack.consume(1, player);
173
		this.createInventory();
174
	}
175
176
	@Override
177
	public Vec3[] getQuadLeashOffsets() {
178
		return Leashable.createQuadLeashOffsets(this, 0.04, 0.41, 0.18, 0.73);
179
	}
180
181
	protected void playChestEquipsSound() {
182
		this.playSound(SoundEvents.DONKEY_CHEST, 1.0F, (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F);
183
	}
184
185
	@Override
186
	public int getInventoryColumns() {
187
		return this.hasChest() ? 5 : 0;
188
	}
189
}
190

Decompiled Minecraft 26.3 source of AbstractChestedHorse (net/minecraft/world/entity/animal/equine/AbstractChestedHorse.java) with Mojang official mappings, stored verbatim on the site.