Code/net/minecraft/world/inventory/HorseInventoryMenu.java

113 lines
5.0 KiB
Java
Raw Permalink Normal View History

2025-07-01 06:20:03 +00:00
package net.minecraft.world.inventory;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.EntityTypeTags;
import net.minecraft.world.Container;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.animal.horse.AbstractHorse;
import net.minecraft.world.entity.animal.horse.Llama;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.ItemStack;
public class HorseInventoryMenu extends AbstractContainerMenu {
private static final ResourceLocation SADDLE_SLOT_SPRITE = ResourceLocation.withDefaultNamespace("container/slot/saddle");
private static final ResourceLocation LLAMA_ARMOR_SLOT_SPRITE = ResourceLocation.withDefaultNamespace("container/slot/llama_armor");
private static final ResourceLocation ARMOR_SLOT_SPRITE = ResourceLocation.withDefaultNamespace("container/slot/horse_armor");
private final Container horseContainer;
private final AbstractHorse horse;
private static final int SLOT_SADDLE = 0;
private static final int SLOT_BODY_ARMOR = 1;
private static final int SLOT_HORSE_INVENTORY_START = 2;
public HorseInventoryMenu(int p_39656_, Inventory p_39657_, Container p_39658_, final AbstractHorse p_39659_, int p_342974_) {
super(null, p_39656_);
this.horseContainer = p_39658_;
this.horse = p_39659_;
p_39658_.startOpen(p_39657_.player);
Container container = p_39659_.createEquipmentSlotContainer(EquipmentSlot.SADDLE);
this.addSlot(new ArmorSlot(container, p_39659_, EquipmentSlot.SADDLE, 0, 8, 18, SADDLE_SLOT_SPRITE) {
@Override
public boolean isActive() {
return p_39659_.canUseSlot(EquipmentSlot.SADDLE) && p_39659_.getType().is(EntityTypeTags.CAN_EQUIP_SADDLE);
}
});
final boolean flag = p_39659_ instanceof Llama;
ResourceLocation resourcelocation = flag ? LLAMA_ARMOR_SLOT_SPRITE : ARMOR_SLOT_SPRITE;
Container container1 = p_39659_.createEquipmentSlotContainer(EquipmentSlot.BODY);
this.addSlot(new ArmorSlot(container1, p_39659_, EquipmentSlot.BODY, 0, 8, 36, resourcelocation) {
@Override
public boolean isActive() {
return p_39659_.canUseSlot(EquipmentSlot.BODY) && (p_39659_.getType().is(EntityTypeTags.CAN_WEAR_HORSE_ARMOR) || flag);
}
});
if (p_342974_ > 0) {
for (int i = 0; i < 3; i++) {
for (int j = 0; j < p_342974_; j++) {
this.addSlot(new Slot(p_39658_, j + i * p_342974_, 80 + j * 18, 18 + i * 18));
}
}
}
this.addStandardInventorySlots(p_39657_, 8, 84);
}
@Override
public boolean stillValid(Player p_39661_) {
return !this.horse.hasInventoryChanged(this.horseContainer) && this.horseContainer.stillValid(p_39661_) && this.horse.isAlive() && p_39661_.canInteractWithEntity(this.horse, 4.0);
}
@Override
public ItemStack quickMoveStack(Player p_39665_, int p_39666_) {
ItemStack itemstack = ItemStack.EMPTY;
Slot slot = this.slots.get(p_39666_);
if (slot != null && slot.hasItem()) {
ItemStack itemstack1 = slot.getItem();
itemstack = itemstack1.copy();
int i = 2 + this.horseContainer.getContainerSize();
if (p_39666_ < i) {
if (!this.moveItemStackTo(itemstack1, i, this.slots.size(), true)) {
return ItemStack.EMPTY;
}
} else if (this.getSlot(1).mayPlace(itemstack1) && !this.getSlot(1).hasItem()) {
if (!this.moveItemStackTo(itemstack1, 1, 2, false)) {
return ItemStack.EMPTY;
}
} else if (this.getSlot(0).mayPlace(itemstack1) && !this.getSlot(0).hasItem()) {
if (!this.moveItemStackTo(itemstack1, 0, 1, false)) {
return ItemStack.EMPTY;
}
} else if (this.horseContainer.getContainerSize() == 0 || !this.moveItemStackTo(itemstack1, 2, i, false)) {
int j = i + 27;
int k = j + 9;
if (p_39666_ >= j && p_39666_ < k) {
if (!this.moveItemStackTo(itemstack1, i, j, false)) {
return ItemStack.EMPTY;
}
} else if (p_39666_ >= i && p_39666_ < j) {
if (!this.moveItemStackTo(itemstack1, j, k, false)) {
return ItemStack.EMPTY;
}
} else if (!this.moveItemStackTo(itemstack1, j, j, false)) {
return ItemStack.EMPTY;
}
return ItemStack.EMPTY;
}
if (itemstack1.isEmpty()) {
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
}
return itemstack;
}
@Override
public void removed(Player p_39663_) {
super.removed(p_39663_);
this.horseContainer.stopOpen(p_39663_);
}
}