-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wandering trades, Trades Config Rework (#184)
* Start on WanderingTrades Signed-off-by: Joseph T. McQuigg <[email protected]> * Change Trades Config Signed-off-by: Joseph T. McQuigg <[email protected]> * Add Mushrooms, lily pads, and flowers to wandering traders Signed-off-by: Joseph T. McQuigg <[email protected]> * Update Config stuff Signed-off-by: Joseph T. McQuigg <[email protected]> * More Trade Config Signed-off-by: Joseph T. McQuigg <[email protected]> * Adjustments Signed-off-by: Joseph T. McQuigg <[email protected]> * ChangeLog Signed-off-by: Joseph T. McQuigg <[email protected]> * Fix some Issues with Null Issues Signed-off-by: Joseph T. McQuigg <[email protected]> --------- Signed-off-by: Joseph T. McQuigg <[email protected]>
- Loading branch information
Showing
9 changed files
with
183 additions
and
148 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
Common/src/main/java/net/potionstudios/biomeswevegone/config/ConfigUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package net.potionstudios.biomeswevegone.config; | ||
|
||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.Codec; | ||
import corgitaco.corgilib.serialization.jankson.JanksonJsonOps; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.Jankson; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.JsonElement; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.JsonGrammar; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.JsonObject; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.api.SyntaxError; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
|
||
public class ConfigUtils { | ||
public static <T> T loadConfig(Path path, Codec<T> codec, T defaultConfig) { | ||
if (!path.toFile().exists()) { | ||
createDefaultFile(path, codec, defaultConfig); | ||
return defaultConfig; | ||
} | ||
|
||
Jankson build = new Jankson.Builder().build(); | ||
try { | ||
String configFile = Files.readString(path).strip(); | ||
JsonObject load = build.load(configFile); | ||
Pair<T, JsonElement> configResult = codec.decode(JanksonJsonOps.INSTANCE, load).result().orElseThrow(); | ||
T config = configResult.getFirst(); | ||
createDefaultFile(path, codec, config); | ||
return config; | ||
} catch (IOException | SyntaxError e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public static <T> void createDefaultFile(Path path, Codec<T> codec, T config) { | ||
JsonElement jsonElement = codec.encodeStart(JanksonJsonOps.INSTANCE, config).result().orElseThrow(); | ||
String json = jsonElement.toJson(JsonGrammar.JSON5); | ||
try { | ||
Files.createDirectories(path.getParent()); | ||
Files.writeString(path, json); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
public record CommentValue<T>(String comment, T value) { | ||
public static <T> CommentValue<T> of(String comment, T value) { | ||
return new CommentValue<>(comment, value); | ||
} | ||
} | ||
} |
79 changes: 17 additions & 62 deletions
79
Common/src/main/java/net/potionstudios/biomeswevegone/config/configs/BWGTradesConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,28 @@ | ||
package net.potionstudios.biomeswevegone.config.configs; | ||
|
||
import com.google.common.base.Suppliers; | ||
import com.mojang.datafixers.util.Pair; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
import corgitaco.corgilib.serialization.codec.CommentedCodec; | ||
import corgitaco.corgilib.serialization.jankson.JanksonJsonOps; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.Jankson; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.JsonElement; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.JsonGrammar; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.JsonObject; | ||
import corgitaco.corgilib.shadow.blue.endless.jankson.api.SyntaxError; | ||
import net.potionstudios.biomeswevegone.PlatformHandler; | ||
import org.jetbrains.annotations.NotNull; | ||
import net.potionstudios.biomeswevegone.config.ConfigLoader; | ||
import net.potionstudios.biomeswevegone.config.ConfigUtils; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.function.Supplier; | ||
public class BWGTradesConfig { | ||
|
||
public record BWGTradesConfig(boolean enableTrades, boolean enableVanillaTradeAdditions) { | ||
public static final BWGTradesConfig INSTANCE = ConfigLoader.loadConfig(BWGTradesConfig.class, "trades"); | ||
|
||
private static final Path PATH = PlatformHandler.PLATFORM_HANDLER.configPath().resolve("trades.json5"); | ||
public BWGTrades trades = new BWGTrades(); | ||
|
||
@NotNull | ||
public static Supplier<BWGTradesConfig> INSTANCE = Suppliers.memoize(BWGTradesConfig::getOrCreateConfigFromDisk); | ||
public static class BWGTrades { | ||
public ConfigUtils.CommentValue<Boolean> disableTrades = ConfigUtils.CommentValue.of("Disable All BWG Trades, If this is set to true none of the values below will matter", false); | ||
} | ||
|
||
private static final Codec<BWGTradesConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group( | ||
CommentedCodec.of(Codec.BOOL, "enable_trades", "Whether to enable BWG Villager Trades").orElse(true).forGetter(config -> true), | ||
CommentedCodec.of(Codec.BOOL, "enable_vanilla_trade_additions", "Whether to add BWG Villager Trades to Vanilla Villagers").orElse(true).forGetter(config -> true) | ||
).apply(instance, BWGTradesConfig::new)); | ||
public BWGVillagerTradesConfig villagerTrades = new BWGVillagerTradesConfig(); | ||
|
||
private static BWGTradesConfig createDefault() { | ||
return new BWGTradesConfig(true, true); | ||
} | ||
public static class BWGVillagerTradesConfig { | ||
public ConfigUtils.CommentValue<Boolean> allowBWGForagerTrades = ConfigUtils.CommentValue.of("Allow BWG Forager Profession Trades", true); | ||
public ConfigUtils.CommentValue<Boolean> enableBWGVanillaProfessionTradeAdditions = ConfigUtils.CommentValue.of("Allows BWG Items to be added to Vanilla Profession Trades", true); | ||
} | ||
|
||
private static BWGTradesConfig getOrCreateConfigFromDisk() { | ||
BWGTradesConfig defaultConfig = createDefault(); | ||
public BWGWanderingTraderTradesConfig wanderingTraderTrades = new BWGWanderingTraderTradesConfig(); | ||
|
||
if (!PATH.toFile().exists()) { | ||
createDefaultFile(defaultConfig); | ||
return defaultConfig; | ||
} | ||
|
||
Jankson build = new Jankson.Builder().build(); | ||
try { | ||
String configFile = Files.readString(PATH).stripTrailing().trim().strip().stripLeading(); | ||
JsonObject load = build.load(configFile); | ||
Pair<BWGTradesConfig, JsonElement> configResult = CODEC.decode(JanksonJsonOps.INSTANCE, load).result().orElseThrow(); | ||
BWGTradesConfig config = configResult.getFirst(); | ||
|
||
BWGTradesConfig toCreate = new BWGTradesConfig(config.enableTrades, config.enableVanillaTradeAdditions); | ||
|
||
createDefaultFile(toCreate); | ||
return toCreate; | ||
|
||
} catch (IOException | SyntaxError e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
private static void createDefaultFile(BWGTradesConfig tradesConfig) { | ||
JsonElement jsonElement = CODEC.encodeStart(JanksonJsonOps.INSTANCE, tradesConfig).result().orElseThrow(); | ||
String json = jsonElement.toJson(JsonGrammar.JSON5); | ||
try { | ||
Files.createDirectories(PATH.getParent()); | ||
Files.writeString(PATH, json); | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
public static class BWGWanderingTraderTradesConfig { | ||
public ConfigUtils.CommentValue<Boolean> enableBWGItemsTrades = ConfigUtils.CommentValue.of("Allows BWG Items to be added to Wandering Trader Offerings", true); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.