From b92a261c2c71c370587e299a38113a1f2b2e2780 Mon Sep 17 00:00:00 2001 From: Aria Date: Mon, 27 Feb 2023 14:03:41 -0700 Subject: [PATCH] Add delayed executor service to libzontreck, moving it out of OTEMod --- gradle.properties | 2 +- .../dev/zontreck/libzontreck/LibZontreck.java | 11 +++- .../util/DelayedExecutorService.java | 65 +++++++++++++++++++ src/main/resources/META-INF/mods.toml | 2 +- 4 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 src/main/java/dev/zontreck/libzontreck/util/DelayedExecutorService.java diff --git a/gradle.properties b/gradle.properties index 238cc1d..57a048b 100644 --- a/gradle.properties +++ b/gradle.properties @@ -5,5 +5,5 @@ org.gradle.daemon=false mc_version=1.18.2 forge_version=40.2.1 -myversion=1.0.4.13 +myversion=1.0.5.1 parchment_version=2022.11.06 \ No newline at end of file diff --git a/src/main/java/dev/zontreck/libzontreck/LibZontreck.java b/src/main/java/dev/zontreck/libzontreck/LibZontreck.java index 5a04ebd..b82ba11 100644 --- a/src/main/java/dev/zontreck/libzontreck/LibZontreck.java +++ b/src/main/java/dev/zontreck/libzontreck/LibZontreck.java @@ -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; } diff --git a/src/main/java/dev/zontreck/libzontreck/util/DelayedExecutorService.java b/src/main/java/dev/zontreck/libzontreck/util/DelayedExecutorService.java new file mode 100644 index 0000000..1686740 --- /dev/null +++ b/src/main/java/dev/zontreck/libzontreck/util/DelayedExecutorService.java @@ -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 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 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; + } +} diff --git a/src/main/resources/META-INF/mods.toml b/src/main/resources/META-INF/mods.toml index 8a39b40..fa3c8fd 100644 --- a/src/main/resources/META-INF/mods.toml +++ b/src/main/resources/META-INF/mods.toml @@ -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/