#Flight disabling not working
10 messages · Page 1 of 1 (latest)
I need a way to have the flying disabled after 5 seconds without the added affect of adding 5 seconds onto the cooldown as well. I just, don't know how to do it.
package net.beez131.customlifestealweapons.item.custom;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraft.item.ToolMaterial;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
public class FlySwordItem extends SwordItem {
public FlySwordItem(ToolMaterial toolMaterial, Settings settings) {
super(toolMaterial, settings);
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
ItemStack stack = user.getStackInHand(hand);
if (!world.isClient()) {
// Enable flying if not on cooldown
if (!user.getItemCooldownManager().isCoolingDown(this)) {
user.getAbilities().allowFlying = true;
user.getAbilities().flying = true;
user.sendAbilitiesUpdate();
// Schedule disabling flight after 5 seconds (100 ticks)
world.getServer().getOverworld().getServer().execute(() -> {
try {
Thread.sleep(5000); // 5 seconds
user.getAbilities().allowFlying = false;
user.getAbilities().flying = false;
user.sendAbilitiesUpdate();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
// Set a cooldown of 10 seconds (200 ticks)
user.getItemCooldownManager().set(this, 100);
}
}
return TypedActionResult.success(stack, world.isClient());
}
}```
!!timer
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+ overridescheduledTickin yourBlock. - 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
ok thanks
ok i tried to, but it just doesn't disable flight at all:
import net.beez131.customlifestealweapons.util.FlightTimerAccess;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack;
import net.minecraft.item.SwordItem;
import net.minecraft.item.ToolMaterial;
import net.minecraft.util.ActionResult;
import net.minecraft.util.Hand;
import net.minecraft.util.TypedActionResult;
import net.minecraft.world.World;
public class FlySwordItem extends SwordItem {
public FlySwordItem(ToolMaterial toolMaterial, Settings settings) {
super(toolMaterial, settings);
}
@Override
public TypedActionResult<ItemStack> use(World world, PlayerEntity user, Hand hand) {
// Always define the stack here so it's accessible throughout the method
ItemStack stack = user.getStackInHand(hand);
if (!world.isClient()) {
user.getItemCooldownManager().set(this, 200);
if (!user.getAbilities().allowFlying) {
user.getAbilities().allowFlying = true;
user.getAbilities().flying = true;
user.sendAbilitiesUpdate();
if (!user.getItemCooldownManager().isCoolingDown(this)) {
// Schedule flight timer for 100 ticks (5 seconds)
if (world.getServer() != null && world.getServer().getOverworld() instanceof FlightTimerAccess timerAccess) {
timerAccess.customlifestealweapons_template_1_21_1$setFlightTimer(user, 100L);
} else {
System.err.println("Server instance is null or does not implement FlightTimerAccess!");
}
}
}
// Return success on the server side
return TypedActionResult.success(stack);
}
// Default return for client-side execution or other cases
return TypedActionResult.pass(stack);
}
}
Flight disabling not working
.