Jump to content

[RESOLVED] 1.7.10 - Open chest GUI or detect when opened


Blackout

Recommended Posts

Hi :)

 

I'm trying to execute some code when a gui chest is open.

To do that I do :

public void playerInteractEvent(PlayerInteractEvent event) {
	if(event.action == PlayerInteractEvent.Action.RIGHT_CLICK_BLOCK) {
		//Player right click on a block
		if(!event.entityPlayer.isSneaking()) {
			//Player is not sneaking
			TileEntity t = event.world.getTileEntity(event.x, event.y, event.z);
			if(t instanceof TileEntityChest) {
				FMLClientHandler.instance().displayGuiScreen(event.entityPlayer, new GuiChest(event.entityPlayer.inventory, (IInventory) t));
				event.setCanceled(true);
				// DO SOMETHING
			}
		}
	}
}

 

It's work, chest gui appears.

But when I select an Item, I have a IndexOfBoundException.

I suppose I don't give the good IIventory to this function but where can I find the good IIventory ?

 

Thanks a lot :)

Link to comment
Share on other sites

Thanks for your reply, I didn't know the GuiOpenEvent it's very usefull :)

 

I have an other problem, my button doesn't' appear now :/

 

This is my code, it's short :

 

@SubscribeEvent
public void onGuiOpen(net.minecraftforge.client.event.GuiOpenEvent event) {
	System.out.println("A gui is open !");
	if(event.gui instanceof GuiContainer) {
		System.out.println("This is a GuiContainer");
		handleGuiContainerLayout((GuiContainer) event.gui);
	}
}

private void handleGuiContainerLayout(GuiContainer gui) {
	GuiSettlementsButton settlementButton = new GuiSettlementsButton(gui);
	List<Object> buttonList = GuiReflect.getInstance().getButtonList(gui);
	if(buttonList != null) {
		System.out.println("We will add our button !");

		buttonList.add(settlementButton);

		System.out.println(buttonList);
	}
}

 

 

This is the button code :

 

public class GuiSettlementsButton extends GuiButtonExt {

public GuiSettlementsButton(GuiContainer gui) {
	super(-1, GuiReflect.getInstance().getGuiLeft(gui), GuiReflect.getInstance().getGuiTop(gui)+gui.height+1,150,20, StatCollector.translateToLocal("tile.SettlementsGuiButton.name"));
}

@Override
public String toString() {
	return "GuiSettlementsButton :\n"
			+ "\tx : "+xPosition+"\n"
			+ "\ty : "+yPosition+"\n"
			+ "\twidth : "+width+"\n"
			+ "\theight : "+height+"\n"
			+ "\ttext : "+this.displayString;
}
}

 

I try with GuiButton instead of GuiButtonExt but, it's the same :/

 

And this is my log when I open a chest:

A gui is open !
This is a GuiContainer
We will add our button !
[GuiSettlementsButton :
x : 0
y : 1
width : 150
height : 20
text : Settlements]

 

I don't understand why this is write 2 times and why my button is not visible.

 

Could you help me ?

 

Thanks :)

 

EDIT : It's work fine now, I just don't understand what mean height, I think this was ySize.

So problem resolved ;)

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.