Skip to content

Commit 28b7dc5

Browse files
committed
Add config hot reloading listening on Data Pack Reload.
1 parent 6432f16 commit 28b7dc5

File tree

3 files changed

+37
-19
lines changed

3 files changed

+37
-19
lines changed

src/main/java/net/kjp12/ddc/Config.java

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class Config {
4242
*/
4343
private static final Gson GSON = new GsonBuilder().enableComplexMapKeySerialization().setPrettyPrinting()
4444
.setLenient().registerTypeAdapter(Identifier.class, new IdentifierTypeAdaptor()).create();
45-
public static final Config INSTANCE;
45+
public static Config instance;
4646

4747
/**
4848
* Whether the config is being initialised for the first time.
@@ -84,6 +84,10 @@ public class Config {
8484
public Map<Identifier, Float> compostableItems;
8585

8686
static {
87+
reload();
88+
}
89+
90+
public static void reload() {
8791
Config instance = null;
8892
if (Files.exists(config)) {
8993
try (var reader = Files.newBufferedReader(config)) {
@@ -92,11 +96,19 @@ public class Config {
9296
Main.logger.warn("Unable to read config, regenerating...", ioe);
9397
}
9498
}
99+
95100
if (instance == null) {
96-
instance = new Config();
97-
instance.generating = true;
101+
if (Config.instance == null) {
102+
instance = new Config();
103+
instance.generating = true;
104+
} else {
105+
Main.logger.warn("Configuration already in memory, writing back to disc...");
106+
Config.instance.write();
107+
return;
108+
}
98109
}
99-
INSTANCE = instance;
110+
111+
Config.instance = instance;
100112
}
101113

102114
/* Private as you shouldn't be directly initialising this. */
@@ -119,11 +131,15 @@ public void generateSettings() {
119131
ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE
120132
.forEach((item, value) -> compostableItems.put(Registry.ITEM.getId(item.asItem()), value));
121133

122-
try (var writer = Files.newBufferedWriter(config)) {
123-
GSON.toJson(this, writer);
124-
} catch (IOException ioe) {
125-
Main.logger.warn("Unable to write config?", ioe);
126-
}
134+
write();
135+
}
136+
}
137+
138+
private void write() {
139+
try (var writer = Files.newBufferedWriter(config)) {
140+
GSON.toJson(this, writer);
141+
} catch (IOException ioe) {
142+
Main.logger.warn("Unable to write config?", ioe);
127143
}
128144
}
129145

src/main/java/net/kjp12/ddc/Main.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ public static void init() {
4141
vanillaCompostableItems = new Object2FloatArrayMap<>(ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE);
4242

4343
// Generate settings if they aren't already present.
44-
if (!Config.INSTANCE.isReady()) {
45-
Config.INSTANCE.generateSettings();
44+
if (!Config.instance.isReady()) {
45+
Config.instance.generateSettings();
4646
}
4747

4848
ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.clear();
@@ -55,8 +55,10 @@ public static void init() {
5555
// This is somewhat hacky, but intends to make sure that other implementations
5656
// such as QSL has the ability to register its own additions via its internal
5757
// registries.
58-
ServerLifecycleEvents.START_DATA_PACK_RELOAD
59-
.register((server, resourceManager) -> ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.clear());
58+
ServerLifecycleEvents.START_DATA_PACK_RELOAD.register((server, resourceManager) -> {
59+
ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.clear();
60+
Config.reload();
61+
});
6062

6163
ServerLifecycleEvents.END_DATA_PACK_RELOAD.register((server, resourceManager, success) -> hotLoad());
6264

@@ -67,7 +69,7 @@ public static void init() {
6769
ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.putAll(vanillaCompostableItems);
6870

6971
vanillaCompostableItems = null;
70-
} else if (Config.INSTANCE.disableDefaultVanillaRegistry) {
72+
} else if (Config.instance.disableDefaultVanillaRegistry) {
7173
logger.warn("The vanillaCompostableItems map is missing. This shouldn't happen!");
7274
}
7375
});
@@ -84,18 +86,18 @@ public static void hotLoad() {
8486
// cleared.
8587
// This intends to clear any interference that may otherwise be caused by
8688
// hot reloading.
87-
if (Config.INSTANCE.disableDefaultVanillaRegistry) {
89+
if (Config.instance.disableDefaultVanillaRegistry) {
8890
// The registry is already cleared by the outer method prior to reloading.
8991
// This only clears if datapack clearing is also enabled.
90-
if (Config.INSTANCE.disableDatapackRegistry) {
92+
if (Config.instance.disableDatapackRegistry) {
9193
logger.info("[DDC] Evoking {} as `disableDatapackRegistry` for the composter is enabled.",
9294
ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE);
9395
ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE.clear();
9496
}
9597
} else {
9698
// Same as above, but redeploys the vanilla registry.
9799
// Not sure why you want this, but here you go.
98-
if (Config.INSTANCE.disableDatapackRegistry) {
100+
if (Config.instance.disableDatapackRegistry) {
99101
logger.info(
100102
"[DDC] Evoking {} in favour of vanilla registry base as `disableDatapackRegistry` for the composter is enabled.",
101103
ComposterBlock.ITEM_TO_LEVEL_INCREASE_CHANCE);
@@ -128,7 +130,7 @@ public static void hotLoad() {
128130
public static void register() {
129131
var compostableItems = new Object2FloatOpenHashMap<ItemConvertible>();
130132

131-
Config.INSTANCE.compostableItems.forEach((k, v) -> {
133+
Config.instance.compostableItems.forEach((k, v) -> {
132134
var item = Registry.ITEM.getOrEmpty(k);
133135
if (item.isEmpty()) {
134136
logger.warn("{} -> {} not preset at current time.", k, v);

src/main/java/net/kjp12/ddc/mixins/MixinComposterBlock.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class MixinComposterBlock {
3434

3535
@Redirect(method = "<clinit>", at = @At(value = "FIELD", opcode = Opcodes.PUTSTATIC, target = "Lnet/minecraft/block/ComposterBlock;ITEM_TO_LEVEL_INCREASE_CHANCE:Lit/unimi/dsi/fastutil/objects/Object2FloatMap;"))
3636
private static void ddc$redirectToLoggingMap(Object2FloatMap<ItemConvertible> value) {
37-
ITEM_TO_LEVEL_INCREASE_CHANCE = Config.INSTANCE.logAllDirectRegistration ? new LoggingObject2FloatMap<>(value)
37+
ITEM_TO_LEVEL_INCREASE_CHANCE = Config.instance.logAllDirectRegistration ? new LoggingObject2FloatMap<>(value)
3838
: value;
3939
}
4040
}

0 commit comments

Comments
 (0)