package net.minecraft.world.entity.vehicle; import net.minecraft.core.NonNullList; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceKey; import net.minecraft.server.level.ServerLevel; import net.minecraft.world.Containers; import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionResult; import net.minecraft.world.damagesource.DamageSource; import net.minecraft.world.entity.Entity; import net.minecraft.world.entity.EntityType; import net.minecraft.world.entity.SlotAccess; import net.minecraft.world.entity.player.Inventory; import net.minecraft.world.entity.player.Player; import net.minecraft.world.inventory.AbstractContainerMenu; import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.storage.loot.LootTable; import net.minecraft.world.phys.Vec3; import org.jetbrains.annotations.Nullable; public abstract class AbstractMinecartContainer extends AbstractMinecart implements ContainerEntity { private NonNullList itemStacks = NonNullList.withSize(36, ItemStack.EMPTY); @Nullable private ResourceKey lootTable; private long lootTableSeed; protected AbstractMinecartContainer(EntityType entityType, Level level) { super(entityType, level); } @Override public void destroy(ServerLevel level, DamageSource damageSource) { super.destroy(level, damageSource); this.chestVehicleDestroyed(damageSource, level, this); } @Override public ItemStack getItem(int slot) { return this.getChestVehicleItem(slot); } @Override public ItemStack removeItem(int slot, int amount) { return this.removeChestVehicleItem(slot, amount); } @Override public ItemStack removeItemNoUpdate(int slot) { return this.removeChestVehicleItemNoUpdate(slot); } @Override public void setItem(int slot, ItemStack stack) { this.setChestVehicleItem(slot, stack); } @Override public SlotAccess getSlot(int slot) { return this.getChestVehicleSlot(slot); } @Override public void setChanged() { } @Override public boolean stillValid(Player player) { return this.isChestVehicleStillValid(player); } @Override public void remove(Entity.RemovalReason reason) { if (!this.level().isClientSide && reason.shouldDestroy()) { Containers.dropContents(this.level(), this, this); } super.remove(reason); } @Override protected void addAdditionalSaveData(CompoundTag tag) { super.addAdditionalSaveData(tag); this.addChestVehicleSaveData(tag, this.registryAccess()); } @Override protected void readAdditionalSaveData(CompoundTag tag) { super.readAdditionalSaveData(tag); this.readChestVehicleSaveData(tag, this.registryAccess()); } @Override public InteractionResult interact(Player player, InteractionHand hand) { return this.interactWithContainerVehicle(player); } @Override protected Vec3 applyNaturalSlowdown(Vec3 speed) { float f = 0.98F; if (this.lootTable == null) { int i = 15 - AbstractContainerMenu.getRedstoneSignalFromContainer(this); f += i * 0.001F; } if (this.isInWater()) { f *= 0.95F; } return speed.multiply(f, 0.0, f); } @Override public void clearContent() { this.clearChestVehicleContent(); } public void setLootTable(ResourceKey lootTable, long seed) { this.lootTable = lootTable; this.lootTableSeed = seed; } @Nullable @Override public AbstractContainerMenu createMenu(int i, Inventory inventory, Player player) { if (this.lootTable != null && player.isSpectator()) { return null; } else { this.unpackChestVehicleLootTable(inventory.player); return this.createMenu(i, inventory); } } protected abstract AbstractContainerMenu createMenu(int containerId, Inventory playerInventory); @Nullable @Override public ResourceKey getContainerLootTable() { return this.lootTable; } @Override public void setContainerLootTable(@Nullable ResourceKey lootTable) { this.lootTable = lootTable; } @Override public long getContainerLootTableSeed() { return this.lootTableSeed; } @Override public void setContainerLootTableSeed(long lootTableSeed) { this.lootTableSeed = lootTableSeed; } @Override public NonNullList getItemStacks() { return this.itemStacks; } @Override public void clearItemStacks() { this.itemStacks = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY); } }