#how do i add a 1 tick delay into my code

15 messages · Page 1 of 1 (latest)

crisp pollen
#

Depends on if you're overriding a method or mixing in.

#

To modify vanilla behaviour.

#

Create a boolean variable and turn it off after one tick

#

You can create it as a field in the class.

#

then you increment the ticks by one when you tick

#

Then, obviously you turn your boolean off

#

That boolean controls whether to run the code or not.

#

As an example

    private long ticksUntilShoot = 30;
    private boolean shot = false;

    @Override
    public void tick() {
        if (this.ticksUntilShoot > 0L) {
            --this.ticksUntilShoot;
        } else {
            if (!this.shot) {
                this.shoot();
                this.shot = true;

                this.playSound(SoundEvents.ENTITY_ENDER_DRAGON_GROWL, 2f, 1f);
            }
        }
    }
#

No, you asked for a way to make it run once and I gave you one

#

No, you got it backwards

#

And remove the first running = true

#

Just say if (running) -> then running = false

#

It would be easier to supply edits to your code if you use code blocks

#
public class AttackEntityHandler implements AttackEntityCallback {
    static boolean isInCallback = false;
    static int timesHit = 0;
    // I changed the name
    static boolean hasRun = false;
    @Override
    public ActionResult interact(PlayerEntity player, World world, Hand hand, Entity entity, @Nullable EntityHitResult hitResult) {
        if (isInCallback) {
            return ActionResult.PASS;
        }
        if (KeyInputHandler.renderingEnabled) {
            if(entity instanceof Sheep) {
                if (timesrun >= 1) {
                    timesrun=0;
                    isInCallback = true;
                    PlayerInventory inventory = player.getInventory();
                    inventory.selectedSlot = 1;
                    // Once hasRun is set to true this will negate to false
                    // which means it will only run once
                    if (!hasRun) {
                        ClientTickEvents.END_CLIENT_TICK.register(client -
                            inventory.selectedSlot = 0;
                            
                            hasRun = true;
                        });
                    }
                    isInCallback = false;
                } else {
                    timesrun += 1;
                }
            }
        }


        return ActionResult.PASS;
    }
}```
#

Each instance of the class will have it set to false by default. I don't know enough about what you're trying to achieve to provide any further advice on when or if you should set it back.