Jump to content

[1.16.4] Passing cart entity down to container constructor


than00ber1

Recommended Posts

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.

Link to comment
Share on other sites

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