Jump to content

Unable to detect an incoming trade


Hayase

Recommended Posts

Player worker = trade.getLastRequestingPlayer();
log("Waiting for a trade");
if (worker != null) { //this is broken
    if (worker.isVisible()) {
        log("I see your trade! Allow me...");
        if (worker.interact("Trade with")) {
            new ConditionalSleep(5000) {
                @[member='Override']
                public boolean condition() throws InterruptedException {
                    return trade.isCurrentlyTrading();
                }
            }.sleep();
        }
        if (trade.isCurrentlyTrading())
            //do stuff
    } else {
        log("I CAN'T SEE YOU!");
        camera.toEntity(worker);
    }
} else {
    log("Where's the trader?");
}

Currently the script doesn't detect a trade. It always returns null for all incoming trades. What is the proper usage to getLastRequestingPlayer()?

Edited by Hayase
Link to comment
Share on other sites

use onMessage

 

Do I need to do anything special with onMessage? I created a function checkForTrade() using the original code and I'm not sure if it's working or not.

@[member='Override']
public void onMessage(Message c) {
    String m = c.getMessage().toLowerCase();
    checkForTrade();
    if (trade.getLastRequestingPlayer() !=null && m.contains(trade.getLastRequestingPlayer().getName().toLowerCase())) {
        log("We found a trade in our onMessage!");
    }
}

Seems to be working now, thanks Saiyan.

Edited by Hayase
Link to comment
Share on other sites

Here's an untested and off-the-cuff example of how I'd go about handling this:


	/*
	 * Concurrent hash map is more suitable, because we're going to read and
	 * write to it from different threads that can run in parallel
	 */
	Map<String, Long> tradeRequests = new ConcurrentHashMap<>();
	
	@[member=Override]
	public int onLoop() throws InterruptedException {
		
		Player worker = null;
		
		// Remove all old requests
		removeOldTradeRequests(15000); // 15 seconds
		
		// Check to see if we've been traded
		if (hasBeenTradedBy("whoever")) {
			
			// Now find that actual player in-game
			worker = players.closest("whoever");
			
			// Check worker is valid
			if (worker != null && worker.exists()) {
				
				// Trade worker back
				worker.interact("Trade");
			}
		}
		
		return 0;
	}

	/*
	 * In here we're simply going to capture all trade requests and store them.
	 * 
	 * Because we're dealing with a hash map, old trade requests will be
	 * overwritten by the newer ones, so we know who traded and when, to help
	 * filter out the old and irrelevant ones.
	 */
	@[member=Override]
	public void onMessage(Message message) throws InterruptedException {
		
		// Basic assertion
		assert (message != null);
		
		// Check message type
		// Check message suffix
		if (message.getType() == Message.MessageType.GAME
				&& message.getMessage().endsWith(" wishes to trade with you.")) {
			
			// Store result
			tradeRequests.put(message.getUsername(), message.getTime());
		}
	}
	
	public final synchronized boolean hasBeenTradedBy(String userName) {
		
		return tradeRequests.containsKey(userName);
	}
	
	/**
	 * Remove all old trade requests from hash map
	 */
	public final synchronized void removeOldTradeRequests(long expirationInMilliseconds) {
		
		final long now = System.currentTimeMillis();
		
		tradeRequests.forEach((userName, timeStamp) -> {
			
			if ((now - timeStamp) >= expirationInMilliseconds) {
				
				tradeRequests.remove(userName);
			}
		});
	}
  • Like 3
Link to comment
Share on other sites

 

Here's an untested and off-the-cuff example of how I'd go about handling this:


	/*
	 * Concurrent hash map is more suitable, because we're going to read and
	 * write to it from different threads that can run in parallel
	 */
	Map<String, Long> tradeRequests = new ConcurrentHashMap<>();
	
	@[member='Override']
	public int onLoop() throws InterruptedException {
		
		Player worker = null;
		
		// Remove all old requests
		removeOldTradeRequests(15000); // 15 seconds
		
		// Check to see if we've been traded
		if (hasBeenTradedBy("whoever")) {
			
			// Now find that actual player in-game
			worker = players.closest("whoever");
			
			// Check worker is valid
			if (worker != null && worker.exists()) {
				
				// Trade worker back
				worker.interact("Trade");
			}
		}
		
		return 0;
	}

	/*
	 * In here we're simply going to capture all trade requests and store them.
	 * 
	 * Because we're dealing with a hash map, old trade requests will be
	 * overwritten by the newer ones, so we know who traded and when, to help
	 * filter out the old and irrelevant ones.
	 */
	@[member='Override']
	public void onMessage(Message message) throws InterruptedException {
		
		// Basic assertion
		assert (message != null);
		
		// Check message type
		// Check message suffix
		if (message.getType() == Message.MessageType.GAME
				&& message.getMessage().endsWith(" wishes to trade with you.")) {
			
			// Store result
			tradeRequests.put(message.getUsername(), message.getTime());
		}
	}
	
	public final synchronized boolean hasBeenTradedBy(String userName) {
		
		return tradeRequests.containsKey(userName);
	}
	
	/**
	 * Remove all old trade requests from hash map
	 */
	public final synchronized void removeOldTradeRequests(long expirationInMilliseconds) {
		
		final long now = System.currentTimeMillis();
		
		tradeRequests.forEach((userName, timeStamp) -> {
			
			if ((now - timeStamp) >= expirationInMilliseconds) {
				
				tradeRequests.remove(userName);
			}
		});
	}

 

I don't think that you need to synchronzie onMessage() as it's always called before (or after, cant remember) the onLoop(), it's on the same thread.

Link to comment
Share on other sites

 

Here's an untested and off-the-cuff example of how I'd go about handling this:


	/*
	 * Concurrent hash map is more suitable, because we're going to read and
	 * write to it from different threads that can run in parallel
	 */
	Map<String, Long> tradeRequests = new ConcurrentHashMap<>();
	
	@[member='Override']
	public int onLoop() throws InterruptedException {
		
		Player worker = null;
		
		// Remove all old requests
		removeOldTradeRequests(15000); // 15 seconds
		
		// Check to see if we've been traded
		if (hasBeenTradedBy("whoever")) {
			
			// Now find that actual player in-game
			worker = players.closest("whoever");
			
			// Check worker is valid
			if (worker != null && worker.exists()) {
				
				// Trade worker back
				worker.interact("Trade");
			}
		}
		
		return 0;
	}

	/*
	 * In here we're simply going to capture all trade requests and store them.
	 * 
	 * Because we're dealing with a hash map, old trade requests will be
	 * overwritten by the newer ones, so we know who traded and when, to help
	 * filter out the old and irrelevant ones.
	 */
	@[member='Override']
	public void onMessage(Message message) throws InterruptedException {
		
		// Basic assertion
		assert (message != null);
		
		// Check message type
		// Check message suffix
		if (message.getType() == Message.MessageType.GAME
				&& message.getMessage().endsWith(" wishes to trade with you.")) {
			
			// Store result
			tradeRequests.put(message.getUsername(), message.getTime());
		}
	}
	
	public final synchronized boolean hasBeenTradedBy(String userName) {
		
		return tradeRequests.containsKey(userName);
	}
	
	/**
	 * Remove all old trade requests from hash map
	 */
	public final synchronized void removeOldTradeRequests(long expirationInMilliseconds) {
		
		final long now = System.currentTimeMillis();
		
		tradeRequests.forEach((userName, timeStamp) -> {
			
			if ((now - timeStamp) >= expirationInMilliseconds) {
				
				tradeRequests.remove(userName);
			}
		});
	}

 

Very elegant documentation it was a pleasure to read

 

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