Add delayed executor service to libzontreck, moving it out of OTEMod

This commit is contained in:
Aria 2023-02-27 14:03:41 -07:00
parent de6486ec44
commit b92a261c2c
4 changed files with 77 additions and 3 deletions

View file

@ -13,6 +13,7 @@ import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingEvent;
import net.minecraftforge.event.entity.living.LivingEvent.LivingUpdateEvent;
import net.minecraftforge.event.server.ServerStartedEvent;
import net.minecraftforge.event.server.ServerStoppingEvent;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@ -25,13 +26,14 @@ public class LibZontreck {
public static final String MOD_ID = "libzontreck";
public static MinecraftServer THE_SERVER;
public static VolatilePlayerStorage playerStorage;
public static boolean ALIVE;
public LibZontreck(){
LibZontreck.playerStorage=new VolatilePlayerStorage();
IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();
// Register the setup method for modloading
bus.addListener(this::setup);
MinecraftForge.EVENT_BUS.register(this);
}
@ -45,6 +47,13 @@ public class LibZontreck {
public void onServerStarted(final ServerStartedEvent event)
{
THE_SERVER = event.getServer();
ALIVE=true;
}
@SubscribeEvent
public void onServerStopping(final ServerStoppingEvent ev)
{
ALIVE=false;
}

View file

@ -0,0 +1,65 @@
package dev.zontreck.libzontreck.util;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import dev.zontreck.libzontreck.LibZontreck;
import net.minecraftforge.event.TickEvent.ServerTickEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.common.Mod.EventBusSubscriber;
@EventBusSubscriber(modid=LibZontreck.MOD_ID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class DelayedExecutorService {
private static int COUNT = 0;
public class DelayedExecution
{
public DelayedExecution(Runnable run, long unix) {
scheduled=run;
unix_time=unix;
}
public Runnable scheduled;
public long unix_time;
}
public List<DelayedExecution> EXECUTORS = new ArrayList<>();
public void schedule(Runnable run, int seconds)
{
long unix = Instant.now().getEpochSecond()+ (seconds);
DelayedExecution exe = new DelayedExecution(run,unix);
EXECUTORS.add(exe);
}
@SubscribeEvent
public void onTick(ServerTickEvent ev)
{
if(!LibZontreck.ALIVE)
{
LibZontreck.LOGGER.info("Tearing down delayed executor service");
return;
}
Iterator<DelayedExecution> it = EXECUTORS.iterator();
while(it.hasNext())
{
DelayedExecution e = it.next();
if(e.unix_time < Instant.now().getEpochSecond())
{
it.remove();
Thread tx = new Thread(e.scheduled);
tx.setName("DelayedExecutorTask-"+String.valueOf(DelayedExecutorService.getNext()));
tx.start();
}
}
}
public static int getNext()
{
COUNT++;
return COUNT;
}
}

View file

@ -19,7 +19,7 @@ modId="libzontreck" #mandatory
# The version number of the mod - there's a few well known ${} variables useable here or just hardcode it
# ${file.jarVersion} will substitute the value of the Implementation-Version as read from the mod's JAR file metadata
# see the associated build.gradle script for how to populate this completely automatically during a build
version="1.0.4.13" #mandatory
version="1.0.5.1" #mandatory
# A display name for the mod
displayName="LibZontreck" #mandatory
# A URL to query for updates for this mod. See the JSON update specification https://mcforge.readthedocs.io/en/latest/gettingstarted/autoupdate/