1

所以...

我正在创建一个插件。

我有一个名为 Basics 的主类

我在全球范围内创建的基础知识:

static Timer enterdungeon = new Timer();
static Timer finddungeon = new Timer();
static Timer lootdungeon = new Timer();

我也有一个名为任务的类

enterdungeon 计时器是一个固定的时间段,使用时似乎可以按预期工作。你的lootdungeon计时器也是如此。

如果触发了基础事件,则可以中断 finddungeon 计时器。

该事件确实触发了该事件的第一行是: finddungeon.cancel();

在它启动lootdungeon计时器之后。

问题是 finddungeon 计时器没有取消,它继续运行,下面是任务类:

import java.util.TimerTask;
import me.boduzapho.Basics.DoWarp.Returner;
import org.bukkit.entity.Player;

public class task extends TimerTask
{
private final Player _player;
private final int ticks;
private int cnt = 0;
private final int _sec;
private final String _message;

public task(Player player, int sec, String message)
{
    this._player = player;
    this._sec = sec;
    this._message = message;
    this.ticks = sec;
}

private void timetoloot(Player p)
{

    p.sendMessage("SUCCESS! Nice Job, Enjoy the loot!");
    Returner loc1 = DoWarp.getwarp("launch", Basics.warps, Basics.wx,Basics.wy, Basics.wz, p);
    DoWarp.warpme(loc1.x, loc1.y, loc1.z, p, false, Basics.plugin);

}

private void failedwhiteblock(Player p)
{
    p.sendMessage("FAIL! You did not find the white block. Sending you back. TRY AGAIN!");
    Returner loc1 = DoWarp.getwarp("launch", Basics.warps, Basics.wx, Basics.wy, Basics.wz, p);
    DoWarp.warpme(loc1.x, loc1.y, loc1.z, p, false, Basics.plugin);

}

private void enterdungeon(Player p)
{
    Basics.Stage.setLine(3, "Off you Go!");
    Basics.Stage.update();
    Basics.Stage.setLine(0, "");
    Basics.Stage.setLine(1, "");
    Basics.Stage.setLine(2, "");
    Basics.Stage.setLine(3, "");
    Basics.Stage.update();

    Basics.cDoClear(p);
    Basics.cDoSpawners(p);
    Basics.cDoRed(p);
    Returner loc1 = DoWarp.getwarp("dstart", Basics.warps, Basics.wx, Basics.wy, Basics.wz, p);
    DoWarp.warpme(loc1.x, loc1.y, loc1.z, p, false, Basics.plugin);
    Basics.DungeonPlayer = p;
    p.sendMessage("Welcome to the Dungeon, you have 1 minuite to locate and click the white block.");
    p.sendMessage("If you fail you will be returned to spawn. If you find it the treasures will be revieled");
    p.sendMessage("and the monsters banished for 1 min so you can loot the chests! After which you will");
    p.sendMessage("Be warped back to spawn with your Loot!");
    Basics.finddungeon.schedule(new task(_player, 30, "Time left to find the WHITE block :"), 0, 1000);
    Basics.enterdungeon.cancel();
}

@Override
public void run()
{
    while (cnt < ticks)
    {
        try
        {
            Thread.sleep(1 * 1000);
            _player.sendMessage(_message + " " + Integer.toString(_sec - cnt));
            ++cnt;
        }
        catch (InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    _player.sendMessage("Done!");
    if (_message == "Time left:")
    {
        enterdungeon(_player);
    }

    if (_message == "Time left to find the WHITE block :")
    {
        failedwhiteblock(_player);
    }

    if (_message == "Time left to LOOT:")
    {
        timetoloot(_player);
    }

    //

    return;

}
}

这是在 Basics(主类)中调用的函数,它应该取消 finddungeon 计时器。

// white block in dungeon
        if (DungeonPlayer == player)
        {
            if ((block != null) && (block.getType() == Material.WOOL))
            {
                player.sendMessage("Canceling finddungeon from Basics");
                finddungeon.cancel();
                cDoClear(player);
                cDoChests(player);

                player.sendMessage("Congradulations! Time to Loot your rewards for finding the White Block!");
                Timer lootdungeon = new Timer();
                lootdungeon.schedule(new task(player, 10, "Time left to LOOT:"), 0, 1000);

                return;
                // ***
            }
        }

任何人都可以对此有所了解吗?

4

1 回答 1

1

原因 TimerTask.cancel 不对活动任务做任何事情,它只是清除调度程序。您必须重写取消方法,或者仅以此为起点:

class MyTimerTask extends TimerTask {
    private volatile Thread thread;

    @Override
    public void run() {
        thread = Thread.currentThread();
        //do your task and keep your eye on InterruptedException when doing Sleeps, Waits
        //also check Thread.interrupted()
    }

    public boolean cancel() {
        Thread thread = this.thread;
        if (thread != null) {
            thread.interrupt();
        }
        return super.cancel();
    }
}
于 2013-05-21T01:13:25.477 回答