Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

[1.16.4] Passing cart entity down to container constructor


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 post
Share on other sites
17 hours ago, than00ber1 said:

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.

That would be the way to do it. Show this code.

Also, you should be using IItemHandler, not IInventory.

Link to post
Share on other sites
Posted (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 by than00ber1
question
Link to post
Share on other sites

You should just make a 2nd constructor in your container that takes in the entity directly instead of doing this song and dance of first creating a temporary buffer only for you to put one int in it, immediately read it again and then look up the entity...

4 minutes ago, than00ber1 said:

Is it ever possible for the packetbuffer to be null? 

No.

Link to post
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.

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



  • Recently Browsing

    No registered users viewing this page.

  • Posts

    • HI! I installed a map with a modpack 5 minutes ago, i put all the files in the right position, but when i open the game it crash and give me the shutdown report. ---- Minecraft Crash Report ---- // Surprise! Haha. Well, this is awkward. Time: 22/06/21 19.54 Description: There was a severe problem during mod loading that has caused the game to fail cpw.mods.fml.common.LoaderException: java.lang.NoClassDefFoundError: net/minecraftforge/fml/common/network/simpleimpl/IMessage     at cpw.mods.fml.common.LoadController.transition(LoadController.java:163)     at cpw.mods.fml.common.Loader.loadMods(Loader.java:544)     at cpw.mods.fml.client.FMLClientHandler.beginMinecraftLoading(FMLClientHandler.java:208)     at net.minecraft.client.Minecraft.func_71384_a(Minecraft.java:480)     at net.minecraft.client.Minecraft.func_99999_d(Minecraft.java:878)     at net.minecraft.client.main.Main.main(SourceFile:148)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at net.minecraft.launchwrapper.Launch.launch(Launch.java:135)     at net.minecraft.launchwrapper.Launch.main(Launch.java:28) Caused by: java.lang.NoClassDefFoundError: net/minecraftforge/fml/common/network/simpleimpl/IMessage     at java.lang.Class.forName0(Native Method)     at java.lang.Class.forName(Class.java:348)     at cpw.mods.fml.common.FMLModContainer.constructMod(FMLModContainer.java:440)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)     at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)     at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)     at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)     at com.google.common.eventbus.EventBus.post(EventBus.java:275)     at cpw.mods.fml.common.LoadController.sendEventToModContainer(LoadController.java:212)     at cpw.mods.fml.common.LoadController.propogateStateMessage(LoadController.java:190)     at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)     at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     at java.lang.reflect.Method.invoke(Method.java:497)     at com.google.common.eventbus.EventSubscriber.handleEvent(EventSubscriber.java:74)     at com.google.common.eventbus.SynchronizedEventSubscriber.handleEvent(SynchronizedEventSubscriber.java:47)     at com.google.common.eventbus.EventBus.dispatch(EventBus.java:322)     at com.google.common.eventbus.EventBus.dispatchQueuedEvents(EventBus.java:304)     at com.google.common.eventbus.EventBus.post(EventBus.java:275)     at cpw.mods.fml.common.LoadController.distributeStateMessage(LoadController.java:119)     at cpw.mods.fml.common.Loader.loadMods(Loader.java:513)     ... 10 more Caused by: java.lang.ClassNotFoundException: net.minecraftforge.fml.common.network.simpleimpl.IMessage     at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:191)     at java.lang.ClassLoader.loadClass(ClassLoader.java:424)     at java.lang.ClassLoader.loadClass(ClassLoader.java:357)     ... 35 more Caused by: java.lang.NullPointerException     at net.minecraft.launchwrapper.LaunchClassLoader.findClass(LaunchClassLoader.java:182)     ... 37 more A detailed walkthrough of the error, its code path and all known details is as follows: --------------------------------------------------------------------------------------- -- System Details -- Details:     Minecraft Version: 1.7.10     Operating System: Windows 10 (amd64) version 10.0     Java Version: 1.8.0_51, Oracle Corporation     Java VM Version: Java HotSpot(TM) 64-Bit Server VM (mixed mode), Oracle Corporation     Memory: 215593488 bytes (205 MB) / 536870912 bytes (512 MB) up to 8589934592 bytes (8192 MB)     JVM Flags: 8 total; -XX:HeapDumpPath=MojangTricksIntelDriversForPerformance_javaw.exe_minecraft.exe.heapdump -Xmx8G -XX:+UnlockExperimentalVMOptions -XX:+UseG1GC -XX:G1NewSizePercent=20 -XX:G1ReservePercent=20 -XX:MaxGCPauseMillis=50 -XX:G1HeapRegionSize=32M     AABB Pool Size: 0 (0 bytes; 0 MB) allocated, 0 (0 bytes; 0 MB) used     IntCache: cache: 0, tcache: 0, allocated: 0, tallocated: 0     FML: MCP v9.05 FML v7.10.99.99 Minecraft Forge 10.13.4.1614 53 mods loaded, 53 mods active     States: 'U' = Unloaded 'L' = Loaded 'C' = Constructed 'H' = Pre-initialized 'I' = Initialized 'J' = Post-initialized 'A' = Available 'D' = Disabled 'E' = Errored     UC    mcp{9.05} [Minecraft Coder Pack] (minecraft.jar)      UC    FML{7.10.99.99} [Forge Mod Loader] (forge-1.7.10-10.13.4.1614-1.7.10.jar)      UC    Forge{10.13.4.1614} [Minecraft Forge] (forge-1.7.10-10.13.4.1614-1.7.10.jar)      UC    battlegear2{1.7.10} [Mine & Blade Battlegear 2 - Bullseye] (1.7.10-MB_Battlegear2-Bullseye-1.0.8.3.jar)      UC    bspkrsCore{6.16} [bspkrsCore] ([1.7.10]bspkrsCore-universal-6.16.jar)      UC    StatusEffectHUD{1.27} [StatusEffectHUD] ([1.7.10]StatusEffectHUD-client-1.27.jar)      UC    cfm{3.4.8} [§9MrCrayfish's Furniture Mod] ([Minecraft-INSIDE.com] mrcrayfishfurnituremodv3.4.81.7.10.jar)      UC    ArchimedesShips{1.7.10 v1.7.1} [Archimedes' Ships] (ArchimedesShips-1.7.1.jar)      UC    Backpack{2.0.1} [Backpack] (backpack-2.0.1-1.7.x.jar)      UC    BiblioCraft{1.11.4} [BiblioCraft] (BiblioCraft[v1.11.4][MC1.7.10].jar)      UC    BiomesOPlenty{2.1.0} [Biomes O' Plenty] (BiomesOPlenty-1.7.10-2.1.0.1889-universal.jar)      UC    CarpentersBlocks{3.3.5} [Carpenter's Blocks] (Carpenter's Blocks v3.3.5 - MC 1.7.10.jar)      UC    OblisgrMod{1.2.1} [OblisgrMod] (clothing.zip)      UC    CustomSpawner{3.3.0} [DrZhark's CustomSpawner] (CustomMobSpawner 3)      UC    customnpcs{1.7.10d} [CustomNpcs] (CustomNPCs_1.7.10d(21feb16).jar)      UC    DamageIndicatorsMod{3.2.0} [Damage Indicators] (Damage-Indicators-Mod-1.7.10.jar)      UE    PTRModelLib{1.0.0} [PTRModelLib] (Decocraft-2.4.2_1.7.10.jar)      UE    props{2.4.2} [Decocraft] (Decocraft-2.4.2_1.7.10.jar)      UC    MoCreatures{6.3.1} [DrZhark's Mo'Creatures Mod] (DrZharks MoCreatures Mod v6)      UC    eplus{3.0.2-d} [Enchanting Plus] (EnchantingPlus-1.7.10-3.0.2-d.jar)      UC    exoticbirds{1.0} [Exotic Birds] (Exotic Birds 1.7.10 - 1.0.3b.jar)      UC    flansmod{4.10.0} [Flan's Mod] (Flans Mod-1.7.10-4.10.0.jar)      UC    fossil{1.7.10 Build 7.2.1} [Fossils and Archeology Revival] (Fossils_Archeology_Revival-1.7.10_Build-7.2.1.jar)      UC    journeymap{5.1.4p2} [JourneyMap] (journeymap-1.7.10-5.1.4p2-unlimited.jar)      UC    lycanitesmobs{1.13.0.5 - MC 1.7.10} [Lycanites Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    arcticmobs{1.13.0.5 - MC 1.7.10} [Lycanites Arctic Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    demonmobs{1.13.0.5 - MC 1.7.10} [Lycanites Demon Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    desertmobs{1.13.0.5 - MC 1.7.10} [Lycanites Desert Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    forestmobs{1.13.0.5 - MC 1.7.10} [Lycanites Forest Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    freshwatermobs{1.13.0.5 - MC 1.7.10} [Lycanites Freshwater Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    infernomobs{1.13.0.5 - MC 1.7.10} [Lycanites Inferno Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    junglemobs{1.13.0.5 - MC 1.7.10} [Lycanites Jungle Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    mountainmobs{1.13.0.5 - MC 1.7.10} [Lycanites Mountain Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    plainsmobs{1.13.0.5 - MC 1.7.10} [Lycanites Plains Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    saltwatermobs{1.13.0.5 - MC 1.7.10} [Lycanites Saltwater Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    shadowmobs{1.13.0.5 - MC 1.7.10} [Lycanites Shadow Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    swampmobs{1.13.0.5 - MC 1.7.10} [Lycanites Swamp Mobs] (LycanitesMobsComplete 1.13.0.5 [1.7.10].jar)      UC    MapWriter{2.1.8} [MapWriter] (MapWriter-1.7.10-2.1.10.jar)      UC    RadixCore{2.1.1} [RadixCore] (RadixCore-1.7.10-2.1.1-universal.jar)      UC    MCA{1.7.10-5.1.2} [Minecraft Comes Alive] (MCA-1.7.10-5.1.2-universal.jar)      UC    mcheli{1.0.3} [MC Helicopter] (mcheli)      UC    MiniBosses{1.2a} [Mini-Bosses] (MiniBosses-1.2a.jar)      UC    modularpots{1.7.10.14} [Modular Flower Pots] (ModularPots-1.7.10.14-2.jar)      UC    mw{1.11.7.7} [Modern Warfare] (mw-1.11.7.7_mc1.7.10.jar)      UC    plantmegapack{4.33} [Plant Mega Pack] (plantmegapack-4.33-1.7.10-1492.jar)      UC    potioncore{@VERSION@} [Potion Core] (PotionCore-1.2_beta_for_1.7.10.jar)      UC    roadstuff{1.0.3-final} [Road Stuff] (RoadStuff-mc1.7.10-1.0.2-final.jar)      UC    row{1.7.10-15.01.18-2} [Rails of War] (RoW-1.7.10-15.01.18-2.jar)      UC    soundfilters{0.8_for_1.7.X} [Sound Filters] (SoundFilters-0.8_for_1.7.X.jar)      UC    Waila{1.5.10} [Waila] (Waila-1.5.10_1.7.10.jar)      UC    weaponmod{v1.14.3} [Balkon's WeaponMod] (weaponmod-1.14.3.jar)      UC    witchery{0.24.1} [Witchery] (witchery-1.7.10-0.24.1.jar)      UC    worldedit{6.1.1} [WorldEdit] (worldedit-forge-mc1.7.10-6.1.1-dist.jar)      GL info: ' Vendor: 'NVIDIA Corporation' Version: '4.6.0 NVIDIA 457.51' Renderer: 'GeForce RTX 3070/PCIe/SSE2'
    • so i tried to join my friends minecraft server and i got this message: Internal Exception: java.lang.InexOutOfBoundsException: readerindex(42) + length(1) exceeds writerIndex(42):PooledUnsafeDirectByteBuf(ridx: 42, widx: 42, cap: 42) debug.log here: https://gist.github.com/odasety/385dd969b2677f42b726d12b7d3bc78f please help
    • No, that's strange, but it has only one state. Moreover, even if I wanted to compare block (not blockstate), i don't know how to do it, because this function require BlockState
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.