Skip to content

Commit

Permalink
Wandering trades, Trades Config Rework (#184)
Browse files Browse the repository at this point in the history
* 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
JT122406 committed Dec 27, 2024
1 parent 95f8d2c commit 225abed
Show file tree
Hide file tree
Showing 9 changed files with 183 additions and 148 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
- Slight Change to FruitBlockProcessor#finalizeProcessing to be more Efficient
- Use SpawnPlacementRegisterEvent on Forge to Register Entity Spawn Placements
- Add Config to Enable/Disable Spawning of Oddion and Man O War
- Add BWG Items to Wandering Trader Trades
- New BWG Trades Config with Wandering Trader Options, and Option to disable Forager Trades

# 1.4.4
- Add Russian Translations (ru_ru) (Credits: j-tap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@ public class ConfigLoader {
*/
public static <T> T loadConfig(@NotNull Class<T> clazz, String name) {
try {
try {
Files.delete(PlatformHandler.PLATFORM_HANDLER.configPath().resolve(name + ".json5"));
} catch (Exception ignored) {}

Path configPath = PlatformHandler.PLATFORM_HANDLER.configPath().resolve(name + ".json");
T value = clazz.getConstructor().newInstance();

Expand Down
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);
}
}
}
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);
}
}
Original file line number Diff line number Diff line change
@@ -1,52 +1,45 @@
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 it.unimi.dsi.fastutil.objects.Object2BooleanMap;
import it.unimi.dsi.fastutil.objects.Object2BooleanOpenHashMap;
import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceKey;
import net.minecraft.world.level.biome.Biome;
import net.minecraft.world.level.levelgen.placement.PlacedFeature;
import net.potionstudios.biomeswevegone.PlatformHandler;
import net.potionstudios.biomeswevegone.config.ConfigUtils;
import net.potionstudios.biomeswevegone.world.level.levelgen.biome.BWGBiomes;
import net.potionstudios.biomeswevegone.world.level.levelgen.feature.placed.BWGOverworldTreePlacedFeatures;
import net.potionstudios.biomeswevegone.world.level.levelgen.feature.placed.BWGVanillaPlacedFeatures;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.function.Supplier;

public record BWGWorldGenConfig(Map<ResourceKey<Biome>, Boolean> enabledBiomes, int regionWeight,
boolean vanillaAdditions) {
boolean vanillaAdditions, Map<ResourceKey<PlacedFeature>, Boolean> vanillaFeatures) {

public static final Path PATH = PlatformHandler.PLATFORM_HANDLER.configPath().resolve("world_generation.json5");
private static final Path PATH = PlatformHandler.PLATFORM_HANDLER.configPath().resolve("world_generation.json5");

@NotNull
public static Supplier<BWGWorldGenConfig> INSTANCE = Suppliers.memoize(BWGWorldGenConfig::getOrCreateConfigFromDisk);

public static final Codec<BWGWorldGenConfig> CODEC = RecordCodecBuilder.create(instance ->
private static final Codec<BWGWorldGenConfig> CODEC = RecordCodecBuilder.create(instance ->
instance.group(
CommentedCodec.of(Codec.unboundedMap(ResourceKey.codec(Registries.BIOME), Codec.BOOL), "enabled_biomes", "Which biomes are enabled, if disabled the biome will default to its vanilla counterpart for the given region").orElse(getDefaultBiomes()).forGetter(BWGWorldGenConfig::enabledBiomes),
CommentedCodec.of(Codec.INT, "region_weight", "How much each BWG region weighs. This weight applies to all 3 BWG Regions").orElse(8).forGetter(BWGWorldGenConfig::regionWeight),
CommentedCodec.of(Codec.BOOL, "vanilla_additions", "Whether to add bwg flowers and features to Vanilla Biomes (Config Option for Fabric Only)").orElse(true).forGetter(config -> true)
CommentedCodec.of(Codec.BOOL, "vanilla_additions", "Whether to add bwg flowers and features to Vanilla Biomes (Config Option for Fabric Only)").orElse(true).forGetter(config -> true),
CommentedCodec.of(Codec.unboundedMap(ResourceKey.codec(Registries.PLACED_FEATURE), Codec.BOOL), "enabled_vanilla_additions", "BWG Features that we add to Vanilla Biomes").orElse(getVanillaPlacedFeatureAdditions()).forGetter(BWGWorldGenConfig::vanillaFeatures)
).apply(instance, BWGWorldGenConfig::new)
);

public static BWGWorldGenConfig createDefault() {
Object2BooleanMap<ResourceKey<Biome>> enabledBiomes = getDefaultBiomes();

return new BWGWorldGenConfig(enabledBiomes, 8, true);
private static BWGWorldGenConfig createDefault() {
return new BWGWorldGenConfig(getDefaultBiomes(), 8, true, getVanillaPlacedFeatureAdditions());
}

private static @NotNull Object2BooleanMap<ResourceKey<Biome>> getDefaultBiomes() {
Expand All @@ -55,51 +48,22 @@ public static BWGWorldGenConfig createDefault() {
enabledBiomes.put(biomeResourceKey, true);
}

enabledBiomes.put(BWGBiomes.ERODED_BOREALIS, false);
enabledBiomes.replace(BWGBiomes.ERODED_BOREALIS, false);
return enabledBiomes;
}

public static BWGWorldGenConfig getOrCreateConfigFromDisk() {
BWGWorldGenConfig defaultWorldGenConfig = createDefault();

if (!PATH.toFile().exists()) {
createDefaultFile(defaultWorldGenConfig);
return defaultWorldGenConfig;
} else {

Jankson build = new Jankson.Builder().build();
try {
String configFile = Files.readString(PATH).stripTrailing().trim().strip().stripLeading();
JsonObject load = build.load(configFile);
Pair<BWGWorldGenConfig, JsonElement> configResult = CODEC.decode(JanksonJsonOps.INSTANCE, load).result().orElseThrow();
BWGWorldGenConfig config = configResult.getFirst();


Map<ResourceKey<Biome>, Boolean> temporary = new Reference2ObjectOpenHashMap<>();

temporary.putAll(defaultWorldGenConfig.enabledBiomes);
temporary.putAll(config.enabledBiomes);
private static @NotNull Object2BooleanMap<ResourceKey<PlacedFeature>> getVanillaPlacedFeatureAdditions() {
Object2BooleanMap<ResourceKey<PlacedFeature>> enabledFeatures = new Object2BooleanOpenHashMap<>();
enabledFeatures.put(BWGVanillaPlacedFeatures.FLOWER_DEFAULT, true);
enabledFeatures.put(BWGVanillaPlacedFeatures.FLOWER_PLAINS, true);
enabledFeatures.put(BWGVanillaPlacedFeatures.FOREST_FLOWERS, true);
enabledFeatures.put(BWGVanillaPlacedFeatures.FLOWER_WARM, true);
enabledFeatures.put(BWGOverworldTreePlacedFeatures.PALM_TREES, true);

BWGWorldGenConfig toCreate = new BWGWorldGenConfig(temporary, config.regionWeight, config.vanillaAdditions);

createDefaultFile(toCreate);

return toCreate;

} catch (IOException | SyntaxError e) {
throw new RuntimeException(e);
}
}
return enabledFeatures;
}

private static void createDefaultFile(BWGWorldGenConfig defaultWorldGenConfig) {
JsonElement jsonElement = CODEC.encodeStart(JanksonJsonOps.INSTANCE, defaultWorldGenConfig).result().orElseThrow();
String json = jsonElement.toJson(JsonGrammar.JSON5);
try {
Files.createDirectories(PATH.getParent());
Files.writeString(PATH, json);
} catch (IOException e) {
throw new RuntimeException(e);
}
private static BWGWorldGenConfig getOrCreateConfigFromDisk() {
return ConfigUtils.loadConfig(PATH, CODEC, createDefault());
}
}
}
Loading

0 comments on commit 225abed

Please sign in to comment.