Jump to content

[1.7.2] Get hovered stack in inventory


sep87x

Recommended Posts

I need to get the stack out of the player's inventory container which is currently being hovered with the mouse. Basically, I have this code set up which checks for a keybinding and invokes onKeyPressed() when the button is pressed. (The KeyBindingWrapper is a utility class I wrote for myself, it's not available in Forge)

 

 

new KeyBindingWrapper("key.forcefieldmod.inventory.assign", Keyboard.KEY_Z, "key.forcefieldmod.category") {

@Override
public void onKeyPressed() {
	GuiScreen currentGuiScreen = ForgeUtils.getMinecraft().currentScreen;

	if (currentGuiScreen instanceof GuiInventory) {
		GuiInventory inv = (GuiInventory) currentGuiScreen;
		// get ze stack
	}
}

};

 

 

What's the best way to accomplish this? I've looked through the inventory's GUI and container class, but I haven't found fields or methods which could be of any use yet.

Link to comment
Share on other sites

In GuiContainer there is a method called: isMouseOverSlot which takes a slot and a 2d vector of your mouse position(x and y).

But unfortunately that method is private.

 

You could write your own method though.

private Container _container;
private ItemStack _itemStack;
private Minecraft _mcClient;

private int _height;
private int _width;

//constructor:
public YOURCLASS(){
	_container = null;
	_itemStack = null;
	_mcClient = FMLClientHandler.instance.getClient();
	/* Somehow initialize _height and _width from the player's active guiscreen.
		_height = ?
		_width = ?
	*/
}

public ItemStack getItemStack(){
	if(this._mcClient != null) {
		_container = this._mcClient.thePlayer.openContainer;
        int mouseX = Mouse.getEventX() * this.width / this.mc.displayWidth;
        int mouseY = this.height - Mouse.getEventY() * this.height / this.mc.displayHeight - 1;
        for (int i = 0; i < _container.inventorySlots.inventorySlots.size(); ++i){
            Slot slot = (Slot)_container.inventorySlots.inventorySlots.get(i);

            if (this.isMouseOverSlot(slot, mouseX, mouseY, _width, _height) && slot.func_111238_b()){
                return this._itemStack = slot.getItemStack();
            }
        }
	}
}

public boolean isMouseOverSlot(Slot slot, int xCoord, int yCoord, int guiLeft, int guiTop){
    int left = guiLeft;
    int top = guiTop;
    xCoord -= top;
    yCoord -= left;
    return xCoord >= slot.xDisplayPosition - 1 && xCoord < slot.xDisplayPosition + 16 + 1 && yCoord >= slot.yDisplayPosition - 1 && yCoord < slot.yDisplayPosition + 16 + 1;
}

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.