#Thread.sleep() for serverside

5 messages · Page 1 of 1 (latest)

calm whale
#

Hi! im trying to make a countdown appear on screen using title packets however how do i make it so it waits a second between each packet

since thread.sleep sleeps the entire server 💀

hot rivet
#

Store the countdown somewhere and update it every tick

still quiver
#

!!timer

worthy duneBOT
#

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 relevant tick event similar to the following.
    public static class StuffTimer implements ServerTickEvents.EndTick {
        public static final StuffTimer INSTANCE = new StuffTimer();
        private long ticksUntilSomething;
        public void setTimer(long ticksUntilSomething) {
            this.ticksUntilSomething = ticksUntilSomething;
        }
        @Override
        public void onEndTick(MinecraftServer server) {
            if (--this.ticksUntilSomething == 0L) {
                doSomething();
                // If you want to repeat this, reset ticksUntilSomething here.
            }
        }
        public static void register() {
            ServerTickEvents.END_SERVER_TICK.register(INSTANCE);
        }
    }

Usage:

    StuffTimer.INSTANCE.setTimer(100L); // do something after 100 ticks
hot rivet
#

oh sweet I knew there was a command like that