Jump to content

How do I make a Gui open when an item is right clicked? - SOLVED


Recommended Posts

Posted

Ok, so I have a pickaxe that I want to open an Inventory for when it is right clicked. Now I am fairly new to making GUI/containers and need help as any tutorials are outdated. I have successfully made a machine that functions as a furnace does except with custom recipes. I imagine a tool inventory would be similar, except I don't think a tool is considered a tile entity. This makes it things complicated when it comes to the GUI Handler.

 

GuiHandler.java (Note: The Endite Reactor is my machine that is already working)

 

  Reveal hidden contents

 

 

ToolAdaptedPick.java (This is the tool)

 

  Reveal hidden contents

 

 

EDIT:

 

GuiAdaptedPick.java (This is the Gui Class)

 

  Reveal hidden contents

 

 

ContainerAdaptedPick.java (This is still a WIP)

 

  Reveal hidden contents

 

 

So to clarify, I am not getting errors with code or anything, I'm just lost on how to start with the tool as it isn't a tile entity. Some guidance would be much appreciated as well as maybe some example code. I already know the basics of java so don't bother just telling me "You need to learn java!!!" That is not helpful.

 

NOTE: This is for minecraft version 1.7.10. If you want anymore of my code, just tell me and I will put it in.

Posted

 

in your item class add

 

@Override

public ItemStack onItemRightClick(ItemStack i, World w, EntityPlayer pl) {

  pl.openGui(  fill in your own parameters here  );

  return i;

}

 

during initialization register your gui

 

NetworkRegistry.INSTANCE.registerGuiHandler(yourMod.instance,new GUIHandler());

 

 

here is what is in my gui handler

 

public class GUIHandler implements IGuiHandler {

 

@Override

public Object getServerGuiElement(int id, EntityPlayer pl, World wld, int x, int y, int z) {

MagicEnchant.debug("getServerGuiElement");

if ( id == GuiEnchantTable.GUI_ID ){

return null;

}

return null;

}

 

@Override

public Object getClientGuiElement(int id, EntityPlayer pl, World wld, int x, int y, int z) {

MagicEnchant.debug("getClientGuiElement");

if ( id == GuiEnchantTable.GUI_ID ){

return new GuiEnchantTable(pl,x,y,z);

}

return null;

}

}

 

 

 

 

Posted

I applied the changes you said and it did something. It recognizes that I right click on the tool but then crashes! I have no red underlines in the code to signify an error, but I assume the code I made is wrong. I have pasted in my gui and container classes in the main thread. The gui class is probably wrong as I just copied my gui class for my machines (block) but editing a few methods and names.

 

Here is the crash report :

 

  Reveal hidden contents

 

 

I am usually good at reading crash reports but I am lost on this one.

 

My main class is way to large to paste in but I already had my Gui handler registered like you said.

 

public void init(FMLInitializationEvent e) {

NetworkRegistry.INSTANCE.registerGuiHandler(this, new GuiHandler());

 

//300 lines of recipes

}

 

Posted

When I made my back pack, I added this to my gui handler for opening a gui that was equiped.

public class TEGuiHandler implements IGuiHandler{

@Override
public Object getServerGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) 
{
	TileEntity tile_entity = world.getTileEntity(x, y, z);
	ItemStack equipped;


	 equipped = getEquippedItem(player);
      if (equipped == null) {
        return null;
      }
      if ((equipped.getItem() instanceof BackPack)) {
         return new ContainerBackPack(player, player.inventory, new ItemInventory(player.getHeldItem()));
      }


	return null;
}

@Override
public Object getClientGuiElement(int ID, EntityPlayer player, World world, int x, int y, int z) 
{
	TileEntity tile_entity = world.getTileEntity(x, y, z);
	ItemStack equipped;

		//Entity BackPack
	 equipped = getEquippedItem(player);
      if (equipped == null) {
        return null;
      }
      if ((equipped.getItem() instanceof BackPack)) {
    	  return new GuiBackPack((ContainerBackPack) new ContainerBackPack(player, player.inventory, new ItemInventory(player.getHeldItem())));
      }

	return null;
}

  public ItemStack getEquippedItem(EntityPlayer player)
  {
    return player.getCurrentEquippedItem();
  }


}

Posted

Would you be able to show me the code for your GUI and container class please? I think that there is an issue in the GUI class and I am lost with the container class. This would also be great if anyone else that has made something like this could show me their code too. I am completely lost.

Posted

I your gui handeler, your added code needs to be a case statement inside your switch statement.  This video might help:

 

Here is my gui handeler:

 

  Reveal hidden contents

 

 

Making inventories for Items is a little complicated, here is mine:

 

Item:

 

  Reveal hidden contents

 

 

Container:

 

  Reveal hidden contents

 

 

Inventory:

 

  Reveal hidden contents

 

 

Hope it helps.  There is a lot of extra stuff.  I dont't think you need to creat a seprate inventory, but it's a good idea.

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.