scheduler and thread sleep

Discussion in 'Plugin Development' started by atesin, Jun 6, 2014.

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

    atesin

    hi ... i am developing a portal plugin (MyPortals), for that i had created 2 events: PortalEnterEvent and PortalLeaveEvent

    when PortalEnterEvent triggers i create a new BukkitScheduler task, that runs 4 seconns later and teleports player .. when PortalLeaveEvent triggers it cancels previous task

    i had many troubles in cancelling task, cancel() methods didn't work for me, also i had not been able to externally set an internal task property (boolean wereCanceled or something) once were scheduled, so run() could check at beginning

    so the technique i used is this, i know is not so elegant but is the only that worked after 1 week of hair pulling .. i have a HashMap warp with the player name and his teleport task id

    Code:java
    1. WarpTask warp = new WarpTask(this, event.player, event.dest, plugin.xpCost);
    2. warp.runTaskLater(plugin, 80);
    3. warps.put(event.player.getName(), warp.getTaskId());


    so warptask check if still are in the hashmap and runs

    Code:java
    1. if (!parent.warps.containsValue(this.getTaskId())) return;
    2. if (!dest.getBlock().getChunk().isLoaded()) dest.getBlock().getChunk().load();
    3. player.teleport(dest);


    when player leaves the portal and PortalLeaveEvent is triggered, it deletes that entry from the hashMap so if run() method does not find it does nothing

    Code:java
    1. warps.remove(event.player.getName());


    i dont know why i can't call cancel() externally nor internally .. i know cancel() never are done because player leaves portal and gets teleported while walking

    until it works, i want to do it right .. what i am doing wrong?

    --

    other problem .. sometimes it seems chunk are not fully loaded because player gets teleported falling in the air and suddenly gets buried
     
  2. Offline

    CubieX

    You have to save the taskID and the player name to a hashmap. So so far your implementation is OK.
    To cancel the task, you can do:
    Code:
    Bukkit.getServer().getScheduler().cancelTask(taskID)
    within your PortalLeaveEvent.
    And delete the player from the map in PortalEnterEvent and PortalLeaveEvent.
     
Thread Status:
Not open for further replies.

Share This Page