Skip to content

Commit

Permalink
Initial update to use autoconfig
Browse files Browse the repository at this point in the history
  • Loading branch information
BoogieMonster1O1 committed Feb 12, 2021
1 parent 2975348 commit 31a1a70
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 168 deletions.
30 changes: 9 additions & 21 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,13 @@ buildscript {
}
}
dependencies {
classpath 'net.fabricmc:fabric-loom:0.5-SNAPSHOT'
classpath 'net.fabricmc:fabric-loom:0.6-SNAPSHOT'
classpath 'com.github.jengelman.gradle.plugins:shadow:6.1.0'
}
}

apply plugin: 'fabric-loom'
apply plugin: 'maven-publish'
apply plugin: 'com.github.johnrengelman.shadow'

sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
Expand All @@ -34,30 +33,22 @@ repositories {
mavenCentral()
maven { url "https://dl.bintray.com/shedaniel/shedaniel-mods" }
maven { url 'https://jitpack.io' }
jcenter()
}

minecraft {
accessWidener 'src/main/resources/vanillafix_aw.txt'
}

configurations {
shade
}

tasks.remapJar.dependsOn(shadowJar)
(tasks.remapJar.input as FileSystemLocationProperty<? extends FileSystemLocation>).set(shadowJar.archivePath)

dependencies {
implementation "blue.endless:jankson:1.1.0"
shade "blue.endless:jankson:1.1.0"
minecraft "com.mojang:minecraft:${project.minecraft_version}"
mappings "net.fabricmc:yarn:${project.yarn_mappings}:v2"
modImplementation "net.fabricmc:fabric-loader:${project.loader_version}"
modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"
minecraft "com.mojang:minecraft:1.16.5"
mappings "net.fabricmc:yarn:1.16.5+build.3:v2"
modImplementation "net.fabricmc:fabric-loader:0.11.1"
modImplementation "net.fabricmc.fabric-api:fabric-api:0.30.0+1.16"

// Cos why not
modImplementation "com.github.Chocohead:Fabric-ASM:${project.fabric_asm_version}"
include "com.github.Chocohead:Fabric-ASM:${project.fabric_asm_version}"
modImplementation "com.github.Chocohead:Fabric-ASM:v2.1"
include "com.github.Chocohead:Fabric-ASM:v2.1"

// Compatibility
modImplementation 'com.github.jellysquid3:lithium-fabric:90c2427'
Expand All @@ -72,6 +63,7 @@ dependencies {
exclude module: 'fabric-api'
}
include 'me.shedaniel.cloth:config-2:4.8.2'
modImplementation include('me.sargunvohra.mcmods:autoconfig1u:3.3.1')

// Accessing the config
modImplementation('io.github.prospector:modmenu:1.14.6+build.31') {
Expand Down Expand Up @@ -111,7 +103,3 @@ jar {
from "LICENSE"
}

shadowJar {
configurations = [project.configurations.shade]
relocate 'blue.endless.jankson', 'org.dimdev.vanillafix.shadowed.blue.endless.jankson'
}
42 changes: 8 additions & 34 deletions src/main/java/org/dimdev/vanillafix/VanillaFix.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;
import java.util.stream.Collectors;

import blue.endless.jankson.Jankson;
import blue.endless.jankson.JsonObject;
import blue.endless.jankson.impl.SyntaxError;
import com.google.common.collect.ImmutableMap;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import me.sargunvohra.mcmods.autoconfig1u.ConfigHolder;
import me.sargunvohra.mcmods.autoconfig1u.serializer.JanksonConfigSerializer;
import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.Jankson;
import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.JsonObject;
import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.impl.SyntaxError;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.dimdev.vanillafix.util.ConfigPair;
Expand All @@ -24,10 +26,10 @@
public class VanillaFix implements ModInitializer {
public static final Logger LOGGER = LogManager.getLogger();
public static final ModContainer MOD = FabricLoader.getInstance().getModContainer("vanillafix").orElseThrow(IllegalStateException::new);
private static ModConfig MOD_CONFIG;
private static final Path PATH = FabricLoader.getInstance().getConfigDir().resolve("vanillafix.json5");
public static final Jankson JANKSON = Jankson.builder().build();
public static final Map<String, ConfigPair> MIXIN_CONFIGS;
public static final ConfigHolder<ModConfig> CONFIG = AutoConfig.register(ModConfig.class, JanksonConfigSerializer::new);

@Override
public void onInitialize() {
Expand All @@ -42,24 +44,6 @@ public void onInitialize() {
}

static {
try {
if (Files.exists(PATH)) {
MOD_CONFIG = JANKSON.fromJson(JANKSON.load(Files.newInputStream(PATH)), ModConfig.class);
} else {
Files.createFile(PATH);
Files.write(PATH, JANKSON.toJson(MOD_CONFIG).toJson(true, true).getBytes());
}
} catch (IOException e) {
LOGGER.error("Error loading config. Using default values");
e.printStackTrace();
MOD_CONFIG = new ModConfig();
} catch (SyntaxError e) {
LOGGER.error("Caught a Syntax error when loading config. Using default values");
LOGGER.error(e.getCompleteMessage());
e.printStackTrace();
MOD_CONFIG = new ModConfig();
}

ImmutableMap.Builder<String, ConfigPair> builder = ImmutableMap.builder();
try (InputStream stream = VanillaFix.class.getResourceAsStream("/data/vanillafix/mixin_configs.json")) {
JsonObject jsonObject = JANKSON.load(stream);
Expand Down Expand Up @@ -89,17 +73,7 @@ public ConfigPair setValue(ConfigPair value) {
MIXIN_CONFIGS = builder.build();
}

public static void save() {
String json = JANKSON.toJson(MOD_CONFIG).toJson(true, true);
try {
Files.write(PATH, json.getBytes());
} catch (IOException e) {
LOGGER.error("Error saving config");
e.printStackTrace();
}
}

public static ModConfig config() {
return MOD_CONFIG;
return CONFIG.get();
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,9 @@
package org.dimdev.vanillafix.client.modsupport;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

import io.github.prospector.modmenu.api.ConfigScreenFactory;
import io.github.prospector.modmenu.api.ModMenuApi;
import me.shedaniel.clothconfig2.api.ConfigBuilder;
import me.shedaniel.clothconfig2.api.ConfigCategory;
import me.shedaniel.clothconfig2.api.ConfigEntryBuilder;
import me.shedaniel.clothconfig2.gui.entries.BooleanListEntry;
import me.shedaniel.clothconfig2.gui.entries.FloatListEntry;
import me.shedaniel.clothconfig2.gui.entries.IntegerListEntry;
import org.dimdev.vanillafix.VanillaFix;
import org.dimdev.vanillafix.util.annotation.Exclude;

import net.minecraft.text.Text;
import net.minecraft.text.TranslatableText;
import me.sargunvohra.mcmods.autoconfig1u.AutoConfig;
import org.dimdev.vanillafix.util.config.ModConfig;

import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
Expand All @@ -24,70 +12,6 @@
public class ModMenuSupport implements ModMenuApi {
@Override
public ConfigScreenFactory<?> getModConfigScreenFactory() {
return (parent) -> {
ConfigBuilder builder = ConfigBuilder.create()
.setParentScreen(parent)
.setSavingRunnable(VanillaFix::save)
.setTitle(new TranslatableText("vanillafix.config.title"));
ConfigEntryBuilder entryBuilder = builder.entryBuilder();
try {
for (Field field : VanillaFix.config().getClass().getDeclaredFields()) {
int mods = field.getModifiers();
if (Modifier.isStatic(mods) || Modifier.isFinal(mods) || Modifier.isTransient(mods) || field.isAnnotationPresent(Exclude.class)) {
continue;
}
String name = field.getName();
ConfigCategory cat = builder.getOrCreateCategory(new TranslatableText("vanillafix.config.category." + name));
Object value = field.get(VanillaFix.config());
for (Field innerField : value.getClass().getDeclaredFields()) {
int innerMods = innerField.getModifiers();
if (Modifier.isStatic(innerMods) || Modifier.isFinal(innerMods) || Modifier.isTransient(innerMods)) {
continue;
}
String innerName = innerField.getName();
Class<?> innerType = innerField.getType();
Text text = new TranslatableText("vanillafix.config.value." + name + "." + innerName);
if (innerType == boolean.class || innerType == Boolean.class) {
BooleanListEntry entry = entryBuilder.startBooleanToggle(text, innerField.getBoolean(value))
.requireRestart()
.setSaveConsumer(bl -> {
try {
innerField.setBoolean(value, bl);
} catch (IllegalAccessException e) {
throw new AssertionError();
}
})
.build();
cat.addEntry(entry);
} else if (innerType == int.class || innerType == Integer.class) {
IntegerListEntry entry = entryBuilder.startIntField(text, innerField.getInt(value))
.requireRestart()
.setSaveConsumer(i -> {
try {
innerField.setInt(value, i);
} catch (IllegalAccessException e) {
throw new AssertionError();
}
}).build();
cat.addEntry(entry);
} else if (innerType == float.class || innerType == Float.class) {
FloatListEntry entry = entryBuilder.startFloatField(text, innerField.getFloat(value))
.requireRestart()
.setSaveConsumer(f -> {
try {
innerField.setFloat(value, f);
} catch (IllegalAccessException e) {
throw new AssertionError();
}
}).build();
cat.addEntry(entry);
}
}
}
return builder.build();
} catch (IllegalAccessException e) {
throw new AssertionError();
}
};
return (parent) -> AutoConfig.getConfigScreen(ModConfig.class, parent).get();
}
}
11 changes: 0 additions & 11 deletions src/main/java/org/dimdev/vanillafix/util/annotation/Exclude.java

This file was deleted.

13 changes: 0 additions & 13 deletions src/main/java/org/dimdev/vanillafix/util/annotation/Tooltip.java

This file was deleted.

32 changes: 27 additions & 5 deletions src/main/java/org/dimdev/vanillafix/util/config/ModConfig.java
Original file line number Diff line number Diff line change
@@ -1,70 +1,92 @@
package org.dimdev.vanillafix.util.config;

import blue.endless.jankson.Comment;
import org.dimdev.vanillafix.util.annotation.Exclude;

public class ModConfig {
import me.sargunvohra.mcmods.autoconfig1u.ConfigData;
import me.sargunvohra.mcmods.autoconfig1u.annotation.Config;
import me.sargunvohra.mcmods.autoconfig1u.annotation.ConfigEntry;
import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.Comment;

@Config(name = "vanillafix")
public class ModConfig implements ConfigData {
@ConfigEntry.Gui.TransitiveObject
public General general = new General();

@ConfigEntry.Gui.TransitiveObject
public BugFixes bugFixes = new BugFixes();

@ConfigEntry.Gui.TransitiveObject
public ClientOnly clientOnly = new ClientOnly();

@Exclude
@ConfigEntry.Gui.Excluded
public AntiCheat antiCheat = new AntiCheat();

public static class General {
@ConfigEntry.Gui.NoTooltip
@Comment("Improve the profiler by splitting it into more sections")
public boolean profilerImprovements = true;
}

public static class BugFixes {
@ConfigEntry.Gui.NoTooltip
@Comment("Disables loading the spawn chunks when the server starts. This drastically reduces world loading times. As a side effect, invisible chunks may appear for the first few seconds when creating a new world")
public boolean disableInitialChunkLoad = true;

@ConfigEntry.Gui.NoTooltip
@Comment("Compare items by item type rather than NBT when looking for items for the crafting recipe")
public boolean fixRecipeBookIngredientsWithTags = true;

@ConfigEntry.Gui.NoTooltip
@Comment("Updates the fall distance before notifying the block fallen upon that the entity has fallen on it")
public boolean updateFallDistance = true;

@ConfigEntry.Gui.NoTooltip
@Comment("Fixes a bug where the stone shore biome has a different water color than other coastal cold biomes")
public boolean fixStoneShoreColors = true;

@ConfigEntry.Gui.NoTooltip
@Comment("Sets the mob cap for phantoms. Setting this to any negative number will disable phantom check")
public int phantomMobCap = -1;

@ConfigEntry.Gui.NoTooltip
@Comment("Prevent placing sugarcane underwater.")
public boolean underwaterSugarcaneFix = true;

@ConfigEntry.Gui.NoTooltip
@Comment("Prevents consuming of food that is being eaten on death when keepInventory is enabled")
public boolean doNotConsumeFoodOnDeath = true;

@ConfigEntry.Gui.NoTooltip
@Comment("Prevents running commands longer than 255 characters from a sign")
public boolean fixSignCommands = true;
}

public static class ClientOnly {
@ConfigEntry.Gui.NoTooltip
@Comment("Optimizes animated textures by ticking only visible textures. Disabled by default as it currently has quite a few issues")
public boolean optimizedAnimatedTextures = false;

@ConfigEntry.Gui.NoTooltip
@Comment("Allows opening screens when inside a nether portal")
public boolean screenInNetherPortal = true;

@ConfigEntry.Gui.NoTooltip
@Comment("Set the profilers location to \"gui\" from \"texture\" when running gui logic")
public boolean splitScreenAndTextureProfiler = true;

@ConfigEntry.Gui.NoTooltip
@Comment("Makes interdimensional teleportation nearly as fast as same-dimension teleportation by removing the \"Downloading terrain...\" screen")
public boolean fastInterdimensionalTeleportation = true;

@ConfigEntry.Gui.NoTooltip
@Comment("Prevents showing particles that can not be seen")
public boolean cullParticles = true;
}

public static class AntiCheat {
@ConfigEntry.Gui.NoTooltip
@Comment("Prevents players from stepping up one block (stepping does not reduce hunger)")
public boolean fixStepHeight = true;

@ConfigEntry.Gui.NoTooltip
@Comment("Prevents players from being invulnerable during or after a teleport to a new dimension")
public boolean noPlayerInvulnerabilityAfterTeleport = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
import java.util.Objects;
import java.util.stream.Stream;

import blue.endless.jankson.JsonArray;
import blue.endless.jankson.JsonElement;
import blue.endless.jankson.JsonNull;
import blue.endless.jankson.JsonObject;
import blue.endless.jankson.JsonPrimitive;
import com.google.common.collect.Lists;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.DataResult;
import com.mojang.serialization.DynamicOps;
import com.mojang.serialization.MapLike;
import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.JsonArray;
import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.JsonElement;
import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.JsonNull;
import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.JsonObject;
import me.sargunvohra.mcmods.autoconfig1u.shadowed.blue.endless.jankson.JsonPrimitive;

public enum JanksonOps implements DynamicOps<JsonElement> {
INSTANCE;
Expand Down

0 comments on commit 31a1a70

Please sign in to comment.