Skip to content

Commit

Permalink
Merge pull request #762 from Prunoideae/2002-random-tick-modify
Browse files Browse the repository at this point in the history
Added `setRandomTickCallback` for block modification
  • Loading branch information
LatvianModder authored Dec 26, 2023
2 parents 6a9330b + 6f235f5 commit 270ba30
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -45,6 +47,10 @@ public interface BlockKJS extends BlockBuilderProvider {
throw new NoMixinException();
}

default void kjs$setRandomTickCallback(Consumer<RandomTickCallbackJS> callback) {
throw new NoMixinException();
}

default void kjs$setSoundType(SoundType v) {
throw new NoMixinException();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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$")
Expand All @@ -22,6 +33,7 @@ public abstract class BlockBehaviourMixin implements BlockKJS {
private CompoundTag kjs$typeData;
private ResourceLocation kjs$id;
private String kjs$idString;
private Consumer<RandomTickCallbackJS> kjs$randomTickCallback;

@Override
public ResourceLocation kjs$getIdLocation() {
Expand Down Expand Up @@ -66,6 +78,20 @@ public abstract class BlockBehaviourMixin implements BlockKJS {
return kjs$typeData;
}

@Override
public void kjs$setRandomTickCallback(Consumer<RandomTickCallbackJS> callback) {
kjs$setIsRandomlyTicking(true);
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
Expand Down

0 comments on commit 270ba30

Please sign in to comment.