感谢所有帮助我!我现在将更详细。我想要做的是一个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");
        }
    }
}

最佳答案

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();
                }
        }
}


然后,创建该可运行对象,并以args形式提供回调方法/ props,并将其提供给调度程序以在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;
        }
    }
}

10-06 05:29