From 8e26af821cf1505495e56a7230505d701424beff Mon Sep 17 00:00:00 2001 From: ishland Date: Tue, 8 Oct 2024 14:14:16 +0800 Subject: [PATCH] fix: blending hopefully --- .../common/ducks/IBlendingAwareVisitor.java | 7 ++++ .../common/gen/CompiledDensityFunction.java | 8 +++++ .../gen/DelegatingBlendingAwareVisitor.java | 32 +++++++++++++++++++ .../gen/SubCompiledDensityFunction.java | 16 ++++++++-- .../dfc/mixin/MixinChunkNoiseSampler.java | 26 ++++++++++++++- 5 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/ducks/IBlendingAwareVisitor.java create mode 100644 c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/DelegatingBlendingAwareVisitor.java diff --git a/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/ducks/IBlendingAwareVisitor.java b/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/ducks/IBlendingAwareVisitor.java new file mode 100644 index 000000000..fa78e1c31 --- /dev/null +++ b/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/ducks/IBlendingAwareVisitor.java @@ -0,0 +1,7 @@ +package com.ishland.c2me.opts.dfc.common.ducks; + +public interface IBlendingAwareVisitor { + + boolean c2me$isBlendingEnabled(); + +} diff --git a/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/CompiledDensityFunction.java b/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/CompiledDensityFunction.java index a29a43f25..f415fb7ca 100644 --- a/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/CompiledDensityFunction.java +++ b/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/CompiledDensityFunction.java @@ -3,6 +3,7 @@ import com.google.common.base.Suppliers; import com.ishland.c2me.opts.dfc.common.ast.EvalType; import com.ishland.c2me.opts.dfc.common.ducks.IArrayCacheCapable; +import com.ishland.c2me.opts.dfc.common.ducks.IBlendingAwareVisitor; import com.ishland.c2me.opts.dfc.common.ducks.IFastCacheLike; import com.ishland.c2me.opts.dfc.common.util.ArrayCache; import com.ishland.c2me.opts.dfc.common.vif.EachApplierVanillaInterface; @@ -32,6 +33,13 @@ private CompiledDensityFunction(CompiledEntry compiledEntry, Supplier args = this.compiledEntry.getArgs(); for (ListIterator iterator = args.listIterator(); iterator.hasNext(); ) { diff --git a/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/DelegatingBlendingAwareVisitor.java b/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/DelegatingBlendingAwareVisitor.java new file mode 100644 index 000000000..86986d490 --- /dev/null +++ b/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/DelegatingBlendingAwareVisitor.java @@ -0,0 +1,32 @@ +package com.ishland.c2me.opts.dfc.common.gen; + +import com.ishland.c2me.opts.dfc.common.ducks.IBlendingAwareVisitor; +import net.minecraft.world.gen.densityfunction.DensityFunction; + +import java.util.Objects; + +public class DelegatingBlendingAwareVisitor implements IBlendingAwareVisitor, DensityFunction.DensityFunctionVisitor { + + private final DensityFunction.DensityFunctionVisitor delegate; + private final boolean blendingEnabled; + + public DelegatingBlendingAwareVisitor(DensityFunction.DensityFunctionVisitor delegate, boolean blendingEnabled) { + this.delegate = Objects.requireNonNull(delegate); + this.blendingEnabled = blendingEnabled; + } + + @Override + public DensityFunction apply(DensityFunction densityFunction) { + return this.delegate.apply(densityFunction); + } + + @Override + public DensityFunction.Noise apply(DensityFunction.Noise noiseDensityFunction) { + return this.delegate.apply(noiseDensityFunction); + } + + @Override + public boolean c2me$isBlendingEnabled() { + return this.blendingEnabled; + } +} diff --git a/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/SubCompiledDensityFunction.java b/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/SubCompiledDensityFunction.java index efe77d141..936376ade 100644 --- a/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/SubCompiledDensityFunction.java +++ b/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/common/gen/SubCompiledDensityFunction.java @@ -3,6 +3,7 @@ import com.google.common.base.Suppliers; import com.ishland.c2me.opts.dfc.common.ast.EvalType; import com.ishland.c2me.opts.dfc.common.ducks.IArrayCacheCapable; +import com.ishland.c2me.opts.dfc.common.ducks.IBlendingAwareVisitor; import com.ishland.c2me.opts.dfc.common.ducks.ICoordinatesFilling; import com.ishland.c2me.opts.dfc.common.util.ArrayCache; import com.ishland.c2me.opts.dfc.common.vif.EachApplierVanillaInterface; @@ -10,12 +11,16 @@ import net.minecraft.world.gen.chunk.Blender; import net.minecraft.world.gen.chunk.ChunkNoiseSampler; import net.minecraft.world.gen.densityfunction.DensityFunction; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.util.Objects; import java.util.function.Supplier; public class SubCompiledDensityFunction implements DensityFunction { + private static final Logger LOGGER = LoggerFactory.getLogger(SubCompiledDensityFunction.class); + private final ISingleMethod singleMethod; private final IMultiMethod multiMethod; protected final Supplier blendingFallback; @@ -35,7 +40,7 @@ private static Supplier unwrap(DensityFunction densityFunction) if (densityFunction instanceof SubCompiledDensityFunction scdf) { return scdf.blendingFallback; } else { - return densityFunction != null ? () -> densityFunction : null; + return densityFunction != null ? Suppliers.ofInstance(densityFunction) : null; } } @@ -91,6 +96,13 @@ public DensityFunction apply(DensityFunctionVisitor visitor) { if (this.getClass() != SubCompiledDensityFunction.class) { throw new AbstractMethodError(); } + if (visitor instanceof IBlendingAwareVisitor blendingAwareVisitor && blendingAwareVisitor.c2me$isBlendingEnabled()) { + DensityFunction fallback1 = this.getFallback(); + if (fallback1 == null) { + throw new IllegalStateException("blendingFallback is no more"); + } + return fallback1.apply(visitor); + } boolean modified = false; Supplier fallback = this.blendingFallback != null ? Suppliers.memoize(() -> { DensityFunction densityFunction = this.blendingFallback.get(); @@ -125,7 +137,7 @@ public CodecHolder getCodecHolder() { throw new UnsupportedOperationException(); } - private DensityFunction getFallback() { + protected DensityFunction getFallback() { return this.blendingFallback != null ? this.blendingFallback.get() : null; } } diff --git a/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/mixin/MixinChunkNoiseSampler.java b/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/mixin/MixinChunkNoiseSampler.java index da118903e..4742a8a05 100644 --- a/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/mixin/MixinChunkNoiseSampler.java +++ b/c2me-opts-dfc/src/main/java/com/ishland/c2me/opts/dfc/mixin/MixinChunkNoiseSampler.java @@ -2,19 +2,24 @@ import com.ishland.c2me.opts.dfc.common.ducks.IArrayCacheCapable; import com.ishland.c2me.opts.dfc.common.ducks.ICoordinatesFilling; +import com.ishland.c2me.opts.dfc.common.gen.DelegatingBlendingAwareVisitor; import com.ishland.c2me.opts.dfc.common.util.ArrayCache; +import net.minecraft.world.gen.chunk.Blender; import net.minecraft.world.gen.chunk.ChunkNoiseSampler; import net.minecraft.world.gen.densityfunction.DensityFunction; import net.minecraft.world.gen.densityfunction.DensityFunctionTypes; +import org.jetbrains.annotations.NotNull; import org.spongepowered.asm.mixin.Final; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Shadow; +import org.spongepowered.asm.mixin.Unique; import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.ModifyArg; import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(ChunkNoiseSampler.class) -public class MixinChunkNoiseSampler implements IArrayCacheCapable, ICoordinatesFilling { +public abstract class MixinChunkNoiseSampler implements IArrayCacheCapable, ICoordinatesFilling { @Shadow @Final private int verticalCellBlockCount; @Shadow @Final private int horizontalCellBlockCount; @@ -22,6 +27,9 @@ public class MixinChunkNoiseSampler implements IArrayCacheCapable, ICoordinatesF @Shadow private int startBlockX; @Shadow private int startBlockZ; @Shadow private boolean isInInterpolationLoop; + + @Shadow public abstract Blender getBlender(); + private final ArrayCache c2me$arrayCache = new ArrayCache(); @Override @@ -55,4 +63,20 @@ private void protectInterpolationLoop(DensityFunction function, CallbackInfoRetu throw new IllegalStateException("Cannot create more wrapping during interpolation loop"); } } + + @ModifyArg(method = "", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/gen/noise/NoiseRouter;apply(Lnet/minecraft/world/gen/densityfunction/DensityFunction$DensityFunctionVisitor;)Lnet/minecraft/world/gen/noise/NoiseRouter;")) + private DensityFunction.DensityFunctionVisitor modifyVisitor1(DensityFunction.DensityFunctionVisitor visitor) { + return c2me$getDelegatingBlendingAwareVisitor(visitor); + } + + @Unique + private @NotNull DelegatingBlendingAwareVisitor c2me$getDelegatingBlendingAwareVisitor(DensityFunction.DensityFunctionVisitor visitor) { + return new DelegatingBlendingAwareVisitor(visitor, this.getBlender() != Blender.getNoBlending()); + } + + @ModifyArg(method = {"", "createMultiNoiseSampler"}, at = @At(value = "INVOKE", target = "Lnet/minecraft/world/gen/densityfunction/DensityFunction;apply(Lnet/minecraft/world/gen/densityfunction/DensityFunction$DensityFunctionVisitor;)Lnet/minecraft/world/gen/densityfunction/DensityFunction;"), require = 7, expect = 7) + private DensityFunction.DensityFunctionVisitor modifyVisitor2(DensityFunction.DensityFunctionVisitor visitor) { + return c2me$getDelegatingBlendingAwareVisitor(visitor); + } + }