HELP ASYNC TASK IS BUG

Discussion in 'BungeeCord Plugin Help' started by VibeCraft, Mar 8, 2019.

  1. Hello, I'm having errors with this code below, can you help me?

    no block = true and
    blocked = verify.getName ()

    you get an error.
    Local variable blocked defined in an enclosing scope must be final or effectively final


    boolean block = false;
    String blocked = "";
    for (Verify verify : VERIFY_LIST) {
    if (block) {
    break;
    }

    ProxyServer.getInstance().getScheduler().runAsync(Proxy.plugin, () -> {
    boolean check = verify.verify(IP);
    if (check == true) {
    block = true;
    blocked = verify.getName();
    }
    });
    }
     
  2. try to use ProxyServer.getInstance().getScheduler().runAsync(Proxy.plugin, new Runnable() {
     
  3. You cannot use not final vatiables in your lambda expressions ;)
    Code (Java):
    /**
    * Thats just and expamle of code, but I'd
    * code smth like this.
    */

    public final class Auth {
        private boolean isBlocked;
     
        /** Very necessary javaDoc */
        public void setBlocked(final boolean isBlocked) {
            this.isBlocked = isBlocked;
        }
     
        /** Very necessary javaDoc */
        public boolean isBlocked() {
            return isBlocked;
        }

        // dunno which args u handle
        public void auth(final Object IP, final Object...args) {
            classes.runAsync(plugin, () -> {
                if (verify.verify(IP))
                    setBlocked(true);
         
                // or u can use smth like this
                setBlocked(verify.verify(IP));
            });
        }
    }
    /*------------------------------------------*
    * But u still shouldn't use non final      *
    * variables not in lambda scopes, cause    *
    * it can cause a lot of errors, especially *
    * in async (multithreading) case           *
    *------------------------------------------*/
     
    #3 NyanGuyMF, Mar 8, 2019
    Last edited: Mar 8, 2019
  4. that doesn't matter at all
     
  5. Things doesn't work async.

    Make them work sync.
     
  6. md_5

    Administrator Developer

    Bungee has no sync
     
    • Funny Funny x 1


  7. dont work, result returning false.

    Class Auth >

    public final class Auth implements Verify2 {

    private boolean isBlocked;

    public void setBlocked(final boolean isBlocked) {
    this.isBlocked = isBlocked;
    }

    public boolean isBlocked() {
    return isBlocked;
    }

    public void sync(String IP) {
    ProxyServer.getInstance().getScheduler().runAsync(Main.plugin, () -> {
    setBlocked(true);
    });
    }
    }

    CLASS FOR TEST THIS>

    verify_list.add(new Auth());

    for (Verify2 verify : verify_list) {
    System.out.println("starting");
    verify.sync("");

    if (verify.isBlocked()) {
    System.out.println("result is true");
    } else {
    System.out.println("result is false");
    }
    }

    VERIFY2 >
    public abstract interface Verify2 {

    public abstract void sync(final String IP);
    public abstract boolean isBlocked();
    }
     
  8. My code was just an example without testing, just edit it for your case :D
    Couldn't you explain the problem, which you want to solve and attach full version of class which is going to solve it?
    I cannot suggest a solution because of I cannot understand the problem :D (problem in this case isn't final field, I mean what generally thi code do?)
     
  9. basically the sync or verify method, check if the IP is proxy, querying a URL, and set whether it is proxy, or not.
     
  10. The most simple way to solve your problem with lambda is use Bukkit task for all this method, are you sure that you want to use async method there?
     
  11. I think so, query URLS results in lag .. even more that are a 5.
     
  12. Ive seen somewhere in the code that runAsync(Plugin, Runnable) does schedule(Plugin, Runnable, 0, TimeUnit.MILLISEONDS)
     

Share This Page