Skip to content

Commit

Permalink
Implemented config file
Browse files Browse the repository at this point in the history
  • Loading branch information
fuj1n committed Sep 24, 2019
1 parent 8b36432 commit 08dd869
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 16 deletions.
20 changes: 16 additions & 4 deletions src/main/java/slimeknights/tmechworks/TMechworks.java
Expand Up @@ -5,20 +5,23 @@
import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.fml.DistExecutor;
import net.minecraftforge.fml.ExtensionPoint;
import net.minecraftforge.fml.ModLoadingContext;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent;
import net.minecraftforge.fml.event.lifecycle.GatherDataEvent;
import net.minecraftforge.fml.event.lifecycle.InterModEnqueueEvent;
import net.minecraftforge.fml.event.lifecycle.InterModProcessEvent;
import net.minecraftforge.fml.event.lifecycle.*;
import net.minecraftforge.fml.javafmlmod.FMLJavaModLoadingContext;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import slimeknights.mantle.util.ModelJsonGenerator;
import slimeknights.tmechworks.client.ClientProxy;
import slimeknights.tmechworks.common.CommonProxy;
import slimeknights.tmechworks.common.MechworksConfig;
import slimeknights.tmechworks.common.MechworksContent;
import slimeknights.tmechworks.common.network.PacketHandler;

import java.io.File;
import java.nio.file.Paths;

@Mod(TMechworks.modId)
public class TMechworks {
public static final String modId = "tmechworks";
Expand All @@ -45,6 +48,11 @@ public TMechworks() {
}

private void preInit(final FMLCommonSetupEvent event) {
File configPath = new File("config");
if(!configPath.exists())
configPath.mkdirs();
MechworksConfig.load();

proxy.preInit();

content.preInit(event);
Expand All @@ -63,6 +71,10 @@ private void postInit(final InterModProcessEvent event) {
content.postInit(event);
}

private void setupClient(final FMLClientSetupEvent event) {
proxy.setupClient();
}

private void gatherData(final GatherDataEvent event) {
DataGenerator generator = event.getGenerator();
generator.addProvider(new ModelJsonGenerator(generator, modId));
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/slimeknights/tmechworks/client/ClientProxy.java
@@ -1,15 +1,48 @@
package slimeknights.tmechworks.client;

import net.minecraftforge.fml.ExtensionPoint;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.moddiscovery.ModInfo;
import slimeknights.tmechworks.TMechworks;
import slimeknights.tmechworks.common.CommonProxy;
import slimeknights.tmechworks.common.MechworksContent;
import slimeknights.tmechworks.common.event.ModelBakeEventListener;

import java.util.List;

public class ClientProxy extends CommonProxy {
@Override
public void preInit() {
super.preInit();
}

@Override
public void init() {
super.init();

ModelBakeEventListener.registerDisguiseBlock(MechworksContent.Blocks.drawbridge.getRegistryName());
ModelBakeEventListener.registerDisguiseBlock(MechworksContent.Blocks.firestarter.getRegistryName());
}

@Override
public void setupClient() {
// TODO mod config gui
// ModList.get().getModContainerById(TMechworks.modId).ifPresent(c -> c.registerExtensionPoint(ExtensionPoint.CONFIGGUIFACTORY, () -> (mc, scr) -> scr));
}

static {
List<ModInfo> mods = ModList.get().getMods();
ModInfo info = mods.stream().filter(x -> x.getModId().equals(TMechworks.modId)).findFirst().orElse(null);

if (info != null) {
ModInfo newInfo = new ModInfo(info.getOwningFile(), info.getModConfig()) {
public boolean hasConfigUI() {
return true;
}
};

mods.set(mods.indexOf(info), newInfo);
}
}
}
4 changes: 4 additions & 0 deletions src/main/java/slimeknights/tmechworks/common/CommonProxy.java
Expand Up @@ -14,4 +14,8 @@ public void init() {
public void postInit() {

}

public void setupClient() {

}
}
46 changes: 38 additions & 8 deletions src/main/java/slimeknights/tmechworks/common/MechworksConfig.java
@@ -1,15 +1,45 @@
package slimeknights.tmechworks.common;

import com.electronwill.nightconfig.core.file.CommentedFileConfig;
import net.minecraftforge.common.ForgeConfigSpec;
import slimeknights.tmechworks.TMechworks;

import java.nio.file.Paths;

public class MechworksConfig {
private static MechworksConfig instance = new MechworksConfig();
public static void load() {
SPEC.setConfig(CommentedFileConfig.builder(Paths.get("config", "Tinkers Mechworks.toml")).build());
}

private static final ForgeConfigSpec.Builder BUILDER = new ForgeConfigSpec.Builder();

// Total extend length designed to be no higher than 64 blocks for advanced drawbridges, therefore going higher than 64 may produce weird results
public int drawbridgeExtendLength = 16;
public int drawbridgeExtendUpgradeValue = 16;
public float drawbridgeSpeed = 0.5F;
public float drawbridgeSpeedUpgradeValue = 0.1F;
public static final Drawbridge DRAWBRIDGE = new Drawbridge();
public static class Drawbridge {
public ForgeConfigSpec.IntValue extendLength;
public ForgeConfigSpec.IntValue extendUpgradeValue;
public ForgeConfigSpec.DoubleValue delay;
public ForgeConfigSpec.DoubleValue speedUpgradeValue;

public static MechworksConfig getInstance() {
return instance;
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();
}
}

private static final ForgeConfigSpec SPEC = BUILDER.build();
}
Expand Up @@ -113,8 +113,8 @@ public void registerItems(final RegistryEvent.Register<Item> event) {

// Machine Upgrades
register(registry, new MachineUpgradeItem(stats -> stats.isAdvanced = true), "upgrade_drawbridge_advanced");
register(registry, new MachineUpgradeItem(stats -> stats.extendLength += MechworksConfig.getInstance().drawbridgeExtendUpgradeValue).setTooltipFormatSupplier(() -> new Object[]{MechworksConfig.getInstance().drawbridgeExtendUpgradeValue}), "upgrade_drawbridge_distance");
register(registry, new MachineUpgradeItem(stats -> stats.extendDelay -= MechworksConfig.getInstance().drawbridgeSpeedUpgradeValue).setTooltipFormatSupplier(() -> new Object[]{MechworksConfig.getInstance().drawbridgeSpeedUpgradeValue}), "upgrade_speed");
register(registry, new MachineUpgradeItem(stats -> stats.extendLength += MechworksConfig.DRAWBRIDGE.extendUpgradeValue.get()).setTooltipFormatSupplier(() -> new Object[]{MechworksConfig.DRAWBRIDGE.extendUpgradeValue}), "upgrade_drawbridge_distance");
register(registry, new MachineUpgradeItem(stats -> stats.extendDelay -= MechworksConfig.DRAWBRIDGE.speedUpgradeValue.get()).setTooltipFormatSupplier(() -> new Object[]{MechworksConfig.DRAWBRIDGE.speedUpgradeValue}), "upgrade_speed");
}

@SubscribeEvent
Expand Down
Expand Up @@ -653,8 +653,8 @@ public ActionResultType doPlaceBlock(DrawbridgeItemUseContext context) {
}

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

Expand Down

0 comments on commit 08dd869

Please sign in to comment.