Venuum Mods
Venuum Mods

Reputation: 23

How can I delay this specific line of code?

So I'm working on my Triggerbot for Minecraft. In order for it to bypass, I need to add a slight delay... I've done some research and tried some different things, but I can't seem to get anything to function, as if I use:

try {
    Thread.sleep(100);
} catch (InterruptedException TriggerDelay) {
    TriggerDelay.printStackTrace();
}

That essentially freezes the entire game, and not just the line of code I want to delay...

Here's the specific section I need to delay, I left out the rest so kids can't skid my Triggerbot..

if(mc.objectMouseOver !=null) {
    if(mc.objectMouseOver.typeOfHit == MovingObjectType.ENTITY) {
        if(mc.objectMouseOver.entityHit instanceof EntityLivingBase) {
            // This is where I need help, I want to delay the following by 100ms...
            mc.thePlayer.swingItem();
            mc.thePlayer.sendQueue.addToSendQueue(new C02PacketUseEntity(mc.objectMouseOver.entityHit, C02PacketUseEntity.Action.ATTACK));

Upvotes: 1

Views: 245

Answers (3)

Andrei Mesh
Andrei Mesh

Reputation: 285

TimeUnit.seconds.sleep(int seconds); or TimeUnit.minutes.sleep(int minutes);

Read more about why this is the most easy-to-use recommended method to use instead of Thread.sleep(long miliseconds); here: http://javarevisited.blogspot.ro/2012/11/What-is-timeunit-sleep-over-threadsleep.html

Upvotes: 0

Tibrogargan
Tibrogargan

Reputation: 4603

This is perhaps closer to what you need, but I'm not 100% sure minecraft will love you for it

if  ( mc.objectMouseOver != null
   && mc.objectMouseOver.typeOfHit == MovingObjectType.ENTITY
   && mc.objectMouseOver.entityHit instanceof EntityLivingBase) {
    (new Thread() {
        public void run() {
            try {
                Thread.sleep(100);
                mc.thePlayer.swingItem();
                mc.thePlayer.sendQueue.addToSendQueue(new C02PacketUseEntity(mc.objectMouseOver.entityHit, C02PacketUseEntity.Action.ATTACK));
            } catch (InterruptedException ex) {
                return;
            }
        }
    }).start();
}

Upvotes: 0

fge
fge

Reputation: 121780

You need two things:

For the first point, look at the Executors class, which gives the ability to create them; as for the second point, ensure that your Runnables have all the necessary data to perform the task at hand.

And that's it, really.

One important thing to consider and which is not obvious is that both ScheduledExecutorService and Runnable define only the behaviour but do not have the authority, nor do they have the intent, of defining the state.

The Executors class provides you with ways to create ScheduledExecutorServices whose state is managed for you; but the Runnables you submit to them are yours to define, state included.

Upvotes: 1

Related Questions