Player.sendMessage()发送两次

时间:2015-01-02 06:21:00

标签: java bukkit

我正在制作 Bukkit插件,此插件是帐户插件,此处理播放器时的事件登录。由于某种原因, BlockPlaceEvent 会阻止事件发生,如果播放器没有登录到他的服务器帐户并向他发送消息。一切都被正确解雇了。但出于某种原因,它给了他两次信息。

代码:

@EventHandler
public void OnPlayerQuit(PlayerQuitEvent PQE) {
    // Case the PQE Entity to a player.
    Player Player = (Player) PQE.getPlayer();

    // Get the players UUID to open the file needed
    String UserUUID = Player.getUniqueId().toString();

    // Open the file and read it
    File Directory = new File("./plugins/ServerUtils/ServerAccounts/" + UserUUID + ".json");
    if(Directory.exists()) {
        try {
            JSONObject NewFinalResult = new JSONObject(this.readFile(Directory));
            NewFinalResult.getJSONObject("info").put("loggedin", false);
            this.overWriteToFile(Directory, NewFinalResult.toString());
        }
        catch(Exception E) {
            E.printStackTrace();
        }
    }

}

@EventHandler
public void OnPlayerChat(PlayerChatEvent PCE) {
    Player Player = (Player) PCE.getPlayer();
    String UserUUID = Player.getUniqueId().toString();
    File Directory = new File(Directories.ServerAccounts + "/" + UserUUID + ".json");
    JSONObject UserFileJSON = new JSONObject(this.readFile(Directory));
    if(UserFileJSON.getJSONObject("info").getBoolean("loggedin") == false) {
        PCE.setCancelled(true);
        Player.sendMessage(ChatColor.RED + "You must be logged in to do this!");
        Player.sendMessage(ChatColor.RED + "To log in, simply type: " + ChatColor.BLUE + "\"/login <password>\"");
    }
}

@EventHandler
public void OnBlockBreak(BlockBreakEvent BBE) {
    Player Player = (Player) BBE.getPlayer();
    String UserUUID = Player.getUniqueId().toString();
    File Directory = new File(Directories.ServerAccounts + "/" + UserUUID + ".json");
    JSONObject UserFileJSON = new JSONObject(this.readFile(Directory));
    if(UserFileJSON.getJSONObject("info").getBoolean("loggedin") == false) {
        BBE.setCancelled(true);
        Player.sendMessage(ChatColor.RED + "You must be logged in to do this!");
        Player.sendMessage(ChatColor.RED + "To log in, simply type: " + ChatColor.BLUE + "\"/login <password>\"");
    }
}

@EventHandler
public void OnBlockPlace(BlockPlaceEvent BPE) {
    Player Player = (Player) BPE.getPlayer();
    String UserUUID = Player.getUniqueId().toString();
    File Directory = new File(Directories.ServerAccounts + "/" + UserUUID + ".json");
    JSONObject UserFileJSON = new JSONObject(this.readFile(Directory));
    if(UserFileJSON.getJSONObject("info").getBoolean("loggedin") == false) {
        BPE.setCancelled(true);
        Player.sendMessage(ChatColor.RED + "You must be logged in to do this!");
        Player.sendMessage(ChatColor.RED + "To log in, simply type: " + ChatColor.BLUE + "\"/login <password>\"");
    }
}

1 个答案:

答案 0 :(得分:1)

修正了它!由于某种原因,消息将发送两次,所以我添加了一个数字变量,确保发送的时间不是一次大,之后,它将其设置为0。 这是代码:

int TimesSent = 0;
@EventHandler
public void OnBlockPlace(BlockPlaceEvent BPE) {
    try {
        Player Player = (Player) BPE.getPlayer();
        String UserUUID = Player.getUniqueId().toString();
        File Directory = new File(Directories.ServerAccounts + "/" + UserUUID + ".json");
        JSONObject UserFileJSON = new JSONObject(this.readFile(Directory));
        if(UserFileJSON.getJSONObject("info").getBoolean("loggedin") == false) {
            BPE.setCancelled(true);
            if(TimesSent < 1) {
                Player.sendMessage(ChatColor.RED + "You must be logged in to place this block!");
                Player.sendMessage(ChatColor.RED + "To log in, simply type: " + ChatColor.BLUE + "\"/login <password>\"");
                TimesSent++;
            }
            else {
                TimesSent--;
            }
        }
    }
    catch(Exception E) {
        if(E instanceof FileNotFoundException) {
            //Ignore
        }
        else {
            E.printStackTrace();
        }
    }
}