Skip to content

Commit

Permalink
remove cloth config implementation and dependency, optimize import, r…
Browse files Browse the repository at this point in the history
…ewrite metadata
  • Loading branch information
wb1016 committed Jan 23, 2024
1 parent dea027e commit ba9728d
Show file tree
Hide file tree
Showing 9 changed files with 105 additions and 115 deletions.
2 changes: 0 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,6 @@ dependencies {

modImplementation "net.fabricmc.fabric-api:fabric-api:${project.fabric_version}"

include(modImplementation("me.shedaniel.cloth:cloth-config-fabric:${project.cloth_config_version}"))

include(implementation(annotationProcessor("io.github.llamalad7:mixinextras-fabric:${project.mixinextras_version}")))
}

Expand Down
4 changes: 1 addition & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,14 @@ loader_version=0.15.6
quilt_build_number = 3

# Mod Properties
mod_version = 0.1.0
mod_version = 0.2.0
maven_group = dev.blueon
author = blueon
archives_base_name=quickleafdecay

# Dependencies
fabric_version=0.94.1+1.20.4

cloth_config_version = 12.0.109

modmenu_version = 9.0.0

mixinextras_version = 0.3.5
70 changes: 29 additions & 41 deletions src/main/java/dev/blueon/quickleafdecay/Config.java
Original file line number Diff line number Diff line change
@@ -1,47 +1,35 @@
package dev.blueon.quickleafdecay;

import me.shedaniel.autoconfig.ConfigData;
import me.shedaniel.autoconfig.annotation.ConfigEntry;

import static dev.blueon.quickleafdecay.QuickLeafDecay.NAMESPACE;

@me.shedaniel.autoconfig.annotation.Config(name = NAMESPACE)
public class Config implements ConfigData {
@ConfigEntry.Gui.Tooltip
public boolean matchLeavesTypes = FeatureControl.Defaults.matchLeavesTypes;

@ConfigEntry.Gui.Tooltip
public boolean unknownLeavesOnlyMatchSelf = FeatureControl.Defaults.unknownLeavesOnlyMatchSelf;

@ConfigEntry.Gui.Tooltip
public boolean matchLogsToLeaves = FeatureControl.Defaults.matchLogsToLeaves;

@ConfigEntry.Gui.Tooltip
public boolean ignorePersistentLeaves = FeatureControl.Defaults.ignorePersistentLeaves;

@ConfigEntry.Gui.Tooltip
public boolean accelerateLeavesDecay = FeatureControl.Defaults.accelerateLeavesDecay;

@ConfigEntry.Gui.Tooltip
@ConfigEntry.Gui.CollapsibleObject
public DecayDelay decayDelay = new DecayDelay();

@ConfigEntry.Gui.Tooltip
public boolean updateDiagonalLeaves = FeatureControl.Defaults.updateDiagonalLeaves;

@ConfigEntry.Gui.Tooltip
public boolean doDecayingLeavesEffects = FeatureControl.Defaults.doDecayingLeavesEffects;

@ConfigEntry.Gui.Tooltip
public boolean fetchTranslationUpdates = FeatureControl.Defaults.fetchTranslationUpdates;
import dev.blueon.quickleafdecay.configloader.ConfigLoader;

public class Config
{
public static boolean matchLeavesTypes = true;
public static boolean unknownLeavesOnlyMatchSelf = true;
public static boolean matchLogsToLeaves = true;
public static boolean ignorePersistentLeaves = true;
public static boolean accelerateLeavesDecay = true;
public static int minDecayDelay = 10;
public static int maxDecayDelay = 60;
public static boolean updateDiagonalLeaves = true;
public static boolean doDecayingLeavesEffects = false;
public static boolean fetchTranslationUpdates = true;

// Saving and loading

public static void load()
{
ConfigLoader.load(Config.class, getFileName());
}

public static class DecayDelay {
@ConfigEntry.Gui.Tooltip
@ConfigEntry.BoundedDiscrete(max = 100)
public int minimum = FeatureControl.Defaults.minDecayDelay;
public static void save()
{
ConfigLoader.save(Config.class, getFileName());
}

@ConfigEntry.BoundedDiscrete(max = 100)
@ConfigEntry.Gui.Tooltip
public int maximum = FeatureControl.Defaults.maxDecayDelay;
private static String getFileName()
{
return "quickleafdecay.json";
}

}
78 changes: 16 additions & 62 deletions src/main/java/dev/blueon/quickleafdecay/FeatureControl.java
Original file line number Diff line number Diff line change
@@ -1,103 +1,54 @@
package dev.blueon.quickleafdecay;

import me.shedaniel.autoconfig.AutoConfig;
import me.shedaniel.autoconfig.serializer.GsonConfigSerializer;
import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.ModContainer;
import net.fabricmc.loader.api.VersionParsingException;
import net.fabricmc.loader.api.metadata.version.VersionPredicate;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.registry.tag.TagKey;
import net.minecraft.util.random.RandomGenerator;
import org.jetbrains.annotations.NotNull;

import java.util.Optional;

public final class FeatureControl {
private static final Config CONFIG_INSTANCE;

static {
boolean shouldLoadConfig = false;

final Optional<ModContainer> optModContainer = FabricLoader.getInstance().getModContainer("cloth-config");
if (optModContainer.isPresent()) {
try {
shouldLoadConfig = VersionPredicate.parse(">=7.0.72").test(optModContainer.get().getMetadata().getVersion());
} catch (VersionParsingException e) {
e.printStackTrace();
}
}

CONFIG_INSTANCE = shouldLoadConfig ?
AutoConfig.register(Config.class, GsonConfigSerializer::new).getConfig() : null;

}

public static class Defaults {
public static final boolean matchLeavesTypes = true;
public static final boolean unknownLeavesOnlyMatchSelf = true;
public static final boolean matchLogsToLeaves = true;
public static final boolean ignorePersistentLeaves = true;
public static final boolean accelerateLeavesDecay = true;
public static final int minDecayDelay = 10;
public static final int maxDecayDelay = 60;
public static final boolean updateDiagonalLeaves = true;
public static final boolean doDecayingLeavesEffects = false;
public static final boolean fetchTranslationUpdates = true;

private Defaults() { }
}

public static boolean isConfigLoaded() {
return CONFIG_INSTANCE != null;
}


public static boolean shouldMatchLeavesTypes() {
return CONFIG_INSTANCE == null ? Defaults.matchLeavesTypes : CONFIG_INSTANCE.matchLeavesTypes;
return Config.matchLeavesTypes;
}

public static boolean shouldUnknownLeavesOnlyMatchSelf() {
return CONFIG_INSTANCE == null ? Defaults.unknownLeavesOnlyMatchSelf : CONFIG_INSTANCE.unknownLeavesOnlyMatchSelf;
return Config.unknownLeavesOnlyMatchSelf;
}

public static boolean shouldMatchLogsToLeaves() {
return CONFIG_INSTANCE == null ? Defaults.matchLogsToLeaves : CONFIG_INSTANCE.matchLogsToLeaves;
return Config.matchLogsToLeaves;
}

public static boolean shouldIgnorePersistentLeaves() {
return CONFIG_INSTANCE == null ? Defaults.ignorePersistentLeaves : CONFIG_INSTANCE.ignorePersistentLeaves;
return Config.ignorePersistentLeaves;
}

public static boolean shouldAccelerateLeavesDecay() {
return CONFIG_INSTANCE == null ? Defaults.accelerateLeavesDecay : CONFIG_INSTANCE.accelerateLeavesDecay;
return Config.accelerateLeavesDecay;
}

public static int getDecayDelay(RandomGenerator random) {
final int minDecayDelay;
final int maxDecayDelay;
if (CONFIG_INSTANCE == null) {
minDecayDelay = Defaults.minDecayDelay;
maxDecayDelay = Defaults.maxDecayDelay;
} else {
minDecayDelay = CONFIG_INSTANCE.decayDelay.minimum;
maxDecayDelay = CONFIG_INSTANCE.decayDelay.maximum;
}

minDecayDelay = Config.minDecayDelay;
maxDecayDelay = Config.maxDecayDelay;

return minDecayDelay < maxDecayDelay ?
random.range(minDecayDelay, maxDecayDelay + 1) : maxDecayDelay;
}

public static boolean shouldUpdateDiagonalLeaves() {
return CONFIG_INSTANCE == null ? Defaults.updateDiagonalLeaves : CONFIG_INSTANCE.updateDiagonalLeaves;
return Config.updateDiagonalLeaves;
}

public static boolean shouldDoDecayingLeavesEffects() {
return CONFIG_INSTANCE == null ? Defaults.doDecayingLeavesEffects : CONFIG_INSTANCE.doDecayingLeavesEffects;
return Config.doDecayingLeavesEffects;
}

public static boolean shouldFetchTranslationUpdates() {
return CONFIG_INSTANCE == null ? Defaults.fetchTranslationUpdates : CONFIG_INSTANCE.fetchTranslationUpdates;
return Config.fetchTranslationUpdates;
}

public static boolean isMatchingLeaves(TagKey<Block> leavesTag, @NotNull BlockState state, @NotNull BlockState currentLeavesState) {
Expand All @@ -106,7 +57,10 @@ public static boolean isMatchingLeaves(TagKey<Block> leavesTag, @NotNull BlockSt
else return state.isIn(leavesTag);
}

public static void init() { }
public static void init() {
Config.load();
Config.save();
}

private FeatureControl() { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.blueon.quickleafdecay.configloader;

public class ConfigException extends RuntimeException
{
public ConfigException(String message, Throwable cause)
{
super(message, cause);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//borrowed from https://github.com/WerDei/Server-Hats/blob/main/src/main/java/net/werdei/configloader/ConfigLoader.java
package dev.blueon.quickleafdecay.configloader;

import com.google.gson.GsonBuilder;
import net.fabricmc.loader.api.FabricLoader;

import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;

public abstract class ConfigLoader
{
private static final GsonBuilder gsonBuilder = new GsonBuilder()
.excludeFieldsWithModifiers(java.lang.reflect.Modifier.TRANSIENT)
.setPrettyPrinting();

public static void load(Class<?> configClass, String fileName)
{
File file = new File(FabricLoader.getInstance().getConfigDir().toFile(), fileName);
try
{
FileReader reader = new FileReader(file);
gsonBuilder.create().fromJson(reader, configClass);
reader.close();
}
catch (Exception ignored) {}

}

public static void save(Class<?> configClass, String fileName) throws ConfigException
{
File file = new File(FabricLoader.getInstance().getConfigDir().toFile(), fileName);
try
{
FileWriter writer = new FileWriter(file);
writer.write(gsonBuilder.create().toJson(configClass.getConstructor().newInstance()));
writer.close();
}
catch (Exception e)
{
throw new ConfigException("Error creating a temporary instance of class " + configClass.getName(), e);
}
}
}
Binary file modified src/main/resources/assets/quickleafdecay/quickleafdecay.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 6 additions & 7 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
"version": "${version}",

"name": "Quick Leaf Decay",
"description": "yea why not",
"description": "hard fork of 'leaves us in peace' mod. provides fast leaf decay and targets less dependencies, fast update.",
"authors": [
"Blue_ON",
"Tfarecnim",
"supersaiyansubtlety"
],
"contact": {
"sources": "https://0w0.kr"
"sources": "https://github.com/wb1016/QuickLeafDecay"
},

"license": "https://0w0.kr",
"license": "https://github.com/wb1016/QuickLeafDecay/blob/main/LICENSE",
"icon": "assets/quickleafdecay/quickleafdecay.png",

"environment": "*",
Expand All @@ -36,15 +36,14 @@
"minecraft": ">=1.20.4"
},
"recommends": {
"modmenu": ">=9.0.0",
"cloth-config2": ">=12.0.109"
"modmenu": ">=9.0.0"
},

"custom": {
"modmenu": {
"links": {
"modmenu.donate": "https://ko-fi.com/flashyreese",
"modmenu.github_releases": "https://github.com/FlashyReese/sodium-extra-fabric/releases",
"modmenu.donate": "https://github.com/sponsors/wb1016",
"modmenu.github_releases": "https://github.com/wb1016/QuickLeafDecay/releases",
"modmenu.modrinth": "https://0w0.kr"
}
}
Expand Down

0 comments on commit ba9728d

Please sign in to comment.