This commit is contained in:
lcy0x1
2024-08-06 13:24:35 +08:00
parent 483d639bba
commit b50d041151
23 changed files with 247 additions and 24 deletions

View File

@@ -4,8 +4,10 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.entity.LivingEntity;
public record DelayedEntityRender(LivingEntity entity, IconRenderRegion region, ResourceLocation rl,
double xo, double yo, double zo,
float tx, float ty, float tw, float th) {
public static DelayedEntityRender icon(LivingEntity entity, ResourceLocation rl) {
return icon(entity, IconRenderRegion.identity(), rl);
}
@@ -14,7 +16,13 @@ public record DelayedEntityRender(LivingEntity entity, IconRenderRegion region,
return new DelayedEntityRender(entity, r, rl, 0, 0, 1, 1);
}
public DelayedEntityRender resize(IconRenderRegion r) {
return new DelayedEntityRender(entity, r.resize(region), rl, tx, ty, tw, th);
public DelayedEntityRender(LivingEntity entity, IconRenderRegion region, ResourceLocation rl,
float tx, float ty, float tw, float th) {
this(entity, region, rl, entity.xOld, entity.yOld, entity.zOld, tx, ty, tw, th);
}
public DelayedEntityRender resize(IconRenderRegion r) {
return new DelayedEntityRender(entity, r.resize(region), rl, xo, yo, zo, tx, ty, tw, th);
}
}

View File

@@ -47,7 +47,7 @@ public class ClientEffectRenderEvents {
AbstractClientPlayer player = Proxy.getClientPlayer();
if (player != null) {
for (var entry : player.getActiveEffectsMap().entrySet()) {
if (entry.getKey() instanceof FirstPlayerRenderEffect effect) {
if (entry.getValue().getEffect().value() instanceof FirstPlayerRenderEffect effect) {
effect.onClientLevelRender(player, entry.getValue());
}
}
@@ -64,7 +64,9 @@ public class ClientEffectRenderEvents {
// cache the previous handler
for (DelayedEntityRender icon : ICONS) {
renderIcon(stack, buffers, icon, event.getPartialTick().getGameTimeDeltaTicks(), camera, renderer.entityRenderDispatcher);
renderIcon(stack, buffers, icon,
event.getPartialTick().getGameTimeDeltaPartialTick(true),
camera, renderer.entityRenderDispatcher);
}
buffers.endBatch();
@@ -115,10 +117,9 @@ public class ClientEffectRenderEvents {
float partial, Camera camera, EntityRenderDispatcher dispatcher) {
LivingEntity entity = icon.entity();
float f = entity.getBbHeight() / 2;
double x0 = Mth.lerp(partial, entity.xOld, entity.getX());
double y0 = Mth.lerp(partial, entity.yOld, entity.getY());
double z0 = Mth.lerp(partial, entity.zOld, entity.getZ());
double x0 = Mth.lerp(partial, icon.xo(), entity.getX());
double y0 = Mth.lerp(partial, icon.yo(), entity.getY());
double z0 = Mth.lerp(partial, icon.zo(), entity.getZ());
Vec3 offset = dispatcher.getRenderer(entity).getRenderOffset(entity, partial);
Vec3 cam_pos = camera.getPosition();
double d2 = x0 - cam_pos.x + offset.x();
@@ -140,17 +141,15 @@ public class ClientEffectRenderEvents {
float u1 = icon.tx() + icon.tw();
float v1 = icon.ty() + icon.th();
iconVertex(entry, ivertexbuilder, ix1, iy0, u0, v1);
iconVertex(entry, ivertexbuilder, ix0, iy0, u1, v1);
iconVertex(entry, ivertexbuilder, ix0, iy1, u1, v0);
iconVertex(entry, ivertexbuilder, ix1, iy1, u0, v0);
iconVertex(entry, ivertexbuilder, ix0, iy0, u0, v1);
iconVertex(entry, ivertexbuilder, ix1, iy0, u1, v1);
iconVertex(entry, ivertexbuilder, ix1, iy1, u1, v0);
iconVertex(entry, ivertexbuilder, ix0, iy1, u0, v0);
pose.popPose();
}
private static void iconVertex(PoseStack.Pose entry, VertexConsumer builder, float x, float y, float u, float v) {
builder.addVertex(entry.pose(), x, y, 0)
.setUv(u, v)
.setNormal(entry, 0.0F, 1.0F, 0.0F);
builder.addVertex(entry.pose(), x, y, 0).setUv(u, v);
}
public static RenderType get2DIcon(ResourceLocation rl) {

View File

@@ -0,0 +1,82 @@
package dev.xkmc.l2core.init.reg.registrate;
import com.mojang.datafixers.util.Either;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderOwner;
import net.minecraft.resources.ResourceKey;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.stream.Stream;
public interface LegacyHolder<T> extends Holder<T>, Supplier<T> {
ResourceKey<T> key();
Holder<T> holder();
@Override
default T value() {
return get();
}
@Override
default boolean isBound() {
return holder().isBound();
}
@Override
default boolean is(ResourceLocation id) {
return key().location().equals(id);
}
@Override
default boolean is(ResourceKey<T> id) {
return key().equals(id);
}
@Override
default boolean is(Predicate<ResourceKey<T>> pred) {
return pred.test(key());
}
@Override
default boolean is(TagKey<T> tag) {
return holder().is(tag);
}
@Deprecated
@Override
default boolean is(Holder<T> holder) {
return holder().is(holder);
}
@Override
default Stream<TagKey<T>> tags() {
return holder().tags();
}
@Override
default Either<ResourceKey<T>, T> unwrap() {
return Either.left(key());
}
@Override
default Optional<ResourceKey<T>> unwrapKey() {
return Optional.of(key());
}
@Override
default Kind kind() {
return Kind.REFERENCE;
}
@Override
default boolean canSerializeIn(HolderOwner<T> pOwner) {
return holder().canSerializeIn(pOwner);
}
}

View File

@@ -4,9 +4,7 @@ import com.tterrag.registrate.util.entry.RegistryEntry;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceKey;
import java.util.function.Supplier;
public record SimpleEntry<T>(RegistryEntry<T, ? extends T> val) implements Supplier<T> {
public record SimpleEntry<T>(RegistryEntry<T, ? extends T> val) implements LegacyHolder<T> {
public ResourceKey<T> key() {
return val.getKey();
@@ -16,8 +14,22 @@ public record SimpleEntry<T>(RegistryEntry<T, ? extends T> val) implements Suppl
return val.get();
}
@Override
public Holder<T> holder() {
return val.getDelegate();
return val;
}
@Override
public int hashCode() {
return key().hashCode();
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
return obj instanceof Holder<?> h &&
h.kind() == Kind.REFERENCE &&
key().equals(h.getKey());
}
}

View File

@@ -77,6 +77,10 @@ public record DCReg(DeferredRegister<DataComponentType<?>> reg) {
return reg(id, Codec.STRING, ByteBufCodecs.STRING_UTF8, false);
}
public <T> DCVal<T> enumVal(String id, EnumCodec<T> codec) {
return reg(id, codec.codec(), codec.stream(), true);
}
public DCVal<ResourceLocation> loc(String id) {
return reg(id, ResourceLocation.CODEC, ResourceLocation.STREAM_CODEC, false);
}
@@ -91,8 +95,8 @@ public record DCReg(DeferredRegister<DataComponentType<?>> reg) {
}
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);
return reg(id, ItemStack.OPTIONAL_CODEC.xmap(DCStack::new, DCStack::stack),
ItemStack.OPTIONAL_STREAM_CODEC.map(DCStack::new, DCStack::stack), true);
}
public DCVal<Component> component(String id) {

View File

@@ -0,0 +1,29 @@
package dev.xkmc.l2core.init.reg.simple;
import com.mojang.serialization.Codec;
import dev.xkmc.l2serial.util.Wrappers;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import java.util.List;
public record EnumCodec<T>(Codec<T> codec, StreamCodec<? super RegistryFriendlyByteBuf, T> stream) {
public static <T extends Enum<T>> EnumCodec<T> of(Class<T> cls, T[] vals) {
var codec = Codec.STRING.xmap((e) -> {
try {
return Enum.valueOf(cls, e);
} catch (Exception var4) {
throw new IllegalArgumentException(e + " is not a valid " + cls.getSimpleName() + ". Valid values are: " + List.of(vals));
}
}, Enum::name);
var stream = ByteBufCodecs.INT.map(e -> vals[e], Enum::ordinal);
return new EnumCodec<>(codec, stream);
}
public EnumCodec<List<T>> toList() {
return new EnumCodec<>(codec.listOf(), stream.apply(Wrappers.cast(ByteBufCodecs.list())));
}
}

View File

@@ -7,6 +7,7 @@ import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.Items;
import net.minecraft.world.item.alchemy.Potion;
import net.minecraft.world.item.alchemy.PotionContents;
import net.minecraft.world.item.crafting.Ingredient;
import net.neoforged.neoforge.common.crafting.ICustomIngredient;
import net.neoforged.neoforge.common.crafting.IngredientType;
@@ -14,6 +15,10 @@ import java.util.stream.Stream;
public record PotionIngredient(Holder<Potion> potion) implements ICustomIngredient {
public static Ingredient of(Holder<Potion> potion){
return new PotionIngredient(potion).toVanilla();
}
@Override
public Stream<ItemStack> getItems() {
ItemStack stack = new ItemStack(Items.POTION);

View File

@@ -0,0 +1,73 @@
package dev.xkmc.l2core.serial.loot;
import com.tterrag.registrate.providers.loot.RegistrateBlockLootTables;
import net.minecraft.Util;
import net.minecraft.advancements.critereon.*;
import net.minecraft.core.Holder;
import net.minecraft.resources.ResourceKey;
import net.minecraft.util.StringRepresentable;
import net.minecraft.world.item.enchantment.Enchantment;
import net.minecraft.world.item.enchantment.Enchantments;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.storage.loot.functions.ApplyBonusCount;
import net.minecraft.world.level.storage.loot.functions.LootItemFunction;
import net.minecraft.world.level.storage.loot.predicates.BonusLevelTableCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemBlockStatePropertyCondition;
import net.minecraft.world.level.storage.loot.predicates.LootItemCondition;
import net.minecraft.world.level.storage.loot.predicates.MatchTool;
import java.util.List;
import java.util.Optional;
public record LootHelper(RegistrateBlockLootTables pvd) {
public <T> Holder<T> resolve(ResourceKey<T> key) {
return pvd.getRegistries().holderOrThrow(key);
}
public EnchantmentPredicate hasEnch(ResourceKey<Enchantment> enchant, int min) {
return new EnchantmentPredicate(resolve(enchant), MinMaxBounds.Ints.atLeast(min));
}
public LootItemCondition.Builder toolHasEnch(ResourceKey<Enchantment> enchant, int min) {
return MatchTool.toolMatches(ItemPredicate.Builder.item().withSubPredicate(
ItemSubPredicates.ENCHANTMENTS, ItemEnchantmentsPredicate.enchantments(
List.of(hasEnch(enchant, min)))));
}
public LootItemCondition.Builder silk() {
return toolHasEnch(Enchantments.SILK_TOUCH, 1);
}
public LootItemCondition.Builder intState(Block block, Property<Integer> prop, int val) {
return LootItemBlockStatePropertyCondition.hasBlockStateProperties(block)
.setProperties(StatePropertiesPredicate.Builder.properties()
.hasProperty(prop, val));
}
public <T extends Comparable<T> & StringRepresentable> LootItemCondition.Builder enumState(Block block, Property<T> prop, T val) {
return LootItemBlockStatePropertyCondition.hasBlockStateProperties(block)
.setProperties(StatePropertiesPredicate.Builder.properties()
.hasProperty(prop, val));
}
public LootItemCondition.Builder intRangeState(Block block, Property<Integer> prop, int min, int max) {
return LootItemBlockStatePropertyCondition.hasBlockStateProperties(block)
.setProperties(Util.make(StatePropertiesPredicate.Builder.properties(),
e -> e.matchers.add(new StatePropertiesPredicate.PropertyMatcher(prop.getName(),
new StatePropertiesPredicate.RangedMatcher(
Optional.of("" + min), Optional.of("" + max))))));
}
public LootItemFunction.Builder fortuneCount(int factor) {
return ApplyBonusCount.addUniformBonusCount(resolve(Enchantments.FORTUNE), factor);
}
public LootItemCondition.Builder fortuneChance(int... denominators) {
float[] fracs = new float[denominators.length];
for (int i = 0; i < fracs.length; i++) fracs[i] = 1f / denominators[i];
return BonusLevelTableCondition.bonusLevelFlatChance(resolve(Enchantments.FORTUNE), fracs);
}
}

View File

@@ -22,6 +22,7 @@ import net.minecraft.world.level.storage.loot.providers.number.UniformGenerator;
import java.util.Optional;
@SuppressWarnings("unused")
@Deprecated
public class LootTableTemplate {
public static LootPool.Builder getPool(int roll, int bonus) {