Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Container class
- package com.ewanfox.mod.blocks.container;
- import com.ewanfox.mod.setup.Registration;
- import net.minecraft.core.BlockPos;
- import net.minecraft.world.entity.player.Inventory;
- import net.minecraft.world.entity.player.Player;
- import net.minecraft.world.inventory.AbstractContainerMenu;
- import net.minecraft.world.inventory.ContainerLevelAccess;
- import net.minecraft.world.inventory.Slot;
- import net.minecraft.world.item.ItemStack;
- import net.minecraft.world.level.Level;
- import net.minecraft.world.level.block.entity.BlockEntity;
- import net.minecraftforge.items.IItemHandler;
- import net.minecraftforge.items.SlotItemHandler;
- import net.minecraftforge.items.wrapper.InvWrapper;
- public class RunePedestalContainer extends AbstractContainerMenu {
- private BlockEntity blockEntity;
- private Player playerEntity;
- private IItemHandler playerInventory;
- public RunePedestalContainer(int windowId, Level world, BlockPos pos, Inventory playerInventory, Player player) {
- super(Registration.RUNEPEDESTAL_CONTAINER.get(), windowId);
- blockEntity = world.getBlockEntity(pos);
- this.playerEntity = player;
- this.playerInventory = new InvWrapper(playerInventory);
- }
- @Override
- public boolean stillValid(Player p_38874_) {
- return stillValid(ContainerLevelAccess.create(blockEntity.getLevel(), blockEntity.getBlockPos()), playerEntity, Registration.RUNEPEDESTAL.get());
- }
- @Override
- public ItemStack quickMoveStack(Player playerIn, int index) {
- ItemStack itemstack = ItemStack.EMPTY;
- Slot slot = this.slots.get(index);
- if (slot != null && slot.hasItem()) {
- ItemStack stack = slot.getItem();
- itemstack = stack.copy();
- if (index == 0) {
- if (!this.moveItemStackTo(stack, 1, 37, true)) {
- return ItemStack.EMPTY;
- }
- slot.onQuickCraft(stack, itemstack);
- } else {
- }
- if (stack.isEmpty()) {
- slot.set(ItemStack.EMPTY);
- } else {
- slot.setChanged();
- }
- if (stack.getCount() == itemstack.getCount()) {
- return ItemStack.EMPTY;
- }
- slot.onTake(playerIn, stack);
- }
- return itemstack;
- }
- private int addSlotRange(IItemHandler handler, int index, int x, int y, int amount, int dx) {
- for (int i = 0 ; i < amount ; i++) {
- addSlot(new SlotItemHandler(handler, index, x, y));
- x += dx;
- index++;
- }
- return index;
- }
- private int addSlotBox(IItemHandler handler, int index, int x, int y, int horAmount, int dx, int verAmount, int dy) {
- for (int j = 0 ; j < verAmount ; j++) {
- index = addSlotRange(handler, index, x, y, horAmount, dx);
- y += dy;
- }
- return index;
- }
- private void layoutPlayerInventorySlots(int leftCol, int topRow) {
- // Player inventory
- addSlotBox(playerInventory, 9, leftCol, topRow, 9, 18, 3, 18);
- addSlotBox(playerInventory,9,leftCol,topRow,9,20,4,20);
- // Hotbar
- topRow += 58;
- addSlotRange(playerInventory, 0, leftCol, topRow, 9, 18);
- }
- }
- //Snippet in block code
- @Override
- public InteractionResult use(BlockState state, Level world, BlockPos pos, Player player, InteractionHand handIn, BlockHitResult hit) {
- if (handIn != InteractionHand.MAIN_HAND) return InteractionResult.FAIL;
- RunePedestalEntity altar = (RunePedestalEntity) world.getBlockEntity(pos);
- if (altar == null) return InteractionResult.FAIL;
- Direction direction = hit.getDirection();
- if (direction == Direction.DOWN) return InteractionResult.FAIL;
- ItemStack handStack = player.getItemInHand(handIn);
- if (!world.isClientSide) {
- BlockEntity blockEntity = world.getBlockEntity(pos);
- if (blockEntity instanceof RunePedestalEntity) {
- MenuProvider containerProvider = new MenuProvider() {
- @Override
- public Component getDisplayName() {
- return new TranslatableComponent("ewanmod.rune_pedestal.inv");
- }
- @Override
- public AbstractContainerMenu createMenu(int windowId, Inventory playerInventory, Player playerEntity) {
- return new RunePedestalContainer(windowId, world, pos, playerInventory, playerEntity);
- }
- };
- NetworkHooks.openGui((ServerPlayer) player, containerProvider, blockEntity.getBlockPos());
- } else {
- throw new IllegalStateException("Our named container provider is missing!");
- }
- }
- return InteractionResult.SUCCESS;
- }
Add Comment
Please, Sign In to add comment