Jump to content

Opening GUI on player's client from server


twixthehero

Recommended Posts

Hello -

 

What I'm trying to do is open a GUI for the player when they login to the server.  I implement IPlayerTracker so that I can get the callback for onPlayerLogin(EntityPlayer player).  In this function, I have attempted to use player.openGui(...) but no GUI ever opens.

 

I "Ctrl+clicked" openGui in Eclipse and onward until I got to FMLNetworkHandler.openGui().  At the bottom of this method, it says:

 

if (player instanceof EntityPlayerMP)
{
   NetworkRegistry.instance().openRemoteGui(mc, (EntityPlayerMP) player, modGuiId, world, x, y, z);
}
else
{
    NetworkRegistry.instance().openLocalGui(mc, player, modGuiId, world, x, y, z);
}

 

Now, I put debug statements in both conditions to see which one was getting called, and it turned out to be .openRemoveGui().  This calls the getServerGuiElement().

 

I don't have a Container (my GUI extends GuiScreen), so I want getClientGuiElement() to be called instead.

 

Any ideas how to do this?

 

Here's how I attempt to open the GUI from my IPlayerTracker:

 

player.openGui(Ingress.instance, Ingress.GUI_TEAM_SELECT, player.worldObj, 0, 0, 0);

 

Thanks

Link to comment
Share on other sites

Do you have a GUIHandler yet? If not then write one and register it with NetworkRegistry.instance().registerGuiHandler(this, new your_GUIHandler()); in the @Mod file. This tells Forge to call your GUIHandler if the first argument in player.openGUI(mod, id, world, arg1, arg2, arg3) (I'll refer to those variables), if now the var mod is your mod then Forge sends a call to the registered GUIHandler.

The your_GUIHandler implements IGuiHanlder and would feature two mthods:

public Object getServerGuiElement(int, EntityPlayer, World, int, int, int) and

public Object getClientGuiElement(int, EntityPlayer, World, int, int, int).

If you call openGUI with the argument I mentioned then your GUIHandler would get those variables passed as following:

One call (from the server) goes to registered_GUIHandler.getServerGuiElement(id, player, world, arg1, arg2, arg3)

the other call (from the client) goes to registered_GUIHandler.getClientGuiElement(id, player, world, arg1, arg2, arg3).

Now all you have to do is switching the id. If the id is equal to the id of your GUI_TEAM_SELECT (choose any id you want as long as you keep it the same in all files, preferably an public static (final) int GUI_TEAM_SELECT_ID; in one class.) you then return null for the server (as you normally return the container, the opened GUI features), and for the client you return a new instance of your GUI. Easy as that.

 

CARE: when using buttons I can't help you much by now as I don't know how to write a PackageHandler (which you have to do) :)

 

Link to comment
Share on other sites

I do have the GUIHandler and it is registered correctly.  The problem is that within my IPlayerTracker, when I call player.openGui(), player is an instance of EntityPlayerMP so it doesn't open the GUI because it's looking for a container.

 

IPlayerTracker:

 

package twixthehero.ingress.server;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTBase;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagString;
import twixthehero.ingress.Ingress;
import cpw.mods.fml.client.FMLClientHandler;
import cpw.mods.fml.common.IPlayerTracker;

/*
* TODO
* Use this to check the server for player's selected team...
*/
public class IngressPlayerTracker implements IPlayerTracker
{
@Override
public void onPlayerLogin(EntityPlayer player)
{
	player.sendChatToPlayer("Welcome to Ingress!");

	NBTTagCompound tag = player.getEntityData();
	NBTBase modeTag = tag.getTag("Team");

	if (modeTag != null)
	{
		player.sendChatToPlayer("Welcome back to the fight for the "
				+ ((NBTTagString) modeTag).data + "!");
	}
	else
	{
		player.sendChatToPlayer("Please select your team.");
		// FMLClientHandler.instance().displayGuiScreen(player, new
		// GUITeamSelect(player));

		player.openGui(Ingress.instance, Ingress.GUI_TEAM_SELECT, player.worldObj, 0, 0, 0); //<-----------------Here
	}
}

 

GUIHandler:

 

package twixthehero.ingress;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.world.World;
import twixthehero.ingress.gui.GUIPortal;
import twixthehero.ingress.gui.GUITeamSelect;
import twixthehero.ingress.gui.PortalContainer;
import twixthehero.ingress.tileentity.PortalTileEntity;
import cpw.mods.fml.common.network.IGuiHandler;

public class IngressGUIHandler implements IGuiHandler
{
@Override
public Object getServerGuiElement(int id, EntityPlayer player, World world,
		int x, int y, int z)
{
	return null;
}

@Override
public Object getClientGuiElement(int id, EntityPlayer player, World world,
		int x, int y, int z)
{
	if (id == Ingress.GUI_TEAM_SELECT)
	{
		player.sendChatToPlayer("opening team select gui");
		return new GUITeamSelect(player);
	}

	TileEntity tileEntity = world.getBlockTileEntity(x, y, z);

	if (tileEntity != null)
		switch (id)
		{
		case Ingress.GUI_PORTAL:
			return new GUIPortal(player.inventory,
					(PortalTileEntity) tileEntity);
		}

	return null;
}
}

Link to comment
Share on other sites

IF you want to open a client-side gui from a server side action you need one of two things.  A: A Container server side.  It can be a dummy container with no slots. (this is my personal route), or B: You will need to manually send a packet to the particular client, and in your packet handling call open GUI.

 

Option B is pretty much re-creating the wheel, but the only way to do it if you absolutely do not want a container.

 

Option A is pretty straightforward --make a container class with no slots, and empty methods-- just make sure to call player.closeGUI() when closing so that the containers stay in sync client/server sides.  Return this 'dummy' container from the server-side getGuiElement call -- and THEN FML will send the packet to client-side to open the GUI.  (it won't send the packet to client if there is no container returned--it assumes all client-side only guis are handled..client-side only).

 

Link to comment
Share on other sites

I'm trying to use a GuiScreen because I don't need to store any items inside it.  Containers are for storing items inside, right?  Why would this GUI need a container?

The GUI is linked to the container. Perhaps that is not the problem, but it is necessary.

IF you want to open a client-side gui from a server side action you need one of two things.  A: A Container server side.  It can be a dummy container with no slots. (this is my personal route), or B: You will need to manually send a packet to the particular client, and in your packet handling call open GUI.

 

Option B is pretty much re-creating the wheel, but the only way to do it if you absolutely do not want a container.

 

Option A is pretty straightforward --make a container class with no slots, and empty methods-- just make sure to call player.closeGUI() when closing so that the containers stay in sync client/server sides.  Return this 'dummy' container from the server-side getGuiElement call -- and THEN FML will send the packet to client-side to open the GUI.  (it won't send the packet to client if there is no container returned--it assumes all client-side only guis are handled..client-side only).

 

 

No, as long as you don't extend GuiContainer, you DON'T NEED a Container! Look at my GuiHandler and you'll see it for yourself: https://github.com/SanAndreasP/TurretModv3/blob/master/sanandreasp/mods/TurretMod3/registry/GuiHandler.java

I my GuiHandler, some cases of the getServerGuiElement it returns null.

 

I believe you need to open the GUI client-side (in case of container GUIs, client- and server-side) in order to open them. If you have a method which only opens server-side, you'll need to send a packet to the client, then open the GUI that way!

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

I'm trying to use a GuiScreen because I don't need to store any items inside it.  Containers are for storing items inside, right?  Why would this GUI need a container?

The GUI is linked to the container. Perhaps that is not the problem, but it is necessary.

IF you want to open a client-side gui from a server side action you need one of two things.  A: A Container server side.  It can be a dummy container with no slots. (this is my personal route), or B: You will need to manually send a packet to the particular client, and in your packet handling call open GUI.

 

Option B is pretty much re-creating the wheel, but the only way to do it if you absolutely do not want a container.

 

Option A is pretty straightforward --make a container class with no slots, and empty methods-- just make sure to call player.closeGUI() when closing so that the containers stay in sync client/server sides.  Return this 'dummy' container from the server-side getGuiElement call -- and THEN FML will send the packet to client-side to open the GUI.  (it won't send the packet to client if there is no container returned--it assumes all client-side only guis are handled..client-side only).

 

 

No, as long as you don't extend GuiContainer, you DON'T NEED a Container! Look at my GuiHandler and you'll see it for yourself: https://github.com/SanAndreasP/TurretModv3/blob/master/sanandreasp/mods/TurretMod3/registry/GuiHandler.java

I my GuiHandler, some cases of the getServerGuiElement it returns null.

 

I believe you need to open the GUI client-side (in case of container GUIs, client- and server-side) in order to open them. If you have a method which only opens server-side, you'll need to send a packet to the client, then open the GUI that way!

Read the OP? Why would I need to read the OP{?}

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Link to comment
Share on other sites

Thanks everyone!

 

I decided to use a packet (since I hadn't used them before, and I figured it was a good time to learn).

 

In my IPlayerTracker, I use:

 

ByteArrayOutputStream os = new ByteArrayOutputStream(4);
DataOutputStream dos = new DataOutputStream(os);

try
{
    dos.writeInt(Ingress.OPEN_TEAM_SELECT_PACKET);
}
catch (IOException e)
{
    e.printStackTrace();
}

Packet250CustomPayload packet = new Packet250CustomPayload();
packet.channel = "IngressGUI";
packet.data = os.toByteArray();
packet.length = os.size();

PacketDispatcher.sendPacketToPlayer(packet, (Player)player);

 

Then in my PacketHandler:

 

@Override
public void onPacketData(INetworkManager manager, Packet250CustomPayload packet, Player player)
{
    if (packet.channel.equals("IngressGUI"))
    {
        handleGUIPacket(player);
    }
}

private void handleGUIPacket(Player player)
{
    if (player instanceof EntityClientPlayerMP)
    {
        EntityClientPlayerMP p = (EntityClientPlayerMP)player;
        p.openGui(Ingress.instance, Ingress.GUI_TEAM_SELECT, p.worldObj, 0, 0, 0);
    }
}

 

Then from there, my getClientGuiElement() handles it.

Link to comment
Share on other sites

Read the OP? Why would I need to read the OP{?}

Sorry, I didn't meant to quote you, I was answering the guy above you and only overviewed your post, my apologies. In my opinion your Option B is working perfectly fine, but a tiny bit uglier, since you create an instance which won't be used in the end and eats up memory (even if it's a very tiny bit) until the next garbage collection. Again, it's my opinion.

Also creating a custom packet is a good way to learn how to handle packets if you didn't already learn that.

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Link to comment
Share on other sites

Read the OP? Why would I need to read the OP{?}

Sorry, I didn't meant to quote you, I was answering the guy above you and only overviewed your post, my apologies. In my opinion your Option B is working perfectly fine, but a tiny bit uglier, since you create an instance which won't be used in the end and eats up memory (even if it's a very tiny bit) until the next garbage collection. Again, it's my opinion.

Also creating a custom packet is a good way to learn how to handle packets if you didn't already learn that.

No, but your reply applies equally to my post. And I was being stupid and not actually reading the whole post.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • A friend found this code, but I don't know where. It seems to be very outdated, maybe from 1.12? and so uses TextureManager$loadTexture and TextureManager$deleteTexture which both don't seem to exist anymore. It also uses Minecraft.getMinecraft().mcDataDir.getCanonicalPath() which I replaced with the resource location of my texture .getPath()? Not sure if thats entirely correct. String textureName = "entitytest.png"; File textureFile = null; try { textureFile = new File(Minecraft.getMinecraft().mcDataDir.getCanonicalPath(), textureName); } catch (Exception ex) { } if (textureFile != null && textureFile.exists()) { ResourceLocation MODEL_TEXTURE = Resources.OTHER_TESTMODEL_CUSTOM; TextureManager texturemanager = Minecraft.getMinecraft().getTextureManager(); texturemanager.deleteTexture(MODEL_TEXTURE); Object object = new ThreadDownloadImageData(textureFile, null, MODEL_TEXTURE, new ImageBufferDownload()); texturemanager.loadTexture(MODEL_TEXTURE, (ITextureObject)object); return true; } else { return false; }   Then I've been trying to go through the source code of the reload resource packs from minecraft, to see if I can "cache" some data and simply reload some textures and swap them out, but I can't seem to figure out where exactly its "loading" the texture files and such. Minecraft$reloadResourcePacks(bool) seems to be mainly controlling the loading screen, and using this.resourcePackRepository.reload(); which is PackRepository$reload(), but that function seems to be using this absolute confusion of a line List<String> list = this.selected.stream().map(Pack::getId).collect(ImmutableList.toImmutableList()); and then this.discoverAvailable() and this.rebuildSelected. The rebuild selected seemed promising, but it seems to just be going through each pack and doing this to them? pack.getDefaultPosition().insert(list, pack, Functions.identity(), false); e.g. putting them into a list of packs and returning that into this.selected? Where do the textures actually get baked/loaded/whatever? Any info on how Minecraft reloads resource packs or how the texture manager works would be appreciated!
    • This might be a long shot , but do you remember how you fixed that?
    • Yeah, I'll start with the ones I added last night.  Wasn't crashing until today and wasn't crashing at all yesterday (+past few days since removing Cupboard), so deductive reasoning says it's likeliest to be one of the new ones.  A few horse armor mods and a corn-based add-on to Farmer's Delight, the latter which I hope to keep - I could do without the horse armor mods if necessary.  Let me try a few things and we'll see. 
    • Add crash-reports with sites like https://mclo.gs/ Add this mod: https://www.curseforge.com/minecraft/mc-mods/packet-fixer/files/5416165
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.