Skip to content

Commit

Permalink
CONFIGS OVERHAUL
Browse files Browse the repository at this point in the history
  • Loading branch information
KrLite committed Mar 10, 2024
1 parent daa5018 commit 0621ed3
Show file tree
Hide file tree
Showing 24 changed files with 196 additions and 118 deletions.
29 changes: 19 additions & 10 deletions src/main/java/net/krlite/knowledges/KnowledgesClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,32 @@
import net.krlite.knowledges.impl.component.AbstractInfoComponent;
import net.krlite.knowledges.manager.KnowledgesComponentManager;
import net.krlite.knowledges.manager.KnowledgesDataManager;
import net.krlite.knowledges.manager.KnowledgesManager;
import net.krlite.knowledges.networking.ClientNetworking;
import net.minecraft.network.PacketByteBuf;
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Identifier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KnowledgesClient implements ClientModInitializer {
public static final Logger LOGGER = LoggerFactory.getLogger(KnowledgesCommon.ID + ":client");

public static final ConfigHolder<KnowledgesClientConfig> CONFIG_HOLDER;
public static final KnowledgesClientConfig CONFIG;
public static final ConfigHolder<KnowledgesClientConfig> CONFIG;
public static final KnowledgesClientConfig DEFAULT_CONFIG = new KnowledgesClientConfig();

static {
AutoConfig.register(KnowledgesClientConfig.class, PartitioningSerializer.wrap(Toml4jConfigSerializer::new));
CONFIG_HOLDER = AutoConfig.getConfigHolder(KnowledgesClientConfig.class);
CONFIG = CONFIG_HOLDER.get();
CONFIG = AutoConfig.getConfigHolder(KnowledgesClientConfig.class);

CONFIG.registerLoadListener((configHolder, knowledgesClientConfig) -> {
KnowledgesManager.fixKeysAndSort(knowledgesClientConfig.components.enabled);
KnowledgesManager.fixKeysAndSort(knowledgesClientConfig.data.enabled);

return ActionResult.PASS;
});
}

public static final KnowledgesComponentManager COMPONENTS = new KnowledgesComponentManager();
Expand All @@ -58,8 +65,8 @@ public static MutableText localize(String category, String... paths) {

@Override
public void onInitializeClient() {
COMPONENTS.fixKeys();
DATA.fixKeys();
KnowledgesManager.fixKeysAndSort(CONFIG.get().components.enabled);
KnowledgesManager.fixKeysAndSort(CONFIG.get().data.enabled);

AbstractInfoComponent.Animation.registerEvents();
new ClientNetworking().register();
Expand Down Expand Up @@ -126,8 +133,8 @@ public void onInitializeClient() {
.forEach(data -> DATA.register(namespace, data));
});

if (CONFIG.general.autoTidiesUp) tidyUpConfig();
CONFIG_HOLDER.save();
tidyUpConfig();
CONFIG.save();

if (!COMPONENTS.asMap().isEmpty()) {
LOGGER.info(String.format(
Expand All @@ -154,8 +161,10 @@ public void onInitializeClient() {
}

public static void tidyUpConfig() {
COMPONENTS.tidyUp();
DATA.tidyUp();
if (CONFIG.get().components.autoTidiesUp)
COMPONENTS.tidyUp();
if (CONFIG.get().data.autoTidiesUp)
DATA.tidyUp();
}

public static void requestDataFor(PacketByteBufWritable writable, Identifier channel) {
Expand Down
21 changes: 14 additions & 7 deletions src/main/java/net/krlite/knowledges/KnowledgesCommon.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,32 @@
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.krlite.knowledges.api.entrypoint.TagProvider;
import net.krlite.knowledges.config.KnowledgesClientConfig;
import net.krlite.knowledges.config.KnowledgesCommonConfig;
import net.krlite.knowledges.config.modmenu.cache.UsernameCache;
import net.krlite.knowledges.manager.KnowledgesManager;
import net.krlite.knowledges.manager.KnowledgesTagManager;
import net.krlite.knowledges.networking.ServerNetworking;
import net.minecraft.util.ActionResult;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KnowledgesCommon implements ModInitializer {
public static final String NAME = "Knowledges", ID = "knowledges";
public static final Logger LOGGER = LoggerFactory.getLogger(ID + ":common");

public static final ConfigHolder<KnowledgesCommonConfig> CONFIG_HOLDER;
public static final KnowledgesCommonConfig CONFIG;

public static final ConfigHolder<KnowledgesCommonConfig> CONFIG;
public static final KnowledgesCommonConfig DEFAULT_CONFIG = new KnowledgesCommonConfig();

static {
AutoConfig.register(KnowledgesCommonConfig.class, PartitioningSerializer.wrap(Toml4jConfigSerializer::new));
CONFIG_HOLDER = AutoConfig.getConfigHolder(KnowledgesCommonConfig.class);
CONFIG = CONFIG_HOLDER.getConfig();
CONFIG = AutoConfig.getConfigHolder(KnowledgesCommonConfig.class);

CONFIG.registerLoadListener((configHolder, knowledgesCommonConfig) -> {
KnowledgesManager.fixKeysAndSort(knowledgesCommonConfig.tags.enabled);

return ActionResult.PASS;
});
}

public static final KnowledgesTagManager TAGS = new KnowledgesTagManager();
Expand All @@ -36,9 +42,10 @@ public class KnowledgesCommon implements ModInitializer {

@Override
public void onInitialize() {
TAGS.fixKeys();
CACHE_USERNAME.load();

KnowledgesManager.fixKeysAndSort(CONFIG.get().tags.enabled);

new ServerNetworking().register();

// Tags
Expand Down Expand Up @@ -73,7 +80,7 @@ public void onInitialize() {
});

tidyUpConfig();
CONFIG_HOLDER.save();
CONFIG.save();
}

public static void tidyUpConfig() {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/net/krlite/knowledges/KnowledgesHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void render(DrawContext context, BiConsumer<RenderProxy, Representable<?>

if (isFirstPerson
&& (!isSpectator || shouldRenderSpectatorCrosshair)
&& (KnowledgesClient.CONFIG.general.visibleInDebugHud || !isInDebugHud)
&& (KnowledgesClient.CONFIG.get().general.visibleInDebugHud || !isInDebugHud)
) {
renderConsumer.accept(new RenderProxy(context), rep);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ public static Optional<FluidState> getFluidState(HitResult hitResult) {
Fluid fluid = fluidState.getFluid();

boolean fluidIsWater = fluid == Fluids.WATER || fluid == Fluids.FLOWING_WATER, fluidIsLava = fluid == Fluids.LAVA || fluid == Fluids.FLOWING_LAVA;
if (KnowledgesClient.CONFIG.components.infoFluid.ignoresWater && fluidIsWater) return Optional.empty();
if (KnowledgesClient.CONFIG.components.infoFluid.ignoresLava && fluidIsLava) return Optional.empty();
if (KnowledgesClient.CONFIG.components.infoFluid.ignoresOtherFluids && !fluidIsWater && !fluidIsLava) return Optional.empty();
if (KnowledgesClient.CONFIG.get().components.infoFluid.ignoresWater && fluidIsWater) return Optional.empty();
if (KnowledgesClient.CONFIG.get().components.infoFluid.ignoresLava && fluidIsLava) return Optional.empty();
if (KnowledgesClient.CONFIG.get().components.infoFluid.ignoresOtherFluids && !fluidIsWater && !fluidIsLava) return Optional.empty();

return Optional.of(fluidState);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@

public class LayoutProxy {
public static double scalar() {
return 0.5 + 0.5 * KnowledgesClient.CONFIG.general.mainScalar / 1000.0;
return 0.5 + 0.5 * KnowledgesClient.CONFIG.get().general.mainScalar;
}

public static Box crosshairSafeArea() {
double size = 16 + 8 * KnowledgesClient.CONFIG.general.crosshairSafeAreaScalar / 1000.0;
double size = 16 + 8 * KnowledgesClient.CONFIG.get().general.crosshairSafeAreaScalar;
return Box.UNIT.scale(size)
.scale(scalar())
.center(Vector.ZERO)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import me.shedaniel.autoconfig.annotation.Config;
import me.shedaniel.autoconfig.annotation.ConfigEntry;
import me.shedaniel.autoconfig.serializer.PartitioningSerializer;
import net.krlite.equator.math.algebra.Theory;
import net.krlite.knowledges.impl.component.CrosshairComponent;
import net.krlite.knowledges.impl.data.info.block.blockinformation.NoteBlockInformationData;

Expand All @@ -20,14 +21,49 @@ public class KnowledgesClientConfig extends PartitioningSerializer.GlobalData {

@Config(name = "general")
public static class General implements ConfigData {
public int mainScalar = 1000;
public int crosshairSafeAreaScalar = 1000;
public double mainScalar = 1.0;
public double crosshairSafeAreaScalar = 1.0;
public double crosshairPrimaryOpacity = 0.72;
public double crosshairSecondaryOpacity = 0.5;
public boolean visibleInDebugHud = false;
public boolean autoTidiesUp = false;

public int mainScalarAsInt() {
return (int) (mainScalar * 1000);
}

public void mainScalarAsInt(int scalar) {
mainScalar = Theory.clamp(scalar / 1000.0, 0.5, 2.0);
}

public int crosshairSafeAreaScalarAsInt() {
return (int) (crosshairSafeAreaScalar * 1000);
}

public void crosshairSafeAreaScalarAsInt(int scalar) {
crosshairSafeAreaScalar = Theory.clamp(scalar / 1000.0, 0.5, 2.0);
}

public int crosshairPrimaryOpacityAsInt() {
return (int) (crosshairPrimaryOpacity * 1000);
}

public void crosshairPrimaryOpacityAsInt(int opacity) {
crosshairPrimaryOpacity = Theory.clamp(opacity / 1000.0, 0.1, 1.0);
}

public int crosshairSecondaryOpacityAsInt() {
return (int) (crosshairSecondaryOpacity * 1000);
}

public void crosshairSecondaryOpacityAsInt(int opacity) {
crosshairSecondaryOpacity = Theory.clamp(opacity / 1000.0, 0.1, 1.0);
}
}

@Config(name = "components")
public static class Components implements ConfigData {
public boolean autoTidiesUp = false;

public Crosshair crosshair = new Crosshair();
public InfoBlock infoBlock = new InfoBlock();
public InfoEntity infoEntity = new InfoEntity();
Expand Down Expand Up @@ -60,6 +96,8 @@ public static class InfoFluid {

@Config(name = "data")
public static class Data implements ConfigData {
public boolean autoTidiesUp = false;

public NoteBlockInformation noteBlockInformation = new NoteBlockInformation();

public static class NoteBlockInformation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,12 @@

@Config(name = "knowledges")
public class KnowledgesCommonConfig extends PartitioningSerializer.GlobalData{
public General general = new General();

public Tags tags = new Tags();

@Config(name = "general")
public static class General implements ConfigData {
public boolean autoTidiesUp = false;
}

@Config(name = "tags")
public static class Tags implements ConfigData {
public boolean autoTidiesUp = false;

public Map<String, Boolean> enabled = new TreeMap<>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,14 @@ public void register(Object object, BooleanListEntry entry) {
.setShouldTabsSmoothScroll(true)
.setShouldListSmoothScroll(true)
.setSavingRunnable(() -> {
if (KnowledgesClient.CONFIG.general.autoTidiesUp) KnowledgesClient.tidyUpConfig();
KnowledgesClient.CONFIG_HOLDER.save();
KnowledgesClient.tidyUpConfig();
KnowledgesClient.CONFIG.save();
});
private final ConfigEntryBuilder entryBuilder = configBuilder.entryBuilder();

public KnowledgesConfigScreen(Screen parent) {
KnowledgesClient.CONFIG.load();

configBuilder.setParentScreen(parent);
BooleanListEntrySyncHelper.clearAll();

Expand Down Expand Up @@ -122,48 +124,63 @@ private void initGeneralEntries() {
category.addEntry(
entryBuilder.startIntSlider(
localize("general", "main_scalar"),
KnowledgesClient.CONFIG.general.mainScalar,
KnowledgesClient.CONFIG.get().general.mainScalarAsInt(),
500, 2000
)
.setDefaultValue(KnowledgesClient.DEFAULT_CONFIG.general.mainScalar)
.setDefaultValue(KnowledgesClient.DEFAULT_CONFIG.general.mainScalarAsInt())
.setTooltip(localize("general", "main_scalar", "tooltip"))
.setSaveConsumer(value -> KnowledgesClient.CONFIG.general.mainScalar = value)
.setSaveConsumer(KnowledgesClient.CONFIG.get().general::mainScalarAsInt)
.setTextGetter(value -> Text.literal(String.format("%.2f", value / 1000.0)))
.build()
);

category.addEntry(
entryBuilder.startIntSlider(
localize("general", "crosshair_safe_area_scalar"),
KnowledgesClient.CONFIG.general.crosshairSafeAreaScalar,
500, 2000
KnowledgesClient.CONFIG.get().general.crosshairSafeAreaScalarAsInt(),
500, 2000
)
.setDefaultValue(KnowledgesClient.DEFAULT_CONFIG.general.crosshairSafeAreaScalar)
.setDefaultValue(KnowledgesClient.DEFAULT_CONFIG.general.crosshairSafeAreaScalarAsInt())
.setTooltip(localize("general", "crosshair_safe_area_scalar", "tooltip"))
.setSaveConsumer(value -> KnowledgesClient.CONFIG.general.crosshairSafeAreaScalar = value)
.setSaveConsumer(KnowledgesClient.CONFIG.get().general::crosshairSafeAreaScalarAsInt)
.setTextGetter(value -> Text.literal(String.format("%.2f", value / 1000.0)))
.build()
);

category.addEntry(
entryBuilder.startBooleanToggle(
localize("general", "visible_in_debug_hud"),
KnowledgesClient.CONFIG.general.visibleInDebugHud
entryBuilder.startIntSlider(
localize("general", "crosshair_primary_opacity"),
KnowledgesClient.CONFIG.get().general.crosshairPrimaryOpacityAsInt(),
100, 1000
)
.setDefaultValue(KnowledgesClient.DEFAULT_CONFIG.general.visibleInDebugHud)
.setTooltip(localize("general", "visible_in_debug_hud", "tooltip"))
.setSaveConsumer(value -> KnowledgesClient.CONFIG.general.visibleInDebugHud = value)
.setDefaultValue(KnowledgesClient.DEFAULT_CONFIG.general.crosshairPrimaryOpacityAsInt())
.setTooltip(localize("general", "crosshair_primary_opacity", "tooltip"))
.setSaveConsumer(KnowledgesClient.CONFIG.get().general::crosshairPrimaryOpacityAsInt)
.setTextGetter(value -> Text.literal(String.format("%.2f", value / 1000.0)))
.build()
);

category.addEntry(
entryBuilder.startIntSlider(
localize("general", "crosshair_secondary_opacity"),
KnowledgesClient.CONFIG.get().general.crosshairSecondaryOpacityAsInt(),
100, 1000
)
.setDefaultValue(KnowledgesClient.DEFAULT_CONFIG.general.crosshairSecondaryOpacityAsInt())
.setTooltip(localize("general", "crosshair_secondary_opacity", "tooltip"))
.setSaveConsumer(KnowledgesClient.CONFIG.get().general::crosshairSecondaryOpacityAsInt)
.setTextGetter(value -> Text.literal(String.format("%.2f", value / 1000.0)))
.build()
);

category.addEntry(
entryBuilder.startBooleanToggle(
localize("general", "auto_tidies_up"),
KnowledgesClient.CONFIG.general.autoTidiesUp
localize("general", "visible_in_debug_hud"),
KnowledgesClient.CONFIG.get().general.visibleInDebugHud
)
.setDefaultValue(KnowledgesClient.DEFAULT_CONFIG.general.autoTidiesUp)
.setTooltip(localize("general", "auto_tidies_up", "tooltip"))
.setSaveConsumer(value -> KnowledgesClient.CONFIG.general.autoTidiesUp = value)
.setDefaultValue(KnowledgesClient.DEFAULT_CONFIG.general.visibleInDebugHud)
.setTooltip(localize("general", "visible_in_debug_hud", "tooltip"))
.setSaveConsumer(value -> KnowledgesClient.CONFIG.get().general.visibleInDebugHud = value)
.build()
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import net.fabricmc.loader.api.FabricLoader;
import net.krlite.knowledges.KnowledgesClient;
import net.krlite.knowledges.KnowledgesCommon;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -19,6 +21,7 @@
import java.util.function.Consumer;

public abstract class Cache<K, V> {
public static final Logger LOGGER = LoggerFactory.getLogger(KnowledgesCommon.ID + ":cache");
public static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve(KnowledgesCommon.ID).resolve("cache");

private final Map<K, V> map = new HashMap<>();
Expand Down Expand Up @@ -81,7 +84,7 @@ public void save() {
file.getParentFile().mkdirs();
file.createNewFile();
} catch (IOException e) {
KnowledgesClient.LOGGER.error("Failed creating file!", e);
LOGGER.error("Failed creating file!", e);
}
}

Expand All @@ -104,7 +107,7 @@ public void load() {
tempMap.forEach(this::put);
}
} catch (Exception e) {
KnowledgesClient.LOGGER.error("Error caching! Deleting file {}", file.getPath());
LOGGER.error("Error caching! Deleting file at {}", file.getPath());
file.delete();
} finally {
loading = false;
Expand All @@ -128,7 +131,7 @@ public void run() {
Files.writeString(file.toPath(), data);
}
} catch (IOException e) {
KnowledgesClient.LOGGER.error("Failed saving cache!");
LOGGER.error("Failed saving cache at {}!", file.getPath());
}
}
}
Expand Down
Loading

0 comments on commit 0621ed3

Please sign in to comment.