Skip to content

Commit

Permalink
Switch to SNR config system
Browse files Browse the repository at this point in the history
  • Loading branch information
IThundxr committed Jul 21, 2024
1 parent 0b6c3ce commit 41ef6d0
Show file tree
Hide file tree
Showing 17 changed files with 377 additions and 210 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,21 @@

package dev.ithundxr.createnumismatics.config;

import dev.ithundxr.createnumismatics.content.backend.Coin;
import com.simibubi.create.foundation.config.ConfigBase;

public class CommonModConfig {
public static int starterSpur = 0;
public static int starterBevel = 0;
public static int starterSprocket = 0;
public static int starterCog = 0;
public static int starterCrown = 0;
public static int starterSun = 0;
@SuppressWarnings("unused")
public class CClient extends ConfigBase {

public static Coin currency = null;
public final ConfigGroup client = group(0, "client", Comments.client);

// Based off of https://github.com/Layers-of-Railways/Railway/blob/68713f0fbb20080b7e207c070b1595bdbbc1bc00/common/src/main/java/com/railwayteam/railways/config/CClient.java

@Override
public String getName() {
return "client";
}

private static class Comments {
static final String client = "Client-only settings - If you're looking for general settings, look inside your worlds serverconfig folder!";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.config;

import com.simibubi.create.foundation.config.ConfigBase;
import dev.ithundxr.createnumismatics.content.backend.Coin;

@SuppressWarnings("unused")
public class CCommon extends ConfigBase {

public final ConfigGroup coins = group(0, "coins", Comments.coins);

public final ConfigEnum<Coin> defaultCoin = e(Coin.SPUR, "defaultCoin", Comments.defaultCoin);

// Based off of https://github.com/Layers-of-Railways/Railway/blob/68713f0fbb20080b7e207c070b1595bdbbc1bc00/common/src/main/java/com/railwayteam/railways/config/CCommon.java

@Override
public String getName() {
return "common";
}

private static class Comments {
static final String coins = "Coin settings";

static final String defaultCoin = "The default coin to be used in UI related displays";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.config;

import com.simibubi.create.foundation.config.ConfigBase;
import dev.ithundxr.createnumismatics.content.backend.Coin;

@SuppressWarnings("unused")
public class CServer extends ConfigBase {

public final ConfigGroup coins = group(0, "coins", Comments.coins);

public final ConfigGroup starter_currency = group(1, "starter_currency", Comments.starterMoney);

public final ConfigInt starter_spurs = i(0, 0, "starter_spurs");
public final ConfigInt starter_bevels = i(0, 0, "starter_bevels");
public final ConfigInt starter_sprockets = i(0, 0, "starter_sprockets");
public final ConfigInt starter_cogs = i(0, 0, "starter_cogs");
public final ConfigInt starter_crowns = i(0, 0, "starter_crowns");
public final ConfigInt starter_suns = i(0, 0, "starter_suns");


//public final ConfigGroup misc = group(0, "misc", Comments.misc);

// Based off of https://github.com/Layers-of-Railways/Railway/blob/68713f0fbb20080b7e207c070b1595bdbbc1bc00/common/src/main/java/com/railwayteam/railways/config/CServer.java

@Override
public String getName() {
return "server";
}

private static class Comments {
static final String coins = "Coin settings";

static final String starterMoney = "How much of this coin type should players receive in their bank account on first join";;

static final String misc = "Miscellaneous settings";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
/*
* Numismatics
* Copyright (c) 2024 The Railways Team
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package dev.ithundxr.createnumismatics.config;

import com.simibubi.create.foundation.config.ConfigBase;
import net.minecraftforge.common.ForgeConfigSpec;
import net.minecraftforge.fml.config.ModConfig;
import org.apache.commons.lang3.NotImplementedException;
import org.apache.commons.lang3.tuple.Pair;
import org.jetbrains.annotations.ApiStatus;

import java.util.EnumMap;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Supplier;

@SuppressWarnings("unused")
public class NumismaticsConfig {
@ApiStatus.Internal
public static final Map<ModConfig.Type, ConfigBase> CONFIGS = new EnumMap<>(ModConfig.Type.class);

//private static CClient client;
private static CCommon common;
private static CServer server;

// public static CClient client() {
// return client;
// }

public static CCommon common() {
return common;
}

public static CServer server() {
return server;
}

public static ConfigBase byType(ModConfig.Type type) {
return CONFIGS.get(type);
}

private static <T extends ConfigBase> T register(Supplier<T> factory, ModConfig.Type side) {
Pair<T, ForgeConfigSpec> specPair = new ForgeConfigSpec.Builder().configure(builder -> {
T config = factory.get();
config.registerAll(builder);
return config;
});

T config = specPair.getLeft();
config.specification = specPair.getRight();
CONFIGS.put(side, config);
return config;
}

@ApiStatus.Internal
public static void registerCommon() {
//client = register(CClient::new, ModConfig.Type.CLIENT);
common = register(CCommon::new, ModConfig.Type.COMMON);
server = register(CServer::new, ModConfig.Type.SERVER);
}

public static void onLoad(ModConfig modConfig) {
for (ConfigBase config : CONFIGS.values())
if (config.specification == modConfig
.getSpec())
config.onLoad();
}

public static void onReload(ModConfig modConfig) {
for (ConfigBase config : CONFIGS.values())
if (config.specification == modConfig
.getSpec())
config.onReload();
}

private static class TomlGroup {
private final Map<String, TomlGroup> subgroups = new HashMap<>();
private final Map<String, String> entries = new HashMap<>();
private final String path;

private static TomlGroup root() {
return new TomlGroup("");
}

private TomlGroup(String path) {
this.path = path;
}

public boolean isRoot() {
return path.isEmpty();
}

public void add(String key, String value) {
if (!isRoot())
throw new NotImplementedException();

String[] pieces = key.split("\\.");
String subKey = pieces[pieces.length - 1];
TomlGroup targetedGroup = this;
for (int i = 0; i < pieces.length - 1; i++) {
targetedGroup = targetedGroup.getOrCreateSubGroup(pieces[i]);
}
targetedGroup.entries.put(subKey, value);
}

private TomlGroup getOrCreateSubGroup(String subKey) {
return subgroups.computeIfAbsent(subKey, (sk) -> new TomlGroup(path.isEmpty() ? sk : path + "." + sk));
}

private void write(StringBuilder b) {
if (!isRoot()) {
b.append("\n[").append(path).append("]");
}

for (Map.Entry<String, String> entry : entries.entrySet()) {
b.append("\n").append(entry.getKey()).append(" = ").append(entry.getValue());
}

for (TomlGroup subGroup : subgroups.values()) {
subGroup.write(b);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import com.simibubi.create.foundation.gui.AllIcons;
import com.simibubi.create.foundation.utility.Components;
import com.simibubi.create.foundation.utility.Couple;
import dev.ithundxr.createnumismatics.config.CommonModConfig;
import dev.ithundxr.createnumismatics.config.NumismaticsConfig;
import dev.ithundxr.createnumismatics.registry.NumismaticsIcons;
import dev.ithundxr.createnumismatics.registry.NumismaticsItems;
import dev.ithundxr.createnumismatics.util.TextUtils;
Expand Down Expand Up @@ -130,8 +130,7 @@ public String getTranslationKey() {
}

public Coin getDescription() {
if (CommonModConfig.currency == null) return SPUR;
return this.value < CommonModConfig.currency.value ? SPUR : CommonModConfig.currency;
return this.value < NumismaticsConfig.common().defaultCoin.get().value ? SPUR : NumismaticsConfig.common().defaultCoin.get();
}

public ItemStack asStack() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
package dev.ithundxr.createnumismatics.content.backend;

import dev.ithundxr.createnumismatics.Numismatics;
import dev.ithundxr.createnumismatics.config.CommonModConfig;
import dev.ithundxr.createnumismatics.config.NumismaticsConfig;
import dev.ithundxr.createnumismatics.content.backend.BankAccount.Type;
import dev.ithundxr.createnumismatics.util.Utils;
import net.minecraft.server.MinecraftServer;
Expand Down Expand Up @@ -101,12 +101,12 @@ public BankAccount getOrCreateAccount(@NotNull UUID uuid, Type type) {
BankAccount account = new BankAccount(uuid, type);

if (type == Type.PLAYER) {
account.deposit(Coin.SPUR, CommonModConfig.starterSpur);
account.deposit(Coin.BEVEL, CommonModConfig.starterBevel);
account.deposit(Coin.SPROCKET, CommonModConfig.starterSprocket);
account.deposit(Coin.COG, CommonModConfig.starterCog);
account.deposit(Coin.CROWN, CommonModConfig.starterCrown);
account.deposit(Coin.SUN, CommonModConfig.starterSun);
account.deposit(Coin.SPUR, NumismaticsConfig.server().starter_spurs.get());
account.deposit(Coin.BEVEL, NumismaticsConfig.server().starter_bevels.get());
account.deposit(Coin.SPROCKET, NumismaticsConfig.server().starter_sprockets.get());
account.deposit(Coin.COG, NumismaticsConfig.server().starter_cogs.get());
account.deposit(Coin.CROWN, NumismaticsConfig.server().starter_crowns.get());
account.deposit(Coin.SUN, NumismaticsConfig.server().starter_suns.get());
}

accounts.put(uuid, account);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
import com.simibubi.create.foundation.gui.widget.IconButton;
import com.simibubi.create.foundation.utility.Components;
import com.simibubi.create.foundation.utility.Couple;
import dev.ithundxr.createnumismatics.config.CommonModConfig;
import dev.ithundxr.createnumismatics.config.NumismaticsConfig;
import dev.ithundxr.createnumismatics.content.backend.Coin;
import dev.ithundxr.createnumismatics.registry.NumismaticsBlocks;
import dev.ithundxr.createnumismatics.registry.NumismaticsGuiTextures;
Expand Down Expand Up @@ -94,11 +94,11 @@ protected void renderBg(@NotNull GuiGraphics graphics, float partialTick, int mo
graphics.drawCenteredString(font, title, x + (background.width - 8) / 2, y + 3, 0xFFFFFF);


Couple<Integer> cogsAndSpurs = CommonModConfig.currency.convert(menu.contentHolder.getBalance());
Couple<Integer> cogsAndSpurs = NumismaticsConfig.common().defaultCoin.get().convert(menu.contentHolder.getBalance());
int cogs = cogsAndSpurs.getFirst();
int spurs = cogsAndSpurs.getSecond();
Component balanceLabel = Components.translatable("gui.numismatics.bank_terminal.balance",
TextUtils.formatInt(cogs), CommonModConfig.currency.getName(cogs), spurs);
TextUtils.formatInt(cogs), NumismaticsConfig.common().defaultCoin.get().getName(cogs), spurs);
graphics.drawCenteredString(font, balanceLabel, x + (background.width - 8) / 2, y + 21, 0xFFFFFF);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import com.simibubi.create.foundation.utility.Couple;
import com.simibubi.create.foundation.utility.Lang;
import dev.ithundxr.createnumismatics.compat.computercraft.ComputerCraftProxy;
import dev.ithundxr.createnumismatics.config.CommonModConfig;
import dev.ithundxr.createnumismatics.config.NumismaticsConfig;
import dev.ithundxr.createnumismatics.content.backend.Coin;
import dev.ithundxr.createnumismatics.content.backend.behaviours.SliderStylePriceBehaviour;
import dev.ithundxr.createnumismatics.content.backend.trust_list.TrustListMenu;
Expand Down Expand Up @@ -107,11 +107,11 @@ public void addCoins(int totalPrice) {

@Override
public boolean addToTooltip(List<Component> tooltip, boolean isPlayerSneaking) {
Couple<Integer> cogsAndSpurs = CommonModConfig.currency.convert(price.getTotalPrice());
Couple<Integer> cogsAndSpurs = NumismaticsConfig.common().defaultCoin.get().convert(price.getTotalPrice());
int cogs = cogsAndSpurs.getFirst();
int spurs = cogsAndSpurs.getSecond();
MutableComponent balanceLabel = Components.translatable("block.numismatics.brass_depositor.tooltip.price",
TextUtils.formatInt(cogs), CommonModConfig.currency.getName(cogs), spurs);
TextUtils.formatInt(cogs), NumismaticsConfig.common().defaultCoin.get().getName(cogs), spurs);
Lang.builder()
.add(balanceLabel.withStyle(Coin.closest(price.getTotalPrice()).rarity.color))
.forGoggles(tooltip);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
import com.simibubi.create.foundation.gui.widget.ScrollInput;
import com.simibubi.create.foundation.utility.Components;
import com.simibubi.create.foundation.utility.Couple;
import dev.ithundxr.createnumismatics.config.CommonModConfig;
import dev.ithundxr.createnumismatics.config.NumismaticsConfig;
import dev.ithundxr.createnumismatics.content.backend.Coin;
import dev.ithundxr.createnumismatics.content.backend.behaviours.SliderStylePriceConfigurationPacket;
import dev.ithundxr.createnumismatics.registry.NumismaticsBlocks;
Expand Down Expand Up @@ -130,11 +130,11 @@ protected void renderBg(@NotNull GuiGraphics graphics, float partialTick, int mo

graphics.drawCenteredString(font, title, x + (background.width - 8) / 2, y + 3, 0xFFFFFF);

Couple<Integer> cogsAndSpurs = CommonModConfig.currency.convert(menu.contentHolder.getTotalPrice());
Couple<Integer> cogsAndSpurs = NumismaticsConfig.common().defaultCoin.get().convert(menu.contentHolder.getTotalPrice());
int cogs = cogsAndSpurs.getFirst();
int spurs = cogsAndSpurs.getSecond();
Component balanceLabel = Components.translatable("block.numismatics.brass_depositor.tooltip.price",
TextUtils.formatInt(cogs), CommonModConfig.currency.getName(cogs), spurs);
TextUtils.formatInt(cogs), NumismaticsConfig.common().defaultCoin.get().getName(cogs), spurs);
graphics.drawCenteredString(font, balanceLabel, x + (background.width - 8) / 2, y + 21, 0xFFFFFF);
}

Expand Down
Loading

0 comments on commit 41ef6d0

Please sign in to comment.