| 1 | package net.minecraft.world.entity.vehicle.minecart;
|
| 2 |
|
| 3 | import net.minecraft.core.NonNullList;
|
| 4 | import net.minecraft.resources.ResourceKey;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.world.Containers;
|
| 7 | import net.minecraft.world.InteractionHand;
|
| 8 | import net.minecraft.world.InteractionResult;
|
| 9 | import net.minecraft.world.damagesource.DamageSource;
|
| 10 | import net.minecraft.world.entity.Entity;
|
| 11 | import net.minecraft.world.entity.EntityType;
|
| 12 | import net.minecraft.world.entity.SlotAccess;
|
| 13 | import net.minecraft.world.entity.player.Inventory;
|
| 14 | import net.minecraft.world.entity.player.Player;
|
| 15 | import net.minecraft.world.entity.vehicle.ContainerEntity;
|
| 16 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 17 | import net.minecraft.world.item.ItemStack;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.storage.ValueInput;
|
| 20 | import net.minecraft.world.level.storage.ValueOutput;
|
| 21 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 22 | import net.minecraft.world.phys.Vec3;
|
| 23 | import org.jspecify.annotations.Nullable;
|
| 24 |
|
| 25 | public abstract class AbstractMinecartContainer extends AbstractMinecart implements ContainerEntity {
|
| 26 | private NonNullList<ItemStack> itemStacks = NonNullList.withSize(36, ItemStack.EMPTY);
|
| 27 | private @Nullable ResourceKey<LootTable> lootTable;
|
| 28 | private long lootTableSeed;
|
| 29 |
|
| 30 | protected AbstractMinecartContainer(final EntityType<?> type, final Level level) {
|
| 31 | super(type, level);
|
| 32 | }
|
| 33 |
|
| 34 | @Override
|
| 35 | public void destroy(final ServerLevel level, final DamageSource source) {
|
| 36 | super.destroy(level, source);
|
| 37 | this.chestVehicleDestroyed(source, level, this);
|
| 38 | }
|
| 39 |
|
| 40 | @Override
|
| 41 | public ItemStack getItem(final int slot) {
|
| 42 | return this.getChestVehicleItem(slot);
|
| 43 | }
|
| 44 |
|
| 45 | @Override
|
| 46 | public ItemStack removeItem(final int slot, final int count) {
|
| 47 | return this.removeChestVehicleItem(slot, count);
|
| 48 | }
|
| 49 |
|
| 50 | @Override
|
| 51 | public ItemStack removeItemNoUpdate(final int slot) {
|
| 52 | return this.removeChestVehicleItemNoUpdate(slot);
|
| 53 | }
|
| 54 |
|
| 55 | @Override
|
| 56 | public void setItem(final int slot, final ItemStack itemStack) {
|
| 57 | this.setChestVehicleItem(slot, itemStack);
|
| 58 | }
|
| 59 |
|
| 60 | @Override
|
| 61 | public SlotAccess getSlot(final int slot) {
|
| 62 | return this.getChestVehicleSlot(slot);
|
| 63 | }
|
| 64 |
|
| 65 | @Override
|
| 66 | public void setChanged() {
|
| 67 | }
|
| 68 |
|
| 69 | @Override
|
| 70 | public boolean stillValid(final Player player) {
|
| 71 | return this.isChestVehicleStillValid(player);
|
| 72 | }
|
| 73 |
|
| 74 | @Override
|
| 75 | public void remove(final Entity.RemovalReason reason) {
|
| 76 | if (!this.level().isClientSide() && reason.shouldDestroy()) {
|
| 77 | Containers.dropContents(this.level(), this, this);
|
| 78 | }
|
| 79 |
|
| 80 | super.remove(reason);
|
| 81 | }
|
| 82 |
|
| 83 | @Override
|
| 84 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 85 | super.addAdditionalSaveData(output);
|
| 86 | this.addChestVehicleSaveData(output);
|
| 87 | }
|
| 88 |
|
| 89 | @Override
|
| 90 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 91 | super.readAdditionalSaveData(input);
|
| 92 | this.readChestVehicleSaveData(input);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 97 | return this.interactWithContainerVehicle(player);
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | protected Vec3 applyNaturalSlowdown(final Vec3 deltaMovement) {
|
| 102 | float keep = 0.98F;
|
| 103 | if (this.lootTable == null) {
|
| 104 | int emptiness = 15 - AbstractContainerMenu.getRedstoneSignalFromContainer(this);
|
| 105 | keep += emptiness * 0.001F;
|
| 106 | }
|
| 107 |
|
| 108 | if (this.isInWater()) {
|
| 109 | keep *= 0.95F;
|
| 110 | }
|
| 111 |
|
| 112 | return deltaMovement.multiply(keep, 0.0, keep);
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | public void clearContent() {
|
| 117 | this.clearChestVehicleContent();
|
| 118 | }
|
| 119 |
|
| 120 | public void setLootTable(final ResourceKey<LootTable> lootTable, final long seed) {
|
| 121 | this.lootTable = lootTable;
|
| 122 | this.lootTableSeed = seed;
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public @Nullable AbstractContainerMenu createMenu(final int containerId, final Inventory inventory, final Player player) {
|
| 127 | if (this.lootTable != null && player.isSpectator()) {
|
| 128 | return null;
|
| 129 | }
|
| 130 |
|
| 131 | this.unpackChestVehicleLootTable(inventory.player);
|
| 132 | return this.createMenu(containerId, inventory);
|
| 133 | }
|
| 134 |
|
| 135 | protected abstract AbstractContainerMenu createMenu(final int containerId, final Inventory inventory);
|
| 136 |
|
| 137 | @Override
|
| 138 | public @Nullable ResourceKey<LootTable> getContainerLootTable() {
|
| 139 | return this.lootTable;
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | public void setContainerLootTable(final @Nullable ResourceKey<LootTable> lootTable) {
|
| 144 | this.lootTable = lootTable;
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public long getContainerLootTableSeed() {
|
| 149 | return this.lootTableSeed;
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public void setContainerLootTableSeed(final long lootTableSeed) {
|
| 154 | this.lootTableSeed = lootTableSeed;
|
| 155 | }
|
| 156 |
|
| 157 | @Override
|
| 158 | public NonNullList<ItemStack> getItemStacks() {
|
| 159 | return this.itemStacks;
|
| 160 | }
|
| 161 |
|
| 162 | @Override
|
| 163 | public void clearItemStacks() {
|
| 164 | this.itemStacks = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
|
| 165 | }
|
| 166 | }
|
| 167 |
|