#Making an event wait for a task

1 messages ยท Page 1 of 1 (latest)

lyric herald
#

Hey, do you guys have any idea on how could I make an event wait for a task to complete?
I mean, you have event A, which has a method "awaitForCompletion", that method passes a suplier as argument, which provides with "EventTaskResult".

How could I make wait the event for the task to complete?
I currently have this

public class StaffSwitchStatusEvent implements StaffEvent {

    private final StaffMember member;
    private boolean cancelled = false;

    private final Lock lock = new ReentrantLock();
    private final Condition eventCompleted = lock.newCondition();


    public StaffSwitchStatusEvent(final StaffMember member) {
        this.member = member;
    }

    /**
     * Get the staff member
     *
     * @return the staff member
     */
    public StaffMember getMember() {
        return member;
    }

    /**
     * Await for an action to complete
     *
     * @param wait the wait action
     * @throws InterruptedException if the asynchronous waiter fails
     * to wait
     */
    public void awaitFor(AwaitEventResult wait) throws InterruptedException {
        lock.lock();
        try {
            // If the event has already completed, return immediately
            if (cancelled) {
                return;
            }

            while (!wait.isComplete() && !cancelled) {
                eventCompleted.await();
            }

            if (!cancelled) eventCompleted.signalAll();
        } finally {
            lock.unlock();
        }
    }

    /**
     * Get if the event is cancelled
     *
     * @return the event status
     */
    @Override
    public boolean isCancelled() {
        return cancelled;
    }

    /**
     * Set if the event is cancelled
     *
     * @param cancel the cancel status
     */
    @Override
    public void setCancelled(final boolean cancel) {
        cancelled = cancel;
    }
}
#

I've already think in using CompletableFuture, but it wouldn't work on my end implementation

sudden nacelle
#

Look into bukkit runnable and complete able futures

lyric herald
# sudden nacelle Look into bukkit runnable and complete able futures

I've already did it. I can't use them as CompletableFuture will break the end event implementation. It has cancellable statements, wich aims to cancel the event from where they are being called. I mean, someone must be able to listen to the event, and run the method "awaitFor", when that happens, how am I suposed to handle the cancel statements on a completable future? I have no control over the "AwaitEventResult" class nor a CompletableFuture if I make the method return a completable future

sudden nacelle
#

What's the end reasoning for this

#

IN dummy terms what does this do if workingm

#

The whole thing not just the waiting

lyric herald
#

It's an event that is fired after a command is run, if the event is cancelled, the command will not run.
If someone runs the awaitFor on that event, how would I know it? CompletableFuture doesn't help me there, because I am not the one running the awaitFor, it would be an external application.

That's why I am using lock and waits

#

AwaitEventResult is an abstract class, with two methods

complete(StaffEvent) (void) and isComplete (boolean)

#
/**
 * Awaited event result
 */
public abstract class AwaitEventResult {

    /**
     * Complete the event
     *
     * @param event the event
     */
    public abstract void complete(final StaffEvent event);

    /**
     * Get if the event is completed
     *
     * @return if the event is completed
     */
    public abstract boolean isComplete();
}
sudden nacelle
#

Why is this being done? Some sort of staff command checker or?

lyric herald
# sudden nacelle Why is this being done? Some sort of staff command checker or?

Yes but no, it's a command that toggles "staff" mode, like change name and everything to keep staff member anonymous.
If there's a plugin that wants to add for example a password step before an staff being able to turn in staff mode, they should be able to run the "awaitFor" method on the StaffSwitchStatusEvent, and wait for the staff to pass the authenticatio and proceed, or fails it and cancels the event

#

That's what the "complete" should do. Also implementation should make "isComplete" return true after complete is run

sudden nacelle
#

Anyone who can see the console would see the password regardless of method of input

#

I am a bit confused why you are not just using command perms though

lyric herald
lyric herald
sudden nacelle
# lyric herald It's just an example, that would be handled by the external application

Seems a bit odd but fair. Easy solution is have your commands all require X permission. But X permission is not given until you log into the external app.

Don't have an await for or anything.

Command one : if perm is needed, call the external app (command ends but sends the args to the external apps object).

External app: login, once logged in it sends that command again but as the console

lyric herald
sudden nacelle
#

Mhm

#

Smarter not harder and all that haha

lyric herald
#

๐Ÿ˜‚

#

Thx for the help btw

sudden nacelle
#

Word to the wise, if it sounds overcomplicated, you've screwed up

#

Code is beautifully, amazing simple

#

Sometimes u gotta take a step back at ya 20000 line monstrosity and go "this could of been done in 5 lines"

lyric herald
#

Complex code makes you feel a better programmer ๐Ÿ˜”

#

But what makes you a good programmer is actually doing simple and readable code

sudden nacelle
#

If u wanna see a great example of "you have over complicated this" go look at the xray plugin addon for core protect

#

Man writes the same command like 10+ times for each Ore, when a hashmap and for loop woulda done just fine lmao

lyric herald
sudden nacelle
#

*100

sudden nacelle
lyric herald
#

Do you mean this?

sudden nacelle
#

I don't shit on people's code but the chap is actually used now as an example for me when explaining why "longer doesn't mean better"

sudden nacelle
lyric herald
#

๐Ÿ˜‚

#

If you want to still have eyes, don't have a look at this

sudden nacelle
#

Oh trust me I've seen 3/4 of the spiggot community I don't need to haha

lyric herald
#

Compare both source codes, you will see the difference

#

It feels good to see how you are evolving

lyric herald
sudden nacelle
#

But yeah im on phone so can't give a huge take but:

-My brother in christ learn about guard clauses

-Try and make ur main sub 50 and wack a crap tonne of the same tests and checks into its own class called at onEnable.

lyric herald
#

I personally don't aim to teach them, it's better to make them learn by themselves. If they really like it, they will keep learning.
If you teach them, they will ask everything and won't actually learn

sudden nacelle
lyric herald
#

I guess spigot community is bigger than I though

sudden nacelle
#

Everyone has hobbies, even lecturer haha

lyric herald
#

I learned everything I know by myself ๐Ÿ˜ณ

sudden nacelle
#

I don't lecture java or coding. Won't say what though as identity and all that

lyric herald
#

Understable

#

I usually don't reveal any information neither ๐Ÿ˜‚

#

Only physical people I know should know who am I

sudden nacelle
#

Aye but anywho yeah guard clauses. Make em ur new best friend and bonk those pyramids

#

If(something){
If (something) {
If (something) {
}
}
}

If(something){
return false }

If(something){
Return false}

If(something){
return false }

One is infinitely neater ๐Ÿ˜‰

lyric herald
#

return something; ๐Ÿค”

sudden nacelle
#

Null if its a guard clause

#

And isn't a boolean class

#

Guard clauses are to do ur checks in self contained checks that don't require nesting