Jump to content

getPlayers() not working as expected


Sana

Recommended Posts

Working on my first ever script, got it complete after about 24 hours but I'm stuck on the very last step which is to trade a player the items I've acquired.

The `if` statement is failing every time, even when I'm less than 5 squares from the player. It's as if the player is invisible from the bot.

This is what I'm doing:

private boolean tradePlayer() {
    Player player = getPlayers().closest(PLAYER_NAME);
    if (player != null && player.interact("Trade with")) {
        Sleep.sleepUntil(() -> getTrade().isCurrentlyTrading(), 60000, random(100, 200));

        getTrade().offerAll(ITEM_ID);
        getTrade().acceptTrade();
        Sleep.sleepUntil(() -> getTrade().isSecondInterfaceOpen(), 60000, random(100, 200));

        getTrade().acceptTrade();
        Sleep.sleepUntil(() -> getTrade().didOtherAcceptTrade(), 60000, random(100, 200));

        return true;
    }

    return false;
}

Any idea why this script function isn't trading the player?

Edited by Lansana Camara
Link to comment
Share on other sites

1 hour ago, Lansana Camara said:

Working on my first ever script, got it complete after about 24 hours but I'm stuck on the very last step which is to trade a player the items I've acquired.

The `if` statement is failing every time, even when I'm less than 5 squares from the player. It's as if the player is invisible from the bot.

This is what I'm doing:


private boolean tradePlayer() {
    Player player = getPlayers().closest(PLAYER_NAME);
    if (player != null && player.interact("Trade with")) {
        Sleep.sleepUntil(() -> getTrade().isCurrentlyTrading(), 60000, random(100, 200));

        getTrade().offerAll(ITEM_ID);
        getTrade().acceptTrade();
        Sleep.sleepUntil(() -> getTrade().isSecondInterfaceOpen(), 60000, random(100, 200));

        getTrade().acceptTrade();
        Sleep.sleepUntil(() -> getTrade().didOtherAcceptTrade(), 60000, random(100, 200));

        return true;
    }

    return false;
}

Any idea why this script function isn't trading the player?

You should never check != null with anything else. Nest them.

Player player = getPlayers().closest(PLAYER_NAME);
    if (player != null){
		if(player.interact("Trade with")) {
		
		}
	}

 

Link to comment
Share on other sites

1 hour ago, Naked said:

You should never check != null with anything else. Nest them.


Player player = getPlayers().closest(PLAYER_NAME);
    if (player != null){
		if(player.interact("Trade with")) {
		
		}
	}

 

Why is this? I come from a GoLang/JavaScript/PHP background, and this is allowed in those C-based languages, so I wasn't aware one shouldn't do this in Java.

By the way, not sure if that was a tip or a solution but it didn't fix the issue. Thanks, though.

Link to comment
Share on other sites

3 hours ago, Naked said:

You should never check != null with anything else. Nest them.


Player player = getPlayers().closest(PLAYER_NAME);
    if (player != null){
		if(player.interact("Trade with")) {
		
		}
	}

 

Why not? I don't see anything wrong with having them on the same line.

-Apa

@Lansana Camara Does the player name have a space in it? The names for players may include non-breaking spaces so you may need to account for this.

Edited by Apaec
  • Like 2
Link to comment
Share on other sites

5 hours ago, FuryShark said:

It works for me, have you remembered to click refresh on osbot? 

also try adding log() s to see where its getting to.

also have you made sure the username is spelt correctly

Yeah it doesn't work, I tried all that. When I log, it seems that the code stops running at the point that I log because the log never appears in that function, but it appears in logic right before the function. So strange. :/

I'm using the following to do my logs since `log()` doesn't log to the OSBot debugger:

getBot().getLogger().debug("...");

Also, If it makes any difference, the characters aren't in a "normal" area. They are at a runecrafting altar...perhaps that somehow messes with the OSBot API's and makes it so that the characters can't be seen?

Edited by Lansana Camara
Link to comment
Share on other sites

44 minutes ago, Apaec said:

Why not? I don't see anything wrong with having them on the same line.

-Apa

@Lansana Camara Does the player name have a space in it? The names for players may include non-breaking spaces so you may need to account for this.

Thanks! The issue was dealing with space. Fixed by converting white spaces to nonbreaking spaces.

  • Like 1
Link to comment
Share on other sites

  • 1 year later...
34 minutes ago, Psychotechno said:

May I asked how you did this?

I ran into a similar issue recently. If you have some names you want to compare to player names in-game, you need to replace any spaces in your names with non-breaking spaces like this:

String friend = "slayerguy 117";
friend = friend.replace(' ', '\u00A0');

Now if you make a comparison to names pulled from api.players methods your Strings should properly match with the names returned by player.getName().

Link to comment
Share on other sites

On 3/20/2021 at 10:46 PM, Delision said:

I ran into a similar issue recently. If you have some names you want to compare to player names in-game, you need to replace any spaces in your names with non-breaking spaces like this:


String friend = "slayerguy 117";
friend = friend.replace(' ', '\u00A0');

Now if you make a comparison to names pulled from api.players methods your Strings should properly match with the names returned by player.getName().

I thank you! 

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...