#[1.21] Adding delay to block

11 messages · Page 1 of 1 (latest)

foggy mulchBOT
#

@robust light: Unknown tag, use !tag to see all available tags

#
Tag list

Post a tag with !!<tagName> and arguments as required.
Specifying the base name is enough when unique.

cheatsheet/dyes
cheatsheet/ggc
cheatsheet/jij
cheatsheet/llc
cheatsheet/maxvalue
cheatsheet/nbttypes
cheatsheet/sides
cheatsheet/signatures
cheatsheet/trees
cheatsheet/world
discord/communitydiscords
discord/cursedlegacyfabricord
discord/discord
discord/fabricord
discord/fabrictranslatorsguild
discord/fssd
discord/ftg
discord/jellycord
discord/legacyfabric
discord/spongecord

robust light
#

!!timer

foggy mulchBOT
#

Fabric itself does not include APIs to schedule something in the future.

DO NOT use threads or java.util.Timer. (This can cause a crash!) Instead:

  • If you are making a block do something in the future: world.scheduleBlockTick + override scheduledTick in your Block.
  • If you are making a custom tickable stuff (usually block entity/entity) do something in the future/periodically: see below, but instead of Mixin just implement yourself
  • If you are making a vanilla tickable thing (world, server, etc) do something in the future/periodically: use the following mixin.
@Mixin(StuffToTick.class) // ServerWorld, MinecraftServer, etc
public class StuffTimer implements StuffTimerAccess {
    @Unique
    private long ticksUntilSomething;

    @Inject(method = "tick", at = @At("TAIL"))
    private void onTick(CallbackInfo ci) { // Fix parameters as needed
        if (--this.ticksUntilSomething == 0L) {
            doSomething();
            // If you want to repeat this, reset ticksUntilSomething here.
        }
    }

    @Override
    public void yourmod_setTimer(long ticksUntilSomething) {
        this.ticksUntilSomething = ticksUntilSomething;
    }
}
public interface StuffTimerAccess {
    void yourmod_setTimer(long ticksUntilSomething);
}

Usage:

MinecraftServer server;
((StuffTimerAccess) server).yourmod_setTimer(100L); // do something after 100 ticks
robust light
#

have tried before, does not seem to work

robust light
#
public class CrumblingLimestoneBlock extends FallingBlock {
    public CrumblingLimestoneBlock(Settings settings) {
        super(settings);
    }

    @Override
    protected MapCodec<? extends FallingBlock> getCodec() {
        return null;
    }

    @Override
    public void onSteppedOn(World world, BlockPos pos, BlockState state, Entity entity) {
        if (!entity.bypassesSteppingEffects()) {
            world.scheduleBlockTick(pos, this, 100);
            if (canFallThrough(world.getBlockState(pos.down())) && pos.getY() >= world.getBottomY()) {
                FallingBlockEntity fallingBlockEntity = FallingBlockEntity.spawnFromBlock(world, pos, state);
                this.configureFallingBlockEntity(fallingBlockEntity);
            }
        }
        super.onSteppedOn(world, pos, state, entity);
    }


    @Override
    protected void scheduledTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
    }

    protected void configureFallingBlockEntity(FallingBlockEntity entity) {
    }
}
#

world.scheduleBlockTick(pos, this, 100); does not seem to work

tame sphinx
#

scheduleBlockTick makes it so that the method scheduledTick will be called on your block after the desired time.
It does NOT suspend the method and wait for the time to then continue the code right at the method call.
You currently have the falling behaviour still in the same method onSteppedOn right after you schedule the block tick -- you need to move the falling behaviour from there into the scheduledTick method you have overridden

robust light
#

scheduledTick seem to be called no matter

foggy mulchBOT
#

You can use codeblocks in discord as shown below:
```java
code
```

You can also specify the syntax highlighting to use by specifying the language after the last backtick (`) on the top.