improve token
This commit is contained in:
@@ -18,7 +18,7 @@ loader_version_range=[2,)
|
||||
mod_id=l2core
|
||||
mod_name=L2Core
|
||||
mod_license=LGPL-2.1
|
||||
mod_version=3.0.3-pre8
|
||||
mod_version=3.0.5-pre1
|
||||
mod_group_id=dev.xkmc
|
||||
mod_authors=lcy0x1
|
||||
mod_description=Core Library mod for all L2 mods
|
||||
|
||||
@@ -0,0 +1,41 @@
|
||||
package dev.xkmc.l2core.capability.conditionals;
|
||||
|
||||
import dev.xkmc.l2serial.serialization.marker.SerialClass;
|
||||
import dev.xkmc.l2serial.serialization.marker.SerialField;
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
|
||||
@SerialClass
|
||||
public class BaseTickingToken extends ConditionalToken {
|
||||
|
||||
@SerialField
|
||||
public int tick = 0;
|
||||
|
||||
public BaseTickingToken() {
|
||||
|
||||
}
|
||||
|
||||
public BaseTickingToken(int time) {
|
||||
this.tick = time;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean tick(Player player) {
|
||||
if (tick > 0) {
|
||||
tick--;
|
||||
onTick(player);
|
||||
return false;
|
||||
} else {
|
||||
onRemove(player);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public void onTick(Player player) {
|
||||
|
||||
}
|
||||
|
||||
public void onRemove(Player player) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -10,6 +10,7 @@ import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
@SerialClass
|
||||
public class ConditionalData extends PlayerCapabilityTemplate<ConditionalData> {
|
||||
@@ -30,8 +31,8 @@ public class ConditionalData extends PlayerCapabilityTemplate<ConditionalData> {
|
||||
data.remove(e);
|
||||
}
|
||||
|
||||
public <T extends ConditionalToken, C extends Context> T getOrCreateData(TokenProvider<T, C> setEffect, C ent) {
|
||||
return Wrappers.cast(data.computeIfAbsent(setEffect.getKey(), e -> setEffect.getData(ent)));
|
||||
public <T extends ConditionalToken> T getOrCreateData(TokenKey<T> setEffect, Supplier<T> fallback) {
|
||||
return Wrappers.cast(data.computeIfAbsent(setEffect, k -> fallback.get()));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
package dev.xkmc.l2core.capability.conditionals;
|
||||
|
||||
public interface Context {
|
||||
}
|
||||
@@ -1,8 +0,0 @@
|
||||
package dev.xkmc.l2core.capability.conditionals;
|
||||
|
||||
public interface TokenProvider<T extends ConditionalToken, C extends Context> {
|
||||
|
||||
T getData(C ent);
|
||||
|
||||
TokenKey<T> getKey();
|
||||
}
|
||||
@@ -7,11 +7,17 @@ import net.minecraft.nbt.CompoundTag;
|
||||
import net.minecraft.world.level.saveddata.SavedData;
|
||||
|
||||
@SerialClass
|
||||
public class BaseSavedData extends SavedData {
|
||||
public class BaseSavedData<T> extends SavedData {
|
||||
|
||||
private final Class<T> cls;
|
||||
|
||||
public BaseSavedData(Class<T> cls) {
|
||||
this.cls = cls;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CompoundTag save(CompoundTag tag, HolderLookup.Provider provider) {
|
||||
new TagCodec(provider).toTag(tag, this);
|
||||
new TagCodec(provider).toTag(tag, cls, this);
|
||||
return tag;
|
||||
}
|
||||
|
||||
|
||||
@@ -2,22 +2,31 @@ package dev.xkmc.l2core.init;
|
||||
|
||||
import com.tterrag.registrate.providers.ProviderType;
|
||||
import com.tterrag.registrate.providers.RegistrateTagsProvider;
|
||||
import net.minecraft.core.Registry;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
import net.minecraft.core.registries.Registries;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
import net.minecraft.resources.ResourceLocation;
|
||||
import net.minecraft.tags.TagKey;
|
||||
import net.minecraft.world.effect.MobEffect;
|
||||
import net.minecraft.world.entity.ai.attributes.Attribute;
|
||||
|
||||
public class L2TagGen {
|
||||
|
||||
public static final ProviderType<RegistrateTagsProvider.IntrinsicImpl<MobEffect>> EFF_TAGS =
|
||||
ProviderType.register("tags/mob_effect",
|
||||
type -> (p, e) -> new RegistrateTagsProvider.IntrinsicImpl<>(p, type, "mob_effects",
|
||||
e.getGenerator().getPackOutput(), Registries.MOB_EFFECT, e.getLookupProvider(),
|
||||
ench -> ResourceKey.create(Registries.MOB_EFFECT, BuiltInRegistries.MOB_EFFECT.getKey(ench)),
|
||||
e.getExistingFileHelper()));
|
||||
public static <T> ProviderType<RegistrateTagsProvider.IntrinsicImpl<T>> getProvider(ResourceKey<Registry<T>> id, Registry<T> reg) {
|
||||
String name = id.location().getPath();
|
||||
return ProviderType.register("tags/" + name,
|
||||
type -> (p, e) -> new RegistrateTagsProvider.IntrinsicImpl<>(p, type, name,
|
||||
e.getGenerator().getPackOutput(), id, e.getLookupProvider(),
|
||||
ench -> reg.getResourceKey(ench).get(),
|
||||
e.getExistingFileHelper()));
|
||||
}
|
||||
|
||||
public static final ProviderType<RegistrateTagsProvider.IntrinsicImpl<MobEffect>> EFF_TAGS =
|
||||
getProvider(Registries.MOB_EFFECT, BuiltInRegistries.MOB_EFFECT);
|
||||
|
||||
public static final ProviderType<RegistrateTagsProvider.IntrinsicImpl<Attribute>> ATTR_TAGS =
|
||||
getProvider(Registries.ATTRIBUTE, BuiltInRegistries.ATTRIBUTE);
|
||||
|
||||
public static final TagKey<MobEffect> TRACKED_EFFECTS = effectTag(ResourceLocation.fromNamespaceAndPath(L2Core.MODID, "tracked_effects"));
|
||||
|
||||
|
||||
@@ -1,16 +1,23 @@
|
||||
package dev.xkmc.l2core.init.reg.registrate;
|
||||
|
||||
import com.tterrag.registrate.util.entry.RegistryEntry;
|
||||
import net.minecraft.core.Holder;
|
||||
import net.minecraft.resources.ResourceKey;
|
||||
|
||||
public record SimpleEntry<T>(RegistryEntry<T, ? extends T> val) {
|
||||
import java.util.function.Supplier;
|
||||
|
||||
ResourceKey<T> key() {
|
||||
public record SimpleEntry<T>(RegistryEntry<T, ? extends T> val) implements Supplier<T> {
|
||||
|
||||
public ResourceKey<T> key() {
|
||||
return val.getKey();
|
||||
}
|
||||
|
||||
T get() {
|
||||
public T get() {
|
||||
return val.get();
|
||||
}
|
||||
|
||||
public Holder<T> holder() {
|
||||
return val.getDelegate();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package dev.xkmc.l2core.init.reg.simple;
|
||||
|
||||
import com.mojang.serialization.Codec;
|
||||
import dev.xkmc.l2core.util.DCStack;
|
||||
import dev.xkmc.l2serial.serialization.codec.CodecAdaptor;
|
||||
import net.minecraft.core.component.DataComponentType;
|
||||
import net.minecraft.core.registries.BuiltInRegistries;
|
||||
@@ -10,6 +11,7 @@ import net.minecraft.network.chat.Component;
|
||||
import net.minecraft.network.chat.ComponentSerialization;
|
||||
import net.minecraft.network.codec.ByteBufCodecs;
|
||||
import net.minecraft.network.codec.StreamCodec;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.neoforged.neoforge.registries.DeferredHolder;
|
||||
import net.neoforged.neoforge.registries.DeferredRegister;
|
||||
|
||||
@@ -53,6 +55,11 @@ public record DCReg(DeferredRegister<DataComponentType<?>> reg) {
|
||||
StreamCodec.of((b, e) -> FriendlyByteBuf.writeUUID(b, e), b -> FriendlyByteBuf.readUUID(b)), true);
|
||||
}
|
||||
|
||||
public DCVal<DCStack> stack(String id) {
|
||||
return reg(id, ItemStack.CODEC.xmap(DCStack::new, DCStack::stack),
|
||||
ItemStack.STREAM_CODEC.map(DCStack::new, DCStack::stack), true);
|
||||
}
|
||||
|
||||
public DCVal<Component> component(String id) {
|
||||
return reg(id, ComponentSerialization.CODEC, ComponentSerialization.STREAM_CODEC, true);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
package dev.xkmc.l2core.util;
|
||||
package dev.xkmc.l2core.serial.loot;
|
||||
|
||||
import net.minecraft.advancements.critereon.EnchantmentPredicate;
|
||||
import net.minecraft.advancements.critereon.MinMaxBounds;
|
||||
29
src/main/java/dev/xkmc/l2core/util/DCStack.java
Normal file
29
src/main/java/dev/xkmc/l2core/util/DCStack.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package dev.xkmc.l2core.util;
|
||||
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
|
||||
public final class DCStack {
|
||||
|
||||
private final ItemStack stack;
|
||||
private final int hashCode;
|
||||
|
||||
public DCStack(ItemStack stack) {
|
||||
this.stack = stack;
|
||||
hashCode = stack.hashCode();
|
||||
}
|
||||
|
||||
public ItemStack stack(){
|
||||
return stack;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return hashCode;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
return obj instanceof DCStack s && hashCode == s.hashCode && stack.equals(s.stack);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user