Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

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.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.