Jump to content

[Solved] [1.13.2] Open GUI in Block#onBlockActivated


Recommended Posts

Posted (edited)

I am trying to make a GUI open when my block is clicked. My current code for Block#onBlockActivated is as such:

@Override
public boolean onBlockActivated(IBlockState state, World world, BlockPos pos, EntityPlayer player, EnumHand hand, EnumFacing side, float hitX, float hitY, float hitZ) {
	if (!world.isRemote() && player instanceof EntityPlayerMP) {
		TileEntity tileEntity = world.getTileEntity(pos);
		if (tileEntity instanceof TileEntityMobFarm) {
			NetworkHooks.openGui((EntityPlayerMP) player, new InteractionObjectMobFarm((TileEntityMobFarm) tileEntity));
		}
	}
	return true;
}

 

I also registered the GUI as such:

ModLoadingContext.get().registerExtensionPoint(ExtensionPoint.GUIFACTORY, () -> {
	return (openContainer) -> {
		ResourceLocation location = openContainer.getId();
		if (location.toString().equals(Reference.MOD_ID + ":mob_farm_gui")) {
			EntityPlayerSP player = Minecraft.getInstance().player;
			BlockPos pos = openContainer.getAdditionalData().readBlockPos(); // This line gives the error.
			TileEntity tileEntity = player.world.getTileEntity(pos);
			if (tileEntity instanceof TileEntityMobFarm) {
				return new GuiMobFarm(player.inventory, (TileEntityMobFarm) tileEntity);
			}
		}
		return null;
	};
});

 

However, it seems that the openContainer.getAdditionalData PacketBuffer does not contain the BlockPos data of the block that is clicked by the player, as calling the readBlockPos method gives the following error:

java.lang.IndexOutOfBoundsException
        at io.netty.buffer.EmptyByteBuf.readLong(EmptyByteBuf.java:601)
        at net.minecraft.network.PacketBuffer.readLong(PacketBuffer.java:749)
        at net.minecraft.network.PacketBuffer.readBlockPos(PacketBuffer.java:135)
        at cn.davidma.tinymobfarm.common.TinyMobFarm.lambda$null$0(TinyMobFarm.java:59)
        at net.minecraftforge.fml.network.FMLPlayMessages$OpenContainer.lambda$null$0(FMLPlayMessages.java:193)
        at java.util.Optional.map(Optional.java:215)
        at net.minecraftforge.fml.network.FMLPlayMessages$OpenContainer.lambda$null$2(FMLPlayMessages.java:193)
        at java.util.Optional.ifPresent(Optional.java:159)
        at net.minecraftforge.fml.network.FMLPlayMessages$OpenContainer.lambda$handle$3(FMLPlayMessages.java:192)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at net.minecraft.client.Minecraft.addScheduledTask(Minecraft.java:1851)
        at net.minecraft.client.Minecraft.addScheduledTask(Minecraft.java:1864)
        at net.minecraftforge.fml.network.NetworkEvent$Context.enqueueWork(NetworkEvent.java:176)
        at net.minecraftforge.fml.network.FMLPlayMessages$OpenContainer.handle(FMLPlayMessages.java:192)
        at net.minecraftforge.fml.network.simple.IndexedMessageCodec.lambda$tryDecode$3(IndexedMessageCodec.java:114)
        at java.util.Optional.ifPresent(Optional.java:159)
        at net.minecraftforge.fml.network.simple.IndexedMessageCodec.tryDecode(IndexedMessageCodec.java:114)
        at net.minecraftforge.fml.network.simple.IndexedMessageCodec.consume(IndexedMessageCodec.java:147)
        at net.minecraftforge.fml.network.simple.SimpleChannel.networkEventListener(SimpleChannel.java:64)
        at java.util.stream.ForEachOps$ForEachOp$OfRef.accept(ForEachOps.java:184)
        at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:175)
        at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
        at java.util.stream.Streams$StreamBuilderImpl.forEachRemaining(Streams.java:419)
        at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
        at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
        at java.util.stream.ForEachOps$ForEachOp.evaluateSequential(ForEachOps.java:151)
        at java.util.stream.ForEachOps$ForEachOp$OfRef.evaluateSequential(ForEachOps.java:174)
        at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
        at java.util.stream.ReferencePipeline.forEach(ReferencePipeline.java:418)
        at net.minecraftforge.eventbus.EventBus.lambda$addListener$11(EventBus.java:201)
        at net.minecraftforge.eventbus.EventBus.post(EventBus.java:257)
        at net.minecraftforge.fml.network.NetworkInstance.dispatch(NetworkInstance.java:82)
        at net.minecraftforge.fml.network.NetworkHooks.lambda$onCustomPayload$0(NetworkHooks.java:75)
        at java.util.Optional.map(Optional.java:215)
        at net.minecraftforge.fml.network.NetworkHooks.onCustomPayload(NetworkHooks.java:75)
        at net.minecraft.client.network.NetHandlerPlayClient.handleCustomPayload(NetHandlerPlayClient.java:1619)
        at net.minecraft.network.play.server.SPacketCustomPayload.processPacket(SPacketCustomPayload.java:50)
        at net.minecraft.network.play.server.SPacketCustomPayload.processPacket(SPacketCustomPayload.java:11)
        at net.minecraft.network.PacketThreadUtil.func_210405_a(SourceFile:10)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
        at java.util.concurrent.FutureTask.run(FutureTask.java:266)
        at net.minecraft.util.Util.runTask(SourceFile:199)
        at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:761)
        at net.minecraft.client.Minecraft.run(Minecraft.java:358)
        at net.minecraft.client.main.Main.main(SourceFile:144)
        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:498)
        at net.minecraftforge.userdev.FMLUserdevClientLaunchProvider.lambda$launchService$0(FMLUserdevClientLaunchProvider.java:55)
        at cpw.mods.modlauncher.LaunchServiceHandlerDecorator.launch(LaunchServiceHandlerDecorator.java:19)
        at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:35)
        at cpw.mods.modlauncher.LaunchServiceHandler.launch(LaunchServiceHandler.java:53)
        at cpw.mods.modlauncher.Launcher.run(Launcher.java:58)
        at cpw.mods.modlauncher.Launcher.main(Launcher.java:44)
        at net.minecraftforge.userdev.UserdevLauncher.main(UserdevLauncher.java:77)

 

How would I get the BlockPos of the block from the OpenContainer inside the lambda?

 

If needed, my code is at: https://github.com/davidmaamoaix/TinyMobFarm/tree/1.13.2

Edited by DavidM

Some tips:

  Reveal hidden contents

 

Posted (edited)

NetworkHooks#openGui() should take three arguments, and you're only passing two.  The third argument is a PacketBuffer, where you send any extra data you need on the client side, including in your case the tile entity position.  E.g.:

NetworkHooks.openGui((EntityPlayerMP) player, yourContainerProvider, buf -> buf.writeBlock(tePos));

Edit: third parameter is now a Consumer<PacketBuffer> as @loordgekpointed out.  That changed in the last 24 hours, time to update :)

Edited by desht
  • Like 1
Posted (edited)

update forge, it got changed

    public static void openGui(EntityPlayerMP player, IInteractionObject containerSupplier, Consumer<PacketBuffer> extraDataWriter)

 

Edited by loordgek
  • Like 1
  • Thanks 1
Posted (edited)

Thanks everyone!

Edited by DavidM

Some tips:

  Reveal hidden contents

 

Posted (edited)
  On 2/19/2019 at 10:45 AM, desht said:

Edit: third parameter is now a Consumer<PacketBuffer> as @loordgekpointed out.  That changed in the last 24 hours, time to update :)

Expand  

Just realized that the method changed in the update.

How would I get a Consumer<PacketBuffer> from a PacketBuffer? From a look of the source code, I assume it requires a method that writes to another PacketBuffer, something like PacketBuffer#writeBytes?

 

Fixed. Thanks for all of your help.

Edited by DavidM

Some tips:

  Reveal hidden contents

 

Posted
  On 2/19/2019 at 11:45 AM, DavidM said:

Just realized that the method changed in the update.

How would I get a Consumer<PacketBuffer> from a PacketBuffer? From a look of the source code, I assume it requires a method that writes to another PacketBuffer, something like PacketBuffer#writeBytes?

 Thanks.

Expand  

 

Instead of constructing the PacketBuffer, writing to it and then calling openGui, simply call openGui with a lambda function that writes to the supplied PacketBuffer. Forge constructs the PacketBuffer for you.

  • Like 1

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

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.