Jump to content

Recommended Posts

Posted (edited)

I looked at someone else's code, and was able to kind of figure it out, but I'm getting IndexOutOfBound Exceptions. 
 

public static class Factory implements IContainerFactory<CustomContainer> {

			@Override
			public CustomContainer create(int windowId, PlayerInventory playerInv, PacketBuffer extraData) {
				return new CustomContainer(windowId, getEntityFromBuf(extraData));
			}

			private CustomEntity getEntityFromBuf(PacketBuffer buf) {
				if (buf == null) {
					return null;
				}
				return DistExecutor.unsafeCallWhenOn(Dist.CLIENT, () -> () -> {
					Entity entity = Minecraft.getInstance().world.getEntityByID(buf.readVarInt());
					if (entity instanceof CustomEntity) {
						return (CustomEntity) entity;
					}
					return null;
				});
			}
		}

In case this is needed: 

public CustomContainer(int id, CustomEntity en) {
			super(RegistryHandler.Custom_CONTAINER.get(), id);
			this.en = en;
		}

And this is the stack trace

[m[1;31m[20:13:06] [Render thread/FATAL] [minecraft/ThreadTaskExecutor]: Error executing task on Client
java.lang.RuntimeException: java.lang.IndexOutOfBoundsException
	at net.minecraftforge.fml.DistExecutor.unsafeCallWhenOn(DistExecutor.java:76) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at com.david.test.entities.CustomEntity$CustomContainer$Factory.getEntityFromBuf(CustomEntity.java:212) ~[main/:?] {re:classloading}
	at com.david.test.entities.CustomEntity$CustomContainer$Factory.create(CustomEntity.java:205) ~[main/:?] {re:classloading}
	at com.david.test.entities.CustomEntity$CustomContainer$Factory.create(CustomEntity.java:1) ~[main/:?] {re:classloading}
	at net.minecraft.inventory.container.ContainerType.create(ContainerType.java:53) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraftforge.fml.network.FMLPlayMessages$OpenContainer.lambda$handle$0(FMLPlayMessages.java:285) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at java.util.Optional.ifPresent(Optional.java:183) ~[?:?] {}
	at net.minecraftforge.fml.network.FMLPlayMessages$OpenContainer.lambda$handle$1(FMLPlayMessages.java:284) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraftforge.fml.network.NetworkEvent$Context.enqueueWork(NetworkEvent.java:215) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraftforge.fml.network.FMLPlayMessages$OpenContainer.handle(FMLPlayMessages.java:282) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraftforge.fml.network.simple.IndexedMessageCodec.lambda$tryDecode$3(IndexedMessageCodec.java:128) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at java.util.Optional.ifPresent(Optional.java:183) ~[?:?] {}
	at net.minecraftforge.fml.network.simple.IndexedMessageCodec.tryDecode(IndexedMessageCodec.java:128) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraftforge.fml.network.simple.IndexedMessageCodec.consume(IndexedMessageCodec.java:162) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraftforge.fml.network.simple.SimpleChannel.networkEventListener(SimpleChannel.java:80) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraftforge.eventbus.EventBus.doCastFilter(EventBus.java:212) ~[eventbus-2.2.0-service.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.lambda$addListener$11(EventBus.java:204) ~[eventbus-2.2.0-service.jar:?] {}
	at net.minecraftforge.eventbus.EventBus.post(EventBus.java:258) ~[eventbus-2.2.0-service.jar:?] {}
	at net.minecraftforge.fml.network.NetworkInstance.dispatch(NetworkInstance.java:84) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraftforge.fml.network.NetworkHooks.lambda$onCustomPayload$1(NetworkHooks.java:73) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at java.util.Optional.map(Optional.java:265) ~[?:?] {}
	at net.minecraftforge.fml.network.NetworkHooks.onCustomPayload(NetworkHooks.java:73) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraft.client.network.play.ClientPlayNetHandler.handleCustomPayload(ClientPlayNetHandler.java:2076) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at net.minecraft.network.play.server.SCustomPayloadPlayPacket.processPacket(SCustomPayloadPlayPacket.java:68) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraft.network.play.server.SCustomPayloadPlayPacket.processPacket(SCustomPayloadPlayPacket.java:11) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraft.network.PacketThreadUtil.lambda$checkThreadAndEnqueue$0(PacketThreadUtil.java:19) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.run(ThreadTaskExecutor.java:139) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.RecursiveEventLoop.run(RecursiveEventLoop.java:22) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.driveOne(ThreadTaskExecutor.java:109) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.util.concurrent.ThreadTaskExecutor.drainTasks(ThreadTaskExecutor.java:97) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading,pl:accesstransformer:B}
	at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:959) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.Minecraft.run(Minecraft.java:587) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading,pl:accesstransformer:B,pl:runtimedistcleaner:A}
	at net.minecraft.client.main.Main.main(Main.java:184) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading,pl:runtimedistcleaner:A}
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?] {}
	at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:?] {}
	at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?] {}
	at java.lang.reflect.Method.invoke(Method.java:567) ~[?:?] {}
	at net.minecraftforge.userdev.FMLUserdevClientLaunchProvider.lambda$launchService$0(FMLUserdevClientLaunchProvider.java:55) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:37) [modlauncher-5.1.2.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:54) [modlauncher-5.1.2.jar:?] {}
	at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:72) [modlauncher-5.1.2.jar:?] {}
	at cpw.mods.modlauncher.Launcher.run(Launcher.java:81) [modlauncher-5.1.2.jar:?] {}
	at cpw.mods.modlauncher.Launcher.main(Launcher.java:65) [modlauncher-5.1.2.jar:?] {}
	at net.minecraftforge.userdev.LaunchTesting.main(LaunchTesting.java:105) [forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {}
Caused by: java.lang.IndexOutOfBoundsException
	at io.netty.buffer.EmptyByteBuf.readByte(EmptyByteBuf.java:531) ~[netty-all-4.1.25.Final.jar:4.1.25.Final] {}
	at net.minecraft.network.PacketBuffer.readByte(PacketBuffer.java:790) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at net.minecraft.network.PacketBuffer.readVarInt(PacketBuffer.java:213) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	at com.david.test.entities.CustomEntity$CustomContainer$Factory.lambda$1(CustomEntity.java:213) ~[main/:?] {re:classloading}
	at net.minecraftforge.fml.DistExecutor.unsafeCallWhenOn(DistExecutor.java:72) ~[forge-1.16.1-32.0.75_mapped_snapshot_20200514-1.16-recomp.jar:?] {re:classloading}
	... 43 more

I'm assuming its a problem with the method getting the CustomEntity from a PacketBuffer, but I have no idea what the problem is. 

Edited by DavidQF555
Posted
  • Give the code for when you open the Gui (the call to `NetworkHooks#openGui`). Your error is most likely from either not writing the data to the packet buffer, or calling the read and write methods of mismatched types (writeInt and readVarInt are two different methods with different effects).
     
  • You do not need to use DistExecutor. You already have an instance of PlayerInventory, which has a PlayerEntity in the `player` variable, which has the player's World in the `world` variable.
     
  • I suggest you inline your `getEntityFromBuf` method into the `create` method if there are no other classes calling that method, as it is unnecessary. I also suggest double-checking your code in CustomContainer, if there are places that will error if `null` is the entity passed into the constructor.
  • 10 months later...

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.