| 1 | package net.minecraft.world.level.block.entity;
|
| 2 |
|
| 3 | import com.google.common.annotations.VisibleForTesting;
|
| 4 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 5 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 6 | import net.minecraft.core.BlockPos;
|
| 7 | import net.minecraft.core.NonNullList;
|
| 8 | import net.minecraft.network.chat.Component;
|
| 9 | import net.minecraft.world.Container;
|
| 10 | import net.minecraft.world.ContainerHelper;
|
| 11 | import net.minecraft.world.entity.player.Inventory;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.entity.player.StackedItemContents;
|
| 14 | import net.minecraft.world.inventory.AbstractContainerMenu;
|
| 15 | import net.minecraft.world.inventory.ContainerData;
|
| 16 | import net.minecraft.world.inventory.CrafterMenu;
|
| 17 | import net.minecraft.world.inventory.CraftingContainer;
|
| 18 | import net.minecraft.world.item.ItemStack;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 | import net.minecraft.world.level.block.CrafterBlock;
|
| 21 | import net.minecraft.world.level.block.state.BlockState;
|
| 22 | import net.minecraft.world.level.storage.ValueInput;
|
| 23 | import net.minecraft.world.level.storage.ValueOutput;
|
| 24 |
|
| 25 | public class CrafterBlockEntity extends RandomizableContainerBlockEntity implements CraftingContainer {
|
| 26 | public static final int CONTAINER_WIDTH = 3;
|
| 27 | public static final int CONTAINER_HEIGHT = 3;
|
| 28 | public static final int CONTAINER_SIZE = 9;
|
| 29 | public static final int SLOT_DISABLED = 1;
|
| 30 | public static final int SLOT_ENABLED = 0;
|
| 31 | public static final int DATA_TRIGGERED = 9;
|
| 32 | public static final int NUM_DATA = 10;
|
| 33 | private static final int DEFAULT_CRAFTING_TICKS_REMAINING = 0;
|
| 34 | private static final int DEFAULT_TRIGGERED = 0;
|
| 35 | private static final Component DEFAULT_NAME = Component.translatable("container.crafter");
|
| 36 | private NonNullList<ItemStack> items = NonNullList.withSize(9, ItemStack.EMPTY);
|
| 37 | private int craftingTicksRemaining = 0;
|
| 38 | protected final ContainerData containerData = new ContainerData() {
|
| 39 | private final int[] slotStates = new int[9];
|
| 40 | private int triggered = 0;
|
| 41 |
|
| 42 | @Override
|
| 43 | public int get(final int dataId) {
|
| 44 | return dataId == 9 ? this.triggered : this.slotStates[dataId];
|
| 45 | }
|
| 46 |
|
| 47 | @Override
|
| 48 | public void set(final int dataId, final int value) {
|
| 49 | if (dataId == 9) {
|
| 50 | this.triggered = value;
|
| 51 | } else {
|
| 52 | this.slotStates[dataId] = value;
|
| 53 | }
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public int getCount() {
|
| 58 | return 10;
|
| 59 | }
|
| 60 | };
|
| 61 |
|
| 62 | public CrafterBlockEntity(final BlockPos worldPosition, final BlockState blockState) {
|
| 63 | super(BlockEntityTypes.CRAFTER, worldPosition, blockState);
|
| 64 | }
|
| 65 |
|
| 66 | @Override
|
| 67 | protected Component getDefaultName() {
|
| 68 | return DEFAULT_NAME;
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | protected AbstractContainerMenu createMenu(final int containerId, final Inventory inventory) {
|
| 73 | return new CrafterMenu(containerId, inventory, this, this.containerData);
|
| 74 | }
|
| 75 |
|
| 76 | public void setSlotState(final int slotId, final boolean enabled) {
|
| 77 | if (this.slotCanBeDisabled(slotId)) {
|
| 78 | this.containerData.set(slotId, enabled ? 0 : 1);
|
| 79 | this.setChanged();
|
| 80 | }
|
| 81 | }
|
| 82 |
|
| 83 | public boolean isSlotDisabled(final int slotId) {
|
| 84 | return slotId >= 0 && slotId < 9 ? this.containerData.get(slotId) == 1 : false;
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public boolean canPlaceItem(final int slot, final ItemStack itemStack) {
|
| 89 | if (this.containerData.get(slot) == 1) {
|
| 90 | return false;
|
| 91 | } else {
|
| 92 | ItemStack slotStack = this.items.get(slot);
|
| 93 | int currentStackSize = slotStack.getCount();
|
| 94 | if (currentStackSize >= slotStack.getMaxStackSize()) {
|
| 95 | return false;
|
| 96 | } else {
|
| 97 | return slotStack.isEmpty() ? true : !this.smallerStackExist(currentStackSize, slotStack, slot);
|
| 98 | }
|
| 99 | }
|
| 100 | }
|
| 101 |
|
| 102 | private boolean smallerStackExist(final int baseSize, final ItemStack baseItem, final int baseSlot) {
|
| 103 | for (int i = baseSlot + 1; i < 9; i++) {
|
| 104 | if (!this.isSlotDisabled(i)) {
|
| 105 | ItemStack slotStack = this.getItem(i);
|
| 106 | if (slotStack.isEmpty() || slotStack.getCount() < baseSize && ItemStack.isSameItemSameComponents(slotStack, baseItem)) {
|
| 107 | return true;
|
| 108 | }
|
| 109 | }
|
| 110 | }
|
| 111 |
|
| 112 | return false;
|
| 113 | }
|
| 114 |
|
| 115 | @Override
|
| 116 | protected void loadAdditional(final ValueInput input) {
|
| 117 | super.loadAdditional(input);
|
| 118 | this.craftingTicksRemaining = input.getIntOr("crafting_ticks_remaining", 0);
|
| 119 | this.items = NonNullList.withSize(this.getContainerSize(), ItemStack.EMPTY);
|
| 120 | if (!this.tryLoadLootTable(input)) {
|
| 121 | ContainerHelper.loadAllItems(input, this.items);
|
| 122 | }
|
| 123 |
|
| 124 | for (int i = 0; i < 9; i++) {
|
| 125 | this.containerData.set(i, 0);
|
| 126 | }
|
| 127 |
|
| 128 | input.getIntArray("disabled_slots").ifPresent(disabledSlots -> {
|
| 129 | for (int ix : disabledSlots) {
|
| 130 | if (this.slotCanBeDisabled(ix)) {
|
| 131 | this.containerData.set(ix, 1);
|
| 132 | }
|
| 133 | }
|
| 134 | });
|
| 135 | this.containerData.set(9, input.getIntOr("triggered", 0));
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | protected void saveAdditional(final ValueOutput output) {
|
| 140 | super.saveAdditional(output);
|
| 141 | output.putInt("crafting_ticks_remaining", this.craftingTicksRemaining);
|
| 142 | if (!this.trySaveLootTable(output)) {
|
| 143 | ContainerHelper.saveAllItems(output, this.items);
|
| 144 | }
|
| 145 |
|
| 146 | this.addDisabledSlots(output);
|
| 147 | this.addTriggered(output);
|
| 148 | }
|
| 149 |
|
| 150 | @Override
|
| 151 | public int getContainerSize() {
|
| 152 | return 9;
|
| 153 | }
|
| 154 |
|
| 155 | @Override
|
| 156 | public boolean isEmpty() {
|
| 157 | for (ItemStack is : this.items) {
|
| 158 | if (!is.isEmpty()) {
|
| 159 | return false;
|
| 160 | }
|
| 161 | }
|
| 162 |
|
| 163 | return true;
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public ItemStack getItem(final int slot) {
|
| 168 | return this.items.get(slot);
|
| 169 | }
|
| 170 |
|
| 171 | @Override
|
| 172 | public void setItem(final int slot, final ItemStack itemStack) {
|
| 173 | if (this.isSlotDisabled(slot)) {
|
| 174 | this.setSlotState(slot, true);
|
| 175 | }
|
| 176 |
|
| 177 | super.setItem(slot, itemStack);
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public boolean stillValid(final Player player) {
|
| 182 | return Container.stillValidBlockEntity(this, player);
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public NonNullList<ItemStack> getItems() {
|
| 187 | return this.items;
|
| 188 | }
|
| 189 |
|
| 190 | @Override
|
| 191 | protected void setItems(final NonNullList<ItemStack> items) {
|
| 192 | this.items = items;
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public int getWidth() {
|
| 197 | return 3;
|
| 198 | }
|
| 199 |
|
| 200 | @Override
|
| 201 | public int getHeight() {
|
| 202 | return 3;
|
| 203 | }
|
| 204 |
|
| 205 | @Override
|
| 206 | public void fillStackedContents(final StackedItemContents contents) {
|
| 207 | for (ItemStack itemStack : this.items) {
|
| 208 | contents.accountSimpleStack(itemStack);
|
| 209 | }
|
| 210 | }
|
| 211 |
|
| 212 | private void addDisabledSlots(final ValueOutput output) {
|
| 213 | IntList disabledSlots = new IntArrayList();
|
| 214 |
|
| 215 | for (int i = 0; i < 9; i++) {
|
| 216 | if (this.isSlotDisabled(i)) {
|
| 217 | disabledSlots.add(i);
|
| 218 | }
|
| 219 | }
|
| 220 |
|
| 221 | output.putIntArray("disabled_slots", disabledSlots.toIntArray());
|
| 222 | }
|
| 223 |
|
| 224 | private void addTriggered(final ValueOutput output) {
|
| 225 | output.putInt("triggered", this.containerData.get(9));
|
| 226 | }
|
| 227 |
|
| 228 | public void setTriggered(final boolean value) {
|
| 229 | this.containerData.set(9, value ? 1 : 0);
|
| 230 | }
|
| 231 |
|
| 232 | @VisibleForTesting
|
| 233 | public boolean isTriggered() {
|
| 234 | return this.containerData.get(9) == 1;
|
| 235 | }
|
| 236 |
|
| 237 | public static void serverTick(final Level level, final BlockPos blockPos, final BlockState blockState, final CrafterBlockEntity entity) {
|
| 238 | int craftingTicksRemaining = entity.craftingTicksRemaining - 1;
|
| 239 | if (craftingTicksRemaining >= 0) {
|
| 240 | entity.craftingTicksRemaining = craftingTicksRemaining;
|
| 241 | if (craftingTicksRemaining == 0) {
|
| 242 | level.setBlockAndUpdate(blockPos, blockState.setValue(CrafterBlock.CRAFTING, false));
|
| 243 | }
|
| 244 | }
|
| 245 | }
|
| 246 |
|
| 247 | public void setCraftingTicksRemaining(final int maxCraftingTicks) {
|
| 248 | this.craftingTicksRemaining = maxCraftingTicks;
|
| 249 | }
|
| 250 |
|
| 251 | public int getRedstoneSignal() {
|
| 252 | int count = 0;
|
| 253 |
|
| 254 | for (int i = 0; i < this.getContainerSize(); i++) {
|
| 255 | ItemStack itemStack = this.getItem(i);
|
| 256 | if (!itemStack.isEmpty() || this.isSlotDisabled(i)) {
|
| 257 | count++;
|
| 258 | }
|
| 259 | }
|
| 260 |
|
| 261 | return count;
|
| 262 | }
|
| 263 |
|
| 264 | private boolean slotCanBeDisabled(final int slotId) {
|
| 265 | return slotId > -1 && slotId < 9 && this.items.get(slotId).isEmpty();
|
| 266 | }
|
| 267 | }
|
| 268 |
|