ResetPlugin.java

mudlibs/tinylib/src/net/sourceforge/pain/tinylib/plugin/reset/ResetPlugin.java · 1.8 KB

From the archive pain_0_45.tgz

Download the archive   « Back to the archive contents

package net.sourceforge.pain.tinylib.plugin.reset;

import net.sourceforge.pain.*;
import net.sourceforge.pain.plugin.*;
import net.sourceforge.pain.tinylib.*;
import net.sourceforge.pain.tinylib.data.type.*;
import net.sourceforge.pain.util.*;

import java.util.*;

public final class ResetPlugin extends Plugin implements PulseListener {

    public static final int RESETS_GRANULARITY = 10 * Pulse.PULSE_PER_SCD; // 3 seconds
    private int nextCheckTime = 0;


    public ResetPlugin() {
    }

    protected void init() throws Exception {
        Codebase.getPulse().addListener(this);
    }

    protected void deinit() {
        Codebase.getPulse().removeListener(this);
    }

    public void pulse(int time) {
        if (nextCheckTime > time) {
            return;
        }
        nextCheckTime = time + RESETS_GRANULARITY;
        Collection resetGroups = Mudlib.getWorld().getResetGroupRegistry().getResetGroups();
        for (Iterator it = resetGroups.iterator(); it.hasNext();) {
            ResetGroup g = (ResetGroup) it.next();
            final int nextResetTime = g.getNextResetTime();
            if (nextResetTime > time) {
                nextCheckTime = nextResetTime < nextCheckTime ? nextResetTime : nextCheckTime;
            } else {
                reset(g);
                int groupResetPeriod = g.getResetPeriod();
                nextCheckTime = groupResetPeriod < nextCheckTime ? groupResetPeriod : nextCheckTime;
                g.setNextResetTime(time + groupResetPeriod);
                break; // one group per pulse
            }
        }
    }

    private static void reset(ResetGroup g) {
        try {
            Codebase.processEvent("ResetEvent", g);
        } catch (Exception e) {
            Log.error("ResetPlugin:", e);
        }
    }

}

Site Stats

Snippets: 1791
Codebases: 571