Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Efficiently Handling Smelting with AnimationListener in Dreambot


    Deep Slayer

    Recommended Posts

    import org.dreambot.api.methods.interactive.GameObjects;
    import org.dreambot.api.methods.interactive.Players;
    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.script.listener.AnimationListener;
    import org.dreambot.api.wrappers.interactive.GameObject;
    import org.dreambot.api.wrappers.interactive.Player;
    
    
    @ScriptManifest(author = "Deep Slayer",
            name = "Testing script",
            version = 1.0, description = "for testing snippets",
            category = Category.MISC
    )
    
    public class TestingPurposes extends AbstractScript implements AnimationListener {
        private long idleAnimationStartTime = -1; // Time when the idle animation starts
        private boolean finishedSmelting = false;
    
        @Override
        public void onPlayerAnimation(Player player, int animation, int animationDelay) {
            // Check if not animating
            if (Players.getLocal().getAnimation() == -1) {
                // If not animating record the time
                if (idleAnimationStartTime == -1) {
                    idleAnimationStartTime = System.currentTimeMillis();
                } else {
                    // Check if it has been more than 2 seconds
                    if (System.currentTimeMillis() - idleAnimationStartTime > 2000) {
                        finishedSmelting = true;
                        log("We are finished smelting");
                    }
                }
            } else {
                // Reset the timer if the player starts a new animation
                idleAnimationStartTime = -1;
                finishedSmelting = false;
                log("We are NOT finished smelting");
            }
        }
        @Override
        public int onLoop() {
    
            GameObject furnace = GameObjects.closest("Furnace");
    
            if (furnace != null && furnace.isOnScreen() && Players.getLocal().isStandingStill()) {
                if (finishedSmelting) {
                    furnace.interact("Smelt");
                    sleep(2000, 2500); // Adjust sleep as necessary
                    finishedSmelting = false; // Reset flag after interaction
                }
            }
    
    
            return 500;
        }
        
    
    
    }
    Edited by Deep Slayer
    Link to comment
    Share on other sites

    I've put together a small snippet that demonstrates how to effectively detect the end of a smithing animation (or really any animation where you're waiting for the character to return to an idle state) using the AnimationListener interface provided by the Dreambot API.

    Purpose: The snippet helps to identify when a player has finished smithing by monitoring for when the character has been in the idle state (animation ID: -1) for more than 2 seconds. This can be particularly useful in scripts where the next action depends on whether the current task has been completed.

    How It Works:

    Implements AnimationListener to monitor animation changes.
    Uses a flag, finishedSmelting, which is set to true once the player has been idle for more than 2 seconds, indicating the end of an activity like smelting.
    Resets finishedSmelting after the desired action is taken, readying it for the next cycle.

    Link to comment
    Share on other sites

    Thanks for sharing, we also have something in the API that works in cases like these if you're fine with sleeping:

    https://dreambot.org/javadocs/org/dreambot/api/utilities/Sleep.html#sleepUntil(org.dreambot.api.utilities.impl.Condition,org.dreambot.api.utilities.impl.Condition,long,long,int)

    Which in the case of animating would look like this:

    Sleep.sleepUntil(() -> !Players.getLocal().isAnimating(), () -> Players.getLocal().isAnimating(), 5000, 200, 10);

    Which would sleep until you've not animated for 10 polling periods (set to 200ms, so that'd be 2 seconds in this case), resetting the overall timeout each time the player is found animating.

    Link to comment
    Share on other sites

    Thank you for pointing out the overloaded sleepUntil method, Pandemic.

    I wasn't aware of this particular usage, which indeed seems efficient for handling animation states. The method you've shared definitely streamlines the process, I've tried to tailor the ChatListener approach to allow for additional random behaviours (like adjusting the screen or switching tabs) during the animation state with ChatListener. This was an attempt to mimic a more human-like behaviour pattern during these periods.

    I appreciate your suggestion, as it offers a solid alternative for simple use cases.

    Link to comment
    Share on other sites

    Technically you can run functions that always return false inside the lambda functions to run while it is sleeping. This could give you your desired results pretty easily.

    
    private boolean doAntiBanStuff(){
    	// enter the stuff you wanna do while the animation sleep is taking place and never return true
    
    	return false;
    }
    
    
    
    
    Sleep.sleepUntil(() -> doAntiBanStuff() || !Players.getLocal().isAnimating(), 5000, 10);

     

    Link to comment
    Share on other sites

    thanks for feedback,

    this is what we need assuming doAntiBanStuff() returns false every time

    Sleep.sleepUntil(() -> doAntiBanStuff() || !Players.getLocal().isAnimating(), () -> Players.getLocal().isAnimating(), 5000, 200, 10);
    

    Thank you guys,

    much more concise and efficient

    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.