Skip to content

Commit

Permalink
Backport to 1.21.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mschae23 committed Dec 4, 2024
1 parent df18222 commit c937e03
Show file tree
Hide file tree
Showing 13 changed files with 67 additions and 40 deletions.
15 changes: 1 addition & 14 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
plugins {
id 'fabric-loom' version '1.8-SNAPSHOT'
id 'fabric-loom' version '1.9-SNAPSHOT'
id 'maven-publish'
}

sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

version = "${project.mod_version}+${project.minecraft_version}"
group = project.maven_group

Expand All @@ -18,15 +15,6 @@ repositories {
url = uri('https://mschae23.de/git/api/packages/mschae23/maven')
}

maven {
name = 'GitHubPackages'
url = uri('https://maven.pkg.github.com/mschae23/codec-config-api')
credentials {
username = project.findProperty('gpr.user') ?: System.getenv('GITHUB_USERNAME')
password = project.findProperty('gpr.key') ?: System.getenv('GITHUB_TOKEN')
}
}

// Repositories for the dependencies of Tax Free Levels
maven { url "https://maven.shedaniel.me/" }
maven { url "https://maven.terraformersmc.com/releases" }
Expand Down Expand Up @@ -80,7 +68,6 @@ processResources {
}

tasks.withType(JavaCompile).configureEach {
// Minecraft 1.18 (1.18-pre2) upwards uses Java 17.
it.options.release = 21
}

Expand Down
6 changes: 3 additions & 3 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ org.gradle.jvmargs=-Xmx1G

# Fabric Properties
# check these on https://fabricmc.net/versions.html
minecraft_version=1.21.4-rc3
yarn_mappings=1.21.4-rc3+build.3
minecraft_version=1.21.1
yarn_mappings=1.21.1+build.3
loader_version=0.16.9

# Mod Properties
Expand All @@ -13,6 +13,6 @@ org.gradle.jvmargs=-Xmx1G
archives_base_name = grind-enchantments

# Dependencies
fabric_api_version=0.110.2+1.21.4
fabric_api_version=0.110.0+1.21.1
codec_config_api_version=3.0.0+1.21.3
tax_free_levels_version=1.4.1-fabric-1.21.1
39 changes: 39 additions & 0 deletions src/main/java/de/mschae23/grindenchantments/CodecUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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;

import java.util.List;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;

public final class CodecUtils {
private CodecUtils() {
}

public static <E> Codec<List<E>> listOrSingle(Codec<E> entryCodec) {
return listOrSingle(entryCodec, entryCodec.listOf());
}

public static <E> Codec<List<E>> listOrSingle(Codec<E> entryCodec, Codec<List<E>> listCodec) {
return Codec.either(listCodec, entryCodec)
.xmap(either -> either.map(list -> list, List::of),
list -> list.size() == 1 ? Either.right(list.getFirst()) : Either.left(list));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void onInitializeClient() {
ClientLifecycleEvents.CLIENT_STARTED.register(client -> {
CLIENT_CONFIG = GrindEnchantmentsMod.initializeClientConfig();
GrindEnchantmentsMod.LOCAL_SERVER_CONFIG = GrindEnchantmentsMod.initializeServerConfig(RegistryWrapper.WrapperLookup.of(
Stream.of(GrindEnchantmentsRegistries.COST_FUNCTION)));
Stream.of(GrindEnchantmentsRegistries.COST_FUNCTION.getReadOnlyWrapper())));

ClientConfigurationNetworking.registerGlobalReceiver(ServerConfigS2CPayload.ID, (payload, context) -> {
//noinspection resource
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,8 @@ private static void convertLegacyConfig() {
final Path newConfigDirPath = FabricLoader.getInstance().getConfigDir().resolve(MODID);

if (!Files.isDirectory(newConfigDirPath)) {
RegistryWrapper.WrapperLookup wrapperLookup = RegistryWrapper.WrapperLookup.of(Stream.of(GrindEnchantmentsRegistries.COST_FUNCTION));
RegistryWrapper.WrapperLookup wrapperLookup = RegistryWrapper.WrapperLookup.of(Stream.of(
GrindEnchantmentsRegistries.COST_FUNCTION.getReadOnlyWrapper()));
Optional<GrindEnchantmentsConfigV3> legacyConfigOpt = readLegacyConfig(wrapperLookup);

if (legacyConfigOpt.isPresent()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public record DedicatedServerConfig(boolean alternativeCostDisplay) {
Codec.BOOL.fieldOf("alternative_cost_display_enabled").forGetter(DedicatedServerConfig::alternativeCostDisplay)
).apply(instance, instance.stable(DedicatedServerConfig::new)));

public static final PacketCodec<PacketByteBuf, DedicatedServerConfig> PACKET_CODEC = PacketCodecs.BOOLEAN.xmap(
public static final PacketCodec<PacketByteBuf, DedicatedServerConfig> PACKET_CODEC = PacketCodecs.BOOL.xmap(
DedicatedServerConfig::new, DedicatedServerConfig::alternativeCostDisplay).cast();

public static final DedicatedServerConfig DEFAULT = new DedicatedServerConfig(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ public record DisenchantConfig(boolean enabled, boolean consumeItem, CostFunctio

public static PacketCodec<PacketByteBuf, DisenchantConfig> createPacketCodec(PacketCodec<PacketByteBuf, CostFunction> costFunctionCodec) {
return PacketCodec.tuple(
PacketCodecs.BOOLEAN, DisenchantConfig::enabled,
PacketCodecs.BOOLEAN, DisenchantConfig::consumeItem,
PacketCodecs.BOOL, DisenchantConfig::enabled,
PacketCodecs.BOOL, DisenchantConfig::consumeItem,
costFunctionCodec, DisenchantConfig::costFunction,
DisenchantConfig::new
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@
import net.minecraft.registry.entry.RegistryEntry;
import net.minecraft.registry.tag.EnchantmentTags;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.CodecUtils;
import de.mschae23.grindenchantments.GrindEnchantmentsMod;
import org.apache.logging.log4j.Level;

Expand All @@ -54,7 +54,7 @@ public record FilterConfig(boolean enabled, ItemConfig item, EnchantmentConfig e

public static PacketCodec<PacketByteBuf, FilterConfig> createPacketCodec() {
return PacketCodec.tuple(
PacketCodecs.BOOLEAN, FilterConfig::enabled,
PacketCodecs.BOOL, FilterConfig::enabled,
ItemConfig.createPacketCodec(), FilterConfig::item,
EnchantmentConfig.createPacketCodec(), FilterConfig::enchantment,
FilterAction.PACKET_CODEC, FilterConfig::curses,
Expand Down Expand Up @@ -137,7 +137,7 @@ public String toString() {

public record ItemConfig(List<Identifier> items, FilterAction action) {
public static final Codec<ItemConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codecs.listOrSingle(Identifier.CODEC).fieldOf("enchantments").forGetter(ItemConfig::items),
CodecUtils.listOrSingle(Identifier.CODEC).fieldOf("enchantments").forGetter(ItemConfig::items),
FilterAction.NON_IGNORE_CODEC.fieldOf("action").forGetter(ItemConfig::action)
).apply(instance, instance.stable(ItemConfig::new)));

Expand All @@ -152,7 +152,7 @@ public static PacketCodec<PacketByteBuf, ItemConfig> createPacketCodec() {
}

public void validateRegistryEntries(RegistryWrapper.WrapperLookup wrapperLookup) {
Optional<? extends RegistryWrapper.Impl<Item>> registryWrapperOpt = wrapperLookup.getOptional(RegistryKeys.ITEM);
Optional<? extends RegistryWrapper.Impl<Item>> registryWrapperOpt = wrapperLookup.getOptionalWrapper(RegistryKeys.ITEM);

if (registryWrapperOpt.isEmpty()) {
GrindEnchantmentsMod.log(Level.WARN, "Item registry is not present");
Expand All @@ -179,7 +179,7 @@ public String toString() {

public record EnchantmentConfig(List<Identifier> enchantments, FilterAction action) {
public static final Codec<EnchantmentConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codecs.listOrSingle(Identifier.CODEC).fieldOf("enchantments").forGetter(EnchantmentConfig::enchantments),
CodecUtils.listOrSingle(Identifier.CODEC).fieldOf("enchantments").forGetter(EnchantmentConfig::enchantments),
FilterAction.CODEC.fieldOf("action").forGetter(EnchantmentConfig::action)
).apply(instance, instance.stable(EnchantmentConfig::new)));

Expand All @@ -194,7 +194,7 @@ public static PacketCodec<PacketByteBuf, EnchantmentConfig> createPacketCodec()
}

public void validateRegistryEntries(RegistryWrapper.WrapperLookup wrapperLookup) {
Optional<? extends RegistryWrapper.Impl<Enchantment>> registryWrapperOpt = wrapperLookup.getOptional(RegistryKeys.ENCHANTMENT);
Optional<? extends RegistryWrapper.Impl<Enchantment>> registryWrapperOpt = wrapperLookup.getOptionalWrapper(RegistryKeys.ENCHANTMENT);

if (registryWrapperOpt.isEmpty()) {
GrindEnchantmentsMod.log(Level.WARN, "Enchantment registry is not present");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public record MoveConfig(boolean enabled, CostFunction costFunction) {

public static PacketCodec<PacketByteBuf, MoveConfig> createPacketCodec(PacketCodec<PacketByteBuf, CostFunction> costFunctionCodec) {
return PacketCodec.tuple(
PacketCodecs.BOOLEAN, MoveConfig::enabled,
PacketCodecs.BOOL, MoveConfig::enabled,
costFunctionCodec, MoveConfig::costFunction,
MoveConfig::new
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@
import net.minecraft.registry.RegistryKeys;
import net.minecraft.registry.RegistryWrapper;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.CodecUtils;
import de.mschae23.grindenchantments.GrindEnchantmentsMod;
import de.mschae23.grindenchantments.cost.AverageCountCostFunction;
import de.mschae23.grindenchantments.cost.CostFunction;
Expand All @@ -44,7 +44,7 @@
public record ResetRepairCostConfig(boolean enabled, List<Identifier> catalystItems, boolean requiresEnchantment, CostFunction costFunction) {
public static final Codec<ResetRepairCostConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("enabled").forGetter(ResetRepairCostConfig::enabled),
Codecs.listOrSingle(Identifier.CODEC).fieldOf("catalyst_items").forGetter(ResetRepairCostConfig::catalystItems),
CodecUtils.listOrSingle(Identifier.CODEC).fieldOf("catalyst_items").forGetter(ResetRepairCostConfig::catalystItems),
Codec.BOOL.fieldOf("requires_enchantment").forGetter(ResetRepairCostConfig::requiresEnchantment),
CostFunction.CODEC.fieldOf("cost_function").forGetter(ResetRepairCostConfig::costFunction)
).apply(instance, instance.stable(ResetRepairCostConfig::new)));
Expand All @@ -60,16 +60,16 @@ public record ResetRepairCostConfig(boolean enabled, List<Identifier> catalystIt

public static PacketCodec<PacketByteBuf, ResetRepairCostConfig> createPacketCodec(PacketCodec<PacketByteBuf, CostFunction> costFunctionCodec) {
return PacketCodec.tuple(
PacketCodecs.BOOLEAN, ResetRepairCostConfig::enabled,
PacketCodecs.BOOL, ResetRepairCostConfig::enabled,
Identifier.PACKET_CODEC.collect(PacketCodecs.toList()), ResetRepairCostConfig::catalystItems,
PacketCodecs.BOOLEAN, ResetRepairCostConfig::requiresEnchantment,
PacketCodecs.BOOL, ResetRepairCostConfig::requiresEnchantment,
costFunctionCodec, ResetRepairCostConfig::costFunction,
ResetRepairCostConfig::new
);
}

public void validateRegistryEntries(RegistryWrapper.WrapperLookup wrapperLookup) {
Optional<? extends RegistryWrapper.Impl<Item>> registryWrapperOpt = wrapperLookup.getOptional(RegistryKeys.ITEM);
Optional<? extends RegistryWrapper.Impl<Item>> registryWrapperOpt = wrapperLookup.getOptionalWrapper(RegistryKeys.ITEM);

if (registryWrapperOpt.isEmpty()) {
GrindEnchantmentsMod.log(Level.WARN, "Item registry is not present");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

import java.util.List;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.CodecUtils;
import de.mschae23.grindenchantments.config.FilterAction;
import de.mschae23.grindenchantments.config.FilterConfig;

Expand All @@ -45,7 +45,7 @@ public FilterConfig latest() {

public record ItemConfig(List<Identifier> items, FilterAction action) {
public static final Codec<ItemConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codecs.listOrSingle(Identifier.CODEC).fieldOf("enchantments").forGetter(ItemConfig::items),
CodecUtils.listOrSingle(Identifier.CODEC).fieldOf("enchantments").forGetter(ItemConfig::items),
FilterAction.NON_IGNORE_CODEC.fieldOf("action").forGetter(ItemConfig::action)
).apply(instance, instance.stable(ItemConfig::new)));

Expand All @@ -54,7 +54,7 @@ public record ItemConfig(List<Identifier> items, FilterAction action) {

public record EnchantmentConfig(List<Identifier> enchantments, FilterAction action) {
public static final Codec<EnchantmentConfig> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codecs.listOrSingle(Identifier.CODEC).fieldOf("enchantments").forGetter(EnchantmentConfig::enchantments),
CodecUtils.listOrSingle(Identifier.CODEC).fieldOf("enchantments").forGetter(EnchantmentConfig::enchantments),
FilterAction.CODEC.fieldOf("action").forGetter(EnchantmentConfig::action)
).apply(instance, instance.stable(EnchantmentConfig::new)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@

import java.util.List;
import net.minecraft.util.Identifier;
import net.minecraft.util.dynamic.Codecs;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import de.mschae23.grindenchantments.CodecUtils;
import de.mschae23.grindenchantments.config.ResetRepairCostConfig;
import de.mschae23.grindenchantments.cost.AverageCountCostFunction;
import de.mschae23.grindenchantments.cost.CostFunction;
Expand All @@ -34,7 +34,7 @@
public record ResetRepairCostConfigV3(boolean enabled, List<Identifier> catalystItems, boolean requiresEnchantment, CostFunction costFunction) {
public static final Codec<ResetRepairCostConfigV3> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Codec.BOOL.fieldOf("enabled").forGetter(ResetRepairCostConfigV3::enabled),
Codecs.listOrSingle(Identifier.CODEC).fieldOf("catalyst_items").forGetter(ResetRepairCostConfigV3::catalystItems),
CodecUtils.listOrSingle(Identifier.CODEC).fieldOf("catalyst_items").forGetter(ResetRepairCostConfigV3::catalystItems),
Codec.BOOL.fieldOf("requires_enchantment").forGetter(ResetRepairCostConfigV3::requiresEnchantment),
CostFunction.CODEC.fieldOf("cost_function").forGetter(ResetRepairCostConfigV3::costFunction)
).apply(instance, instance.stable(ResetRepairCostConfigV3::new)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ static <M extends CostFunction> CostFunctionType<M> register(String id, CostFunc

static PacketCodec<ByteBuf, CostFunctionType<?>> createPacketCodec() {
return RegistryKey.createPacketCodec(GrindEnchantmentsRegistries.COST_FUNCTION_KEY).xmap(
key -> GrindEnchantmentsRegistries.COST_FUNCTION.getOptionalValue(key).orElseThrow(
key -> GrindEnchantmentsRegistries.COST_FUNCTION.getOrEmpty(key).orElseThrow(
() -> new IllegalStateException("Can't decode '" + key.getValue() + "', unregistered value")),
type -> GrindEnchantmentsRegistries.COST_FUNCTION.getKey(type).orElseThrow(
() -> new IllegalStateException("Can't encode '" + type + "', unregistered value"))
Expand Down

0 comments on commit c937e03

Please sign in to comment.