This commit is contained in:
lcy0x1
2024-07-12 19:11:22 +08:00
parent ee7d9ccee9
commit 526630b08c
35 changed files with 394 additions and 409 deletions

View File

@@ -2,7 +2,6 @@ package dev.xkmc.l2core.base.effects;
import net.minecraft.world.effect.MobEffectInstance;
import net.minecraft.world.entity.LivingEntity;
import net.neoforged.bus.api.Event;
import net.neoforged.bus.api.ICancellableEvent;
import net.neoforged.neoforge.event.entity.living.MobEffectEvent;
import org.jetbrains.annotations.NotNull;

View File

@@ -7,7 +7,6 @@ import net.minecraft.nbt.CompoundTag;
import net.neoforged.neoforge.attachment.AttachmentType;
import net.neoforged.neoforge.attachment.IAttachmentHolder;
import net.neoforged.neoforge.attachment.IAttachmentSerializer;
import org.jetbrains.annotations.Nullable;
import java.util.Objects;
import java.util.function.Supplier;

View File

@@ -3,5 +3,4 @@ package dev.xkmc.l2core.capability.attachment;
public class BaseAttachment {
}

View File

@@ -15,36 +15,36 @@ import java.util.function.Supplier;
*/
public class GeneralCapabilityHolder<E extends IAttachmentHolder, T extends GeneralCapabilityTemplate<E, T>> extends AttachmentDef<T> {
public static final Map<ResourceLocation, GeneralCapabilityHolder<?, ?>> INTERNAL_MAP = new ConcurrentHashMap<>();
public static final Map<ResourceLocation, GeneralCapabilityHolder<?, ?>> INTERNAL_MAP = new ConcurrentHashMap<>();
public final ResourceLocation id;
public final Class<E> entity_class;
private final Predicate<E> pred;
public final ResourceLocation id;
public final Class<E> entity_class;
private final Predicate<E> pred;
public GeneralCapabilityHolder(ResourceLocation id, Class<T> holder_class, Supplier<T> sup,
Class<E> entity_class, Predicate<E> pred) {
super(holder_class, sup);
this.id = id;
this.entity_class = entity_class;
this.pred = pred;
INTERNAL_MAP.put(id, this);
}
public GeneralCapabilityHolder(ResourceLocation id, Class<T> holder_class, Supplier<T> sup,
Class<E> entity_class, Predicate<E> pred) {
super(holder_class, sup);
this.id = id;
this.entity_class = entity_class;
this.pred = pred;
INTERNAL_MAP.put(id, this);
}
public T getOrCreate(E e) {
return e.getData(type());
}
public T getOrCreate(E e) {
return e.getData(type());
}
public Optional<T> getExisting(E e) {
return e.getExistingData(type());
}
public Optional<T> getExisting(E e) {
return e.getExistingData(type());
}
public boolean isFor(IAttachmentHolder holder) {
return entity_class.isInstance(holder) && isProper(Wrappers.cast(holder));
}
public boolean isFor(IAttachmentHolder holder) {
return entity_class.isInstance(holder) && isProper(Wrappers.cast(holder));
}
public boolean isProper(E entity) {
return pred.test(entity);
}
public boolean isProper(E entity) {
return pred.test(entity);
}
}

View File

@@ -15,7 +15,7 @@ public class ConditionalToken {
/**
* return true to retain
* */
*/
public boolean retainOnDeath(Player player) {
return false;
}

View File

@@ -13,37 +13,37 @@ import java.util.TreeSet;
@SerialClass
public class PlayerFlagData extends PlayerCapabilityTemplate<PlayerFlagData> {
public static void addFlag(LivingEntity entity, String str) {
if (entity instanceof Player player) {
L2LibReg.FLAGS.type().getOrCreate(player).addFlag(str);
if (player instanceof ServerPlayer sp)
L2LibReg.FLAGS.type().network.toClient(sp);
} else {
entity.addTag(str);
}
}
public static void addFlag(LivingEntity entity, String str) {
if (entity instanceof Player player) {
L2LibReg.FLAGS.type().getOrCreate(player).addFlag(str);
if (player instanceof ServerPlayer sp)
L2LibReg.FLAGS.type().network.toClient(sp);
} else {
entity.addTag(str);
}
}
public static boolean hasFlag(LivingEntity entity, String str) {
if (entity instanceof Player player) {
return L2LibReg.FLAGS.type().getExisting(player).map(e -> e.hasFlag(str)).orElse(false);
} else {
return entity.getTags().contains(str);
}
}
public static boolean hasFlag(LivingEntity entity, String str) {
if (entity instanceof Player player) {
return L2LibReg.FLAGS.type().getExisting(player).map(e -> e.hasFlag(str)).orElse(false);
} else {
return entity.getTags().contains(str);
}
}
@SerialField
private final TreeSet<String> flags = new TreeSet<>();
@SerialField
private final TreeSet<String> flags = new TreeSet<>();
public void addFlag(String str) {
flags.add(str);
}
public void addFlag(String str) {
flags.add(str);
}
public boolean hasFlag(String flag) {
return flags.contains(flag);
}
public boolean hasFlag(String flag) {
return flags.contains(flag);
}
public static void register() {
public static void register() {
}
}
}

View File

@@ -7,7 +7,6 @@ import dev.xkmc.l2serial.serialization.marker.SerialField;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.entity.player.Player;
import org.jetbrains.annotations.Nullable;
import java.util.UUID;
import java.util.function.Predicate;

View File

@@ -5,25 +5,25 @@ import net.minecraft.server.level.ServerPlayer;
public class PlayerCapabilityNetworkHandler<T extends PlayerCapabilityTemplate<T>> {
public final PlayerCapabilityHolder<T> holder;
public final PlayerCapabilityHolder<T> holder;
public PlayerCapabilityNetworkHandler(PlayerCapabilityHolder<T> holder) {
this.holder = holder;
}
public PlayerCapabilityNetworkHandler(PlayerCapabilityHolder<T> holder) {
this.holder = holder;
}
public void toClient(ServerPlayer e) {
holder.getExisting(e).ifPresent(x -> L2Core.PACKET_HANDLER.toClientPlayer(
PlayerCapToClient.of(e, PlayerCapToClient.Action.CLIENT, holder, x), e));
}
public void toClient(ServerPlayer e) {
holder.getExisting(e).ifPresent(x -> L2Core.PACKET_HANDLER.toClientPlayer(
PlayerCapToClient.of(e, PlayerCapToClient.Action.CLIENT, holder, x), e));
}
public void toTracking(ServerPlayer e) {
holder.getExisting(e).ifPresent(x -> L2Core.PACKET_HANDLER.toTrackingOnly(
PlayerCapToClient.of(e, PlayerCapToClient.Action.TRACK, holder, x), e));
}
public void toTracking(ServerPlayer e) {
holder.getExisting(e).ifPresent(x -> L2Core.PACKET_HANDLER.toTrackingOnly(
PlayerCapToClient.of(e, PlayerCapToClient.Action.TRACK, holder, x), e));
}
public void startTracking(ServerPlayer tracker, ServerPlayer target) {
holder.getExisting(target).ifPresent(x -> L2Core.PACKET_HANDLER.toClientPlayer(
PlayerCapToClient.of(target, PlayerCapToClient.Action.TRACK, holder, x), tracker));
}
public void startTracking(ServerPlayer tracker, ServerPlayer target) {
holder.getExisting(target).ifPresent(x -> L2Core.PACKET_HANDLER.toClientPlayer(
PlayerCapToClient.of(target, PlayerCapToClient.Action.TRACK, holder, x), tracker));
}
}

View File

@@ -4,7 +4,6 @@ import com.tterrag.registrate.providers.ProviderType;
import com.tterrag.registrate.util.DataIngredient;
import dev.xkmc.l2core.init.reg.registrate.L2Registrate;
import dev.xkmc.l2core.serial.recipe.ConditionalRecipeWrapper;
import net.minecraft.Util;
import net.minecraft.data.recipes.RecipeBuilder;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.resources.ResourceKey;
@@ -16,7 +15,6 @@ import net.minecraft.world.level.storage.loot.LootPool;
import net.minecraft.world.level.storage.loot.LootTable;
import net.neoforged.neoforge.client.model.generators.ModelFile;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class PatchouliHelper {

View File

@@ -40,144 +40,144 @@ import java.util.List;
@EventBusSubscriber(value = Dist.CLIENT, modid = L2Core.MODID, bus = EventBusSubscriber.Bus.GAME)
public class ClientEffectRenderEvents {
private static final ArrayList<DelayedEntityRender> ICONS = new ArrayList<>();
private static final ArrayList<DelayedEntityRender> ICONS = new ArrayList<>();
@SubscribeEvent
public static void clientTick(ClientTickEvent.Post event) {
AbstractClientPlayer player = Proxy.getClientPlayer();
if (player != null) {
for (var entry : player.getActiveEffectsMap().entrySet()) {
if (entry.getKey() instanceof FirstPlayerRenderEffect effect) {
effect.onClientLevelRender(player, entry.getValue());
}
}
}
}
@SubscribeEvent
public static void clientTick(ClientTickEvent.Post event) {
AbstractClientPlayer player = Proxy.getClientPlayer();
if (player != null) {
for (var entry : player.getActiveEffectsMap().entrySet()) {
if (entry.getKey() instanceof FirstPlayerRenderEffect effect) {
effect.onClientLevelRender(player, entry.getValue());
}
}
}
}
@SubscribeEvent
public static void levelRenderLast(RenderLevelStageEvent event) {
if (event.getStage() != RenderLevelStageEvent.Stage.AFTER_WEATHER) return;
LevelRenderer renderer = event.getLevelRenderer();
MultiBufferSource.BufferSource buffers = Minecraft.getInstance().renderBuffers().bufferSource();
Camera camera = Minecraft.getInstance().gameRenderer.getMainCamera();
PoseStack stack = event.getPoseStack();
@SubscribeEvent
public static void levelRenderLast(RenderLevelStageEvent event) {
if (event.getStage() != RenderLevelStageEvent.Stage.AFTER_WEATHER) return;
LevelRenderer renderer = event.getLevelRenderer();
MultiBufferSource.BufferSource buffers = Minecraft.getInstance().renderBuffers().bufferSource();
Camera camera = Minecraft.getInstance().gameRenderer.getMainCamera();
PoseStack stack = event.getPoseStack();
// cache the previous handler
for (DelayedEntityRender icon : ICONS) {
renderIcon(stack, buffers, icon, event.getPartialTick().getGameTimeDeltaTicks(), camera, renderer.entityRenderDispatcher);
}
buffers.endBatch();
// cache the previous handler
for (DelayedEntityRender icon : ICONS) {
renderIcon(stack, buffers, icon, event.getPartialTick().getGameTimeDeltaTicks(), camera, renderer.entityRenderDispatcher);
}
buffers.endBatch();
ICONS.clear();
}
ICONS.clear();
}
@SubscribeEvent
public static void onLivingEntityRender(RenderLivingEvent.Post<?, ?> event) {
LivingEntity entity = event.getEntity();
if (!L2LibReg.EFFECT.type().isProper(entity)) return;
if (entity.getTags().contains("ClientOnly")) return;//TODO
ClientEffectCap cap = L2LibReg.EFFECT.type().getOrCreate(entity);
List<Pair<ClientRenderEffect, Integer>> l0 = new ArrayList<>();
for (var entry : cap.map.entrySet()) {
if (entry.getKey().value() instanceof ClientRenderEffect effect) {
l0.add(Pair.of(effect, entry.getValue()));
}
}
if (l0.isEmpty()) return;
List<Pair<Integer, DelayedEntityRender>> l1 = new ArrayList<>();
int index = 0;
@SubscribeEvent
public static void onLivingEntityRender(RenderLivingEvent.Post<?, ?> event) {
LivingEntity entity = event.getEntity();
if (!L2LibReg.EFFECT.type().isProper(entity)) return;
if (entity.getTags().contains("ClientOnly")) return;//TODO
ClientEffectCap cap = L2LibReg.EFFECT.type().getOrCreate(entity);
List<Pair<ClientRenderEffect, Integer>> l0 = new ArrayList<>();
for (var entry : cap.map.entrySet()) {
if (entry.getKey().value() instanceof ClientRenderEffect effect) {
l0.add(Pair.of(effect, entry.getValue()));
}
}
if (l0.isEmpty()) return;
List<Pair<Integer, DelayedEntityRender>> l1 = new ArrayList<>();
int index = 0;
for (var e : l0) {
int lv = e.getSecond();
int size = l1.size();
int I = index;
e.getFirst().render(entity, lv, x -> l1.add(Pair.of(I, x)));
if (l1.size() > size) {
index++;
}
}
for (var e : l0) {
int lv = e.getSecond();
int size = l1.size();
int I = index;
e.getFirst().render(entity, lv, x -> l1.add(Pair.of(I, x)));
if (l1.size() > size) {
index++;
}
}
int n = index;
int w = (int) Math.ceil(Math.sqrt(n));
int h = (int) Math.ceil(n * 1d / w);
int n = index;
int w = (int) Math.ceil(Math.sqrt(n));
int h = (int) Math.ceil(n * 1d / w);
for (var e : l1) {
int i = e.getFirst();
int iy = i / w;
int iw = Math.min(w, n - iy * w);
int ix = i - iy * w;
ICONS.add(e.getSecond().resize(IconRenderRegion.of(w, ix, iy, iw, h)));
}
for (var e : l1) {
int i = e.getFirst();
int iy = i / w;
int iw = Math.min(w, n - iy * w);
int ix = i - iy * w;
ICONS.add(e.getSecond().resize(IconRenderRegion.of(w, ix, iy, iw, h)));
}
}
}
private static void renderIcon(PoseStack pose, MultiBufferSource buffer, DelayedEntityRender icon,
float partial, Camera camera, EntityRenderDispatcher dispatcher) {
LivingEntity entity = icon.entity();
float f = entity.getBbHeight() / 2;
private static void renderIcon(PoseStack pose, MultiBufferSource buffer, DelayedEntityRender icon,
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());
Vec3 offset = dispatcher.getRenderer(entity).getRenderOffset(entity, partial);
Vec3 cam_pos = camera.getPosition();
double d2 = x0 - cam_pos.x + offset.x();
double d3 = y0 - cam_pos.y + offset.y();
double d0 = z0 - cam_pos.z + offset.z();
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());
Vec3 offset = dispatcher.getRenderer(entity).getRenderOffset(entity, partial);
Vec3 cam_pos = camera.getPosition();
double d2 = x0 - cam_pos.x + offset.x();
double d3 = y0 - cam_pos.y + offset.y();
double d0 = z0 - cam_pos.z + offset.z();
pose.pushPose();
pose.translate(d2, d3 + f, d0);
pose.mulPose(camera.rotation());
PoseStack.Pose entry = pose.last();
VertexConsumer ivertexbuilder = buffer.getBuffer(get2DIcon(icon.rl()));
pose.pushPose();
pose.translate(d2, d3 + f, d0);
pose.mulPose(camera.rotation());
PoseStack.Pose entry = pose.last();
VertexConsumer ivertexbuilder = buffer.getBuffer(get2DIcon(icon.rl()));
float ix0 = -0.5f + icon.region().x();
float ix1 = ix0 + icon.region().scale();
float iy0 = -0.5f + icon.region().y();
float iy1 = iy0 + icon.region().scale();
float u0 = icon.tx();
float v0 = icon.ty();
float u1 = icon.tx() + icon.tw();
float v1 = icon.ty() + icon.th();
float ix0 = -0.5f + icon.region().x();
float ix1 = ix0 + icon.region().scale();
float iy0 = -0.5f + icon.region().y();
float iy1 = iy0 + icon.region().scale();
float u0 = icon.tx();
float v0 = icon.ty();
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);
pose.popPose();
}
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);
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);
}
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);
}
public static RenderType get2DIcon(ResourceLocation rl) {
return RenderType.create(
"entity_body_icon",
DefaultVertexFormat.POSITION_TEX,
VertexFormat.Mode.QUADS, 256, false, true,
RenderType.CompositeState.builder()
.setShaderState(RenderStateShard.RENDERTYPE_ENTITY_GLINT_SHADER)
.setTextureState(new RenderStateShard.TextureStateShard(rl, false, false))
.setTransparencyState(RenderStateShard.ADDITIVE_TRANSPARENCY)
.setDepthTestState(RenderStateShard.NO_DEPTH_TEST)
.createCompositeState(false)
);
}
public static RenderType get2DIcon(ResourceLocation rl) {
return RenderType.create(
"entity_body_icon",
DefaultVertexFormat.POSITION_TEX,
VertexFormat.Mode.QUADS, 256, false, true,
RenderType.CompositeState.builder()
.setShaderState(RenderStateShard.RENDERTYPE_ENTITY_GLINT_SHADER)
.setTextureState(new RenderStateShard.TextureStateShard(rl, false, false))
.setTransparencyState(RenderStateShard.ADDITIVE_TRANSPARENCY)
.setDepthTestState(RenderStateShard.NO_DEPTH_TEST)
.createCompositeState(false)
);
}
public static void sync(EffectToClient eff) {
if (Minecraft.getInstance().level == null) return;
Entity e = Minecraft.getInstance().level.getEntity(eff.entity());
if (!(e instanceof LivingEntity le)) return;
if (!L2LibReg.EFFECT.type().isProper(le)) return;
ClientEffectCap cap = L2LibReg.EFFECT.type().getOrCreate(le);
if (eff.exist()) {
cap.map.put(eff.effect(), eff.level());
} else {
cap.map.remove(eff.effect());
}
}
public static void sync(EffectToClient eff) {
if (Minecraft.getInstance().level == null) return;
Entity e = Minecraft.getInstance().level.getEntity(eff.entity());
if (!(e instanceof LivingEntity le)) return;
if (!L2LibReg.EFFECT.type().isProper(le)) return;
ClientEffectCap cap = L2LibReg.EFFECT.type().getOrCreate(le);
if (eff.exist()) {
cap.map.put(eff.effect(), eff.level());
} else {
cap.map.remove(eff.effect());
}
}
}

View File

@@ -105,6 +105,7 @@ public interface EECVal<T> extends Val<DataComponentType<List<ConditionalEffect<
}
}
interface Special<T> extends Val<DataComponentType<T>> {
record Impl<T>(

View File

@@ -3,5 +3,4 @@ package dev.xkmc.l2core.init.reg.ench;
public class LegacyEnchantment {
}

View File

@@ -2,11 +2,7 @@ package dev.xkmc.l2core.serial.advancements;
import com.mojang.serialization.Codec;
import dev.xkmc.l2serial.serialization.codec.CodecAdaptor;
import net.minecraft.advancements.critereon.ContextAwarePredicate;
import net.minecraft.advancements.critereon.SimpleCriterionTrigger;
import net.minecraft.resources.ResourceLocation;
import java.util.function.BiFunction;
public class BaseCriterion<T extends BaseCriterionInstance<T, R>, R extends BaseCriterion<T, R>> extends SimpleCriterionTrigger<T> {

View File

@@ -67,8 +67,8 @@ public class CriterionBuilder implements IAdvBuilder {
public static CriterionBuilder enchanted(TagKey<Item> item, Holder<Enchantment> enchantment) {
return one(InventoryChangeTrigger.TriggerInstance.hasItems(ItemPredicate.Builder.item().of(item)
.withSubPredicate(ItemSubPredicates.ENCHANTMENTS,
ItemEnchantmentsPredicate.enchantments(List.of(
new EnchantmentPredicate(enchantment, MinMaxBounds.Ints.ANY))))));
ItemEnchantmentsPredicate.enchantments(List.of(
new EnchantmentPredicate(enchantment, MinMaxBounds.Ints.ANY))))));
}
public static CriterionBuilder one(Criterion<?> instance) {
@@ -76,7 +76,7 @@ public class CriterionBuilder implements IAdvBuilder {
}
public enum RequirementsStrategy {
AND, OR;
AND, OR
}
public static CriterionBuilder and() {

View File

@@ -7,6 +7,6 @@ import java.util.List;
public interface IAdvBuilder {
void onBuild(String id, Advancement.Builder builder, List<ICondition> conditions);
void onBuild(String id, Advancement.Builder builder, List<ICondition> conditions);
}

View File

@@ -8,11 +8,11 @@ import java.util.List;
public record ModLoadedAdv(String... modid) implements IAdvBuilder {
@Override
public void onBuild(String id, Advancement.Builder builder, List<ICondition> conditions) {
for (var e : modid) {
conditions.add(new ModLoadedCondition(e));
}
}
@Override
public void onBuild(String id, Advancement.Builder builder, List<ICondition> conditions) {
for (var e : modid) {
conditions.add(new ModLoadedCondition(e));
}
}
}

View File

@@ -15,7 +15,7 @@ public class MergedConfigType<T extends BaseConfig> extends BaseConfigType<T> {
return result;
}
result = new ConfigMerger<>(cls).apply(configs.values());
result.id = ResourceLocation.fromNamespaceAndPath(parent.modid, id);
result.id = ResourceLocation.fromNamespaceAndPath(parent.modid, id);
return result;
}

View File

@@ -7,9 +7,9 @@ import java.util.Optional;
public class AbstractConfigParser {
public static Optional<Object> parse(String path, List<String> line) {
return Optional.ofNullable(ConfigTracker.INSTANCE.fileMap().get(path))
.map(file -> file.getConfigData().get(line));
}
public static Optional<Object> parse(String path, List<String> line) {
return Optional.ofNullable(ConfigTracker.INSTANCE.fileMap().get(path))
.map(file -> file.getConfigData().get(line));
}
}

View File

@@ -9,37 +9,37 @@ import java.util.function.BooleanSupplier;
public record BooleanConfigValue(String path, List<String> line) implements BooleanSupplier {
public static BooleanConfigValue of(String file, ModConfigSpec.ConfigValue<Double> config) {
return new BooleanConfigValue(file, config.getPath());
}
public static BooleanConfigValue of(String file, ModConfigSpec.ConfigValue<Double> config) {
return new BooleanConfigValue(file, config.getPath());
}
public static BooleanConfigValue of(String data) {
int last = data.lastIndexOf('/');
var line = data.substring(last + 1).split("\\.");
return new BooleanConfigValue(data.substring(0, last), List.of(line));
}
public static BooleanConfigValue of(String data) {
int last = data.lastIndexOf('/');
var line = data.substring(last + 1).split("\\.");
return new BooleanConfigValue(data.substring(0, last), List.of(line));
}
public static Codec<BooleanSupplier> CODEC = Codec.either(Codec.BOOL, Codec.STRING)
.xmap(e -> e.map(x -> (BooleanSupplier) (() -> x), BooleanConfigValue::of),
e -> e instanceof BooleanConfigValue val ?
Either.right(val.toData()) :
Either.left(e.getAsBoolean()));
public static Codec<BooleanSupplier> CODEC = Codec.either(Codec.BOOL, Codec.STRING)
.xmap(e -> e.map(x -> (BooleanSupplier) (() -> x), BooleanConfigValue::of),
e -> e instanceof BooleanConfigValue val ?
Either.right(val.toData()) :
Either.left(e.getAsBoolean()));
public boolean getAsBoolean() {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Boolean val ? val : false)
.orElse(false);
}
public boolean getAsBoolean() {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Boolean val ? val : false)
.orElse(false);
}
public String toData() {
StringBuilder lines = new StringBuilder();
for (var e : line) {
if (!lines.isEmpty()) {
lines.append(".");
}
lines.append(e);
}
return path + "/" + lines;
}
public String toData() {
StringBuilder lines = new StringBuilder();
for (var e : line) {
if (!lines.isEmpty()) {
lines.append(".");
}
lines.append(e);
}
return path + "/" + lines;
}
}

View File

@@ -9,19 +9,19 @@ import java.util.ArrayList;
public record BooleanValueCondition(String path, ArrayList<String> line, boolean expected) implements ICondition {
public static BooleanValueCondition of(String file, ModConfigSpec.ConfigValue<Boolean> config, boolean value) {
return new BooleanValueCondition(file, new ArrayList<>(config.getPath()), value);
}
public static BooleanValueCondition of(String file, ModConfigSpec.ConfigValue<Boolean> config, boolean value) {
return new BooleanValueCondition(file, new ArrayList<>(config.getPath()), value);
}
@Override
public boolean test(IContext context) {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Boolean bool && bool == expected).orElse(false);
}
@Override
public boolean test(IContext context) {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Boolean bool && bool == expected).orElse(false);
}
@Override
public MapCodec<BooleanValueCondition> codec() {
return L2LibReg.CONDITION_BOOL.get();
}
@Override
public MapCodec<BooleanValueCondition> codec() {
return L2LibReg.CONDITION_BOOL.get();
}
}

View File

@@ -9,38 +9,38 @@ import java.util.function.DoubleSupplier;
public record DoubleConfigValue(String path, List<String> line) implements DoubleSupplier {
public static DoubleConfigValue of(String file, ModConfigSpec.ConfigValue<Double> config) {
return new DoubleConfigValue(file, config.getPath());
}
public static DoubleConfigValue of(String file, ModConfigSpec.ConfigValue<Double> config) {
return new DoubleConfigValue(file, config.getPath());
}
public static DoubleConfigValue of(String data) {
int last = data.lastIndexOf('/');
var line = data.substring(last + 1).split("\\.");
return new DoubleConfigValue(data.substring(0, last), List.of(line));
}
public static DoubleConfigValue of(String data) {
int last = data.lastIndexOf('/');
var line = data.substring(last + 1).split("\\.");
return new DoubleConfigValue(data.substring(0, last), List.of(line));
}
public static Codec<DoubleSupplier> CODEC = Codec.either(Codec.DOUBLE, Codec.STRING)
.xmap(e -> e.map(x -> (DoubleSupplier) (() -> x), DoubleConfigValue::of),
e -> e instanceof DoubleConfigValue val ?
Either.right(val.toData()) :
Either.left(e.getAsDouble()));
public static Codec<DoubleSupplier> CODEC = Codec.either(Codec.DOUBLE, Codec.STRING)
.xmap(e -> e.map(x -> (DoubleSupplier) (() -> x), DoubleConfigValue::of),
e -> e instanceof DoubleConfigValue val ?
Either.right(val.toData()) :
Either.left(e.getAsDouble()));
@Override
public double getAsDouble() {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Number val ? val.doubleValue() : 0d)
.orElse(0d);
}
@Override
public double getAsDouble() {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Number val ? val.doubleValue() : 0d)
.orElse(0d);
}
public String toData() {
StringBuilder lines = new StringBuilder();
for (var e : line) {
if (!lines.isEmpty()) {
lines.append(".");
}
lines.append(e);
}
return path + "/" + lines;
}
public String toData() {
StringBuilder lines = new StringBuilder();
for (var e : line) {
if (!lines.isEmpty()) {
lines.append(".");
}
lines.append(e);
}
return path + "/" + lines;
}
}

View File

@@ -9,19 +9,19 @@ import java.util.ArrayList;
public record DoubleValueCondition(String path, ArrayList<String> line, double low, double high) implements ICondition {
public static DoubleValueCondition of(String file, ModConfigSpec.ConfigValue<Double> config, double low, double high) {
return new DoubleValueCondition(file, new ArrayList<>(config.getPath()), low, high);
}
public static DoubleValueCondition of(String file, ModConfigSpec.ConfigValue<Double> config, double low, double high) {
return new DoubleValueCondition(file, new ArrayList<>(config.getPath()), low, high);
}
@Override
public boolean test(IContext context) {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Double val && low <= val && val <= high).orElse(false);
}
@Override
public boolean test(IContext context) {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Double val && low <= val && val <= high).orElse(false);
}
@Override
public MapCodec<DoubleValueCondition> codec() {
return L2LibReg.CONDITION_DOUBLE.get();
}
@Override
public MapCodec<DoubleValueCondition> codec() {
return L2LibReg.CONDITION_DOUBLE.get();
}
}

View File

@@ -9,37 +9,37 @@ import java.util.function.IntSupplier;
public record IntConfigValue(String path, List<String> line) implements IntSupplier {
public static IntConfigValue of(String file, ModConfigSpec.ConfigValue<Integer> config) {
return new IntConfigValue(file, config.getPath());
}
public static IntConfigValue of(String file, ModConfigSpec.ConfigValue<Integer> config) {
return new IntConfigValue(file, config.getPath());
}
public static IntConfigValue of(String data) {
int last = data.lastIndexOf('/');
var line = data.substring(last + 1).split("\\.");
return new IntConfigValue(data.substring(0, last), List.of(line));
}
public static IntConfigValue of(String data) {
int last = data.lastIndexOf('/');
var line = data.substring(last + 1).split("\\.");
return new IntConfigValue(data.substring(0, last), List.of(line));
}
public static Codec<IntSupplier> CODEC = Codec.either(Codec.INT, Codec.STRING)
.xmap(e -> e.map(x -> (IntSupplier) (() -> x), IntConfigValue::of),
e -> e instanceof IntConfigValue val ?
Either.right(val.toData()) :
Either.left(e.getAsInt()));
public static Codec<IntSupplier> CODEC = Codec.either(Codec.INT, Codec.STRING)
.xmap(e -> e.map(x -> (IntSupplier) (() -> x), IntConfigValue::of),
e -> e instanceof IntConfigValue val ?
Either.right(val.toData()) :
Either.left(e.getAsInt()));
public int getAsInt() {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Number val ? val.intValue() : 0)
.orElse(0);
}
public int getAsInt() {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Number val ? val.intValue() : 0)
.orElse(0);
}
public String toData() {
StringBuilder lines = new StringBuilder();
for (var e : line) {
if (!lines.isEmpty()) {
lines.append(".");
}
lines.append(e);
}
return path + "/" + lines;
}
public String toData() {
StringBuilder lines = new StringBuilder();
for (var e : line) {
if (!lines.isEmpty()) {
lines.append(".");
}
lines.append(e);
}
return path + "/" + lines;
}
}

View File

@@ -16,7 +16,7 @@ public record IntValueCondition(String path, ArrayList<String> line, int low, in
@Override
public boolean test(IContext context) {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof Integer val && low <= val && val <= high).orElse(false);
.map(e -> e instanceof Integer val && low <= val && val <= high).orElse(false);
}

View File

@@ -10,19 +10,19 @@ import java.util.List;
public record ListStringValueCondition(String path, ArrayList<String> line, String key) implements ICondition {
public static ListStringValueCondition of(String file, ModConfigSpec.ConfigValue<List<String>> config, String key) {
return new ListStringValueCondition(file, new ArrayList<>(config.getPath()), key);
}
public static ListStringValueCondition of(String file, ModConfigSpec.ConfigValue<List<String>> config, String key) {
return new ListStringValueCondition(file, new ArrayList<>(config.getPath()), key);
}
@Override
public boolean test(IContext context) {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof List<?> val && val.contains(key)).orElse(false);
}
@Override
public boolean test(IContext context) {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof List<?> val && val.contains(key)).orElse(false);
}
@Override
public MapCodec<ListStringValueCondition> codec() {
return L2LibReg.CONDITION_LIST_STR.get();
}
@Override
public MapCodec<ListStringValueCondition> codec() {
return L2LibReg.CONDITION_LIST_STR.get();
}
}

View File

@@ -9,19 +9,19 @@ import java.util.ArrayList;
public record StringValueCondition(String path, ArrayList<String> line, String key) implements ICondition {
public static StringValueCondition of(String file, ModConfigSpec.ConfigValue<String> config, String key) {
return new StringValueCondition(file, new ArrayList<>(config.getPath()), key);
}
public static StringValueCondition of(String file, ModConfigSpec.ConfigValue<String> config, String key) {
return new StringValueCondition(file, new ArrayList<>(config.getPath()), key);
}
@Override
public boolean test(IContext context) {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof String val && val.equals(key)).orElse(false);
}
@Override
public boolean test(IContext context) {
return AbstractConfigParser.parse(path, line)
.map(e -> e instanceof String val && val.equals(key)).orElse(false);
}
@Override
public MapCodec<StringValueCondition> codec() {
return L2LibReg.CONDITION_STR.get();
}
@Override
public MapCodec<StringValueCondition> codec() {
return L2LibReg.CONDITION_STR.get();
}
}

View File

@@ -22,7 +22,7 @@ public class AddItemModifier extends LootModifier {
public static final MapCodec<AddItemModifier> MAP_CODEC = RecordCodecBuilder.mapCodec(i -> LootModifier.codecStart(i).and(i.group(
BuiltInRegistries.ITEM.byNameCodec().fieldOf("item").forGetter(m -> m.item),
BuiltInRegistries.ITEM.byNameCodec().optionalFieldOf("fail").forGetter(m -> m.fail == Items.AIR ? Optional.<Item>empty() : Optional.of(m.fail)),
BuiltInRegistries.ITEM.byNameCodec().optionalFieldOf("fail").forGetter(m -> m.fail == Items.AIR ? Optional.empty() : Optional.of(m.fail)),
DoubleConfigValue.CODEC.optionalFieldOf("chance")
.forGetter(m -> Optional.ofNullable(m.chance))
)).apply(i, AddItemModifier::new));

View File

@@ -11,28 +11,28 @@ import net.minecraft.world.level.storage.loot.predicates.LootItemConditionType;
@SerialClass
public class PlayerFlagCondition implements LootItemCondition {
@SerialField
public String flag;
@SerialField
public String flag;
@Deprecated
public PlayerFlagCondition() {
@Deprecated
public PlayerFlagCondition() {
}
}
public PlayerFlagCondition(String flag) {
this.flag = flag;
}
public PlayerFlagCondition(String flag) {
this.flag = flag;
}
@Override
public LootItemConditionType getType() {
return L2LibReg.LIC_FLAG.get();
}
@Override
public LootItemConditionType getType() {
return L2LibReg.LIC_FLAG.get();
}
@Override
public boolean test(LootContext ctx) {
if (!ctx.hasParam(LootContextParams.LAST_DAMAGE_PLAYER)) return false;
var player = ctx.getParam(LootContextParams.LAST_DAMAGE_PLAYER);
return L2LibReg.FLAGS.type().getExisting(player).map(e -> e.hasFlag(flag)).orElse(false);
}
@Override
public boolean test(LootContext ctx) {
if (!ctx.hasParam(LootContextParams.LAST_DAMAGE_PLAYER)) return false;
var player = ctx.getParam(LootContextParams.LAST_DAMAGE_PLAYER);
return L2LibReg.FLAGS.type().getExisting(player).map(e -> e.hasFlag(flag)).orElse(false);
}
}

View File

@@ -1,6 +1,5 @@
package dev.xkmc.l2core.serial.recipe;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import net.minecraft.MethodsReturnNonnullByDefault;
import net.minecraft.network.RegistryFriendlyByteBuf;

View File

@@ -1,8 +1,6 @@
package dev.xkmc.l2core.serial.recipe;
import net.minecraft.core.HolderLookup;
import net.minecraft.core.RegistryAccess;
import net.minecraft.world.Container;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeInput;

View File

@@ -9,7 +9,6 @@ import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.data.recipes.RecipeBuilder;
import net.minecraft.data.recipes.RecipeOutput;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.Container;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.crafting.RecipeInput;
import org.jetbrains.annotations.Nullable;

View File

@@ -12,27 +12,27 @@ import org.jetbrains.annotations.Nullable;
public record ConditionalRecipeWrapper(RecipeOutput pvd, ICondition... conditions) implements RecipeOutput {
public static RecipeOutput mod(RecipeOutput pvd, String... modid) {
ICondition[] ans = new ICondition[modid.length];
for (int i = 0; i < ans.length; ++i) {
ans[i] = new ModLoadedCondition(modid[i]);
}
return new ConditionalRecipeWrapper(pvd, ans);
public static RecipeOutput mod(RecipeOutput pvd, String... modid) {
ICondition[] ans = new ICondition[modid.length];
for (int i = 0; i < ans.length; ++i) {
ans[i] = new ModLoadedCondition(modid[i]);
}
return new ConditionalRecipeWrapper(pvd, ans);
}
}
public static RecipeOutput of(RecipeOutput pvd, ICondition... cond) {
return new ConditionalRecipeWrapper(pvd, cond);
}
public static RecipeOutput of(RecipeOutput pvd, ICondition... cond) {
return new ConditionalRecipeWrapper(pvd, cond);
}
@Override
public Advancement.Builder advancement() {
return pvd.advancement();
}
@Override
public Advancement.Builder advancement() {
return pvd.advancement();
}
@Override
public void accept(ResourceLocation id, Recipe<?> recipe, @Nullable AdvancementHolder advancement, ICondition... conditions) {
pvd.accept(id, recipe, advancement, MathHelper.merge(conditions(), conditions));
}
@Override
public void accept(ResourceLocation id, Recipe<?> recipe, @Nullable AdvancementHolder advancement, ICondition... conditions) {
pvd.accept(id, recipe, advancement, MathHelper.merge(conditions(), conditions));
}
}

View File

@@ -13,24 +13,24 @@ import org.jetbrains.annotations.Nullable;
public record DataRecipeWrapper(RecipeOutput pvd, ItemStack stack) implements RecipeOutput {
public static RecipeOutput of(RecipeOutput pvd, ItemStack stack) {
return new DataRecipeWrapper(pvd, stack);
}
public static RecipeOutput of(RecipeOutput pvd, ItemStack stack) {
return new DataRecipeWrapper(pvd, stack);
}
@Override
public Advancement.Builder advancement() {
return pvd.advancement();
}
@Override
public Advancement.Builder advancement() {
return pvd.advancement();
}
@Override
public void accept(ResourceLocation id, Recipe<?> recipe, @Nullable AdvancementHolder advancement, ICondition... conditions) {
if (recipe instanceof ShapedRecipe r) {
r.result.applyComponents(stack.getComponents());
}
if (recipe instanceof ShapelessRecipe r) {
r.result.applyComponents(stack.getComponents());
}
pvd.accept(id, recipe, advancement, conditions);
}
@Override
public void accept(ResourceLocation id, Recipe<?> recipe, @Nullable AdvancementHolder advancement, ICondition... conditions) {
if (recipe instanceof ShapedRecipe r) {
r.result.applyComponents(stack.getComponents());
}
if (recipe instanceof ShapelessRecipe r) {
r.result.applyComponents(stack.getComponents());
}
pvd.accept(id, recipe, advancement, conditions);
}
}

View File

@@ -7,7 +7,6 @@ import dev.xkmc.l2serial.util.Wrappers;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.Container;
import net.minecraft.world.item.crafting.Recipe;
import net.minecraft.world.item.crafting.RecipeInput;
import net.minecraft.world.item.crafting.RecipeSerializer;

View File

@@ -12,7 +12,7 @@ public final class DCStack {
hashCode = stack.hashCode();
}
public ItemStack stack(){
public ItemStack stack() {
return stack;
}