Jump to content

Container size 41 is smaller than expected 45


Zaksen

Recommended Posts

I make a crate that need to have 45 but when i try open it, he not opening. in logs he sand like Container size 41 is smaller than expected 45

GoldCrate

Spoiler
package com.zaksen.fancydecorativeblocks.blocks;

import com.zaksen.fancydecorativeblocks.FancyBlockEntities;
import net.minecraft.core.BlockPos;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.BaseEntityBlock;
import net.minecraft.world.level.block.RenderShape;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.entity.BlockEntityTicker;
import net.minecraft.world.level.block.entity.BlockEntityType;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Material;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraftforge.network.NetworkHooks;
import org.jetbrains.annotations.Nullable;

import java.util.Random;

public class GoldCrate extends BaseEntityBlock {

    public GoldCrate() {
        super(Properties.of(Material.METAL).sound(SoundType.METAL).destroyTime(1f).explosionResistance(2f));
    }

    @Override
    public InteractionResult use(BlockState BState, Level Level, BlockPos BPos, Player P, InteractionHand Hand, BlockHitResult Result) {
        if(!Level.isClientSide)
        {
            BlockEntity entity = Level.getBlockEntity(BPos);
            if(entity instanceof GoldCrateEntity)
            {
                NetworkHooks.openGui(((ServerPlayer)P), (GoldCrateEntity)entity, BPos);
            }
            else
            {
                throw new IllegalStateException("Container provider is missing!");
            }
        }
        return InteractionResult.sidedSuccess(Level.isClientSide());
    }

    @Override
    public void tick(BlockState BState, ServerLevel Level, BlockPos BPos, Random Rand) {
        super.tick(BState, Level, BPos, Rand);
    }

    @Override
    public void onRemove(BlockState BState, Level Level, BlockPos BPos, BlockState BState2, boolean bool) {
        if(BState.getBlock() != BState2.getBlock())
        {
            BlockEntity blockEntity = Level.getBlockEntity(BPos);
            if(blockEntity instanceof GoldCrateEntity)
            {
                ((GoldCrateEntity) blockEntity).drops();
            }
        }
        super.onRemove(BState, Level, BPos, BState2, bool);
    }

    @Override
    public RenderShape getRenderShape(BlockState BState) {
        return RenderShape.MODEL;
    }

    @Nullable
    @Override
    public BlockEntity newBlockEntity(BlockPos BPos, BlockState BState) {
        return new GoldCrateEntity(BPos, BState);
    }

    @Nullable
    @Override
    public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level Level, BlockState BState, BlockEntityType<T> BlockEntity) {
        return createTickerHelper(BlockEntity, FancyBlockEntities.GOLD_CRATE.get(), GoldCrateEntity::tick);
    }
}

 

GoldCrateEntity
 

Spoiler
package com.zaksen.fancydecorativeblocks.blocks;

import com.zaksen.fancydecorativeblocks.FancyBlockEntities;
import com.zaksen.fancydecorativeblocks.screen.GoldCrateMenu;
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.level.block.state.BlockState;
import org.jetbrains.annotations.Nullable;

public class GoldCrateEntity extends BaseStorageEntityBlock{
    public GoldCrateEntity(BlockPos BPos, BlockState BState) {
        super(FancyBlockEntities.GOLD_CRATE.get(), BPos, BState, 45, "Gold Crate");
    }

    @Override
    public @Nullable AbstractContainerMenu createMenu(int ContainerId, Inventory Inv, Player P) {
        return new GoldCrateMenu(ContainerId, Inv, this);
    }
}

 

GoldCrateMenu
 

Spoiler
package com.zaksen.fancydecorativeblocks.screen;

import com.zaksen.fancydecorativeblocks.FancyBlocks;
import com.zaksen.fancydecorativeblocks.blocks.GoldCrateEntity;
import net.minecraft.network.FriendlyByteBuf;
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.CapabilityItemHandler;
import net.minecraftforge.items.SlotItemHandler;

public class GoldCrateMenu extends AbstractContainerMenu {
    private final GoldCrateEntity blockEntity;
    private final Level level;

    protected GoldCrateMenu(int ContrainerId, Inventory Inv, FriendlyByteBuf extraData) {
        this(ContrainerId, Inv, Inv.player.level.getBlockEntity(extraData.readBlockPos()));
    }

    public GoldCrateMenu(int ContrainerId, Inventory Inv, BlockEntity entity) {
        super(FancyMenuTypes.GOLD_CRATE_MENU.get(), ContrainerId);
        checkContainerSize(Inv,45);
        blockEntity = ((GoldCrateEntity) entity);
        this.level = Inv.player.level;

        addPlayerInventory(Inv);
        addPlayerHotbar(Inv);

        this.blockEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY).ifPresent(iItemHandler -> {
            this.addSlot(new SlotItemHandler(iItemHandler, 0, 8, 18));
            this.addSlot(new SlotItemHandler(iItemHandler, 1, 26, 18));
            this.addSlot(new SlotItemHandler(iItemHandler, 2, 44, 18));
            this.addSlot(new SlotItemHandler(iItemHandler, 3, 62, 18));
            this.addSlot(new SlotItemHandler(iItemHandler, 4, 80, 18));
            this.addSlot(new SlotItemHandler(iItemHandler, 5, 98, 18));
            this.addSlot(new SlotItemHandler(iItemHandler, 6, 116, 18));
            this.addSlot(new SlotItemHandler(iItemHandler, 7, 134, 18));
            this.addSlot(new SlotItemHandler(iItemHandler, 8, 152, 18));
            this.addSlot(new SlotItemHandler(iItemHandler, 9, 8, 36));
            this.addSlot(new SlotItemHandler(iItemHandler, 10, 26, 36));
            this.addSlot(new SlotItemHandler(iItemHandler, 11, 44, 36));
            this.addSlot(new SlotItemHandler(iItemHandler, 12, 62, 36));
            this.addSlot(new SlotItemHandler(iItemHandler, 13, 80, 36));
            this.addSlot(new SlotItemHandler(iItemHandler, 14, 98, 36));
            this.addSlot(new SlotItemHandler(iItemHandler, 15, 116, 36));
            this.addSlot(new SlotItemHandler(iItemHandler, 16, 134, 36));
            this.addSlot(new SlotItemHandler(iItemHandler, 17, 152, 36));
            this.addSlot(new SlotItemHandler(iItemHandler, 18, 8, 54));
            this.addSlot(new SlotItemHandler(iItemHandler, 19, 26, 54));
            this.addSlot(new SlotItemHandler(iItemHandler, 20, 44, 54));
            this.addSlot(new SlotItemHandler(iItemHandler, 21, 62, 54));
            this.addSlot(new SlotItemHandler(iItemHandler, 22, 80, 54));
            this.addSlot(new SlotItemHandler(iItemHandler, 23, 98, 54));
            this.addSlot(new SlotItemHandler(iItemHandler, 24, 116, 54));
            this.addSlot(new SlotItemHandler(iItemHandler, 25, 134, 54));
            this.addSlot(new SlotItemHandler(iItemHandler, 26, 152, 54));
            this.addSlot(new SlotItemHandler(iItemHandler, 27, 8, 72));
            this.addSlot(new SlotItemHandler(iItemHandler, 28, 26, 72));
            this.addSlot(new SlotItemHandler(iItemHandler, 29, 44, 72));
            this.addSlot(new SlotItemHandler(iItemHandler, 30, 62, 72));
            this.addSlot(new SlotItemHandler(iItemHandler, 31, 80, 72));
            this.addSlot(new SlotItemHandler(iItemHandler, 32, 98, 72));
            this.addSlot(new SlotItemHandler(iItemHandler, 33, 116, 72));
            this.addSlot(new SlotItemHandler(iItemHandler, 34, 134, 72));
            this.addSlot(new SlotItemHandler(iItemHandler, 35, 152, 72));
            this.addSlot(new SlotItemHandler(iItemHandler, 36, 8, 90));
            this.addSlot(new SlotItemHandler(iItemHandler, 37, 26, 90));
            this.addSlot(new SlotItemHandler(iItemHandler, 38, 44, 90));
            this.addSlot(new SlotItemHandler(iItemHandler, 39, 62, 90));
            this.addSlot(new SlotItemHandler(iItemHandler, 40, 80, 90));
            this.addSlot(new SlotItemHandler(iItemHandler, 41, 98, 90));
            this.addSlot(new SlotItemHandler(iItemHandler, 42, 116, 90));
            this.addSlot(new SlotItemHandler(iItemHandler, 43, 134, 90));
            this.addSlot(new SlotItemHandler(iItemHandler, 44, 152, 90));
        });
    }

    @Override
    public boolean stillValid(Player P) {
        return stillValid(ContainerLevelAccess.create(level, blockEntity.getBlockPos()), P, FancyBlocks.GOLD_CRATE.get());
    }

    private void addPlayerInventory(Inventory playerInv)
    {
        for(int i = 0; i < 3; ++i)
        {
            for(int l = 0; l < 9; ++l)
            {
                this.addSlot(new Slot(playerInv, l + i * 9 + 9, 8 + l * 18, 84 + i * 18));
            }
        }
    }

    private void addPlayerHotbar(Inventory playerInventory)
    {
        for(int i = 0; i < 9; ++i)
        {
            this.addSlot(new Slot(playerInventory, i, 8 + i * 18, 142));
        }
    }

    private static final int HOTBAR_SLOT_COUNT = 9;
    private static final int PLAYER_INVENTORY_ROW_COUNT = 3;
    private static final int PLAYER_INVENTORY_COLUMN_COUNT = 9;
    private static final int PLAYER_INVENTORY_SLOT_COUNT = PLAYER_INVENTORY_COLUMN_COUNT * PLAYER_INVENTORY_ROW_COUNT;
    private static final int VANILLA_SLOT_COUNT = HOTBAR_SLOT_COUNT + PLAYER_INVENTORY_SLOT_COUNT;
    private static final int VANILLA_FIRST_SLOT_INDEX = 0;
    private static final int TE_INVENTORY_FIRST_SLOT_INDEX = VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT;
    private static final int TE_INVENTORY_SLOT_COUNT = 45;

    @Override
    public ItemStack quickMoveStack(Player p, int index)
    {
        Slot sourceSlot = slots.get(index);
        if(sourceSlot == null || !sourceSlot.hasItem()) return ItemStack.EMPTY;
        ItemStack sourceStack = sourceSlot.getItem();
        ItemStack copyOfSourceStack = sourceStack.copy();

        if (index < VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT)
        {
            // This is a vanilla container slot so merge the stack into the tile inventory
            if (!moveItemStackTo(sourceStack, TE_INVENTORY_FIRST_SLOT_INDEX, TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT, false))
            {
                return ItemStack.EMPTY;  // EMPTY_ITEM
            }
        }
        else if (index < TE_INVENTORY_FIRST_SLOT_INDEX + TE_INVENTORY_SLOT_COUNT)
        {
            // This is a TE slot so merge the stack into the players inventory
            if (!moveItemStackTo(sourceStack, VANILLA_FIRST_SLOT_INDEX, VANILLA_FIRST_SLOT_INDEX + VANILLA_SLOT_COUNT, false))
            {
                return ItemStack.EMPTY;
            }
        }
        else
        {
            System.out.println("Invalid Slot Index: " + index);
            return ItemStack.EMPTY;
        }

        if(sourceStack.getCount() == 0)
        {
            sourceSlot.set(ItemStack.EMPTY);
        }
        else
        {
            sourceSlot.setChanged();
        }
        sourceSlot.onTake(p, sourceStack);
        return copyOfSourceStack;
    }
}

 

GoldCrateSreen
 

Spoiler
package com.zaksen.fancydecorativeblocks.screen;

import com.mojang.blaze3d.systems.RenderSystem;
import com.mojang.blaze3d.vertex.PoseStack;
import com.zaksen.fancydecorativeblocks.FancyDecorativeBlocks;
import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen;
import net.minecraft.client.renderer.GameRenderer;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.player.Inventory;

public class GoldCrateScreen extends AbstractContainerScreen<GoldCrateMenu> {
    private static final ResourceLocation TEXTURE = new ResourceLocation(FancyDecorativeBlocks.MOD_ID, "textures/gui/gold_crate_gui.png");

    public GoldCrateScreen(GoldCrateMenu Menu, Inventory Inv, Component Title) {
        super(Menu, Inv, Title);
    }

    @Override
    protected void renderBg(PoseStack PoseStack, float PartialTick, int MouseX, int MouseY) {
        RenderSystem.setShader(GameRenderer::getPositionTexShader);
        RenderSystem.setShaderColor(1.0f, 1.0f, 1.0f, 1.0f);
        RenderSystem.setShaderTexture(0, TEXTURE);
        imageWidth = 176;
        imageHeight = 202;
        titleLabelX = 8;
        titleLabelY = -22;
        inventoryLabelX = 8;
        inventoryLabelY = 94;
        int x = (width - imageWidth) / 2;
        int y = (height - imageHeight) / 2;

        this.blit(PoseStack, x, y, 0, 0, imageWidth, imageHeight);
    }

    @Override
    public void render(PoseStack PoseStack, int MouseX, int MouseY, float Delata)
    {
        renderBackground(PoseStack);
        super.render(PoseStack, MouseX, MouseY, Delata);
        renderTooltip(PoseStack, MouseX, MouseY);
    }
}

 

if you need there is Github Repo:
https://github.com/zaDR0tic/FancyDecorativeBlocks

Edited by Zaksen
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.