Jump to content

[SOLVED] [1.10.2] Sending a packet from server to client to open a GUI


baegmon

Recommended Posts

I am creating my custom GUI for a quiz mod and have run into some trouble.

When I right click my block to open up the quiz GUI, it opens nicely.

However when I try to open it using a command, it does not work.

After some googling, I have found that it is required to send a packet from the server to the client to inform it to open the GUI.

 

As such, I have implemented a SimpleNetworkWrapper as found in this tutorial.

 

I initialize my network wrapper within the FMLPreInitializationEvent with:

 

network = NetworkRegistry.INSTANCE.newSimpleChannel("GUIChannel");
network.registerMessage(MyMessage.Handler.class, MyMessage.class, 0, Side.SERVER);

 

Then from my command, I send the packet:

 

@Override
public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
Main.network.sendTo(new MyMessage("ASDF"), (EntityPlayerMP) sender);

 

My Handler class attempts to open the gui:

 

public static class Handler implements IMessageHandler<MyMessage, IMessage> {

        @Override
        public IMessage onMessage(MyMessage message, MessageContext ctx) {

            EntityPlayer player = ctx.getServerHandler().playerEntity;

            player.openGui(Main.INSTANCE, GUIHandler.TUTGUID, player.getEntityWorld(), player.getPosition().getX(),
                    player.getPosition().getY(), player.getPosition().getZ());

            return null; 
        }

}

 

This does not seem to open the GUI and I'm not quite sure what I'm doing wrong.

How do I modify this to make it functional via a command? Thanks.

Link to comment
Share on other sites

Wherever you found out that a packet is needed to open a GUI that source is wrong. As long as you have a

Container

with your GUI you should in fact only call

openGui

on the server, FML will handle the networking for you.

If you do not have a container

IGuiHandler

(and thereby

openGui

) is the wrong tool. In that case you want a custom packet indeed and then just open the GUI directly (using

Minecraft::displayGuiScreen

) on the client.

 

Oh, I must've been looking up the wrong examples.

Calling Minecraft::displayGuiScreen has opened it up.

Thanks for your help.

Link to comment
Share on other sites

Sorry for bumping the thread, I thought it was working but it was only working in single player mode.

 

Wherever you found out that a packet is needed to open a GUI that source is wrong. As long as you have a

Container

with your GUI you should in fact only call

openGui

on the server, FML will handle the networking for you.

If you do not have a container

IGuiHandler

(and thereby

openGui

) is the wrong tool. In that case you want a custom packet indeed and then just open the GUI directly (using

Minecraft::displayGuiScreen

) on the client.

 

It is a container-less GUI so I'm trying to get it working by sending a custom packet from the server to the client and calling "Minecraft::displayGuiScreen" but it seems to crash the server during startup phase.

 

I've made the following changes:

 

In my message handler:

 

    public static class Handler implements IMessageHandler<OpenGUI, IMessage> {

        @Override
        public IMessage onMessage(OpenGUI message, MessageContext ctx) {

            /*
            EntityPlayerMP player = ctx.getServerHandler().playerEntity;
            World world = player.worldObj;
            if(!world.isRemote){
                Minecraft.getMinecraft().displayGuiScreen(new TutorialGUI());
            }
            */
            System.out.println("MESSAGE");


            return null;
        }
    }

 

The command to open the GUI on the client:

 

    @Override
    public void execute(MinecraftServer server, ICommandSender sender, String[] args) throws CommandException {
        if(args.length > 0){
            throw new WrongUsageException(getCommandUsage(sender));
        } else {

            World world = sender.getEntityWorld();

            if(world.isRemote){
                EntityPlayerMP player = (EntityPlayerMP) sender;
                Main.network.sendTo(new OpenGUI(), player);

            }
            
        }
    }

 

Printing a message is delivered to the client but as soon as I try to call "Minecraft::displayGuiScreen", it crashes. Is there something that I'm doing wrong?

 

Also, I noticed a previous post http://www.minecraftforge.net/forum/index.php?topic=25821.msg131607#msg131607 where it says "You need to redirect any code that contains client references through your client proxy."

 

How do I go about doing that?

 

Link to comment
Share on other sites

  • 1 year later...

HI i'm from future (1.12.2)

 

Now we can walk around client-only method problem by passing empty IMessageHandler instance at server side (CommonProxy)

I'm not sure when registerMessage started to take instance of IMessageHandler.

 

CommonProxy.java

	public void registerPacketHandler() {
		HungryAnimals.simpleChannel.registerMessage(HandlerServerDGEditInt.class, PacketServerDGEditInt.class, 0, Side.SERVER);
		HungryAnimals.simpleChannel.registerMessage(HandlerServerDGEditDouble.class, PacketServerDGEditDouble.class, 1, Side.SERVER);
		HungryAnimals.simpleChannel.registerMessage(HandlerServerDGSet.class, PacketServerDGSet.class, 2, Side.SERVER);
		HungryAnimals.simpleChannel.registerMessage((message, ctx)->{return null;}, PacketClientSpawnParticle.class, 3, Side.CLIENT);
		HungryAnimals.simpleChannel.registerMessage((message, ctx)->{return null;}, PacketClientSyncTamable.class, 4, Side.CLIENT);
		HungryAnimals.simpleChannel.registerMessage((message, ctx)->{return null;}, PacketClientSyncHungry.class, 5, Side.CLIENT);
		HungryAnimals.simpleChannel.registerMessage((message, ctx)->{return null;}, PacketClientSyncProducingFluid.class, 6, Side.CLIENT);
		HungryAnimals.simpleChannel.registerMessage((message, ctx)->{return null;}, PacketClientSyncProducingInteraction.class, 7, Side.CLIENT);
	}

 

ClientProxy.java

	@Override
	public void registerPacketHandler() {
		HungryAnimals.simpleChannel.registerMessage(HandlerServerDGEditInt.class, PacketServerDGEditInt.class, 0, Side.SERVER);
		HungryAnimals.simpleChannel.registerMessage(HandlerServerDGEditDouble.class, PacketServerDGEditDouble.class, 1, Side.SERVER);
		HungryAnimals.simpleChannel.registerMessage(HandlerServerDGSet.class, PacketServerDGSet.class, 2, Side.SERVER);
		HungryAnimals.simpleChannel.registerMessage(HandlerClientSpawnParticle.class, PacketClientSpawnParticle.class, 3, Side.CLIENT);
		HungryAnimals.simpleChannel.registerMessage(HandlerClientSyncTamable.class, PacketClientSyncTamable.class, 4, Side.CLIENT);
		HungryAnimals.simpleChannel.registerMessage(HandlerClientSyncHungry.class, PacketClientSyncHungry.class, 5, Side.CLIENT);
		HungryAnimals.simpleChannel.registerMessage(HandlerClientSyncProducingFluid.class, PacketClientSyncProducingFluid.class, 6, Side.CLIENT);
		HungryAnimals.simpleChannel.registerMessage(HandlerClientSyncProducingInteraction.class, PacketClientSyncProducingInteraction.class, 7, Side.CLIENT);
	}

 

 

 

  • Thanks 1
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.