Skip to content

Commit

Permalink
Port to Fabric 1.20.6+
Browse files Browse the repository at this point in the history
  • Loading branch information
EnnuiL committed Jun 10, 2024
1 parent 02766af commit 7e7213f
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 122 deletions.
11 changes: 7 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ group = project.maven_group

repositories {
maven { url 'https://maven.terraformersmc.com/releases/' }
maven { url 'https://www.jitpack.io' }
}

loom {
Expand Down Expand Up @@ -37,25 +38,27 @@ dependencies {
modImplementation(libs.mod.menu) {
transitive = false
}
modImplementation libs.wrench.wrapper

implementation libs.quilt.parsers.json
shadow(libs.quilt.parsers.json) {
transitive = false
}

include libs.wrench.wrapper
}

processResources {
inputs.property 'version', version

filesMatching('quilt.mod.json') {
filesMatching('fabric.mod.json') {
expand 'version': version
}
}

tasks.withType(JavaCompile).configureEach {
it.options.encoding = 'UTF-8'
// Minecraft 1.18 (1.18-pre2) upwards uses Java 17.
it.options.release = 17
// Minecraft 1.20.6 (24w14a) upwards uses Java 21.
it.options.release.set(21)
}

java {
Expand Down
12 changes: 7 additions & 5 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
# The latest versions are available at https://quiltmc.org/en/usage/latest-versions
[versions]
minecraft = "1.20.1"
quilt_mappings = "1.20.1+build.23"
minecraft = "1.20.6"
quilt_mappings = "1.20.6+build.6"

quilt_loom = "1.6.8"
quilt_loader = "0.26.0"
shadow = "8.1.7"

quilted_fabric_api = "7.5.0+0.91.0-1.20.1"
mod_menu = "7.2.2"
fabric_api = "0.100.0+1.20.6"
mod_menu = "10.0.0-beta.1"
quilt_parsers_json = "0.3.0"
wrench_wrapper = "a29721e25d" # 0.1.0+1.20.4

[libraries]
minecraft = { module = "com.mojang:minecraft", version.ref = "minecraft" }
quilt_mappings = { module = "org.quiltmc:quilt-mappings", version.ref = "quilt_mappings" }
quilt_loader = { module = "org.quiltmc:quilt-loader", version.ref = "quilt_loader" }

quilted_fabric_api = { module = "org.quiltmc.quilted-fabric-api:quilted-fabric-api", version.ref = "quilted_fabric_api" }
quilted_fabric_api = { module = "net.fabricmc.fabric-api:fabric-api", version.ref = "fabric_api" }
mod_menu = { module = "com.terraformersmc:modmenu", version.ref = "mod_menu" }
quilt_parsers_json = { module = "org.quiltmc.parsers:json", version.ref = "quilt_parsers_json" }
wrench_wrapper = { module = "com.github.Up-Mods:wrench_wrapper", version.ref = "wrench_wrapper" }

[plugins]
quilt_loom = { id = "org.quiltmc.loom", version.ref = "quilt_loom" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@
// A JSONfied version of Quilt Config's Json5Serializer
// TODO - Contribute something to upstream!
public final class JsonSerializer implements Serializer {
public JsonSerializer() {}
public static final JsonSerializer INSTANCE = new JsonSerializer();

private JsonSerializer() {}

@Override
public String getFileExtension() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.mojang.brigadier.arguments.IntegerArgumentType;
import io.github.ennuil.boring_default_game_rules.mixin.BoundedIntRuleAccessor;
import io.github.ennuil.boring_default_game_rules.mixin.DoubleRuleAccessor;
import io.github.ennuil.boring_default_game_rules.mixin.EnumRuleAccessor;
import io.github.ennuil.boring_default_game_rules.utils.LoggingUtils;
import io.github.ennuil.boring_default_game_rules.wrench_wrapper.WrenchWrapper;
import net.fabricmc.fabric.api.gamerule.v1.FabricGameRuleVisitor;
import net.fabricmc.fabric.api.gamerule.v1.rule.DoubleRule;
import net.fabricmc.fabric.api.gamerule.v1.rule.EnumRule;
Expand All @@ -19,8 +21,6 @@
import net.minecraft.util.Language;
import net.minecraft.world.GameRules;
import org.jetbrains.annotations.Nullable;
import org.quiltmc.loader.api.QuiltLoader;
import org.quiltmc.loader.api.config.v2.QuiltConfig;

import java.io.IOException;
import java.io.Reader;
Expand All @@ -34,11 +34,11 @@
public class ModConfigManager {
public static final String GENERATE_ME = "GENERATE_ME";
public static final String GENERATE_ME_MAYBE = "GENERATE_ME_MAYBE";
public static final Path SCHEMA_DIRECTORY_PATH = QuiltLoader.getConfigDir().resolve("boring_default_game_rules");
public static final Path SCHEMA_DIRECTORY_PATH = WrenchWrapper.getConfigDir().resolve("boring_default_game_rules");
public static final Path SCHEMA_PATH = SCHEMA_DIRECTORY_PATH.resolve("config.schema.json");
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().create();

public static final ModConfig CONFIG = QuiltConfig.create("boring_default_game_rules", "config", ModConfig.class);
public static final ModConfig CONFIG = WrenchWrapper.create("boring_default_game_rules", "config", ModConfig.class);

private static JsonObject defaultGameRulesProperties;
private static String newSchemaHash = "";
Expand Down Expand Up @@ -118,16 +118,16 @@ public static void updateConfig(GameRules newGameRules) {
if (newGameRules != null) {
GameRules.accept(new FabricGameRuleVisitor() {
@Override
public void visitBoolean(GameRules.Key<GameRules.BooleanRule> key, GameRules.Type<GameRules.BooleanRule> type) {
if (newGameRules.get(key).get() != defaultGameRules.get(key).get()) {
CONFIG.defaultGameRules.value().put(key.getName(), newGameRules.get(key).get());
public void visitBooleanGameRule(GameRules.Key<GameRules.BooleanGameRule> key, GameRules.Type<GameRules.BooleanGameRule> type) {
if (newGameRules.get(key).getValue() != defaultGameRules.get(key).getValue()) {
CONFIG.defaultGameRules.value().put(key.getName(), newGameRules.get(key).getValue());
}
}

@Override
public void visitInt(GameRules.Key<GameRules.IntRule> key, GameRules.Type<GameRules.IntRule> type) {
if (newGameRules.get(key).get() != defaultGameRules.get(key).get()) {
CONFIG.defaultGameRules.value().put(key.getName(), newGameRules.get(key).get());
public void visitIntGameRule(GameRules.Key<GameRules.IntGameRule> key, GameRules.Type<GameRules.IntGameRule> type) {
if (newGameRules.get(key).getValue() != defaultGameRules.get(key).getValue()) {
CONFIG.defaultGameRules.value().put(key.getName(), newGameRules.get(key).getValue());
}
}

Expand All @@ -154,19 +154,20 @@ private static void generateGameRulePropertiesOnClient() {
defaultGameRulesProperties = new JsonObject();
GameRules.accept(new FabricGameRuleVisitor() {
@Override
public void visitBoolean(GameRules.Key<GameRules.BooleanRule> key, GameRules.Type<GameRules.BooleanRule> type) {
public void visitBooleanGameRule(GameRules.Key<GameRules.BooleanGameRule> key, GameRules.Type<GameRules.BooleanGameRule> type) {
addBooleanGameRule(
key.getName(),
I18n.translate(key.getTranslationKey()),
I18n.hasTranslation(key.getTranslationKey() + ".description")
? Text.translatable(key.getTranslationKey() + ".description").getString()
: null,
type.createRule().get());
type.createGameRule().getValue());
}

@Override
public void visitInt(GameRules.Key<GameRules.IntRule> key, GameRules.Type<GameRules.IntRule> type) {
if (type.createRule() instanceof BoundedIntRule boundedType) {
public void visitIntGameRule(GameRules.Key<GameRules.IntGameRule> key, GameRules.Type<GameRules.IntGameRule> type) {
var gameRule = type.createGameRule();
if (gameRule instanceof BoundedIntRule boundedType) {
int minimum = ((BoundedIntRuleAccessor) (Object) boundedType).getMinimumValue();
int maximum = ((BoundedIntRuleAccessor) (Object) boundedType).getMaximumValue();
addIntegerGameRule(
Expand All @@ -175,7 +176,7 @@ public void visitInt(GameRules.Key<GameRules.IntRule> key, GameRules.Type<GameRu
I18n.hasTranslation(key.getTranslationKey() + ".description")
? Text.translatable(key.getTranslationKey() + ".description").getString()
: null,
boundedType.get(),
boundedType.getValue(),
minimum,
maximum);
} else {
Expand All @@ -185,15 +186,15 @@ public void visitInt(GameRules.Key<GameRules.IntRule> key, GameRules.Type<GameRu
I18n.hasTranslation(key.getTranslationKey() + ".description")
? Text.translatable(key.getTranslationKey() + ".description").getString()
: null,
type.createRule().get(),
null,
null);
type.createGameRule().getValue(),
type.argumentType.get() instanceof IntegerArgumentType intArgType ? intArgType.getMinimum() : null,
type.argumentType.get() instanceof IntegerArgumentType intArgType ? intArgType.getMaximum() : null);
}
}

@Override
public void visitDouble(GameRules.Key<DoubleRule> key, GameRules.Type<DoubleRule> type) {
DoubleRule doubleRule = type.createRule();
var doubleRule = type.createGameRule();
double maximum = ((DoubleRuleAccessor) (Object) doubleRule).getMaximumValue();
double minimum = ((DoubleRuleAccessor) (Object) doubleRule).getMinimumValue();
addDoubleGameRule(
Expand All @@ -210,7 +211,7 @@ public void visitDouble(GameRules.Key<DoubleRule> key, GameRules.Type<DoubleRule

@Override
public <E extends Enum<E>> void visitEnum(GameRules.Key<EnumRule<E>> key, GameRules.Type<EnumRule<E>> type) {
EnumRule<E> enumRule = type.createRule();
var enumRule = type.createGameRule();
addEnumGameRule(
key.getName(),
I18n.translate(key.getTranslationKey()),
Expand All @@ -229,19 +230,19 @@ private static void generateGameRulePropertiesOnServer() {
final Language language = Language.getInstance();

@Override
public void visitBoolean(GameRules.Key<GameRules.BooleanRule> key, GameRules.Type<GameRules.BooleanRule> type) {
public void visitBooleanGameRule(GameRules.Key<GameRules.BooleanGameRule> key, GameRules.Type<GameRules.BooleanGameRule> type) {
addBooleanGameRule(
key.getName(),
language.get(key.getTranslationKey()),
language.hasTranslation(key.getTranslationKey() + ".description")
? language.get(key.getTranslationKey() + ".description")
: null,
type.createRule().get());
type.createGameRule().getValue());
}

@Override
public void visitInt(GameRules.Key<GameRules.IntRule> key, GameRules.Type<GameRules.IntRule> type) {
if (type.createRule() instanceof BoundedIntRule boundedType) {
public void visitIntGameRule(GameRules.Key<GameRules.IntGameRule> key, GameRules.Type<GameRules.IntGameRule> type) {
if (type.createGameRule() instanceof BoundedIntRule boundedType) {
int minimum = ((BoundedIntRuleAccessor) (Object) boundedType).getMinimumValue();
int maximum = ((BoundedIntRuleAccessor) (Object) boundedType).getMaximumValue();
addIntegerGameRule(
Expand All @@ -250,7 +251,7 @@ public void visitInt(GameRules.Key<GameRules.IntRule> key, GameRules.Type<GameRu
language.hasTranslation(key.getTranslationKey() + ".description")
? language.get(key.getTranslationKey() + ".description")
: null,
boundedType.get(),
boundedType.getValue(),
minimum,
maximum);
} else {
Expand All @@ -260,15 +261,15 @@ public void visitInt(GameRules.Key<GameRules.IntRule> key, GameRules.Type<GameRu
language.hasTranslation(key.getTranslationKey() + ".description")
? language.get(key.getTranslationKey() + ".description")
: null,
type.createRule().get(),
null,
null);
type.createGameRule().getValue(),
type.argumentType.get() instanceof IntegerArgumentType intArgType ? intArgType.getMinimum() : null,
type.argumentType.get() instanceof IntegerArgumentType intArgType ? intArgType.getMaximum() : null);
}
}

@Override
public void visitDouble(GameRules.Key<DoubleRule> key, GameRules.Type<DoubleRule> type) {
DoubleRule doubleRule = type.createRule();
DoubleRule doubleRule = type.createGameRule();
double maximum = ((DoubleRuleAccessor) (Object) doubleRule).getMaximumValue();
double minimum = ((DoubleRuleAccessor) (Object) doubleRule).getMinimumValue();
addDoubleGameRule(
Expand All @@ -284,7 +285,7 @@ public void visitDouble(GameRules.Key<DoubleRule> key, GameRules.Type<DoubleRule

@Override
public <E extends Enum<E>> void visitEnum(GameRules.Key<EnumRule<E>> key, GameRules.Type<EnumRule<E>> type) {
EnumRule<E> enumRule = type.createRule();
EnumRule<E> enumRule = type.createGameRule();
addEnumGameRule(
key.getName(),
language.get(key.getTranslationKey()),
Expand Down Expand Up @@ -411,7 +412,7 @@ private static <E extends Enum<E>> void addEnumGameRule(String name, String visu
}

public static void generateGameRulesHash() {
GameRules.RULE_TYPES.keySet().forEach(key -> newSchemaHash += key.getName());
GameRules.GAME_RULE_TYPES.keySet().forEach(key -> newSchemaHash += key.getName());
newSchemaHash = Hashing.sha256().hashString(newSchemaHash, StandardCharsets.UTF_8).toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
package io.github.ennuil.boring_default_game_rules.events;

import org.quiltmc.loader.api.ModContainer;
import org.quiltmc.qsl.base.api.entrypoint.client.ClientModInitializer;
import org.quiltmc.qsl.base.api.entrypoint.server.DedicatedServerModInitializer;
import io.github.ennuil.boring_default_game_rules.config.JsonSerializer;
import io.github.ennuil.boring_default_game_rules.wrench_wrapper.WrenchWrapper;
import net.fabricmc.api.ClientModInitializer;
import net.fabricmc.api.DedicatedServerModInitializer;

import io.github.ennuil.boring_default_game_rules.config.ModConfigManager;
import net.fabricmc.api.ModInitializer;

public class LoadConfigEvent implements ClientModInitializer, DedicatedServerModInitializer {
public class LoadConfigEvent implements ModInitializer, ClientModInitializer, DedicatedServerModInitializer {
@Override
public void onInitializeClient(ModContainer mod) {
public void onInitialize() {
WrenchWrapper.getConfigEnvironment().registerSerializer(JsonSerializer.INSTANCE);
}

@Override
public void onInitializeClient() {
ModConfigManager.init(true);
}

@Override
public void onInitializeServer(ModContainer mod) {
public void onInitializeServer() {
ModConfigManager.init(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import net.fabricmc.fabric.api.gamerule.v1.rule.DoubleRule;
import net.fabricmc.fabric.api.gamerule.v1.rule.EnumRule;
import net.minecraft.world.GameRules;
import net.minecraft.world.GameRules.BooleanRule;
import net.minecraft.world.GameRules.IntRule;
import org.spongepowered.asm.mixin.Final;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Shadow;
Expand All @@ -20,20 +18,20 @@
public class GameRulesMixin {
@Shadow
@Final
private Map<GameRules.Key<?>, GameRules.Rule<?>> rules;
private Map<GameRules.Key<?>, GameRules.AbstractGameRule<?>> gameRules;

@Inject(method = "<init>()V", at = @At("TAIL"))
private <E extends Enum<E>> void overrideDefaults(CallbackInfo info) {
ModConfigManager.validateInit();

if (ModConfigManager.CONFIG.defaultGameRules.value().isEmpty()) return;

this.rules.forEach((key, rule) -> ModConfigManager.CONFIG.defaultGameRules.value().forEach((defaultKey, defaultValue) -> {
this.gameRules.forEach((key, rule) -> ModConfigManager.CONFIG.defaultGameRules.value().forEach((defaultKey, defaultValue) -> {
if (key.getName().equals(defaultKey)) {
if (rule instanceof IntRule intRule) {
intRule.set(((Number) defaultValue).intValue(), null);
} else if (rule instanceof BooleanRule booleanRule) {
booleanRule.set((Boolean) defaultValue, null);
if (rule instanceof GameRules.IntGameRule intRule) {
intRule.setValue(((Number) defaultValue).intValue(), null);
} else if (rule instanceof GameRules.BooleanGameRule booleanRule) {
booleanRule.setValue((Boolean) defaultValue, null);
} else if (rule instanceof DoubleRule doubleRule) {
((DoubleRuleAccessor) (Object) doubleRule).setValue(((Number) defaultValue).doubleValue());
doubleRule.changed(null);
Expand Down
Loading

0 comments on commit 7e7213f

Please sign in to comment.