From db5bf4dfcf58a0ac578b173377148bc5d24fb8e5 Mon Sep 17 00:00:00 2001 From: Li Junyu Date: Tue, 26 Dec 2023 16:56:17 +0800 Subject: [PATCH 1/2] Update to let non-KJS block can be ticked --- .../latvian/mods/kubejs/core/BlockKJS.java | 6 +++++ .../mixin/common/BlockBehaviourMixin.java | 25 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/common/src/main/java/dev/latvian/mods/kubejs/core/BlockKJS.java b/common/src/main/java/dev/latvian/mods/kubejs/core/BlockKJS.java index d537049c6..a57a4dea2 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/core/BlockKJS.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/core/BlockKJS.java @@ -1,6 +1,7 @@ package dev.latvian.mods.kubejs.core; import dev.latvian.mods.kubejs.block.BlockBuilder; +import dev.latvian.mods.kubejs.block.RandomTickCallbackJS; import dev.latvian.mods.kubejs.util.UtilsJS; import dev.latvian.mods.rhino.util.RemapPrefixForJS; import net.minecraft.nbt.CompoundTag; @@ -10,6 +11,7 @@ import net.minecraft.world.level.block.state.BlockState; import java.util.List; +import java.util.function.Consumer; @RemapPrefixForJS("kjs$") public interface BlockKJS extends BlockBuilderProvider { @@ -45,6 +47,10 @@ public interface BlockKJS extends BlockBuilderProvider { throw new NoMixinException(); } + default void kjs$setRandomTickCallback(Consumer callback) { + throw new NoMixinException(); + } + default void kjs$setSoundType(SoundType v) { throw new NoMixinException(); } diff --git a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/BlockBehaviourMixin.java b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/BlockBehaviourMixin.java index b4d774cd4..b42966341 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/BlockBehaviourMixin.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/BlockBehaviourMixin.java @@ -1,19 +1,30 @@ package dev.latvian.mods.kubejs.core.mixin.common; import dev.latvian.mods.kubejs.block.BlockBuilder; +import dev.latvian.mods.kubejs.block.RandomTickCallbackJS; import dev.latvian.mods.kubejs.core.BlockKJS; +import dev.latvian.mods.kubejs.level.BlockContainerJS; import dev.latvian.mods.kubejs.registry.RegistryInfo; import dev.latvian.mods.kubejs.util.UtilsJS; import dev.latvian.mods.rhino.util.RemapPrefixForJS; +import net.minecraft.core.BlockPos; import net.minecraft.nbt.CompoundTag; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.util.RandomSource; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.SoundType; import net.minecraft.world.level.block.state.BlockBehaviour; +import net.minecraft.world.level.block.state.BlockState; import org.jetbrains.annotations.Nullable; import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mutable; import org.spongepowered.asm.mixin.gen.Accessor; +import org.spongepowered.asm.mixin.injection.At; +import org.spongepowered.asm.mixin.injection.Inject; +import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; + +import java.util.function.Consumer; @Mixin(BlockBehaviour.class) @RemapPrefixForJS("kjs$") @@ -22,6 +33,7 @@ public abstract class BlockBehaviourMixin implements BlockKJS { private CompoundTag kjs$typeData; private ResourceLocation kjs$id; private String kjs$idString; + private Consumer kjs$randomTickCallback; @Override public ResourceLocation kjs$getIdLocation() { @@ -66,6 +78,19 @@ public abstract class BlockBehaviourMixin implements BlockKJS { return kjs$typeData; } + @Override + public void kjs$setRandomTickCallback(Consumer callback) { + this.kjs$randomTickCallback = callback; + } + + @Inject(method = "randomTick", at = @At("HEAD"), cancellable = true) + private void onRandomTick(BlockState blockState, ServerLevel serverLevel, BlockPos blockPos, RandomSource randomSource, CallbackInfo ci) { + if (kjs$randomTickCallback != null) { + kjs$randomTickCallback.accept(new RandomTickCallbackJS(new BlockContainerJS(serverLevel, blockPos), randomSource)); + ci.cancel(); + } + } + @Override @Accessor("hasCollision") @Mutable From 6f235f5bd0f97652ea9728e37ef54eb6a44d2ffc Mon Sep 17 00:00:00 2001 From: Li Junyu Date: Tue, 26 Dec 2023 17:02:57 +0800 Subject: [PATCH 2/2] Ensure callback is ticked --- .../mods/kubejs/core/mixin/common/BlockBehaviourMixin.java | 1 + 1 file changed, 1 insertion(+) diff --git a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/BlockBehaviourMixin.java b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/BlockBehaviourMixin.java index b42966341..d72750f89 100644 --- a/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/BlockBehaviourMixin.java +++ b/common/src/main/java/dev/latvian/mods/kubejs/core/mixin/common/BlockBehaviourMixin.java @@ -80,6 +80,7 @@ public abstract class BlockBehaviourMixin implements BlockKJS { @Override public void kjs$setRandomTickCallback(Consumer callback) { + kjs$setIsRandomlyTicking(true); this.kjs$randomTickCallback = callback; }