Skip to content

Commit

Permalink
Fix "cost_function" being used in V1 configs
Browse files Browse the repository at this point in the history
- and move current configs to legacy package
  • Loading branch information
mschae23 committed Nov 28, 2024
1 parent 3bda648 commit 600ba25
Show file tree
Hide file tree
Showing 16 changed files with 233 additions and 146 deletions.
12 changes: 6 additions & 6 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/versions.html
minecraft_version=1.21.2-rc1
yarn_mappings=1.21.2-rc1+build.1
loader_version=0.16.7
minecraft_version=1.21.4-pre3
yarn_mappings=1.21.4-pre3+build.2
loader_version=0.16.9

# Mod Properties
mod_version = 3.2.2
mod_version = 4.0.0
maven_group = de.mschae23.minecraft.mod
archives_base_name = grind-enchantments

# Dependencies
fabric_api_version=0.106.0+1.21.2
codec_config_api_version=2.0.0+1.20.5-rc3
fabric_api_version=0.110.0+1.21.4
codec_config_api_version=2.1.0+1.21.3
tax_free_levels_version=1.4.1-fabric-1.21.1
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import net.minecraft.text.MutableText;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
import de.mschae23.grindenchantments.config.DedicatedServerConfig;
import de.mschae23.grindenchantments.config.legacy.DedicatedServerConfig;
import de.mschae23.grindenchantments.config.FilterConfig;
import de.mschae23.grindenchantments.cost.CostFunction;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,14 @@
import net.fabricmc.api.ModInitializer;
import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents;
import net.fabricmc.loader.api.FabricLoader;
import de.mschae23.config.api.ConfigIo;
import de.mschae23.config.api.ModConfig;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import de.mschae23.grindenchantments.config.GrindEnchantmentsV3Config;
import de.mschae23.grindenchantments.config.v1.GrindEnchantmentsV1Config;
import de.mschae23.grindenchantments.config.v2.GrindEnchantmentsV2Config;
import com.mojang.serialization.MapCodec;
import de.mschae23.config.api.ConfigIo;
import de.mschae23.config.api.ModConfig;
import de.mschae23.grindenchantments.config.legacy.v1.GrindEnchantmentsConfigV1;
import de.mschae23.grindenchantments.config.legacy.v2.GrindEnchantmentsConfigV2;
import de.mschae23.grindenchantments.config.legacy.GrindEnchantmentsConfigV3;
import de.mschae23.grindenchantments.cost.CostFunctionType;
import de.mschae23.grindenchantments.event.ApplyLevelCostEvent;
import de.mschae23.grindenchantments.event.GrindstoneEvents;
Expand All @@ -51,16 +52,27 @@ public class GrindEnchantmentsMod implements ModInitializer {

public static final Path CONFIG_PATH = Paths.get(MODID + ".json");

private static final GrindEnchantmentsV3Config LATEST_CONFIG_DEFAULT = GrindEnchantmentsV3Config.DEFAULT;
private static final int LATEST_CONFIG_VERSION = LATEST_CONFIG_DEFAULT.version();
private static final Codec<ModConfig<GrindEnchantmentsV3Config>> CONFIG_CODEC = ModConfig.createCodec(LATEST_CONFIG_VERSION, GrindEnchantmentsMod::getConfigType);

private static GrindEnchantmentsV3Config CONFIG = LATEST_CONFIG_DEFAULT;
private static GrindEnchantmentsConfigV3 LEGACY_CONFIG = GrindEnchantmentsConfigV3.DEFAULT;

@Override
public void onInitialize() {
final GrindEnchantmentsConfigV3 legacyLatestConfigDefault = GrindEnchantmentsConfigV3.DEFAULT;
final int legacyLatestConfigVersion = legacyLatestConfigDefault.version();
@SuppressWarnings({"unchecked", "deprecation"})
final MapCodec<? extends ModConfig<GrindEnchantmentsConfigV3>>[] legacyConfigCodecs = new MapCodec[] {
GrindEnchantmentsConfigV1.TYPE_CODEC, GrindEnchantmentsConfigV2.TYPE_CODEC, GrindEnchantmentsConfigV3.TYPE_CODEC
};

final Codec<ModConfig<GrindEnchantmentsConfigV3>> legacyConfigCodec = ModConfig.createCodec(legacyLatestConfigVersion, version ->
getConfigType(0, legacyConfigCodecs, version));

// TODO Proper solution to client-side configs
// ClientLifecycleEvents.CLIENT_STARTED.register(client ->
// CONFIG = ConfigIo.initializeConfig(Paths.get(MODID + ".json"), LATEST_CONFIG_VERSION, LATEST_CONFIG_DEFAULT, CONFIG_CODEC,
// JsonOps.INSTANCE, LOGGER::info, LOGGER::error)
// );
ServerLifecycleEvents.SERVER_STARTING.register(server ->
CONFIG = ConfigIo.initializeConfig(Paths.get(MODID + ".json"), LATEST_CONFIG_VERSION, LATEST_CONFIG_DEFAULT, CONFIG_CODEC,
LEGACY_CONFIG = ConfigIo.initializeConfig(Paths.get(MODID + ".json"), legacyLatestConfigVersion, legacyLatestConfigDefault, legacyConfigCodec,
RegistryOps.of(JsonOps.INSTANCE, server.getRegistryManager()), LOGGER::info, LOGGER::error)
);

Expand Down Expand Up @@ -89,21 +101,22 @@ public void onInitialize() {
}
}

@SuppressWarnings("deprecation")
private static ModConfig.Type<GrindEnchantmentsV3Config, ?> getConfigType(int version) {
return new ModConfig.Type<>(version, switch (version) {
case 1 -> GrindEnchantmentsV1Config.TYPE_CODEC;
case 2 -> GrindEnchantmentsV2Config.TYPE_CODEC;
default -> GrindEnchantmentsV3Config.TYPE_CODEC;
});
private static <T extends ModConfig<T>> ModConfig.Type<T, ?> getConfigType(int versionOffset, MapCodec<? extends ModConfig<T>>[] codecs, int version) {
for (int i = codecs.length; i > 0; i--) {
if (version == i) {
return new ModConfig.Type<>(i + versionOffset, codecs[i - 1]);
}
}

return new ModConfig.Type<>(codecs.length + versionOffset, codecs[codecs.length - 1]);
}

public static GrindEnchantmentsV3Config getConfig() {
return CONFIG;
public static GrindEnchantmentsConfigV3 getConfig() {
return LEGACY_CONFIG;
}

public static void log(Level level, Object message) {
LOGGER.log(level, "[Grind Enchantments] " + message);
LOGGER.log(level, "[Grind Enchantments] {}", message);
}

public static Identifier id(String path) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.mschae23.grindenchantments.config;
package de.mschae23.grindenchantments.config.legacy;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.mschae23.grindenchantments.config;
package de.mschae23.grindenchantments.config.legacy;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,36 +17,40 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.mschae23.grindenchantments.config;
package de.mschae23.grindenchantments.config.legacy;

import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.config.api.ModConfig;
import de.mschae23.grindenchantments.config.DisenchantConfig;
import de.mschae23.grindenchantments.config.FilterConfig;
import de.mschae23.grindenchantments.config.MoveConfig;
import de.mschae23.grindenchantments.config.ResetRepairCostConfig;

public record GrindEnchantmentsV3Config(DisenchantConfig disenchant, MoveConfig move, ResetRepairCostConfig resetRepairCost,
public record GrindEnchantmentsConfigV3(DisenchantConfig disenchant, MoveConfig move, ResetRepairCostConfig resetRepairCost,
FilterConfig filter,
DedicatedServerConfig dedicatedServerConfig, ClientConfig clientConfig) implements ModConfig<GrindEnchantmentsV3Config> {
public static final MapCodec<GrindEnchantmentsV3Config> TYPE_CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
DisenchantConfig.CODEC.fieldOf("disenchant_to_book").forGetter(GrindEnchantmentsV3Config::disenchant),
MoveConfig.CODEC.fieldOf("move_enchantments").forGetter(GrindEnchantmentsV3Config::move),
ResetRepairCostConfig.CODEC.fieldOf("reset_repair_cost").forGetter(GrindEnchantmentsV3Config::resetRepairCost),
FilterConfig.CODEC.fieldOf("filter").forGetter(GrindEnchantmentsV3Config::filter),
DedicatedServerConfig.CODEC.orElse(DedicatedServerConfig.DEFAULT).fieldOf("dedicated_server_options").forGetter(GrindEnchantmentsV3Config::dedicatedServerConfig),
ClientConfig.CODEC.fieldOf("client_options").forGetter(GrindEnchantmentsV3Config::clientConfig)
).apply(instance, instance.stable(GrindEnchantmentsV3Config::new)));
DedicatedServerConfig dedicatedServerConfig, ClientConfig clientConfig) implements ModConfig<GrindEnchantmentsConfigV3> {
public static final MapCodec<GrindEnchantmentsConfigV3> TYPE_CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
DisenchantConfig.CODEC.fieldOf("disenchant_to_book").forGetter(GrindEnchantmentsConfigV3::disenchant),
MoveConfig.CODEC.fieldOf("move_enchantments").forGetter(GrindEnchantmentsConfigV3::move),
ResetRepairCostConfig.CODEC.fieldOf("reset_repair_cost").forGetter(GrindEnchantmentsConfigV3::resetRepairCost),
FilterConfig.CODEC.fieldOf("filter").forGetter(GrindEnchantmentsConfigV3::filter),
DedicatedServerConfig.CODEC.orElse(DedicatedServerConfig.DEFAULT).fieldOf("dedicated_server_options").forGetter(GrindEnchantmentsConfigV3::dedicatedServerConfig),
ClientConfig.CODEC.fieldOf("client_options").forGetter(GrindEnchantmentsConfigV3::clientConfig)
).apply(instance, instance.stable(GrindEnchantmentsConfigV3::new)));

public static final ModConfig.Type<GrindEnchantmentsV3Config, GrindEnchantmentsV3Config> TYPE = new ModConfig.Type<>(3, TYPE_CODEC);
public static final ModConfig.Type<GrindEnchantmentsConfigV3, GrindEnchantmentsConfigV3> TYPE = new ModConfig.Type<>(3, TYPE_CODEC);

public static final GrindEnchantmentsV3Config DEFAULT =
new GrindEnchantmentsV3Config(DisenchantConfig.DEFAULT, MoveConfig.DEFAULT, ResetRepairCostConfig.DEFAULT, FilterConfig.DEFAULT, DedicatedServerConfig.DEFAULT, ClientConfig.DEFAULT);
public static final GrindEnchantmentsConfigV3 DEFAULT =
new GrindEnchantmentsConfigV3(DisenchantConfig.DEFAULT, MoveConfig.DEFAULT, ResetRepairCostConfig.DEFAULT, FilterConfig.DEFAULT, DedicatedServerConfig.DEFAULT, ClientConfig.DEFAULT);

@Override
public Type<GrindEnchantmentsV3Config, ?> type() {
public Type<GrindEnchantmentsConfigV3, ?> type() {
return TYPE;
}

@Override
public GrindEnchantmentsV3Config latest() {
public GrindEnchantmentsConfigV3 latest() {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2024 mschae23
*
* This file is part of Grind enchantments.
*
* Grind enchantments is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.mschae23.grindenchantments.config.legacy.v1;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.config.DisenchantConfig;
import de.mschae23.grindenchantments.cost.CostFunction;

@Deprecated
public record DisenchantConfigV1(boolean enabled, boolean consumeItem, CostFunction costFunction) {
public static final Codec<DisenchantConfigV1> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("enabled").forGetter(DisenchantConfigV1::enabled),
Codec.BOOL.fieldOf("consume_enchanted_item").forGetter(DisenchantConfigV1::consumeItem),
CostFunction.TYPE_CODEC.fieldOf("cost_config").forGetter(DisenchantConfigV1::costFunction)
).apply(instance, instance.stable(DisenchantConfigV1::new)));

public DisenchantConfig latest() {
return new DisenchantConfig(this.enabled, this.consumeItem, this.costFunction);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Copyright (C) 2024 mschae23
*
* This file is part of Grind enchantments.
*
* Grind enchantments is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.mschae23.grindenchantments.config.legacy.v1;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.config.api.ModConfig;
import de.mschae23.grindenchantments.config.legacy.ClientConfig;
import de.mschae23.grindenchantments.config.legacy.DedicatedServerConfig;
import de.mschae23.grindenchantments.config.legacy.v2.GrindEnchantmentsConfigV2;
import de.mschae23.grindenchantments.config.legacy.GrindEnchantmentsConfigV3;

@Deprecated
@SuppressWarnings("DeprecatedIsStillUsed")
public record GrindEnchantmentsConfigV1(de.mschae23.grindenchantments.config.legacy.v1.DisenchantConfigV1 disenchant, MoveConfigV1 move, boolean allowCurses,
DedicatedServerConfig dedicatedServerConfig, ClientConfig clientConfig) implements ModConfig<GrindEnchantmentsConfigV3> {
public static final MapCodec<GrindEnchantmentsConfigV1> TYPE_CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
DisenchantConfigV1.CODEC.fieldOf("disenchant_to_book").forGetter(GrindEnchantmentsConfigV1::disenchant),
MoveConfigV1.CODEC.fieldOf("move_enchantments").forGetter(GrindEnchantmentsConfigV1::move),
Codec.BOOL.orElse(Boolean.FALSE).fieldOf("allow_removing_curses").forGetter(GrindEnchantmentsConfigV1::allowCurses),
DedicatedServerConfig.CODEC.orElse(DedicatedServerConfig.DEFAULT).fieldOf("dedicated_server_options").forGetter(GrindEnchantmentsConfigV1::dedicatedServerConfig),
ClientConfig.CODEC.fieldOf("client_options").forGetter(GrindEnchantmentsConfigV1::clientConfig)
).apply(instance, instance.stable(GrindEnchantmentsConfigV1::new)));

public static final Type<GrindEnchantmentsConfigV3, GrindEnchantmentsConfigV1> TYPE = new Type<>(1, TYPE_CODEC);

@Override
public Type<GrindEnchantmentsConfigV3, ?> type() {
return TYPE;
}

@Override
public GrindEnchantmentsConfigV3 latest() {
return new GrindEnchantmentsConfigV2(this.disenchant.latest(), this.move.latest(), this.allowCurses, this.dedicatedServerConfig, this.clientConfig).latest();
}

@Override
public boolean shouldUpdate() {
return true;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright (C) 2024 mschae23
*
* This file is part of Grind enchantments.
*
* Grind enchantments is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package de.mschae23.grindenchantments.config.legacy.v1;

import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.config.MoveConfig;
import de.mschae23.grindenchantments.cost.CostFunction;

@Deprecated
public record MoveConfigV1(boolean enabled, CostFunction costFunction) {
public static final Codec<MoveConfigV1> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("enabled").forGetter(MoveConfigV1::enabled),
CostFunction.TYPE_CODEC.fieldOf("cost_config").forGetter(MoveConfigV1::costFunction)
).apply(instance, instance.stable(MoveConfigV1::new)));

public MoveConfig latest() {
return new MoveConfig(this.enabled, this.costFunction);
}
}
Loading

0 comments on commit 600ba25

Please sign in to comment.