Jump to content

Inventory Item Drag


Mr Pro Pop

Recommended Posts

Hello guys,  I had been trying to make an inventory item drag since ages but i couldn't figure it out, So for example it moves the item from slot 1 to slot 2 or something, I tried but with no luck could not figure it out, If someone can provide me with the code and help i will be much appreciated.

We need to figure this out to do an amazing script with it such as bank organizer and more stuff!

All comments/posts is allowed and much appreciated! [ Please if you're not willing to help DONT post ]

 

Best regards Mr Pro Pop.

Thanks in advance!

 

@venetox @@Explv

Edited by Mr Pro Pop
Link to comment
Share on other sites

threw this together quick. the @ Override is one line, osbot forums think's it's a member so it separated it
 

public void moveInvItem(int from, int to) {
        if (from < 0) return;
        Rectangle toRec = InventorySlotDestination.getSlot(to);
        getMouse().continualClick(new RectangleDestination(getBot(),InventorySlotDestination.getSlot(from)), new Condition() {
            @[member=Override]
            public boolean evaluate() {
                if (toRec.contains(getMouse().getPosition())) return true;
                getMouse().move(new RectangleDestination(getBot(),toRec));
                return false;
            }
        });
    }

example usage
 
moveInvItem(getInventory().getSlot(ITEMID),SLOTID);

Edited by Th3
  • Like 3
Link to comment
Share on other sites

threw this together quick. the @ Override is one line, osbot forums think's it's a member so it separated it

 

public void moveInvItem(int from, int to) {
        if (from < 0) return;
        Rectangle toRec = InventorySlotDestination.getSlot(to);
        getMouse().continualClick(new RectangleDestination(getBot(),InventorySlotDestination.getSlot(from)), new Condition() {
            @[member='Override']
            public boolean evaluate() {
                if (toRec.contains(getMouse().getPosition())) return true;
                getMouse().move(new RectangleDestination(getBot(),toRec));
                return false;
            }
        });
    }

example usage

 

moveInvItem(getInventory().getSlot(ITEMID),SLOTID);

 

 

Didn't know a bunch of that stuff existed.

My similar approach ghetto version:

public void moveItem(int start_slot, int end_slot) {

		Rectangle start_slot_bounds = getSlotBounds(start_slot);
		Rectangle end_slot_bounds = getSlotBounds(end_slot);

		Mouse mouse = getMouse();
		mouse.move(new RectangleDestination(getBot(), start_slot_bounds));
		getBot().getMouseEventHandler().generateBotMouseEvent(MouseEvent.MOUSE_PRESSED, System.currentTimeMillis(), 0, (int) mouse.getPosition().getX(), (int) mouse.getPosition().getY(), 0, false, 0, true);
		mouse.move(new RectangleDestination(getBot(), end_slot_bounds));
		getBot().getMouseEventHandler().generateBotMouseEvent(MouseEvent.MOUSE_RELEASED, System.currentTimeMillis(), 0, (int) mouse.getPosition().getX(), (int) mouse.getPosition().getY(), 0, false, 0, true);
			
	}

	public Rectangle getSlotBounds(int slot_id) {
		
		int min_x = 563;
		int min_y = 213;
		
		int item_dimension = 31;
		int gap_x = 11;
		int gap_y = 5;

		int column = (int) Math.floor(slot_id / 4);
		int row = slot_id % 4;

		int slot_y = min_y + (item_dimension * column) + (gap_y * column);
		int slot_x = min_x + (item_dimension * row) + (gap_x * row);
		
		return new Rectangle(slot_x, slot_y, item_dimension, item_dimension);
		
	}
Edited by House
  • Like 1
Link to comment
Share on other sites

Thanks you @@Th3 for the help, I really appreciate!

 

@@House i didnt understand how to does it work, wont start with me! -> Structure please!

 

err,

 

getSlotBounds() just generates the rectangle that the item in the inventory takes up,

then it just moves the mouse to the first items area, 

simulates a mouse button press,

moves into the target area,

simulates a mouse release.

moveItem(0,27);

 Example moves from first first slot in the top left to the very bottom right.

 

I do recommend using Th3's version though, its the same thing however it uses api methods that serve the same purpose,

this means its easier to implement interrupts in the evaluate method as well.

Edited by House
Link to comment
Share on other sites

The code I use is similar to @@Th3's, I will try to comment and explain it as best as possible.

public boolean moveItemInSlot(int startSlot, final int endSlot) throws InterruptedException { 
		if (getInventory().isItemSelected()) { // If we currently have something selected, unselect it, since we can't move items if we have something selected
			getInventory().deselectItem();
		}
		return getMouse().continualClick(getInventory().getMouseDestination(startSlot), new Condition() {
			@ Override // Remove space here, forum likes to tag Ovveride 
			public boolean evaluate() {
				getMouse().move(getInventory().getMouseDestination(endSlot), true);
				return getInventory().getMouseDestination(endSlot).getBoundingBox().contains(getMouse().getPosition());
			}
		});
	}

So this is what it does.

 

getMouse() Gets the instance of your mouse.

getInventory().getMouseDestination(startSlot) returns the position the mouse should click to be clicking he slot you want to move from.

continualClick() Moves the mouse to the specified destination (position of startSlot) and performs a left click for as long as the condition evaluates to false

new Condition() creates the condition the continualClick will evaluate and until it returns true.

 

inside evaluate()

getMouse() Gets the instance of your mouse again.

 

getInventory().getMouseDestination(slot2) gets the position the mouse should move to in order to be on endSlot

move() is then used to move the mouse to endSlot

 

getInventory().getMouseDestination(endSlot).getBoundingBox().conains(getMouse().getPosition()) is then used to check if the mouse is inside the endSlot's bounding box, if it is, then evaluate will return true and the mouse will let go of the left click and its all done, if not it will keep moving to the end slot until it is inside the bounding box.

 

Hope this helps explain it a bit. Its a bit hard to explain, but it is fairly simple once you pull it all apart.

 

 

Edited by venetox
Link to comment
Share on other sites

  • 3 years later...

Old topic, but for those looking for an updated solution as InventorySlotDestination is deprecated.

This worked for me:

    public void moveInvItem(int from, int to) {
        if (from < 0) return;
        Rectangle toRec = getInventory().getSlotBoundingBox(to);
        RectangleDestination rectDest = new RectangleDestination(getBot(), getInventory().getSlotBoundingBox(getInventory().getSlot(from)));
        getMouse().continualClick(rectDest, new Condition() {
            public boolean evaluate() {
                if (toRec.contains(getMouse().getPosition())) return true;
                getMouse().move(new RectangleDestination(getBot(), toRec));
                return false;
            }
        });
        new ConditionalSleep(1200) {
            @Override
            public boolean condition() throws InterruptedException {
                return getInventory().getSlot(from) == to;
            }
        }.sleep();
    }

 

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
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...