#Any way to add a 1 tick delay? Do delay methods exist?

11 messages · Page 1 of 1 (latest)

mossy sail
#

!!timer

weary spadeBOT
#

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
mossy sail
#

long is a primitive data type?

#

it's not, it's used to store ticks in this case

#

!!learnjava

weary spadeBOT
#

To start modding Minecraft using Fabric, it's strongly recommended to know Java.
Minecraft, in its codebase, and mods, using Fabric API, use more advanced Java concepts like lambdas, generics and polymorphism.
If you don't know all of these concepts, you may have some difficulties modding.

Here are some online resources that will help learning Java
JetBrains Academy (free online course): https://www.jetbrains.com/academy/
Codecademy (free online course): https://www.codecademy.com/learn/learn-java
University of Helsinki (free online course): https://java-programming.mooc.fi/
Basic Java Tutorials: https://docs.oracle.com/javase/tutorial/
Introduction to Programming using Java by David J. Eck (free online textbook): http://math.hws.edu/javanotes/

After Learning Java
For most mods you will want to make, you will have to use the Spongepowered Mixin Java library.
To learn more about Mixin, you can use the faq/mixin bot tag by typing !!faq/mixin in #bot-posting .

mossy sail
#

yeah don't use threads

mossy sail
#

that's what the mixin does

turbid umbra
#

The game already keeps track of ticks.

#

You just set a variable and inc or dec it your desired amount to switch a boolean.