coriin

CastingTableMenu

Jan 15th, 2024
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.92 KB | None | 0 0
  1. package net.coriin.rechroma.screen;
  2.  
  3. import net.coriin.rechroma.block.ModBlocks;
  4. import net.coriin.rechroma.block.entity.custom.CastingTableBlockEntity;
  5. import net.coriin.rechroma.screen.slot.ModResultSlot;
  6. import net.minecraft.network.FriendlyByteBuf;
  7. import net.minecraft.world.entity.player.Inventory;
  8. import net.minecraft.world.entity.player.Player;
  9. import net.minecraft.world.inventory.AbstractContainerMenu;
  10. import net.minecraft.world.inventory.ContainerLevelAccess;
  11. import net.minecraft.world.inventory.MenuType;
  12. import net.minecraft.world.inventory.Slot;
  13. import net.minecraft.world.item.ItemStack;
  14. import net.minecraft.world.level.Level;
  15. import net.minecraft.world.level.block.entity.BlockEntity;
  16. import net.minecraftforge.items.CapabilityItemHandler;
  17. import net.minecraftforge.items.SlotItemHandler;
  18. import org.jetbrains.annotations.Nullable;
  19.  
  20. public class CastingTableMenu extends AbstractContainerMenu {
  21.  
  22.     private final CastingTableBlockEntity blockEntity;
  23.     private final Level level;
  24.     public CastingTableMenu(int pContainerId, Inventory inv, FriendlyByteBuf extraData) {
  25.         this(pContainerId, inv, inv.player.level.getBlockEntity(extraData.readBlockPos()));
  26.     }
  27.     public CastingTableMenu(int pContainerId, Inventory inv, BlockEntity entity) {
  28.         super(ModMenuTypes.CASTING_TABLE_MENU.get(), pContainerId);
  29.         checkContainerSize(inv, 10);
  30.         blockEntity = ((CastingTableBlockEntity) entity);
  31.         this.level = inv.player.level;
  32.  
  33.         addPlayerInventory(inv);
  34.         addPlayerHotbar(inv);
  35.  
  36.  
  37.         this.blockEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(handler -> {
  38.             this.addSlot(new SlotItemHandler(handler, 0, 62, 37));
  39.             this.addSlot(new SlotItemHandler(handler, 1, 80, 37));
  40.             this.addSlot(new SlotItemHandler(handler, 2, 98, 37));
  41.             this.addSlot(new SlotItemHandler(handler, 3, 62, 55));
  42.             this.addSlot(new SlotItemHandler(handler, 4, 80, 55));
  43.             this.addSlot(new SlotItemHandler(handler, 5, 103, 55));
  44.             this.addSlot(new SlotItemHandler(handler, 6, 62, 73));
  45.             this.addSlot(new SlotItemHandler(handler, 7, 80, 73));
  46.             this.addSlot(new SlotItemHandler(handler, 8, 98, 73));
  47.  
  48.             this.addSlot(new ModResultSlot(handler, 9, 187, 10));
  49.         });
  50.     }
  51.  
  52.  
  53.  
  54.  
  55.     private static final int HOTBAR_SLOT_COUNT = 9;
  56.     private static final int PLAYER_INVENTORY_ROW_COUNT = 3;
  57.     private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
  58.     private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
  59.     private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;
  60.     private static final int VANILLA_FIRST_SLOT_INDEX = 0;
  61.     private static final int BE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
  62.  
  63.     // THIS YOU HAVE TO DEFINE!
  64.     private static final int BE_INVENTORY_SLOT_COUNT = 10;
  65.  
  66.     @Override
  67.     public ItemStack quickMoveStack(Player playerIn, int index) {
  68.         Slot sourceSlot = slots.get(index);
  69.         if (sourceSlot == null || !sourceSlot.hasItem()) return ItemStack.EMPTY;
  70.         ItemStack sourceStack = sourceSlot.getItem();
  71.         ItemStack copyOfSourceStack = sourceStack.copy();
  72.  
  73.         if (index < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT) {
  74.  
  75.             if (!moveItemStackTo(sourceStack, BE_INVENTORY_FIRST_SLOT_INDEX, BE_INVENTORY_FIRST_SLOT_INDEX
  76.                     + BE_INVENTORY_SLOT_COUNT, false)) {
  77.                 return ItemStack.EMPTY;
  78.             }
  79.         } else if (index < BE_INVENTORY_FIRST_SLOT_INDEX + BE_INVENTORY_SLOT_COUNT) {
  80.  
  81.             if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false)) {
  82.                 return ItemStack.EMPTY;
  83.             }
  84.         } else {
  85.             System.out.println("Invalid slotIndex:" + index);
  86.             return ItemStack.EMPTY;
  87.         }
  88.  
  89.         if (sourceStack.getCount() == 0) {
  90.             sourceSlot.set(ItemStack.EMPTY);
  91.         } else {
  92.             sourceSlot.setChanged();
  93.         }
  94.         sourceSlot.onTake(playerIn, sourceStack);
  95.         return copyOfSourceStack;
  96.     }
  97.  
  98.     @Override
  99.     public boolean stillValid(Player pPlayer) {
  100.         return stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()),
  101.                 pPlayer, ModBlocks.CASTING_TABLE.get());
  102.     }
  103.  
  104.     private void addPlayerInventory(Inventory playerInventory) {
  105.         for (int i = 0; i < 3; ++i) {
  106.             for (int l = 0; l < 9; ++l) {
  107.                 this.addSlot(new Slot(playerInventory, l + i * 9 + 9, 8 + l * 18, 86 + i * 18));
  108.             }
  109.         }
  110.     }
  111.  
  112.     private void addPlayerHotbar(Inventory playerInventory) {
  113.         for (int i = 0; i < 9; ++i) {
  114.             this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 144));
  115.         }
  116.     }
  117. }
  118.  
Add Comment
Please, Sign In to add comment