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

I am trying to pass down my cart entity instance down to my container. I've tried a few different things which worked but never with the correct constructor paramaters (with my entity class).

Heres how register my container,

    public static final RegistryObject<ContainerType<ContainerModuleCartMiner>> MINER_CART_CONTAINER = CONTAINERS.register("miner_module_cart",
            () -> new ContainerType(ContainerModuleCartMiner::new));

and call it like this within my entity class:

    // Entity class implements `IInventory` and `INamedContainerProvider`

	// server side?
    @Override
    public ActionResultType processInitialInteract(PlayerEntity player, Hand hand) {
        if (!player.world.isRemote()) NetworkHooks.openGui((ServerPlayerEntity) player, this);
        return ActionResultType.SUCCESS;
    }
	
	// client side?
    @Override
    public Container createMenu(int id, PlayerInventory inventory, PlayerEntity player) {
        return new ContainerModuleCartMiner(id, playerInventory, this);
    }

And my container looks like this:

    // constr. 1 
	public ContainerModuleCartMiner(int id, PlayerInventory playerInventory) {
        this(id, playerInventory, new Inventory(0));
    }

	// constr. 2
    public ContainerModuleCartMiner(int id, PlayerInventory playerInventory, IInventory inventory) {
        super(ContainerRegistry.MINER_CART_CONTAINER.get(), id);

		if (inventory instanceof EntityModuleCartMiner)
			System.out.println(playerInventory.player.world.isRemote) // outputs: false, client only

        this.loadPlayerInventory(playerInventory, ScreenModuleCartMiner.SIZE_X, ScreenModuleCartMiner.SIZE_Y);
    }

The first constructor gets called on the server side which subsequently calls the second constructor with an empty inventory, only on the client side does the second constructor get called (from my understanding).

Checking whether `inventory` is an instance of my entity only works in the client and not on the server.

Is there a way to call the same constructor on both the client/server side with params `int`, `PlayerInventory` and `Entity` instead of `IInventory`directly?

 

Also, I've tried calling the constructor with `int`, `PlayerInventory` and `PacketBuffer` and pass int the Entity's id along and retreive it on the other end with `world.getEntityById(buffer.readInt())` but it always seem to be null.

  • Author

Alright it works now, heres what I did to make it work.

On the entity's side I call open the container like this:

    @Override
    public ActionResultType processInitialInteract(PlayerEntity player, Hand hand) {

        if (!player.world.isRemote()) {
            NetworkHooks.openGui((ServerPlayerEntity) player, this, b -> b.writeInt(this.getEntityId()));
        }

        return ActionResultType.SUCCESS;
    }

    @Override
    public Container createMenu(int id, PlayerInventory inventory, PlayerEntity player) {
        PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
        buffer.writeInt(this.getEntityId());
        return new ContainerModuleCartMiner(id, playerInventory, buffer);
    }

Where I assign the entity's id to a buffer and in the container:

    public ContainerModuleCartMiner(int id, PlayerInventory playerInventory, PacketBuffer buffer) {
        super(ContainerRegistry.MINER_CART_CONTAINER.get(), id);

        Entity entity = playerInventory.player.world.getEntityByID(buffer.readInt());

        if (entity instanceof EntityModuleCartMiner)
            System.out.println(playerInventory.player.world.isRemote()); // outputs both 'ture' & 'false', meaning both client and server managed to get the entity from the buffer int
    }

 

Edit: Is it ever possible for the packetbuffer to be null? 

Edited by than00ber1
question

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.