Guest User

Untitled

a guest
Oct 21st, 2021
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.89 KB | None | 0 0
  1. //Container class
  2. package com.ewanfox.mod.blocks.container;
  3.  
  4. import com.ewanfox.mod.setup.Registration;
  5. import net.minecraft.core.BlockPos;
  6. import net.minecraft.world.entity.player.Inventory;
  7. import net.minecraft.world.entity.player.Player;
  8. import net.minecraft.world.inventory.AbstractContainerMenu;
  9. import net.minecraft.world.inventory.ContainerLevelAccess;
  10. import net.minecraft.world.inventory.Slot;
  11. import net.minecraft.world.item.ItemStack;
  12. import net.minecraft.world.level.Level;
  13. import net.minecraft.world.level.block.entity.BlockEntity;
  14. import net.minecraftforge.items.IItemHandler;
  15. import net.minecraftforge.items.SlotItemHandler;
  16. import net.minecraftforge.items.wrapper.InvWrapper;
  17.  
  18. public class RunePedestalContainer extends AbstractContainerMenu {
  19.     private BlockEntity blockEntity;
  20.     private Player playerEntity;
  21.     private IItemHandler playerInventory;
  22.  
  23.     public RunePedestalContainer(int windowId, Level world, BlockPos pos, Inventory playerInventory, Player player) {
  24.         super(Registration.RUNEPEDESTAL_CONTAINER.get(), windowId);
  25.         blockEntity = world.getBlockEntity(pos);
  26.         this.playerEntity = player;
  27.         this.playerInventory = new InvWrapper(playerInventory);
  28.  
  29.  
  30.     }
  31.     @Override
  32.     public boolean stillValid(Player p_38874_) {
  33.         return stillValid(ContainerLevelAccess.create(blockEntity.getLevel(), blockEntity.getBlockPos()), playerEntity, Registration.RUNEPEDESTAL.get());
  34.     }
  35.     @Override
  36.     public ItemStack quickMoveStack(Player playerIn, int index) {
  37.         ItemStack itemstack = ItemStack.EMPTY;
  38.         Slot slot = this.slots.get(index);
  39.         if (slot != null && slot.hasItem()) {
  40.             ItemStack stack = slot.getItem();
  41.             itemstack = stack.copy();
  42.             if (index == 0) {
  43.                 if (!this.moveItemStackTo(stack, 1, 37, true)) {
  44.                     return ItemStack.EMPTY;
  45.                 }
  46.                 slot.onQuickCraft(stack, itemstack);
  47.             } else {
  48.  
  49.             }
  50.  
  51.             if (stack.isEmpty()) {
  52.                 slot.set(ItemStack.EMPTY);
  53.             } else {
  54.                 slot.setChanged();
  55.             }
  56.  
  57.             if (stack.getCount() == itemstack.getCount()) {
  58.                 return ItemStack.EMPTY;
  59.             }
  60.  
  61.             slot.onTake(playerIn, stack);
  62.         }
  63.  
  64.         return itemstack;
  65.     }
  66.  
  67.  
  68.  
  69.     private int addSlotRange(IItemHandler handler, int index, int x, int y, int amount, int dx) {
  70.         for (int i = 0 ; i < amount ; i++) {
  71.             addSlot(new SlotItemHandler(handler, index, x, y));
  72.             x += dx;
  73.             index++;
  74.         }
  75.         return index;
  76.     }
  77.  
  78.     private int addSlotBox(IItemHandler handler, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) {
  79.         for (int j = 0 ; j < verAmount ; j++) {
  80.             index = addSlotRange(handler, index, x, y, horAmount, dx);
  81.             y += dy;
  82.         }
  83.         return index;
  84.     }
  85.     private void layoutPlayerInventorySlots(int leftCol, int topRow) {
  86.         // Player inventory
  87.         addSlotBox(playerInventory, 9, leftCol, topRow, 9, 18, 3, 18);
  88.         addSlotBox(playerInventory,9,leftCol,topRow,9,20,4,20);
  89.  
  90.         // Hotbar
  91.         topRow += 58;
  92.         addSlotRange(playerInventory, 0, leftCol, topRow, 9, 18);
  93.     }
  94. }
  95. //Snippet in block code
  96.  @Override
  97.     public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit) {
  98.         if (handIn != InteractionHand.MAIN_HAND) return     InteractionResult.FAIL;
  99.         RunePedestalEntity altar = (RunePedestalEntity) world.getBlockEntity(pos);
  100.         if (altar == null) return InteractionResult.FAIL;
  101.         Direction direction = hit.getDirection();
  102.         if (direction == Direction.DOWN) return InteractionResult.FAIL;
  103.         ItemStack handStack = player.getItemInHand(handIn);
  104.         if (!world.isClientSide) {
  105.             BlockEntity blockEntity = world.getBlockEntity(pos);
  106.             if (blockEntity instanceof RunePedestalEntity) {
  107.                 MenuProvider containerProvider = new MenuProvider() {
  108.                     @Override
  109.                     public Component getDisplayName() {
  110.                         return new TranslatableComponent("ewanmod.rune_pedestal.inv");
  111.                     }
  112.  
  113.                     @Override
  114.                     public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player playerEntity) {
  115.                         return new RunePedestalContainer(windowId, world, pos, playerInventory, playerEntity);
  116.                     }
  117.                 };
  118.                 NetworkHooks.openGui((ServerPlayer) player, containerProvider, blockEntity.getBlockPos());
  119.             } else {
  120.                 throw new IllegalStateException("Our named container provider is missing!");
  121.             }
  122.         }
  123.         return InteractionResult.SUCCESS;
  124.     }
  125.  
  126.  
Add Comment
Please, Sign In to add comment