Skip to content

Commit

Permalink
Switch to using forge config loader rather than manually doing it
Browse files Browse the repository at this point in the history
  • Loading branch information
fuj1n committed Aug 7, 2020
1 parent 343006f commit 6765e83
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 94 deletions.
15 changes: 5 additions & 10 deletions src/main/java/slimeknights/tmechworks/TMechworks.java
@@ -1,10 +1,11 @@
package slimeknights.tmechworks;

import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.lifecycle.FMLClientSetupEvent;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
Expand All @@ -19,25 +20,22 @@
import slimeknights.tmechworks.common.entities.MechworksFakePlayer;
import slimeknights.tmechworks.common.network.PacketHandler;

import java.nio.file.Path;
import java.nio.file.Paths;

@Mod(TMechworks.modId)
public class TMechworks {
public static final String modId = "tmechworks";
public static final String modName = "Tinkers' Mechworks";

public static final Path CONFIG_ROOT = Paths.get("config", "Tinkers Mechworks");

public static final Logger log = LogManager.getLogger(modId);

public static TMechworks instance;
public static CommonProxy proxy = DistExecutor.runForDist(()->()->new ClientProxy(), ()->()->new CommonProxy());
public static CommonProxy proxy = DistExecutor.safeRunForDist(()-> ClientProxy::new, ()-> CommonProxy::new);
public static MechworksContent content;

public TMechworks() {
instance = this;

ModLoadingContext.get().registerConfig(ModConfig.Type.COMMON, MechworksConfig.COMMON_SPEC);

IEventBus bus = FMLJavaModLoadingContext.get().getModEventBus();

bus.addListener(this::preInit);
Expand All @@ -53,9 +51,6 @@ public TMechworks() {
}

private void preInit(final FMLCommonSetupEvent event) {
CONFIG_ROOT.toFile().mkdirs();
MechworksConfig.load();

proxy.preInit();

content.preInit(event);
Expand Down
Expand Up @@ -22,8 +22,8 @@ public ResourceLocation getIconSheet() {
}

@OnlyIn(Dist.CLIENT)
@SuppressWarnings("unchecked")
public final int unsafeGetIconFor(Object value) {
//noinspection unchecked
return getIconFor((T) value);
}

Expand Down
Expand Up @@ -72,8 +72,8 @@ public static class Items {
// Upgrades
public static final ItemObject<Item> upgrade_blank = ITEMS.register("upgrade_blank", MechworksItem::new);
public static final ItemObject<MachineUpgradeItem> upgrade_drawbridge_advanced = ITEMS.register("upgrade_drawbridge_advanced", () -> new MachineUpgradeItem(stats -> stats.isAdvanced = true));
public static final ItemObject<MechworksItem> upgrade_drawbridge_distance = ITEMS.register("upgrade_drawbridge_distance", () -> new MachineUpgradeItem(stats -> stats.extendLength += MechworksConfig.DRAWBRIDGE.extendUpgradeValue.get()).setTooltipFormatSupplier(() -> new Object[]{MechworksConfig.DRAWBRIDGE.extendUpgradeValue.get()}));
public static final ItemObject<MechworksItem> upgrade_speed = ITEMS.register("upgrade_speed", () -> new MachineUpgradeItem(stats -> stats.extendDelay -= MechworksConfig.DRAWBRIDGE.speedUpgradeValue.get()).setTooltipFormatSupplier(() -> new Object[]{MechworksConfig.DRAWBRIDGE.speedUpgradeValue.get()}));
public static final ItemObject<MechworksItem> upgrade_drawbridge_distance = ITEMS.register("upgrade_drawbridge_distance", () -> new MachineUpgradeItem(stats -> stats.extendLength += MechworksConfig.COMMON_CONFIG.drawbridge.extendUpgradeValue.get()).setTooltipFormatSupplier(() -> new Object[]{MechworksConfig.COMMON_CONFIG.drawbridge.extendUpgradeValue.get()}));
public static final ItemObject<MechworksItem> upgrade_speed = ITEMS.register("upgrade_speed", () -> new MachineUpgradeItem(stats -> stats.extendDelay -= MechworksConfig.COMMON_CONFIG.drawbridge.speedUpgradeValue.get()).setTooltipFormatSupplier(() -> new Object[]{MechworksConfig.COMMON_CONFIG.drawbridge.speedUpgradeValue.get()}));
}

public static class TileEntities {
Expand Down
Expand Up @@ -681,8 +681,8 @@ public ActionResultType doPlaceBlock(DrawbridgeItemUseContext context) {
}

public static class DrawbridgeStats {
public int extendLength = MechworksConfig.DRAWBRIDGE.extendLength.get();
public float extendDelay = MechworksConfig.DRAWBRIDGE.delay.get().floatValue();
public int extendLength = MechworksConfig.COMMON_CONFIG.drawbridge.extendLength.get();
public float extendDelay = MechworksConfig.COMMON_CONFIG.drawbridge.delay.get().floatValue();
public boolean isAdvanced = false;
}

Expand Down
@@ -1,104 +1,109 @@
package slimeknights.tmechworks.common.config;

import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import net.minecraftforge.common.ForgeConfigSpec;
import slimeknights.tmechworks.TMechworks;
import org.apache.commons.lang3.tuple.Pair;
import slimeknights.tmechworks.library.Util;

import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;

public class MechworksConfig {
public static final Path CONFIG_PATH = Paths.get(TMechworks.CONFIG_ROOT.toString(), "Config.toml");
public static final Common COMMON_CONFIG;
public static final ForgeConfigSpec COMMON_SPEC;

public static void load() {
CommentedFileConfig config = CommentedFileConfig.builder(CONFIG_PATH).build();
config.load();
SPEC.setConfig(config);
}
public static class Common {
public final WorldGeneration worldGen;
public final Drawbridge drawbridge;

private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();
private Common(ForgeConfigSpec.Builder builder) {
worldGen = new WorldGeneration(builder);
drawbridge = new Drawbridge(builder);
}

public static final WorldGeneration WORLD_GENERATION = new WorldGeneration();
public static final class WorldGeneration {
public final ForgeConfigSpec.BooleanValue enabled;
public static final class WorldGeneration {
public final ForgeConfigSpec.BooleanValue enabled;

public final Ore COPPER = new Ore("copper");
public final Ore ALUMINUM = new Ore("aluminum");
public final Ore copper;
public final Ore aluminum;

WorldGeneration() {
BUILDER.push("world");
WorldGeneration(ForgeConfigSpec.Builder builder) {
builder.comment("Everything to do with world generation").push("world");

enabled = BUILDER
.comment("Whether world generation is enabled as a whole")
.define("enabled", true);
enabled = builder
.comment("Whether world generation is enabled as a whole")
.define("enabled", true);

BUILDER.pop();
}
copper = new Ore(builder, "copper");
aluminum = new Ore(builder, "aluminum");

public static class Ore {
public ForgeConfigSpec.BooleanValue enabled;
builder.pop();
}

public ForgeConfigSpec.BooleanValue isWhitelist;
public ForgeConfigSpec.ConfigValue<List<? extends String>> filter;
public static class Ore {
public ForgeConfigSpec.BooleanValue enabled;

Ore(String name) {
this(name, true);
}
public ForgeConfigSpec.BooleanValue isWhitelist;
public ForgeConfigSpec.ConfigValue<List<? extends String>> filter;

Ore(String name, boolean onByDefault) {
this(name, onByDefault, false, Collections.emptyList());
}
Ore(ForgeConfigSpec.Builder builder, String name) {
this(builder, name, true);
}

Ore(String name, boolean onByDefault, boolean isWhitelist, List<String> filter) {
BUILDER.push("world." + name);
Ore(ForgeConfigSpec.Builder builder, String name, boolean onByDefault) {
this(builder, name, onByDefault, false, Collections.emptyList());
}

enabled = BUILDER
.comment("Whether or not this ore is generated")
.define("enabled", onByDefault);
this.isWhitelist = BUILDER
.comment("If true, the filter will act as a whitelist, otherwise, blacklist")
.define("isWhitelist", isWhitelist);
this.filter = BUILDER
.comment("A list of fully qualified biome names, for example \"minecraft:river\"")
.defineList("filter", filter, (obj) -> obj != null && Util.validateResourceName(obj.toString()));
Ore(ForgeConfigSpec.Builder builder, String name, boolean onByDefault, boolean isWhitelist, List<String> filter) {
builder.comment("Generation settings for " + name + " ore").push(name);

BUILDER.pop(2);
enabled = builder
.comment("Whether or not this ore is generated")
.define("enabled", onByDefault);
this.isWhitelist = builder
.comment("If true, the filter will act as a whitelist, otherwise, blacklist")
.define("isWhitelist", isWhitelist);
this.filter = builder
.comment("A list of fully qualified biome names, for example \"minecraft:river\"")
.defineList("filter", filter, (obj) -> obj != null && Util.validateResourceName(obj.toString()));

builder.pop();
}
}
}
}

public static final Drawbridge DRAWBRIDGE = new Drawbridge();

public static final class Drawbridge {
public final ForgeConfigSpec.IntValue extendLength;
public final ForgeConfigSpec.IntValue extendUpgradeValue;
public final ForgeConfigSpec.DoubleValue delay;
public final ForgeConfigSpec.DoubleValue speedUpgradeValue;

Drawbridge() {
BUILDER.push("drawbridge");

extendLength = BUILDER
.comment("Total drawbridge distance (with upgrades) going above 66 in an advanced drawbridge may cause slots to overlap with player inventory slots")
.comment("The distance that the base drawbridge can extend")
.defineInRange("extendLength", 16, 1, 64);
extendUpgradeValue = BUILDER
.comment("How much each distance upgrade increases the max distance by")
.defineInRange("extendUpgradeValue", 16, 0, 64);

delay = BUILDER
.comment("The base delay between each block place/destroy")
.defineInRange("delay", 0.5D, 0F, Integer.MAX_VALUE);
speedUpgradeValue = BUILDER
.comment("The amount by which each speed upgrade decreases the delay")
.defineInRange("speedUpgradeValue", 0.1D, 0F, Integer.MAX_VALUE);

BUILDER.pop();
public static final class Drawbridge {
public final ForgeConfigSpec.IntValue extendLength;
public final ForgeConfigSpec.IntValue extendUpgradeValue;
public final ForgeConfigSpec.DoubleValue delay;
public final ForgeConfigSpec.DoubleValue speedUpgradeValue;

Drawbridge(ForgeConfigSpec.Builder builder) {
builder.comment("All the settings to do with the drawbridge").push("drawbridge");

extendLength = builder
.comment("Total drawbridge distance (with upgrades) going above 66 in an advanced drawbridge may cause slots to overlap with player inventory slots")
.comment("The distance that the base drawbridge can extend")
.defineInRange("extendLength", 16, 1, 64);
extendUpgradeValue = builder
.comment("How much each distance upgrade increases the max distance by")
.defineInRange("extendUpgradeValue", 16, 0, 64);

delay = builder
.comment("The base delay between each block place/destroy")
.defineInRange("delay", 0.5D, 0F, Integer.MAX_VALUE);
speedUpgradeValue = builder
.comment("The amount by which each speed upgrade decreases the delay")
.defineInRange("speedUpgradeValue", 0.1D, 0F, Integer.MAX_VALUE);

builder.pop();
}
}
}

private static final ForgeConfigSpec SPEC = BUILDER.build();
static {
Pair<Common, ForgeConfigSpec> commonPair = new ForgeConfigSpec.Builder().configure(Common::new);

COMMON_CONFIG = commonPair.getLeft();
COMMON_SPEC = commonPair.getRight();
}
}
Expand Up @@ -18,12 +18,12 @@

public class MechworksWorld {
private static final List<OreProperties> OVERWORLD_ORES = ImmutableList.of(
new OreProperties(MechworksContent.Blocks.copper_ore.get().getDefaultState(), new CountRangeConfig(8, 40, 0, 75), 8, MechworksConfig.WORLD_GENERATION.COPPER),
new OreProperties(MechworksContent.Blocks.aluminum_ore.get().getDefaultState(), new CountRangeConfig(8, 40, 0, 75), 8, MechworksConfig.WORLD_GENERATION.ALUMINUM)
new OreProperties(MechworksContent.Blocks.copper_ore.get().getDefaultState(), new CountRangeConfig(8, 40, 0, 75), 8, MechworksConfig.COMMON_CONFIG.worldGen.copper),
new OreProperties(MechworksContent.Blocks.aluminum_ore.get().getDefaultState(), new CountRangeConfig(8, 40, 0, 75), 8, MechworksConfig.COMMON_CONFIG.worldGen.aluminum)
);

public static void registerWorldGeneration() {
if (!MechworksConfig.WORLD_GENERATION.enabled.get()) {
if (!MechworksConfig.COMMON_CONFIG.worldGen.enabled.get()) {
return;
}

Expand All @@ -40,9 +40,9 @@ private static class OreProperties {
private final BlockState state;
private final CountRangeConfig countRange;
private final int frequency;
private final MechworksConfig.WorldGeneration.Ore config;
private final MechworksConfig.Common.WorldGeneration.Ore config;

OreProperties(BlockState state, CountRangeConfig countRange, int frequency, MechworksConfig.WorldGeneration.Ore config) {
OreProperties(BlockState state, CountRangeConfig countRange, int frequency, MechworksConfig.Common.WorldGeneration.Ore config) {
this.state = state;
this.countRange = countRange;
this.frequency = frequency;
Expand Down

0 comments on commit 6765e83

Please sign in to comment.