Jump to content

Recommended Posts

Posted

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

Posted

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) :)

 

Posted

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;
}
}

Posted
  On 6/19/2013 at 4:11 AM, twixthehero said:

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.

BEWARE OF GOD

---

Co-author of Pentachoron Labs' SBFP Tech.

Posted

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

 

Posted
  On 6/19/2013 at 2:53 PM, ObsequiousNewt said:

  Quote

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.

  Quote

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

  Quote

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.

Posted
  On 6/19/2013 at 7:07 PM, SanAndreasP said:

  Quote

  Quote

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.

  Quote

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.

Posted

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.

Posted
  On 6/19/2013 at 10:48 PM, ObsequiousNewt said:

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

  Quote

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.

Posted
  On 6/19/2013 at 11:34 PM, SanAndreasP said:

  Quote

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.

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

    • Hi everyone, I'm currently developing a Forge 1.21 mod for Minecraft and I want to display a custom HUD overlay for a minigame. My goal: When the game starts, all players should see an item/block icon (from the base game, not a custom texture) plus its name/text in the HUD – similar to how the bossbar overlay works. The HUD should appear centered above the hotbar (or at a similar prominent spot), and update dynamically (icon and name change as the target item changes). What I've tried: I looked at many online tutorials and several GitHub repos (e.g. SeasonHUD, MiniHUD), but most of them use NeoForge or Forge versions <1.20 that provide the IGuiOverlay API (e.g. implements IGuiOverlay, RegisterGuiOverlaysEvent). In Forge 1.21, it seems that neither IGuiOverlay nor RegisterGuiOverlaysEvent exist anymore – at least, I can't import them and they are missing from the docs and code completion. I tried using RenderLevelStageEvent as a workaround but it is probably not intended for custom HUDs. I am not using NeoForge, and switching the project to NeoForge is currently not an option for me. I tried to look at the original minecraft source code to see how elements like hearts, hotbar etc are drawn on the screen but I am too new to Minecraft modding to understand. What I'm looking for: What is the correct way to add a custom HUD element (icon + text) in Forge 1.21, given that the previous overlay API is missing? Is there a new recommended event, callback, or method in Forge 1.21 for custom HUD overlays, or is everyone just using a workaround? Is there a minimal open-source example repo for Forge 1.21 that demonstrates a working HUD overlay without relying on NeoForge or deprecated Forge APIs? My ideal solution: Centered HUD element with an in-game item/block icon (from the base game's assets, e.g. a diamond or any ItemStack / Item) and its name as text, with a transparent background rectangle. It should be visible to the players when the mini game is running. Easy to update the item (e.g. static variable or other method), so it can change dynamically during the game. Any help, code snippets, or up-to-date references would be really appreciated! If this is simply not possible right now in Forge 1.21, it would also help to know that for sure. Thank you very much in advance!
    • The simple answer is there is not an easy way. You would need to know how to program in Java, as well as at least some familiarity with how Forge works so you could port the differences. You would also need the sourcecode for the original mod, and permission from the author to modify it, if they did not use some sort of open source license. So it's not impossible, but it would take some effort, but doing so would open up a whole new world of possibilities for you!
    • Does it still crash if you remove holdmyitems? Looks like that mod doesn't work on a server as far as I can tell from the error.  
    • Crashes the server when trying to start. Error code -1. Log  
  • Topics

×
×
  • Create New...

Important Information

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