pausing before executing code

Discussion in 'Plugin Development' started by spoonikle, Mar 9, 2011.

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

    spoonikle

    OK, i am trying to optimize my code, I have been running into some issues with my old code.

    I was using java utility timer threads to delay the use of a method, but i am unable to get accurate player data this way, as the player variable is being changed to another players name sometimes. So in an effort to stop this variable cross pollination i am working on a rewrite of my completely amateur code.

    My question is this -

    How do I delay the execution of code within a method? Here is my current solution -
    Code:
        public void onPlayerPickupItem(PlayerPickupItemEvent event) {
            Player player = event.getPlayer();
            CraftEntity fish = (CraftEntity) event.getItem();
            int fishId = ((EntityItem) fish.getHandle()).a.id;
            if (fishId == 349) {
                caughtFish.put(player, true);
                try {
                    Thread.sleep(2000); // do nothing for 2000 miliseconds (2
                                        // second)
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                caughtFish.remove(player);
            }
        }
    }
    here i want a variable to have a short life, (2 seconds in this case).

    will this work? is there a better way?
     
  2. Offline

    xZise

    This won't work, as the server can't respond for 2 seconds (so if somebody break a block it will change after 2 seconds).

    You should use a Timer which won't pause the server.

    Fabian
     
  3. Offline

    spoonikle

    this would pause bukkit? wow... my original code used a timer... but i think there is a problem with this code.

    Code:
    public class CatchFish extends PlayerListener {
    
        private boolean caughtFish = false;
    
        public void setCaughtFish(boolean caughtFish) {
            this.caughtFish = caughtFish;
        }
    
        public void onPlayerPickupItem(PlayerPickupItemEvent event) {
            CraftEntity fish = (CraftEntity) event.getItem();
            int fishId = ((EntityItem) fish.getHandle()).a.id;
            if (fishId == 349) {
                caughtFish = true;
                new CatchReset(2); // resets Catch fish value.
            }
        }
    
        public boolean isCaughtFish() {
            return caughtFish;
        }
    
        public class CatchReset {
            Timer timer;
    
            public CatchReset(int seconds) {
                timer = new Timer();
                timer.schedule(new RemindTask(), seconds * 1000);
            }
    
            // Declaration of the timer and its method.
            class RemindTask extends TimerTask {
                public void run() {
                    setCaughtFish(false);
                    timer.cancel();
                }
            }
        }
    }
    
    should i make caughtFish Static?
     
  4. Offline

    Edward Hand

  5. Offline

    Bubby4j

    Hey just a idea... is it possible to have a plugin run in a different thread?
     
  6. Offline

    Cheesier

    An entire plugin ? I'm not sure but it should, and its not safe running it in another thread, that can corrupt data.
     
  7. Offline

    darknesschaos

    yeah, it doesn't seem too wise unless A) you know what you are doing, and B)If your plugin actually needs it.
     
Thread Status:
Not open for further replies.

Share This Page