如何在60秒后在bukkit中发送消息?

时间:2013-09-12 19:22:40

标签: java bukkit

感谢所有帮助我的人!我现在会更详细。我想要做的是一个Bukkit插件,一分钟之后,如果播放器掉了东西,控制台会显示一条信息消息,比如“玩家正在移动”,但我可以让第一条消息显示出来:“玩家掉了东西”我认为错误在我使用的布尔值上。拜托,任何人都可以帮助我使用bukkit吗?这是我的代码:

public class HgCake extends JavaPlugin implements Listener{
    boolean reference = false;
    @Override
    public void onEnable() {
        Bukkit.getServer().getPluginManager().registerEvents(this, this);
    }

    @EventHandler
    public void onDropItem (PlayerDropItemEvent e) {
        getLogger().info("Player dropped something");
        reference = true;
    }

    public void onPlayerMove (PlayerMoveEvent e){
        if (reference = true){
            getLogger().info("Players are moving");
        }
    }
}

3 个答案:

答案 0 :(得分:2)

Bukkit有一个内置的调度系统,您可以阅读 Scheduler Programming

使用它而不是普通的Java计时器,相信我。从长远来看,它会让你的生活更轻松。

要执行您想要执行的操作,您需要BukkitRunnable课程才能提供给调度程序。

这是一个我为了示例目的而过度简化的通用词:

public class Callback extends BukkitRunnable{
        private Object targetObject;
        public Method targetMethod;
        private Object[] perameters;

        public Callback(Object targetObject, String methodName, Object[] argsOrNull){
                try {
                    this.targetMethod = targetObject.getClass().getMethod(methodName, (Class<?>[]) argsOrNull);
                } catch (Exception e){
                    e.printStackTrace();
                }
                this.targetObject = targetObject;
                this.perameters = argsOrNull;
        }

        public void run(){
                try {
                    this.targetMethod.invoke(this.targetObject,perameters);
                } catch (Exception e){
                    e.printStackTrace();
                }
        }
}

然后你创建一个runnable的对象,将回调方法/ props作为args提供给调度程序,使其在60秒内运行:

对于运动部分,您只需在物品掉落且没有人移动的情况下观察它。

public class DropWatcher implements Listener {
    private Boolean hasAnythingMoved;
    private Boolean dropped;
    private Pwncraft plugin;
    private Player player;

    public DropWatcher(Pwncraft plugin, Player player){
        this.player = player;
        this.hasAnythingMoved = false;
        this.dropped = false;
        this.plugin = plugin;
        this.plugin.pluginManager.registerEvents(this, plugin);
    }

    //Drop event listener: When the player drops an item, it sets dropped to true, and initiates the countdown.
    @EventHandler
    public void onDropItem (PlayerDropItemEvent e) {
        if(e.getPlayer().equals(this.player) && !this.dropped){
            this.dropped = true;
            BukkitCallbackTask doInSixtySeconds = new BukkitCallbackTask(this, "timesUp" , null);
            doInSixtySeconds.runTaskLater(plugin, 1200); // time is in ticks (20 ticks +/- = 1 sec), so 1200 ticks = 1 min.
        }
    }

    //Watches for other-players' movement, and sets hasAnythingMoved to true if so.
    @EventHandler
    public void onMove (PlayerMoveEvent e){
        if(!e.getPlayer().equals(this.player) && this.dropped && !this.hasAnythingMoved){
            this.hasAnythingMoved = true;
        }
    }

    /*
    This is the method the runnable calls when the timer is up.
    It checks for movement, and if so, sends a message and explodes the player 
    (Just because it can. You're welcome to veto the explosion.) 
    */
    public void timesUp(){

        if(this.hasAnythingMoved){
            this.player.sendMessage("Someone moved! Time to party!");
            this.player.getWorld().createExplosion(this.player.getLocation(), 5F);
            this.dropped = false;
            this.hasAnythingMoved = false;
        }
    }
}

答案 1 :(得分:0)

我忘记了我认为的PlayerMoveEvent的@EventHandler

应该是:

public class HgCake extends JavaPlugin implements Listener{
boolean reference = false;
@Override
public void onEnable() {
    Bukkit.getServer().getPluginManager().registerEvents(this, this);
}

@EventHandler
public void onDropItem (PlayerDropItemEvent e) {
    getLogger().info("Player dropped something");
    reference = true;
}

@EventHandler
public void onPlayerMove (PlayerMoveEvent e){
    if (reference = true){
        getLogger().info("Players are moving");
    }
}

}

答案 2 :(得分:0)

    int plannedActivity = getServer().getScheduler().runTaskLaterAsynchronously(this, new Runnable() {

        public void run() {

            //whatever you want to do

        }

    }, 120L);