Development Assistance Listener Scheduler Help

Discussion in 'Plugin Help/Development/Requests' started by Silverminer_HD, Mar 2, 2015.

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

    Silverminer_HD

    I have a Problem with this Listener:
    Code:
        @EventHandler
            public void CreateBoat(VehicleCreateEvent wevent){
            if(wevent.getVehicle().getType() == EntityType.BOAT){
                if(wevent.getVehicle().isEmpty()){
                    wevent.getVehicle().remove();
                }
            }
        }
    }
    I want to test, if a Boat is empty 5 sec. after Creating it... I think i should use
    a Scheduler! Now my problem! I don't know how to use a Scheduler in this Listener,
    because i never used Scheduler before!
    Thanks for the help and sorry for my bad english ;)

    If you need more Information's to help me, just say it!

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  2. Offline

    pie_flavor

    @Silverminer_HD It's good that you're coming here before you can learn a bad practice. It's really quite simple.
    Code:
    @EventHandler
    public void createBoat(VehicleCreateEvent wevent) {
        if (wevent.getVehicle().getType() == EntityType.BOAT) {
            new BukkitRunnable() {
                public void run() {
                    if (wevent.getVehicle().isEmpty()) {
                        wevent.getVehicle().remove();
                    }
                }
            }.runTaskLater(plugin, 100L);
        }
    }
    To run the task later, you just have to call runTaskLater(Plugin, long) on a BukkitRunnable.
    Code:
    new BukkitRunnable() {
    public void run() {
    This creates a new BukkitRunnable (which is abstract) that specifies a run() method like it has to. Really just a throwaway class. You can also create a class which extends BukkitRunnable and has a run() method, and just run it on an instance of that.
    If you would rather use a Runnable instead of a BukkitRunnable, you have to use this:
    Code:
    Bukkit.getScheduler().runTaskLater(Plugin, Runnable, long);
    Again, you can create a throwaway Runnable for this, or create a whole new class. An advantage to using getScheduler is you can use a lambda with it (if you know what that means, otherwise ignore it).
     
    Silverminer_HD likes this.
  3. Offline

    Silverminer_HD

    Thanks! Later when i get home i'll try this out!
     
Thread Status:
Not open for further replies.

Share This Page