| 1 | package net.minecraft.world.entity.vehicle;
|
| 2 |
|
| 3 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 4 | import net.minecraft.core.NonNullList;
|
| 5 | import net.minecraft.resources.ResourceKey;
|
| 6 | import net.minecraft.server.MinecraftServer;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.server.level.ServerPlayer;
|
| 9 | import net.minecraft.world.Container;
|
| 10 | import net.minecraft.world.ContainerHelper;
|
| 11 | import net.minecraft.world.Containers;
|
| 12 | import net.minecraft.world.InteractionResult;
|
| 13 | import net.minecraft.world.MenuProvider;
|
| 14 | import net.minecraft.world.damagesource.DamageSource;
|
| 15 | import net.minecraft.world.entity.Entity;
|
| 16 | import net.minecraft.world.entity.SlotAccess;
|
| 17 | import net.minecraft.world.entity.monster.piglin.PiglinAi;
|
| 18 | import net.minecraft.world.entity.player.Player;
|
| 19 | import net.minecraft.world.item.ItemStack;
|
| 20 | import net.minecraft.world.level.Level;
|
| 21 | import net.minecraft.world.level.gamerules.GameRules;
|
| 22 | import net.minecraft.world.level.storage.ValueInput;
|
| 23 | import net.minecraft.world.level.storage.ValueOutput;
|
| 24 | import net.minecraft.world.level.storage.loot.LootParams;
|
| 25 | import net.minecraft.world.level.storage.loot.LootTable;
|
| 26 | import net.minecraft.world.level.storage.loot.parameters.LootContextParamSets;
|
| 27 | import net.minecraft.world.level.storage.loot.parameters.LootContextParams;
|
| 28 | import net.minecraft.world.phys.AABB;
|
| 29 | import net.minecraft.world.phys.Vec3;
|
| 30 | import org.jspecify.annotations.Nullable;
|
| 31 |
|
| 32 | public interface ContainerEntity extends Container, MenuProvider {
|
| 33 | Vec3 position();
|
| 34 |
|
| 35 | AABB getBoundingBox();
|
| 36 |
|
| 37 | @Nullable ResourceKey<LootTable> getContainerLootTable();
|
| 38 |
|
| 39 | void setContainerLootTable(final @Nullable ResourceKey<LootTable> lootTable);
|
| 40 |
|
| 41 | long getContainerLootTableSeed();
|
| 42 |
|
| 43 | void setContainerLootTableSeed(final long lootTableSeed);
|
| 44 |
|
| 45 | NonNullList<ItemStack> getItemStacks();
|
| 46 |
|
| 47 | void clearItemStacks();
|
| 48 |
|
| 49 | Level level();
|
| 50 |
|
| 51 | boolean isRemoved();
|
| 52 |
|
| 53 | @Override
|
| 54 | default boolean isEmpty() {
|
| 55 | return this.isChestVehicleEmpty();
|
| 56 | }
|
| 57 |
|
| 58 | default void addChestVehicleSaveData(final ValueOutput output) {
|
| 59 | if (this.getContainerLootTable() != null) {
|
| 60 | output.putString("LootTable", this.getContainerLootTable().identifier().toString());
|
| 61 | if (this.getContainerLootTableSeed() != 0L) {
|
| 62 | output.putLong("LootTableSeed", this.getContainerLootTableSeed());
|
| 63 | }
|
| 64 | } else {
|
| 65 | ContainerHelper.saveAllItems(output, this.getItemStacks());
|
| 66 | }
|
| 67 | }
|
| 68 |
|
| 69 | default void readChestVehicleSaveData(final ValueInput input) {
|
| 70 | this.clearItemStacks();
|
| 71 | ResourceKey<LootTable> lootTable = input.read("LootTable", LootTable.KEY_CODEC).orElse(null);
|
| 72 | this.setContainerLootTable(lootTable);
|
| 73 | this.setContainerLootTableSeed(input.getLongOr("LootTableSeed", 0L));
|
| 74 | if (lootTable == null) {
|
| 75 | ContainerHelper.loadAllItems(input, this.getItemStacks());
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | default void chestVehicleDestroyed(final DamageSource source, final ServerLevel level, final Entity entity) {
|
| 80 | if (level.getGameRules().get(GameRules.ENTITY_DROPS)) {
|
| 81 | Containers.dropContents(level, entity, this);
|
| 82 | if (source.getDirectEntity() instanceof Player player) {
|
| 83 | PiglinAi.angerNearbyPiglins(level, player, true);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | default InteractionResult interactWithContainerVehicle(final Player player) {
|
| 89 | player.openMenu(this);
|
| 90 | return InteractionResult.SUCCESS;
|
| 91 | }
|
| 92 |
|
| 93 | default void unpackChestVehicleLootTable(final @Nullable Player player) {
|
| 94 | MinecraftServer server = this.level().getServer();
|
| 95 | if (this.getContainerLootTable() != null && server != null) {
|
| 96 | LootTable lootTable = server.reloadableRegistries().getLootTable(this.getContainerLootTable());
|
| 97 | if (player != null) {
|
| 98 | CriteriaTriggers.GENERATE_LOOT.trigger((ServerPlayer)player, this.getContainerLootTable());
|
| 99 | }
|
| 100 |
|
| 101 | this.setContainerLootTable(null);
|
| 102 | LootParams.Builder builder = new LootParams.Builder((ServerLevel)this.level()).withParameter(LootContextParams.ORIGIN, this.position());
|
| 103 | if (player != null) {
|
| 104 | builder.withLuck(player.getLuck()).withParameter(LootContextParams.THIS_ENTITY, player);
|
| 105 | }
|
| 106 |
|
| 107 | lootTable.fill(this, builder.create(LootContextParamSets.CHEST), this.getContainerLootTableSeed());
|
| 108 | }
|
| 109 | }
|
| 110 |
|
| 111 | default void clearChestVehicleContent() {
|
| 112 | this.unpackChestVehicleLootTable(null);
|
| 113 | this.getItemStacks().clear();
|
| 114 | }
|
| 115 |
|
| 116 | default boolean isChestVehicleEmpty() {
|
| 117 | for (ItemStack itemStack : this.getItemStacks()) {
|
| 118 | if (!itemStack.isEmpty()) {
|
| 119 | return false;
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | return true;
|
| 124 | }
|
| 125 |
|
| 126 | default ItemStack removeChestVehicleItemNoUpdate(final int slot) {
|
| 127 | this.unpackChestVehicleLootTable(null);
|
| 128 | ItemStack itemStack = this.getItemStacks().get(slot);
|
| 129 | if (itemStack.isEmpty()) {
|
| 130 | return ItemStack.EMPTY;
|
| 131 | }
|
| 132 |
|
| 133 | this.getItemStacks().set(slot, ItemStack.EMPTY);
|
| 134 | return itemStack;
|
| 135 | }
|
| 136 |
|
| 137 | default ItemStack getChestVehicleItem(final int slot) {
|
| 138 | this.unpackChestVehicleLootTable(null);
|
| 139 | return this.getItemStacks().get(slot);
|
| 140 | }
|
| 141 |
|
| 142 | default ItemStack removeChestVehicleItem(final int slot, final int count) {
|
| 143 | this.unpackChestVehicleLootTable(null);
|
| 144 | return ContainerHelper.removeItem(this.getItemStacks(), slot, count);
|
| 145 | }
|
| 146 |
|
| 147 | default void setChestVehicleItem(final int slot, final ItemStack itemStack) {
|
| 148 | this.unpackChestVehicleLootTable(null);
|
| 149 | this.getItemStacks().set(slot, itemStack);
|
| 150 | itemStack.limitSize(this.getMaxStackSize(itemStack));
|
| 151 | }
|
| 152 |
|
| 153 | default @Nullable SlotAccess getChestVehicleSlot(final int slot) {
|
| 154 | return slot >= 0 && slot < this.getContainerSize() ? new SlotAccess() {
|
| 155 | @Override
|
| 156 | public ItemStack get() {
|
| 157 | return ContainerEntity.this.getChestVehicleItem(slot);
|
| 158 | }
|
| 159 |
|
| 160 | @Override
|
| 161 | public boolean set(final ItemStack itemStack) {
|
| 162 | ContainerEntity.this.setChestVehicleItem(slot, itemStack);
|
| 163 | return true;
|
| 164 | }
|
| 165 | } : null;
|
| 166 | }
|
| 167 |
|
| 168 | default boolean isChestVehicleStillValid(final Player player) {
|
| 169 | return !this.isRemoved() && player.isWithinEntityInteractionRange(this.getBoundingBox(), 4.0);
|
| 170 | }
|
| 171 | }
|
| 172 |
|