Jump to content

Recommended Posts

Posted

Hello, I am new to modding so I got a bit confused on how GUI works, watched a few tutorials but all of them are for specific blocks GUI, my idea is a bit different.

My idea is to open a GUI when ever a player interacts with another, on the player that interacts it would open a GUI saying "Waiting..." and on the other player it would open a GUI with two buttons saying "Yes" or "No".

My code to get the interaction working is the following:

@SubscribeEvent
public void onRingUsage(EntityInteract event) {
  if (event.getTarget() instanceof EntityPlayer) {
    if (event.getEntityPlayer().isSneaking()) {
      if (event.getEntityPlayer().getHeldItemMainhand() != null) {
        if(event.getEntityPlayer().getHeldItemMainhand().getItem() == ModItems.ring) {
          //Opens different GUI on both players here
        }
      }
    }
  }
}

I really don't know how to open the GUIs for both players, can someone help me?

 

Cheers.

Posted
23 minutes ago, diesieben07 said:

You would use the same technique you use for other GUIs: You make an IGuiHandler and then use EntityPlayer::openGui to open it.

Thank you!

Got it to work i think, though it's not really working how I wanted, would you mind taking a look at my code?

GuiHandler.java:

public class GuiHandler implements IGuiHandler {	

	public static final int GUI_ID_ASK = 0;
	public static final int GUI_ID_WAITING = 1;
    
	@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 == GUI_ID_ASK) {
	        return new GuiAsk();
		}
		if (ID == GUI_ID_WAITING) {
			return new GuiWaiting();
		}
		return null;
	}	
}

EventsHandler.java

@SubscribeEvent
public void onRingUsage(EntityInteract event) {
	if (event.getTarget() instanceof EntityPlayer) {
		if (event.getEntityPlayer().isSneaking()) {
			if (event.getEntityPlayer().getHeldItemMainhand() != null) {
				if(event.getEntityPlayer().getHeldItemMainhand().getItem() == ModItems.ring) {
					if (event.getWorld().isRemote) {
						EntityPlayer player = event.getWorld().getPlayerEntityByUUID(event.getTarget().getUniqueID());
						player.openGui(Marriage.instance, 0, event.getWorld(), (int) player.posX, (int) player.posY, (int) player.posZ);
				    	event.getEntityPlayer().openGui(Marriage.instance, 1, event.getWorld(), (int) event.getEntityPlayer().posX, (int) event.getEntityPlayer().posY, (int) event.getEntityPlayer().posZ);
					}
				}
			}
		}
	}
}

 

With this, only one GUI opens, always on the clicking player.

Posted
40 minutes ago, diesieben07 said:

Only call openGui on the server. You must have a Container as well if you want to do it this way.

Otherwise you would need a custom packet to send to the client to open the GUI there using Minecraft::displayGuiScreen.

Isn't container needed only if the GUI has inventory?

Posted
2 hours ago, diesieben07 said:

No, Container is needed if you want a server-side representation of the GUI. It does not necessarily need to have inventory slots.

I've been trying a lot but I don't seem to be able to get it to work, would it be okay if you showed me an example of a container that would work with my case?

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.