Jump to content

[1.16.5] Inventory not accepting items from hopper


65_7a

Recommended Posts

Hi, I've created a new custom inventory and the inventory doesn't seem to accept items from a hopper.

I have used this as an example: https://github.com/TheGreyGhost/MinecraftByExample/tree/master/src/main/java/minecraftbyexample/mbe30_inventory_basic

I've looked at Minecraft's ChestTileEntity.java and ChestContainer.java but I still can't figure it out.

 

ExampleChestTileEntity.java:

public class ExampleChestTileEntity extends TileEntity implements INamedContainerProvider {
    public static final int NUMBER_OF_SLOTS = 9;

    private final ExampleChestContents exampleChestContents;

    public ExampleChestTileEntity() {
        super(TileEntityTypeInit.EXAMPLE_CHEST.get());
        exampleChestContents = ExampleChestContents.createForTileEntity(NUMBER_OF_SLOTS, this::canPlayerAccessInventory, this::setChanged);
        exampleChestContents.setOpenInventoryNotificationLambda(() -> playSound(SoundEvents.CHEST_OPEN));
        exampleChestContents.setCloseInventoryNotificationLambda(() -> playSound(SoundEvents.CHEST_CLOSE));
    }

    public boolean canPlayerAccessInventory(PlayerEntity player) {
        if (this.level.getBlockEntity(this.getBlockPos()) != this) return false; // Check if tileentity is still there
        final double X_CENTRE_OFFSET = 0.5;
        final double Y_CENTRE_OFFSET = 0.5;
        final double Z_CENTRE_OFFSET = 0.5;
        final double MAXIMUM_DISTANCE_SQ = 8.0 * 8.0;
        return player.distanceToSqr(
                getBlockPos().getX() + X_CENTRE_OFFSET,
                getBlockPos().getY() + Y_CENTRE_OFFSET,
                getBlockPos().getZ() + Z_CENTRE_OFFSET
        ) < MAXIMUM_DISTANCE_SQ; // Check if player isn't too far away
    }

    private static final String INVENTORY_NBT = "contents";

    @Override
    public CompoundNBT save(CompoundNBT parentNBTTagCompound) {
        super.save(parentNBTTagCompound); // Save position
        CompoundNBT inventoryNBT = exampleChestContents.serializeNBT();
        parentNBTTagCompound.put(INVENTORY_NBT, inventoryNBT); // Save chest contents
        return parentNBTTagCompound;
    }

    @Override
    public void load(BlockState blockState, CompoundNBT parentNBTTagCompound) {
        super.load(blockState, parentNBTTagCompound); // Load position
        CompoundNBT inventoryNBT = parentNBTTagCompound.getCompound(INVENTORY_NBT);
        exampleChestContents.deserializeNBT(inventoryNBT); // Load chest contents
        if (exampleChestContents.getContainerSize() != NUMBER_OF_SLOTS)
            throw new IllegalArgumentException("Corrupted NBT: Number of inventory slots did not match expected.");
    }

    @Nullable
    @Override
    public SUpdateTileEntityPacket getUpdatePacket() {
        CompoundNBT nbtTagCompound = new CompoundNBT();
        save(nbtTagCompound);
        int tileEntityType = 42;  // arbitrary number
        return new SUpdateTileEntityPacket(this.getBlockPos(), tileEntityType, nbtTagCompound);
    }

    @Override
    public void onDataPacket(NetworkManager net, SUpdateTileEntityPacket packet) {
        BlockState blockState = level.getBlockState(getBlockPos());
        load(blockState, packet.getTag());
    }

    @Override
    public CompoundNBT getUpdateTag() {
        CompoundNBT nbtTagCompound = new CompoundNBT();
        save(nbtTagCompound);
        return nbtTagCompound;
    }

    @Override
    public void handleUpdateTag(BlockState state, CompoundNBT tag) {
        this.load(state, tag);
    }

    public void dropAllContents(World world, BlockPos blockPos) {
        InventoryHelper.dropContents(world, blockPos, exampleChestContents);
    }

    @Override
    public ITextComponent getDisplayName() {
        return new TranslationTextComponent("container.callumsmod.example_chest");
    }

    private void playSound(SoundEvent soundEvent) {
        double x = (double) this.worldPosition.getX() + 0.5D;
        double y = (double) this.worldPosition.getY() + 0.5D;
        double z = (double) this.worldPosition.getZ() + 0.5D;

        this.level.playSound(null, x, y, z, soundEvent, SoundCategory.BLOCKS, 0.5F, this.level.random.nextFloat() * 0.1F + 0.9F);
    }

    @Nullable
    @Override
    public Container createMenu(int windowID, PlayerInventory playerInventory, PlayerEntity playerEntity) {
        return ExampleChestContainer.createContainerServerSide(windowID, playerInventory, exampleChestContents);
    }
}

 

Thanks.

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.

Announcements



×
×
  • Create New...

Important Information

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