than00ber1 Posted June 11, 2021 Posted June 11, 2021 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. Quote
than00ber1 Posted June 12, 2021 Author Posted June 12, 2021 (edited) 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 June 12, 2021 by than00ber1 question Quote
Recommended Posts
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.