custom ench desc: further flex
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
			
		||||
package dev.xkmc.l2core.base.entity;
 | 
			
		||||
 | 
			
		||||
import com.mojang.datafixers.util.Pair;
 | 
			
		||||
import com.mojang.serialization.Codec;
 | 
			
		||||
import net.minecraft.core.BlockPos;
 | 
			
		||||
import net.minecraft.core.RegistryAccess;
 | 
			
		||||
@@ -25,9 +26,9 @@ public class SyncedData {
 | 
			
		||||
	public static final Serializer<Optional<UUID>> UUID;
 | 
			
		||||
 | 
			
		||||
	static {
 | 
			
		||||
		INT = new Serializer<>(EntityDataSerializers.INT, Codec.INT);
 | 
			
		||||
		BLOCK_POS = new Serializer<>(EntityDataSerializers.BLOCK_POS, BlockPos.CODEC);
 | 
			
		||||
		UUID = new Serializer<>(EntityDataSerializers.OPTIONAL_UUID, UUIDUtil.CODEC.xmap(Optional::of, Optional::get));
 | 
			
		||||
		INT = new Simple<>(EntityDataSerializers.INT, Codec.INT);
 | 
			
		||||
		BLOCK_POS = new Simple<>(EntityDataSerializers.BLOCK_POS, BlockPos.CODEC);
 | 
			
		||||
		UUID = new Opt<>(EntityDataSerializers.OPTIONAL_UUID, UUIDUtil.CODEC);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private final Definer cls;
 | 
			
		||||
@@ -71,7 +72,7 @@ public class SyncedData {
 | 
			
		||||
 | 
			
		||||
		private Data(Serializer<T> ser, T init, @Nullable String name) {
 | 
			
		||||
			this.ser = ser;
 | 
			
		||||
			this.data = cls.define(ser.ser());
 | 
			
		||||
			this.data = cls.define(ser.data());
 | 
			
		||||
			this.init = init;
 | 
			
		||||
			this.name = name;
 | 
			
		||||
		}
 | 
			
		||||
@@ -93,7 +94,19 @@ public class SyncedData {
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public record Serializer<T>(EntityDataSerializer<T> ser, Codec<T> codec) {
 | 
			
		||||
	public interface Serializer<T> {
 | 
			
		||||
 | 
			
		||||
		EntityDataSerializer<T> data();
 | 
			
		||||
 | 
			
		||||
		@Nullable
 | 
			
		||||
		Tag write(RegistryAccess pvd, T t);
 | 
			
		||||
 | 
			
		||||
		@Nullable
 | 
			
		||||
		T read(RegistryAccess pvd, Tag tag);
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public record Simple<T>(EntityDataSerializer<T> data, Codec<T> codec) implements Serializer<T> {
 | 
			
		||||
 | 
			
		||||
		@Nullable
 | 
			
		||||
		public Tag write(RegistryAccess pvd, T t) {
 | 
			
		||||
@@ -107,6 +120,19 @@ public class SyncedData {
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public record Opt<T>(EntityDataSerializer<Optional<T>> data, Codec<T> codec) implements Serializer<Optional<T>> {
 | 
			
		||||
 | 
			
		||||
		@Nullable
 | 
			
		||||
		public Tag write(RegistryAccess pvd, Optional<T> val) {
 | 
			
		||||
			return val.map(e -> codec.encodeStart(pvd.createSerializationContext(NbtOps.INSTANCE), e).getOrThrow()).orElse(null);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		public Optional<T> read(RegistryAccess pvd, Tag tag) {
 | 
			
		||||
			return codec.decode(pvd.createSerializationContext(NbtOps.INSTANCE), tag).result().map(Pair::getFirst);
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public interface Definer {
 | 
			
		||||
 | 
			
		||||
		<T> EntityDataAccessor<T> define(EntityDataSerializer<T> ser);
 | 
			
		||||
 
 | 
			
		||||
@@ -83,8 +83,14 @@ public class ClientEventHandler {
 | 
			
		||||
				compound.set(i, Either.left(comp));
 | 
			
		||||
				flag = true;
 | 
			
		||||
			} else color = EnchColor.DEFAULT;
 | 
			
		||||
			if (skip) continue;
 | 
			
		||||
 | 
			
		||||
			var legacy = LegacyEnchantment.findFirst(ench.get(), CustomDescEnchantment.class);
 | 
			
		||||
			if (legacy.isPresent()){
 | 
			
		||||
				comp = legacy.get().title(stack, list.get(i), alt, book, color);
 | 
			
		||||
				compound.set(i, Either.left(comp));
 | 
			
		||||
				flag = true;
 | 
			
		||||
			}
 | 
			
		||||
			if (skip) continue;
 | 
			
		||||
			if (legacy.isEmpty()) {
 | 
			
		||||
				if (I18n.exists(tr.getKey() + ".desc")) {
 | 
			
		||||
					compound.set(i, Either.right(List.of(comp, Component.translatable(tr.getKey() + ".desc")
 | 
			
		||||
@@ -93,7 +99,7 @@ public class ClientEventHandler {
 | 
			
		||||
				}
 | 
			
		||||
			} else {
 | 
			
		||||
				int lv = map.getLevel(ench.get());
 | 
			
		||||
				var es = legacy.get().descFull(lv, tr.getKey() + ".desc", alt, book, color);
 | 
			
		||||
				var es = legacy.get().descFull(stack, lv, tr.getKey() + ".desc", alt, book, color);
 | 
			
		||||
				compound.set(i, Either.right(Stream.concat(Stream.of(comp), es.stream().map(e -> (Component) lit.copy().append(e))).toList()));
 | 
			
		||||
				flag = true;
 | 
			
		||||
			}
 | 
			
		||||
 
 | 
			
		||||
@@ -6,15 +6,26 @@ import net.minecraft.network.chat.MutableComponent;
 | 
			
		||||
import net.minecraft.world.effect.MobEffect;
 | 
			
		||||
import net.minecraft.world.effect.MobEffectInstance;
 | 
			
		||||
import net.minecraft.world.effect.MobEffectUtil;
 | 
			
		||||
import net.minecraft.world.item.ItemStack;
 | 
			
		||||
import org.jetbrains.annotations.ApiStatus;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
public interface CustomDescEnchantment {
 | 
			
		||||
 | 
			
		||||
	default List<Component> descFull(ItemStack stack, int lv, String key, boolean alt, boolean book, EnchColor color) {
 | 
			
		||||
		return descFull(lv, key, alt, book, color);
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	@ApiStatus.OverrideOnly
 | 
			
		||||
	default List<Component> descFull(int lv, String key, boolean alt, boolean book, EnchColor color) {
 | 
			
		||||
		return List.of(Component.translatable(key).withStyle(color.desc()));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	default Component title(ItemStack stack, Component comp, boolean alt, boolean book, EnchColor color) {
 | 
			
		||||
		return comp.copy().withStyle(color.base());
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	static MutableComponent perc(double val) {
 | 
			
		||||
		return Component.literal(Math.round(val * 100) + "%").withStyle(ChatFormatting.DARK_AQUA);
 | 
			
		||||
	}
 | 
			
		||||
 
 | 
			
		||||
@@ -95,11 +95,12 @@ public class EnchReg {
 | 
			
		||||
						e.withEffect(unit.get()))))));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public EnchVal enchLegacy(String id, String name, String desc, UnaryOperator<EnchVal.Builder> cons, Supplier<LegacyEnchantment> factory) {
 | 
			
		||||
	public <T extends LegacyEnchantment> EnchVal.Legacy<T> enchLegacy(String id, String name, String desc, UnaryOperator<EnchVal.Builder> cons, Supplier<T> factory) {
 | 
			
		||||
		var unit = legacy.register(id, factory);
 | 
			
		||||
		return enchBase(id, name, desc, key -> new EnchVal.Simple(key,
 | 
			
		||||
		return enchBase(id, name, desc, key -> new EnchVal.LegacyImpl<>(key, unit,
 | 
			
		||||
				Lazy.of(() -> cons.apply(new EnchVal.Builder(key.location()).effect(e ->
 | 
			
		||||
						e.withSpecialEffect(L2LibReg.LEGACY.get(), List.of(unit.get())))))));
 | 
			
		||||
						e.withSpecialEffect(L2LibReg.LEGACY.get(), List.of(unit.get())))))
 | 
			
		||||
		));
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	public void build(BootstrapContext<Enchantment> ctx) {
 | 
			
		||||
 
 | 
			
		||||
@@ -18,6 +18,7 @@ import net.minecraft.world.item.enchantment.ItemEnchantments;
 | 
			
		||||
import net.neoforged.neoforge.common.CommonHooks;
 | 
			
		||||
import net.neoforged.neoforge.common.Tags;
 | 
			
		||||
import net.neoforged.neoforge.common.util.Lazy;
 | 
			
		||||
import net.neoforged.neoforge.registries.DeferredHolder;
 | 
			
		||||
import org.jetbrains.annotations.Nullable;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
@@ -62,6 +63,12 @@ public interface EnchVal {
 | 
			
		||||
		EECVal.Flag unit();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	interface Legacy<T extends LegacyEnchantment> extends EnchVal {
 | 
			
		||||
 | 
			
		||||
		DeferredHolder<LegacyEnchantment, T> legacy();
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	record Simple(ResourceKey<Enchantment> id, Lazy<Builder> builder) implements Impl {
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
@@ -70,6 +77,12 @@ public interface EnchVal {
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	record LegacyImpl<T extends LegacyEnchantment>(
 | 
			
		||||
			ResourceKey<Enchantment> id, DeferredHolder<LegacyEnchantment, T> legacy, Lazy<Builder> builder
 | 
			
		||||
	) implements Legacy<T>, Impl {
 | 
			
		||||
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	class Builder {
 | 
			
		||||
 | 
			
		||||
		private HolderSetBuilder<Item> supported, primary;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user