Returning value after delay.

Discussion in 'Plugin Development' started by GeorgeeeHD, Feb 29, 2016.

Thread Status:
Not open for further replies.
  1. Offline

    GeorgeeeHD

    I am basically trying to make the following code work.

    Code:
        private int value;
      
        public int getInt()
        {
            task();
            return value;
        }
      
        public void task()
        {
            new BukkitRunnable()
            {
                @Override
                public void run()
                {
                    value = 1;
                }
            }.runTaskLater(this, 1);
        }
    This does not work as the getInt() method returns before the field "value" gets assigned an actual value. A workaround could be to separate the setting and getting into two methods and run the getting after a delay, but is it possible to do it in one method? Thanks for any help
     
  2. Offline

    DoggyCode™

    Can't you use Class.value; ?
     
  3. Offline

    HoeMC

    This will not work because BukkitRunnable creates a new thread; the variable value gets assigned after getInt() returns.

    You can either:

    1) Use Thread.Sleep() to suspend the whole thread before reassigning value.

    2) Give task() an integer return type and return value after performing required arithmetic, then return that in getInt().

    Is there a reason for your getInt() method though? All it does is make a method call then return a reassigned variable from that call. It seems redundant.
     
    Last edited: Mar 1, 2016
  4. Offline

    GeorgeeeHD

    No, this is just an example of what I want to do. In reality, this value is retrieved from another server, so there is latency. If I were to return instantly, it will not have been retrieved yet. So I need to return after s delay
     
  5. Offline

    DoggyCode™

    Async delayed task?
     
  6. Offline

    GeorgeeeHD

    @HoeMC
    This is just an example of what I want to do. Please see my last post
     
  7. Offline

    HoeMC

    Right OP, you really should explain what you want to achieve.

    Do you mean another minecraft server? A HTTP server? Are you retrieving data via sockets, POST, through BungeeCord or some other method?

    I don't see the need for you to add a delay, this should be accounted for by Java.

    Please include more detail of what you want to do and how you plan to do it.
     
  8. Offline

    mine-care

    I hope you are not talking about sleeping the main thread right?
    How are you retreiving this value? TCP/UDP connection? If you use one of them, there are already methods in them that will block untill the expected value is received. Still DONT do it on the main thread.
    As i said, that depends on the way you receive data from the other server. Can you explain the actual case, and what you are actually trying to do? it will help us provide an on-the-spot answer.
     
  9. Offline

    HoeMC

    I was under the assumption this was within another thread. He hasn't provided enough of his code though, so it was wrong of me to assume that.
     
    mine-care likes this.
  10. Offline

    GeorgeeeHD

    I am getting the value from another server using BungeeCord. There is a 50ms delay (1 tick) between calling the method and the value being received. I want to be able to request and return the data in the same method but that would mean using a delay. You cannot return a value from within a Bukkit task and I don't want to sleep the main thread as this is being run as a plugin on the server and this would lag the server. I seem to remember looking this up a while ago and seeing something to do with Callbacks. I've never used them before and tried finding it again and couldn't. I'm not sure if that is relevant.
     
  11. Offline

    mrCookieSlime

    Locked. We do not support Bungee as it requires Authentication to be disabled.
     
Thread Status:
Not open for further replies.

Share This Page