Fix initialization order

This commit is contained in:
Sollace 2024-10-03 20:45:00 +01:00
parent 7de1675a82
commit 236cc9a921
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
10 changed files with 43 additions and 45 deletions

View file

@ -27,6 +27,7 @@ import com.minelittlepony.unicopia.entity.effect.SeaponyGraceStatusEffect;
import com.minelittlepony.unicopia.entity.effect.UPotions;
import com.minelittlepony.unicopia.entity.mob.UEntities;
import com.minelittlepony.unicopia.item.UItems;
import com.minelittlepony.unicopia.item.component.UDataComponentTypes;
import com.minelittlepony.unicopia.network.Channel;
import com.minelittlepony.unicopia.particle.UParticles;
import com.minelittlepony.unicopia.server.world.BlockDestructionManager;
@ -63,6 +64,7 @@ public class Unicopia implements ModInitializer {
public void onInitialize() {
Channel.bootstrap();
UCriteria.bootstrap();
UDataComponentTypes.bootstrap();
UEntities.bootstrap();
Commands.bootstrap();
TrinketsDelegate.getInstance(null).bootstrap();

View file

@ -19,7 +19,7 @@ public record RacePredicate(Optional<Set<Race>> include, Optional<Set<Race>> exc
RACE_SET_CODEC.optionalFieldOf("include").forGetter(RacePredicate::include),
RACE_SET_CODEC.optionalFieldOf("exclude").forGetter(RacePredicate::exclude)
).apply(instance, RacePredicate::of));
public static final Codec<RacePredicate> CODEC = CodecUtils.xor(BASE_CODEC, RACE_SET_CODEC.xmap(include -> of(Optional.of(include), Optional.empty()), a -> a.include().orElse(Set.of())));
public static final Codec<RacePredicate> CODEC = Codec.withAlternative(BASE_CODEC, RACE_SET_CODEC, a -> of(a, Set.of()));
public static RacePredicate of(Set<Race> include, Set<Race> exclude) {
return of(Optional.of(include).filter(s -> !s.isEmpty()), Optional.of(exclude).filter(s -> !s.isEmpty()));

View file

@ -1,6 +1,7 @@
package com.minelittlepony.unicopia.diet;
import com.minelittlepony.unicopia.diet.affliction.Affliction;
import com.minelittlepony.unicopia.diet.affliction.AfflictionType;
import com.minelittlepony.unicopia.diet.affliction.EmptyAffliction;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
@ -11,7 +12,7 @@ import net.minecraft.network.codec.PacketCodec;
public record Ailment(Affliction effects) {
public static final Ailment EMPTY = new Ailment(EmptyAffliction.INSTANCE);
public static final Codec<Ailment> CODEC = RecordCodecBuilder.create(instance -> instance.group(
Affliction.CODEC.fieldOf("effects").forGetter(Ailment::effects)
AfflictionType.CODEC.fieldOf("effects").forGetter(Ailment::effects)
).apply(instance, Ailment::new));
public static final PacketCodec<RegistryByteBuf, Ailment> PACKET_CODEC = Affliction.PACKET_CODEC.xmap(Ailment::new, Ailment::effects);
public static final PacketCodec<RegistryByteBuf, Ailment> PACKET_CODEC = AfflictionType.PACKET_CODEC.xmap(Ailment::new, Ailment::effects);
}

View file

@ -2,34 +2,12 @@ package com.minelittlepony.unicopia.diet.affliction;
import java.util.List;
import com.mojang.datafixers.util.Either;
import com.mojang.serialization.Codec;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;
public interface Affliction {
Codec<Affliction> SINGLE_CODEC = AfflictionType.CODEC.dispatch("type", affliction -> affliction.getType(), type -> type.codec());
Codec<Affliction> CODEC = Codec.xor(SINGLE_CODEC, Codec.list(SINGLE_CODEC).xmap(
afflictions -> {
afflictions = afflictions.stream().filter(f -> !f.isEmpty()).toList();
return switch (afflictions.size()) {
case 0 -> EmptyAffliction.INSTANCE;
case 1 -> afflictions.get(0);
default -> new CompoundAffliction(afflictions);
};
},
affliction -> ((CompoundAffliction)affliction).afflictions()
)).xmap(
either -> either.left().or(either::right).get(),
affliction -> affliction instanceof CompoundAffliction ? Either.right(affliction) : Either.left(affliction)
);
PacketCodec<RegistryByteBuf, Affliction> PACKET_CODEC = AfflictionType.PACKET_CODEC.dispatch(Affliction::getType, AfflictionType::packetCodec);
void afflict(PlayerEntity player, ItemStack stack);
default boolean isEmpty() {

View file

@ -2,6 +2,7 @@ package com.minelittlepony.unicopia.diet.affliction;
import com.minelittlepony.unicopia.Unicopia;
import com.minelittlepony.unicopia.util.RegistryUtils;
import com.minelittlepony.unicopia.util.serialization.CodecUtils;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
@ -15,8 +16,18 @@ import net.minecraft.util.Util;
public record AfflictionType<T extends Affliction>(Identifier id, MapCodec<T> codec, PacketCodec<? super RegistryByteBuf, T> packetCodec) {
public static final String DEFAULT_ID = "unicopia:apply_status_effect";
public static final Registry<AfflictionType<?>> REGISTRY = RegistryUtils.createDefaulted(Unicopia.id("affliction_type"), DEFAULT_ID);
public static final Codec<AfflictionType<?>> CODEC = REGISTRY.getCodec();
public static final PacketCodec<RegistryByteBuf, AfflictionType<?>> PACKET_CODEC = PacketCodecs.registryValue(REGISTRY.getKey());
public static final Codec<Affliction> CODEC = CodecUtils.apply(REGISTRY.getCodec()
.dispatch("type", Affliction::getType, AfflictionType::codec), elementCodec -> Codec
.withAlternative(elementCodec, Codec.list(elementCodec), afflictions -> {
afflictions = afflictions.stream().filter(f -> !f.isEmpty()).toList();
return switch (afflictions.size()) {
case 0 -> EmptyAffliction.INSTANCE;
case 1 -> afflictions.get(0);
default -> new CompoundAffliction(afflictions);
};
}));
public static final PacketCodec<RegistryByteBuf, Affliction> PACKET_CODEC = PacketCodecs.registryValue(REGISTRY.getKey())
.dispatch(Affliction::getType, AfflictionType::packetCodec);
public static final AfflictionType<EmptyAffliction> EMPTY = register("empty", EmptyAffliction.CODEC, EmptyAffliction.PACKET_CODEC);
public static final AfflictionType<CompoundAffliction> MANY = register("many", CompoundAffliction.CODEC, CompoundAffliction.PACKET_CODEC);

View file

@ -9,13 +9,17 @@ import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.network.RegistryByteBuf;
import net.minecraft.network.codec.PacketCodec;
import net.minecraft.network.codec.PacketCodecs;
import net.minecraft.text.Text;
public record CompoundAffliction (List<Affliction> afflictions) implements Affliction {
public static final MapCodec<CompoundAffliction> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Affliction.CODEC.listOf().fieldOf("afflictions").forGetter(CompoundAffliction::afflictions)
AfflictionType.CODEC.listOf().fieldOf("afflictions").forGetter(CompoundAffliction::afflictions)
).apply(instance, CompoundAffliction::new));
public static final PacketCodec<RegistryByteBuf, CompoundAffliction> PACKET_CODEC = null;
public static final PacketCodec<RegistryByteBuf, CompoundAffliction> PACKET_CODEC = PacketCodec.tuple(
AfflictionType.PACKET_CODEC.collect(PacketCodecs.toList()), CompoundAffliction::afflictions,
CompoundAffliction::new
);
public static CompoundAffliction of(Affliction...afflictions) {
return new CompoundAffliction(List.of(afflictions));

View file

@ -329,7 +329,6 @@ public interface UItems {
CompostingChanceRegistry.INSTANCE.add(APPLE_PIE_SLICE, 0.1F);
CompostingChanceRegistry.INSTANCE.add(BUTTERFLY, 0.1F);
UDataComponentTypes.bootstrap();
UEnchantments.bootstrap();
URecipes.bootstrap();
UItemGroups.bootstrap();

View file

@ -1,6 +1,5 @@
package com.minelittlepony.unicopia.item.component;
import com.minelittlepony.unicopia.util.serialization.CodecUtils;
import com.mojang.serialization.Codec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
@ -15,10 +14,7 @@ public record Appearance(ItemStack item, boolean replaceFully) {
public static final Appearance DEFAULT = new Appearance(ItemStack.EMPTY, false);
public static final Appearance DEFAULT_FULLY_DISGUISED = new Appearance(ItemStack.EMPTY, true);
public static final Codec<Appearance> CODEC = RecordCodecBuilder.create(instance -> instance.group(
CodecUtils.xor(
ItemStack.CODEC,
Registries.ITEM.getCodec().xmap(Item::getDefaultStack, ItemStack::getItem)
).fieldOf("item").forGetter(Appearance::item),
Codec.withAlternative(ItemStack.CODEC, Registries.ITEM.getCodec(), Item::getDefaultStack).fieldOf("item").forGetter(Appearance::item),
Codec.BOOL.fieldOf("replace_fully").forGetter(Appearance::replaceFully)
).apply(instance, Appearance::of));
public static final PacketCodec<RegistryByteBuf, Appearance> PACKET_CODEC = PacketCodec.tuple(

View file

@ -15,12 +15,13 @@ import net.minecraft.util.math.Vec3d;
import net.minecraft.world.World;
public class WeakTarget {
public static final Codec<WeakTarget> CODEC = CodecUtils.xor(
public static final Codec<WeakTarget> CODEC = Codec.withAlternative(
RecordCodecBuilder.create(instance -> instance.group(
CodecUtils.VECTOR.fieldOf("position").forGetter(i -> i.fixedPosition),
Codec.INT.fieldOf("targetId").forGetter(i -> i.targetId)
).apply(instance, WeakTarget::new)),
CodecUtils.VECTOR.xmap(pos -> new WeakTarget(pos, null), target -> target.fixedPosition)
CodecUtils.VECTOR,
pos -> new WeakTarget(pos, null)
);
public static final PacketCodec<PacketByteBuf, WeakTarget> PACKET_CODEC = PacketCodec.tuple(
PacketCodecUtils.VECTOR, i -> i.fixedPosition,

View file

@ -3,9 +3,10 @@ package com.minelittlepony.unicopia.util.serialization;
import java.util.ArrayList;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Collectors;
import com.mojang.datafixers.util.Either;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.DataResult;
@ -26,11 +27,11 @@ import net.minecraft.util.math.Vec3d;
public interface CodecUtils {
Codec<ItemConvertible> ITEM = Registries.ITEM.getCodec().xmap(i -> () -> i, ItemConvertible::asItem);
Codec<Optional<BlockPos>> OPTIONAL_POS = Codecs.optional(BlockPos.CODEC);
Codec<Vec3d> VECTOR = RecordCodecBuilder.create(instance -> instance.group(
Codec<Vec3d> VECTOR = Codec.withAlternative(RecordCodecBuilder.create(instance -> instance.group(
Codec.DOUBLE.fieldOf("x").forGetter(Vec3d::getX),
Codec.DOUBLE.fieldOf("y").forGetter(Vec3d::getY),
Codec.DOUBLE.fieldOf("z").forGetter(Vec3d::getZ)
).apply(instance, Vec3d::new));
).apply(instance, Vec3d::new)), Codec.DOUBLE.listOf(3, 3), list -> new Vec3d(list.get(0), list.get(1), list.get(2)));
/**
* Combines the result of two unrelated codecs into a single object.
* <p>
@ -79,8 +80,8 @@ public interface CodecUtils {
});
}
static <K> Codec<K> xor(Codec<K> left, Codec<K> right) {
return Codec.xor(left, right).xmap(either -> either.left().or(either::right).get(), Either::left);
static <K> Codec<K> apply(Codec<K> codec, UnaryOperator<Codec<K>> func) {
return func.apply(codec);
}
static <K> Codec<Set<K>> setOf(Codec<K> codec) {
@ -95,11 +96,16 @@ public interface CodecUtils {
}
static MapCodec<TriState> tristateOf(String fieldName) {
return Codec.BOOL.optionalFieldOf(fieldName)
.<TriState>xmap(b -> b.map(TriState::of).orElse(TriState.DEFAULT), t -> Optional.ofNullable(t.get()));
return Codec.BOOL.optionalFieldOf(fieldName).xmap(
b -> b.map(TriState::of).orElse(TriState.DEFAULT),
t -> Optional.ofNullable(t.get())
);
}
static <T> Codec<DefaultedList<T>> defaultedList(Codec<T> elementCodec, T empty) {
return elementCodec.listOf().flatXmap(elements -> DataResult.success(new DefaultedList<>(elements, empty) {}), DataResult::success);
return elementCodec.listOf().xmap(
elements -> new DefaultedList<>(elements, empty) {},
Function.identity()
);
}
}