Files
autotrade/src/main/java/com/github/sebseb7/autotrade/config/Configs.java
2026-05-04 02:04:01 +02:00

113 lines
6.2 KiB
Java

package com.github.sebseb7.autotrade.config;
import com.github.sebseb7.autotrade.Reference;
import com.google.common.collect.ImmutableList;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import fi.dy.masa.malilib.config.ConfigUtils;
import fi.dy.masa.malilib.config.IConfigHandler;
import fi.dy.masa.malilib.config.IConfigValue;
import fi.dy.masa.malilib.config.options.ConfigBoolean;
import fi.dy.masa.malilib.config.options.ConfigInteger;
import fi.dy.masa.malilib.config.options.ConfigString;
import fi.dy.masa.malilib.util.JsonUtils;
import java.io.File;
import net.fabricmc.loader.api.FabricLoader;
public class Configs implements IConfigHandler {
private static final String CONFIG_FILE_NAME = Reference.MOD_ID + ".json";
public static class Generic {
public static final ConfigBoolean ENABLED = new ConfigBoolean("enabled", false,
"Do auto trading with villagers in range");
public static final ConfigBoolean ITEM_FRAME = new ConfigBoolean("selectUsingItemFrame", true,
"Select buy/sell items with item frames (max. distance 3) with items nametagged \"buy\" or \"sell\"");
public static final ConfigBoolean GLASS_BLOCK = new ConfigBoolean("selectUsingGlassBlock", false,
"Select input and output containers by placing red (input) and blue (output) stained glass blocks <selectionBlockOffset> blocks above them (or below if negative)");
public static final ConfigInteger SELECTOR_OFFSET = new ConfigInteger("selectionBlockOffset", 3, -10, 10,
"x Blocks below a red stained glass block will be input container, x Blocks below a blue stained glass block will be output container (or above, if x is negative)");
public static final ConfigBoolean ENABLE_SELL = new ConfigBoolean("enableSell", false,
"Enable selling (if disabled emeralds are taken from the input container)");
public static final ConfigString SELL_ITEM = new ConfigString("sellItem", "minecraft:gold_ingot",
"The item to sell for emerald. Optional suffix #enc1=lv&enc2=lv (enchantment registry ids) matches exact enchantments, e.g. enchanted books.");
public static final ConfigInteger SELL_LIMIT = new ConfigInteger("sellLimit", 64, 1, 64,
"max price to sell for");
public static final ConfigBoolean ENABLE_BUY = new ConfigBoolean("enableBuy", false,
"Enable buying (if disabled emeralds are placed in the output container)");
public static final ConfigString BUY_ITEM = new ConfigString("buyItem", "minecraft:redstone",
"The item to buy using emerald. Optional suffix #enc1=lv&enc2=lv matches exact enchantments (use set-buy hotkey with the book in hand).");
public static final ConfigInteger BUY_LIMIT = new ConfigInteger("buyLimit", 64, 1, 64, "max price to buy for");
public static final ConfigInteger MAX_INPUT_ITEMS = new ConfigInteger("maxInputStacks", 9, 1, 35,
"stacks to take from input container (or emerald container in buy-only mode), also the max amount of input and emerald kept.");
public static final ConfigInteger INPUT_CONTAINER_X = new ConfigInteger("inputContainerX", 0, -30000000,
30000000, "Input container X (not used when sell disabled)");
public static final ConfigInteger INPUT_CONTAINER_Y = new ConfigInteger("inputContainerY", 0, -64, 320,
"Input container Y (not used when sell disabled)");
public static final ConfigInteger INPUT_CONTAINER_Z = new ConfigInteger("inputContainerZ", 0, -30000000,
30000000, "Input container Z (not used when sell disabled)");
public static final ConfigInteger OUTPUT_CONTAINER_X = new ConfigInteger("outputContainerX", 0, -30000000,
30000000, "Output container X (not used when buy disabled)");
public static final ConfigInteger OUTPUT_CONTAINER_Y = new ConfigInteger("outputContainerY", 0, -64, 320,
"Output container Y (not used when buy disabled)");
public static final ConfigInteger OUTPUT_CONTAINER_Z = new ConfigInteger("outputContainerZ", 0, -30000000,
30000000, "Output container Z (not used when buy disabled)");
public static final ConfigInteger VOID_TRADING_DELAY = new ConfigInteger("voidTradingDelay", 0, 0, 30000000,
"delay in ticks for void trading");
public static final ConfigBoolean VOID_TRADING_DELAY_AFTER_TELEPORT = new ConfigBoolean("delayAfterTeleport",
true,
"true: Start the delay after the villager was unloaded; false: Start the delay after the trade has been initiated");
public static final ConfigInteger CONTAINER_CLOSE_DELAY = new ConfigInteger("containerCloseDelay", 0, 0,
30000000, "delay in ticks; to get signal from trapped chest");
public static final ConfigBoolean SHOW_TRADES = new ConfigBoolean("showTrades", true,
"Display villager/wandering-trader trades above their heads (requires trading with them once to cache the offers)");
public static final ImmutableList<IConfigValue> OPTIONS = ImmutableList.of(ENABLED, ITEM_FRAME, GLASS_BLOCK,
SELECTOR_OFFSET, ENABLE_SELL, SELL_ITEM, SELL_LIMIT, ENABLE_BUY, BUY_ITEM, BUY_LIMIT, MAX_INPUT_ITEMS,
INPUT_CONTAINER_X, INPUT_CONTAINER_Y, INPUT_CONTAINER_Z, OUTPUT_CONTAINER_X, OUTPUT_CONTAINER_Y,
OUTPUT_CONTAINER_Z, VOID_TRADING_DELAY, VOID_TRADING_DELAY_AFTER_TELEPORT, CONTAINER_CLOSE_DELAY,
SHOW_TRADES);
}
public static void loadFromFile() {
File configFile = new File(getConfigDirectory(), CONFIG_FILE_NAME);
if (configFile.exists() && configFile.isFile() && configFile.canRead()) {
JsonElement element = JsonUtils.parseJsonFile(configFile.toPath());
if (element != null && element.isJsonObject()) {
JsonObject root = element.getAsJsonObject();
ConfigUtils.readConfigBase(root, "Generic", Generic.OPTIONS);
ConfigUtils.readConfigBase(root, "Hotkeys", Hotkeys.HOTKEY_LIST);
}
}
}
public static void saveToFile() {
File dir = getConfigDirectory();
if ((dir.exists() && dir.isDirectory()) || dir.mkdirs()) {
JsonObject root = new JsonObject();
ConfigUtils.writeConfigBase(root, "Generic", Generic.OPTIONS);
ConfigUtils.writeConfigBase(root, "Hotkeys", Hotkeys.HOTKEY_LIST);
JsonUtils.writeJsonToFile(root, new File(dir, CONFIG_FILE_NAME).toPath());
}
}
@Override
public void load() {
loadFromFile();
}
@Override
public void save() {
saveToFile();
}
private static File getConfigDirectory() {
return FabricLoader.getInstance().getConfigDir().toFile();
}
}