Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
Fix some bug and add DynamicDuration
Browse files Browse the repository at this point in the history
  • Loading branch information
wohaopa committed Sep 6, 2024
1 parent 19516be commit c8a40e8
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 44 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/github/wohaopa/GTNHModify/CommonProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/github/wohaopa/GTNHModify/EventHandler.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}
}
76 changes: 41 additions & 35 deletions src/main/java/com/github/wohaopa/GTNHModify/config/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class GGNaquadahReactorTweaker extends ITweaker {

Integer integer = 10;
int integer = 10;

@Override
public void apply() {
Expand All @@ -16,4 +16,9 @@ public void apply() {
public Object getSettings() {
return integer;
}

@Override
public void setSetting(Object s) {
integer = (int) s;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ void exportRecipe() {}
public Object getSettings() {
return null;
}

public void setSetting(Object setting) {}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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()),
Expand Down Expand Up @@ -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();
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class EnergyLessTweaker extends GT_RecipeTweaker {

Integer integer = 0;
int integer = 0;

@Override
protected void modifyGT_Recipe(GT_Recipe aRecipe) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class InputOne extends GT_RecipeTweaker {

Integer integer = 1;
int integer = 1;

@Override
protected void modifyGT_Recipe(GT_Recipe aRecipe) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class OneTickTweaker extends GT_RecipeTweaker {

Integer integer = 0;
int integer = 0;

@Override
protected void modifyGT_Recipe(GT_Recipe aRecipe) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

public class Output64 extends GT_RecipeTweaker {

Integer integer = 64;
int integer = 64;

@Override
protected void modifyGT_Recipe(GT_Recipe aRecipe) {
Expand Down Expand Up @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

public class TenthsTweaker extends GT_RecipeTweaker {

Integer integer = 10;
int integer = 10;

@Override
protected void modifyGT_Recipe(GT_Recipe aRecipe) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -22,4 +22,9 @@ protected void apply() {
public Object getSettings() {
return integer;
}

@Override
public void setSetting(Object s) {
integer = (int) s;
}
}

0 comments on commit c8a40e8

Please sign in to comment.