Trading Players Snippet



  • I see a lot of questions on how to handle trades so I thought I'd add a quick snippet. Always open to suggestions/improvements:

    import org.rspeer.runetek.api.component.Trade;
    import org.rspeer.runetek.api.scene.Player;
    import org.rspeer.runetek.api.scene.Players;
    import org.rspeer.runetek.event.listeners.ChatMessageListener;
    import org.rspeer.runetek.event.types.ChatMessageEvent;
    import org.rspeer.runetek.event.types.ChatMessageType;
    import org.rspeer.script.Script;
    import org.rspeer.script.ScriptCategory;
    import org.rspeer.script.ScriptMeta;
    import org.rspeer.ui.Log;
    
    @ScriptMeta(name = "Trading Example",  desc = "Trades a player & logs messages from trader", developer = "ADivorcedFork", category = ScriptCategory.OTHER)
    public class TradingExample extends Script implements ChatMessageListener {
    
        private String traderName = "";
        private boolean tradePending = false;
    
        @Override
        public void onStart() {
            Log.info("Starting Trading Example...");
            traderName = "";
            tradePending = false;
        }
    
        @Override
        public int loop() {
            if (Trade.isOpen(false)) {
                // handle first trade window...
            }
            else if (Trade.isOpen(true)){
                // handle second trade window...
            }
            // If someone is requesting to trade you & you're not in trade, accept trade...
            else {
                Player trader = Players.getNearest(traderName);
                if (tradePending && trader != null) {
                    Players.getNearest(traderName).interact("Trade with");
                    tradePending = false;
                }
            }
            return 500;
        }
    
        @Override
        public void notify(ChatMessageEvent msg) {
            // If not in a trade and a player trades you...
            if (!Trade.isOpen() && msg.getType().equals(ChatMessageType.TRADE)) {
                traderName = msg.getSource();
                tradePending = true;
            }
        }
    }
    


  • Cheers! Was needing this



  • Any examples of Trade.Offer? Cant quit understand that one. No examples or explained documentation.



  • I'm just learning as well but I figured it's very similar to the bank interface


 

65
Online

8.6k
Users

905
Topics

11.7k
Posts