From c8a40e83744c5ec3ac24378ec190bc30557b1d9e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=9D=E5=A4=8F=E5=90=8C=E5=AD=A6?= <2411829240@qq.com> Date: Fri, 6 Sep 2024 20:03:19 +0800 Subject: [PATCH] Fix some bug and add DynamicDuration --- .../wohaopa/GTNHModify/CommonProxy.java | 4 + .../wohaopa/GTNHModify/EventHandler.java | 31 ++++++++ .../wohaopa/GTNHModify/config/Config.java | 76 ++++++++++--------- .../tweakers/GGNaquadahReactorTweaker.java | 7 +- .../wohaopa/GTNHModify/tweakers/ITweaker.java | 2 + .../wohaopa/GTNHModify/tweakers/Tweakers.java | 5 +- .../tweakers/gt/DynamicDuration.java | 71 +++++++++++++++++ .../tweakers/gt/EnergyLessTweaker.java | 7 +- .../GTNHModify/tweakers/gt/InputOne.java | 7 +- .../tweakers/gt/OneTickTweaker.java | 7 +- .../GTNHModify/tweakers/gt/Output64.java | 7 +- .../GTNHModify/tweakers/gt/TenthsTweaker.java | 7 +- .../GTNHModify/tweakers/handler/Handler.java | 7 +- 13 files changed, 194 insertions(+), 44 deletions(-) create mode 100644 src/main/java/com/github/wohaopa/GTNHModify/EventHandler.java create mode 100644 src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/DynamicDuration.java diff --git a/src/main/java/com/github/wohaopa/GTNHModify/CommonProxy.java b/src/main/java/com/github/wohaopa/GTNHModify/CommonProxy.java index d99e2b9..8693202 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/CommonProxy.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/CommonProxy.java @@ -3,6 +3,7 @@ import com.github.wohaopa.GTNHModify.config.Config; import com.github.wohaopa.GTNHModify.tweakers.Tweakers; +import cpw.mods.fml.common.FMLCommonHandler; import cpw.mods.fml.common.event.FMLInitializationEvent; import cpw.mods.fml.common.event.FMLPostInitializationEvent; import cpw.mods.fml.common.event.FMLPreInitializationEvent; @@ -14,6 +15,9 @@ public class CommonProxy { // GameRegistry." (Remove if not needed) public void preInit(FMLPreInitializationEvent event) { Config.init(event.getSuggestedConfigurationFile()); + FMLCommonHandler.instance() + .bus() + .register(new EventHandler()); } // load "Do your mod setup. Build whatever data structures you care about. Register recipes." (Remove if not needed) diff --git a/src/main/java/com/github/wohaopa/GTNHModify/EventHandler.java b/src/main/java/com/github/wohaopa/GTNHModify/EventHandler.java new file mode 100644 index 0000000..e75f452 --- /dev/null +++ b/src/main/java/com/github/wohaopa/GTNHModify/EventHandler.java @@ -0,0 +1,31 @@ +package com.github.wohaopa.GTNHModify; + +import com.github.wohaopa.GTNHModify.tweakers.Tweakers; +import com.github.wohaopa.GTNHModify.tweakers.gt.DynamicDuration; + +import cpw.mods.fml.common.eventhandler.SubscribeEvent; +import cpw.mods.fml.common.gameevent.TickEvent; + +public class EventHandler { + + long lastUpdateTime = 0; + long ticks = 0; + + @SubscribeEvent + public void onTickPost(TickEvent.ServerTickEvent event) { + if (Tweakers.Dynamic_Duration.enabled) if (event.phase == TickEvent.Phase.END) { + ticks++; + long now = System.currentTimeMillis(); + if (now - lastUpdateTime > 10000) { // 10秒 + if (lastUpdateTime != 0) { + if (ticks < 195) { + DynamicDuration.instance.setF((float) (ticks * 50.0) / (now - lastUpdateTime)); + DynamicDuration.update(); + } + ticks = 0; + } + lastUpdateTime = now; + } + } + } +} diff --git a/src/main/java/com/github/wohaopa/GTNHModify/config/Config.java b/src/main/java/com/github/wohaopa/GTNHModify/config/Config.java index 3eb052d..1d9b7c8 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/config/Config.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/config/Config.java @@ -40,41 +40,47 @@ private static void loadConfig() { for (Tweakers tweaker : Tweakers.values()) { tweaker.enabled = config .getBoolean(tweaker.name, Configuration.CATEGORY_GENERAL, false, tweaker.description); - if (tweaker.setting != null) { - if (tweaker.setting instanceof String) { - tweaker.setting = config.getString( - tweaker.name + "_setting", - Configuration.CATEGORY_GENERAL, - (String) tweaker.setting, - tweaker.description); - } else if (tweaker.setting instanceof Boolean) { - tweaker.setting = config.getBoolean( - tweaker.name + "_setting", - Configuration.CATEGORY_GENERAL, - (Boolean) tweaker.setting, - tweaker.description); - } else if (tweaker.setting instanceof Integer) { - tweaker.setting = config.getInt( - tweaker.name + "_setting", - Configuration.CATEGORY_GENERAL, - (int) tweaker.setting, - Integer.MIN_VALUE, - Integer.MAX_VALUE, - tweaker.description); - } else if (tweaker.setting instanceof String[]) { - tweaker.setting = config.getStringList( - tweaker.name + "_setting", - Configuration.CATEGORY_GENERAL, - (String[]) tweaker.setting, - tweaker.description); - } else if (tweaker.setting instanceof Float) { - tweaker.setting = config.getFloat( - tweaker.name + "_setting", - Configuration.CATEGORY_GENERAL, - (Float) tweaker.setting, - Float.MIN_VALUE, - Float.MAX_VALUE, - tweaker.description); + Object setting = tweaker.tweaker.getSettings(); + if (setting != null) { + if (setting instanceof String) { + tweaker.tweaker.setSetting( + config.getString( + tweaker.name + "_setting", + Configuration.CATEGORY_GENERAL, + (String) setting, + tweaker.description)); + } else if (setting instanceof Boolean) { + tweaker.tweaker.setSetting( + config.getBoolean( + tweaker.name + "_setting", + Configuration.CATEGORY_GENERAL, + (Boolean) setting, + tweaker.description)); + } else if (setting instanceof Integer) { + tweaker.tweaker.setSetting( + config.getInt( + tweaker.name + "_setting", + Configuration.CATEGORY_GENERAL, + (int) setting, + Integer.MIN_VALUE, + Integer.MAX_VALUE, + tweaker.description)); + } else if (setting instanceof String[]) { + tweaker.tweaker.setSetting( + config.getStringList( + tweaker.name + "_setting", + Configuration.CATEGORY_GENERAL, + (String[]) setting, + tweaker.description)); + } else if (setting instanceof Float) { + tweaker.tweaker.setSetting( + config.getFloat( + tweaker.name + "_setting", + Configuration.CATEGORY_GENERAL, + (Float) setting, + Float.MIN_VALUE, + Float.MAX_VALUE, + tweaker.description)); } } diff --git a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/GGNaquadahReactorTweaker.java b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/GGNaquadahReactorTweaker.java index e9ccce9..a13fb1b 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/GGNaquadahReactorTweaker.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/GGNaquadahReactorTweaker.java @@ -4,7 +4,7 @@ public class GGNaquadahReactorTweaker extends ITweaker { - Integer integer = 10; + int integer = 10; @Override public void apply() { @@ -16,4 +16,9 @@ public void apply() { public Object getSettings() { return integer; } + + @Override + public void setSetting(Object s) { + integer = (int) s; + } } diff --git a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/ITweaker.java b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/ITweaker.java index 1298978..efaff67 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/ITweaker.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/ITweaker.java @@ -17,4 +17,6 @@ void exportRecipe() {} public Object getSettings() { return null; } + + public void setSetting(Object setting) {} } diff --git a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/Tweakers.java b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/Tweakers.java index bf51af3..709e867 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/Tweakers.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/Tweakers.java @@ -1,5 +1,6 @@ package com.github.wohaopa.GTNHModify.tweakers; +import com.github.wohaopa.GTNHModify.tweakers.gt.DynamicDuration; import com.github.wohaopa.GTNHModify.tweakers.gt.EnergyLessTweaker; import com.github.wohaopa.GTNHModify.tweakers.gt.InputOne; import com.github.wohaopa.GTNHModify.tweakers.gt.OneTickTweaker; @@ -18,6 +19,8 @@ public enum Tweakers { EnergyLess("GT_Recipe EnergyLess", "GT_Recipe requires no energy.", new EnergyLessTweaker()), Input_1("GT_Recipe Input_1", "The input of GT_Recipe is 1.", new InputOne()), Output("GT_Recipe Input_64", "The output of GT_Recipe is 64.", new Output64()), + Dynamic_Duration("GT_Recipe DynamicDuration", "The duration will calculate the multiplier based on real time", + DynamicDuration.instance), // GG Naquadah reactor GGNaquadahReactor("Other GGNaquadahReactor", "Ten times the burn time.", new GGNaquadahReactorTweaker()), @@ -45,12 +48,10 @@ public static void initialize() { public final String description; public final ITweaker tweaker; public boolean enabled; - public Object setting; Tweakers(String name, String description, ITweaker tweaker) { this.name = name; this.description = description; this.tweaker = tweaker; - setting = tweaker.getSettings(); } } diff --git a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/DynamicDuration.java b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/DynamicDuration.java new file mode 100644 index 0000000..78f0afd --- /dev/null +++ b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/DynamicDuration.java @@ -0,0 +1,71 @@ +package com.github.wohaopa.GTNHModify.tweakers.gt; + +import com.github.wohaopa.GTNHModify.GTNHModifyMod; + +import gregtech.api.util.GT_Recipe; + +public class DynamicDuration extends GT_RecipeTweaker { + + public static DynamicDuration instance = new DynamicDuration(); + private static final Thread updateThread = new Thread(() -> { + while (true) { + if (DynamicDuration.needsUpdate) { + GTNHModifyMod.LOG.info("Updating DynamicDuration: f = {}", instance.aFloat); + long a = System.currentTimeMillis(); + instance.apply(); + DynamicDuration.needsUpdate = false; + GTNHModifyMod.LOG.info("Updating DynamicDuration: use time {}ms", System.currentTimeMillis() - a); + } + Thread.yield(); + try { + Thread.sleep(100); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + }); + private static boolean running = false; + private static boolean needsUpdate = false; + + static { + updateThread.setDaemon(true); + updateThread.setName("GTNHModify DynamicDuration UpdateThread"); + } + + public static void update() { + needsUpdate = true; + if (!running) { + running = true; + updateThread.start(); + } + } + + public void setF(float f) { + f0 = f / f0 * aFloat; + } + + private float aFloat = 0.9f; + private float f0 = 1f; + + @Override + protected void modifyGT_Recipe(GT_Recipe aRecipe) { + aRecipe.mDuration *= (int) f0; + if (aRecipe.mDuration < 1) aRecipe.mDuration = 1; + } + + @Override + protected void modifyGT_Recipe_AssemblyLine(GT_Recipe.GT_Recipe_AssemblyLine aRecipe) { + aRecipe.mDuration *= (int) f0; + if (aRecipe.mDuration < 1) aRecipe.mDuration = 1; + } + + @Override + public Object getSettings() { + return aFloat; + } + + @Override + public void setSetting(Object s) { + aFloat = (float) s; + } +} diff --git a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/EnergyLessTweaker.java b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/EnergyLessTweaker.java index aee0f06..13c23c7 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/EnergyLessTweaker.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/EnergyLessTweaker.java @@ -4,7 +4,7 @@ public class EnergyLessTweaker extends GT_RecipeTweaker { - Integer integer = 0; + int integer = 0; @Override protected void modifyGT_Recipe(GT_Recipe aRecipe) { @@ -20,4 +20,9 @@ protected void modifyGT_Recipe_AssemblyLine(GT_Recipe.GT_Recipe_AssemblyLine aRe public Object getSettings() { return integer; } + + @Override + public void setSetting(Object s) { + integer = (int) s; + } } diff --git a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/InputOne.java b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/InputOne.java index 5d8c3d5..6ed7d6e 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/InputOne.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/InputOne.java @@ -7,7 +7,7 @@ public class InputOne extends GT_RecipeTweaker { - Integer integer = 1; + int integer = 1; @Override protected void modifyGT_Recipe(GT_Recipe aRecipe) { @@ -49,4 +49,9 @@ protected void modifyGT_Recipe_AssemblyLine(GT_Recipe.GT_Recipe_AssemblyLine aRe public Object getSettings() { return integer; } + + @Override + public void setSetting(Object s) { + integer = (int) s; + } } diff --git a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/OneTickTweaker.java b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/OneTickTweaker.java index 946ab40..8d21564 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/OneTickTweaker.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/OneTickTweaker.java @@ -4,7 +4,7 @@ public class OneTickTweaker extends GT_RecipeTweaker { - Integer integer = 0; + int integer = 0; @Override protected void modifyGT_Recipe(GT_Recipe aRecipe) { @@ -20,4 +20,9 @@ protected void modifyGT_Recipe_AssemblyLine(GT_Recipe.GT_Recipe_AssemblyLine aRe public Object getSettings() { return integer; } + + @Override + public void setSetting(Object s) { + integer = (int) s; + } } diff --git a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/Output64.java b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/Output64.java index c1603a1..d9a0a05 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/Output64.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/Output64.java @@ -7,7 +7,7 @@ public class Output64 extends GT_RecipeTweaker { - Integer integer = 64; + int integer = 64; @Override protected void modifyGT_Recipe(GT_Recipe aRecipe) { @@ -41,4 +41,9 @@ protected void modifyGT_Recipe_AssemblyLine(GT_Recipe.GT_Recipe_AssemblyLine aRe public Object getSettings() { return integer; } + + @Override + public void setSetting(Object s) { + integer = (int) s; + } } diff --git a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/TenthsTweaker.java b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/TenthsTweaker.java index 4ddc38e..a69efaf 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/TenthsTweaker.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/gt/TenthsTweaker.java @@ -4,7 +4,7 @@ public class TenthsTweaker extends GT_RecipeTweaker { - Integer integer = 10; + int integer = 10; @Override protected void modifyGT_Recipe(GT_Recipe aRecipe) { @@ -20,4 +20,9 @@ protected void modifyGT_Recipe_AssemblyLine(GT_Recipe.GT_Recipe_AssemblyLine aRe public Object getSettings() { return integer; } + + @Override + public void setSetting(Object s) { + integer = (int) s; + } } diff --git a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/handler/Handler.java b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/handler/Handler.java index 0ac69ec..60bc52b 100644 --- a/src/main/java/com/github/wohaopa/GTNHModify/tweakers/handler/Handler.java +++ b/src/main/java/com/github/wohaopa/GTNHModify/tweakers/handler/Handler.java @@ -6,7 +6,7 @@ public abstract class Handler extends ITweaker { private boolean enable = false; - Integer integer = 1; + int integer = 1; public int handle(Object owner, int number) { if (enable) return integer; @@ -22,4 +22,9 @@ protected void apply() { public Object getSettings() { return integer; } + + @Override + public void setSetting(Object s) { + integer = (int) s; + } }