| 1 | package net.minecraft.world.entity.vehicle.boat;
|
| 2 |
|
| 3 | import java.util.function.Supplier;
|
| 4 | import net.minecraft.core.NonNullList;
|
| 5 | import net.minecraft.resources.ResourceKey;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.world.Containers;
|
| 8 | import net.minecraft.world.InteractionHand;
|
| 9 | import net.minecraft.world.InteractionResult;
|
| 10 | import net.minecraft.world.damagesource.DamageSource;
|
| 11 | import net.minecraft.world.entity.ContainerUser;
|
| 12 | import net.minecraft.world.entity.Entity;
|
| 13 | import net.minecraft.world.entity.EntityType;
|
| 14 | import net.minecraft.world.entity.HasCustomInventoryScreen;
|
| 15 | import net.minecraft.world.entity.SlotAccess;
|
| 16 | import net.minecraft.world.entity.monster.piglin.PiglinAi;
|
| 17 | import net.minecraft.world.entity.player.Inventory;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.entity.vehicle.ContainerEntity;
|
| 20 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 21 | import net.minecraft.world.inventory.ChestMenu;
|
| 22 | import net.minecraft.world.item.Item;
|
| 23 | import net.minecraft.world.item.ItemStack;
|
| 24 | import net.minecraft.world.level.Level;
|
| 25 | import net.minecraft.world.level.gameevent.GameEvent;
|
| 26 | import net.minecraft.world.level.storage.ValueInput;
|
| 27 | import net.minecraft.world.level.storage.ValueOutput;
|
| 28 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 29 | import net.minecraft.world.phys.Vec3;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 |
|
| 32 | public abstract class AbstractChestBoat extends AbstractBoat implements HasCustomInventoryScreen, ContainerEntity {
|
| 33 | private static final int CONTAINER_SIZE = 27;
|
| 34 | private NonNullList<ItemStack> itemStacks = NonNullList.withSize(27, ItemStack.EMPTY);
|
| 35 | private @Nullable ResourceKey<LootTable> lootTable;
|
| 36 | private long lootTableSeed;
|
| 37 |
|
| 38 | public AbstractChestBoat(final EntityType<? extends AbstractChestBoat> type, final Level level, final Supplier<Item> dropItem) {
|
| 39 | super(type, level, dropItem);
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | protected float getSinglePassengerXOffset() {
|
| 44 | return 0.15F;
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | protected int getMaxPassengers() {
|
| 49 | return 1;
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | protected void addAdditionalSaveData(final ValueOutput output) {
|
| 54 | super.addAdditionalSaveData(output);
|
| 55 | this.addChestVehicleSaveData(output);
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | protected void readAdditionalSaveData(final ValueInput input) {
|
| 60 | super.readAdditionalSaveData(input);
|
| 61 | this.readChestVehicleSaveData(input);
|
| 62 | }
|
| 63 |
|
| 64 | @Override
|
| 65 | public void destroy(final ServerLevel level, final DamageSource source) {
|
| 66 | this.destroy(level, this.getDropItem());
|
| 67 | this.chestVehicleDestroyed(source, level, this);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public void remove(final Entity.RemovalReason reason) {
|
| 72 | if (!this.level().isClientSide() && reason.shouldDestroy()) {
|
| 73 | Containers.dropContents(this.level(), this, this);
|
| 74 | }
|
| 75 |
|
| 76 | super.remove(reason);
|
| 77 | }
|
| 78 |
|
| 79 | @Override
|
| 80 | public InteractionResult interact(final Player player, final InteractionHand hand, final Vec3 location) {
|
| 81 | InteractionResult superInteraction = super.interact(player, hand, location);
|
| 82 | if (superInteraction != InteractionResult.PASS) {
|
| 83 | return superInteraction;
|
| 84 | }
|
| 85 |
|
| 86 | if (this.canAddPassenger(player) && !player.isSecondaryUseActive()) {
|
| 87 | return InteractionResult.PASS;
|
| 88 | }
|
| 89 |
|
| 90 | InteractionResult result = this.interactWithContainerVehicle(player);
|
| 91 | if (result.consumesAction() && player.level() instanceof ServerLevel serverLevel) {
|
| 92 | this.gameEvent(GameEvent.CONTAINER_OPEN, player);
|
| 93 | PiglinAi.angerNearbyPiglins(serverLevel, player, true);
|
| 94 | }
|
| 95 |
|
| 96 | return result;
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public void openCustomInventoryScreen(final Player player) {
|
| 101 | player.openMenu(this);
|
| 102 | if (player.level() instanceof ServerLevel level) {
|
| 103 | this.gameEvent(GameEvent.CONTAINER_OPEN, player);
|
| 104 | PiglinAi.angerNearbyPiglins(level, player, true);
|
| 105 | }
|
| 106 | }
|
| 107 |
|
| 108 | @Override
|
| 109 | public void clearContent() {
|
| 110 | this.clearChestVehicleContent();
|
| 111 | }
|
| 112 |
|
| 113 | @Override
|
| 114 | public int getContainerSize() {
|
| 115 | return 27;
|
| 116 | }
|
| 117 |
|
| 118 | @Override
|
| 119 | public ItemStack getItem(final int slot) {
|
| 120 | return this.getChestVehicleItem(slot);
|
| 121 | }
|
| 122 |
|
| 123 | @Override
|
| 124 | public ItemStack removeItem(final int slot, final int count) {
|
| 125 | return this.removeChestVehicleItem(slot, count);
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public ItemStack removeItemNoUpdate(final int slot) {
|
| 130 | return this.removeChestVehicleItemNoUpdate(slot);
|
| 131 | }
|
| 132 |
|
| 133 | @Override
|
| 134 | public void setItem(final int slot, final ItemStack itemStack) {
|
| 135 | this.setChestVehicleItem(slot, itemStack);
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public SlotAccess getSlot(final int slot) {
|
| 140 | return this.getChestVehicleSlot(slot);
|
| 141 | }
|
| 142 |
|
| 143 | @Override
|
| 144 | public void setChanged() {
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public boolean stillValid(final Player player) {
|
| 149 | return this.isChestVehicleStillValid(player);
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public @Nullable AbstractContainerMenu createMenu(final int containerId, final Inventory inventory, final Player player) {
|
| 154 | if (this.lootTable != null && player.isSpectator()) {
|
| 155 | return null;
|
| 156 | }
|
| 157 |
|
| 158 | this.unpackLootTable(inventory.player);
|
| 159 | return ChestMenu.threeRows(containerId, inventory, this);
|
| 160 | }
|
| 161 |
|
| 162 | public void unpackLootTable(final @Nullable Player player) {
|
| 163 | this.unpackChestVehicleLootTable(player);
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public @Nullable ResourceKey<LootTable> getContainerLootTable() {
|
| 168 | return this.lootTable;
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | public void setContainerLootTable(final @Nullable ResourceKey<LootTable> lootTable) {
|
| 173 | this.lootTable = lootTable;
|
| 174 | }
|
| 175 |
|
| 176 | @Override
|
| 177 | public long getContainerLootTableSeed() {
|
| 178 | return this.lootTableSeed;
|
| 179 | }
|
| 180 |
|
| 181 | @Override
|
| 182 | public void setContainerLootTableSeed(final long lootTableSeed) {
|
| 183 | this.lootTableSeed = lootTableSeed;
|
| 184 | }
|
| 185 |
|
| 186 | @Override
|
| 187 | public NonNullList<ItemStack> getItemStacks() {
|
| 188 | return this.itemStacks;
|
| 189 | }
|
| 190 |
|
| 191 | @Override
|
| 192 | public void clearItemStacks() {
|
| 193 | this.itemStacks = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
|
| 194 | }
|
| 195 |
|
| 196 | @Override
|
| 197 | public void stopOpen(final ContainerUser containerUser) {
|
| 198 | this.level().gameEvent(GameEvent.CONTAINER_CLOSE, this.position(), GameEvent.Context.of(containerUser.getLivingEntity()));
|
| 199 | }
|
| 200 | }
|
| 201 |
|