Package

Source Code of TickDelayer

import com.sk89q.craftbook.access.Action;
import com.sk89q.craftbook.access.WorldInterface;

import java.util.*;

/**
* Thread for delaying redstone inputs.
*
* @author Lymia
* @author sk89q
*/
public class TickDelayer implements Runnable {

    /**
     * List of actions to delay.
     */
    private Set<Action> delayedActions = Collections.synchronizedSet(new LinkedHashSet<Action>());

    private WorldInterface world;

    public TickDelayer(WorldInterface world) {

        this.world = world;
    }

    /**
     * Delay an action.
     *
     * @param action
     */
    public void delayAction(Action action) {

        delayedActions.add(action);
    }

    /**
     * Run thread.
     */
    public void run() {

        long currentTick = world.getTime();

        ArrayList<Action> actionQueue = new ArrayList<Action>();

        for (Iterator<Action> it = delayedActions.iterator(); it.hasNext(); ) {
            Action action = it.next();
            if (action.getRunAt() <= currentTick) {
                it.remove();
                actionQueue.add(action);
            }
        }

        for (Action action : actionQueue) {
            try {
                action.run();
            } catch (Throwable t) {
                t.printStackTrace();
            }
        }
    }
}
TOP

Related Classes of TickDelayer

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.