Skip to content

Out of Sync Delay

This is the example for the Out of Sync Delay. It is used to keep playing the replay for a few seconds after it goes out of sync. So it doesn’t stop immediately when it goes out of sync.

package com.example;

import java.util.Timer;
import java.util.TimerTask;

import com.originmint.managers.LoggingManager;
import com.originmint.managers.PluginManager;
import com.originmint.managers.ReplayManager;
import com.originmint.plugin.IPlugin;
import com.originmint.plugin.eventbus.Subscribe;
import com.originmint.plugin.events.SyncEvent;
import com.originmint.plugin.settings.IntSlider;

public class Example implements IPlugin {

  IntSlider delay = new IntSlider("example.delay", "Delay(S)", "Delay in seconds", 0, 15, 5);

  @Override
  public void onEnable() {
      // Register the plugin with the PluginManager to receive events
      PluginManager.getInstance().PLUGIN_EVENT_BUS.register(this);
  }


  @Override
  public void onDisable() {
      // Unregister the plugin with the PluginManager to stop receiving events
      PluginManager.getInstance().PLUGIN_EVENT_BUS.unregister(this);
  }

  boolean outOfSync = false;

  // Subscribe to the SyncEvents
  @Subscribe
  public void onClientTick(SyncEvent event) {
      if(outOfSync){
          event.cancelEvent();
          return;
      }
      if(event.phase == SyncEvent.Phase.PRE && !outOfSync){
          event.cancelEvent();
          outOfSync = true;
          Timer timer = new Timer();
          LoggingManager.getInstance().log("Out of sync deteccted, stopping in " + delay.value + " seconds!", LoggingManager.LOG_LEVEL.ERROR);
          timer.schedule(new TimerTask() {

              @Override
              public void run() {
                  ReplayManager.getInstance().outOfSync();
                  LoggingManager.getInstance().log("Out of sync: Full stopping: "+ event.type.name(), LoggingManager.LOG_LEVEL.ERROR);
                  outOfSync = false;
              }

          }, delay.value * 1000);
      }
  }
}