类型不匹配:无法从元素类型Object转换为Player

时间:2012-02-04 01:29:34

标签: java

这是代码。不确定什么是错的。发生在说

的那一行
for (Player players : World.getPlayers()) {
    if (cmd[0].equalsIgnoreCase("ticket")) {
        if (player.getRights() > 0) {
            if (World.getTicketList().size() == 0) {
                player.getPackets().sendGameMessage("No one needs help so sit back and relax.");
                return true;
            }
            String username = World.getTicketList().get(0).toString();
            Player p2 = World.getPlayer(username);
            player.setNextWorldTile(p2);
            p2.getPackets().sendGameMessage("Please ask your question to " + Misc.formatPlayerNameForDisplay(player.getUsername()) + ".");
            World.remoteTicket(username.toLowerCase());
            return true;
        }
        if (World.getTicketList().contains(player.getUsername().toLowerCase())) {
            player.getPackets().sendGameMessage("You already submitted a ticket.", true);
            return true;
        }
        World.addToTicket(player.getUsername());
        player.getPackets().sendGameMessage("Your ticket has been submitted. Your ticket number is " + World.getTicketId(player.getUsername()) + 1 + ".");
        player.getPackets().sendGameMessage("Make sure that you are not in a PvP area..");
        String username = World.getTicketList().get(0).toString();
        for (Player players : World.getPlayers()) {
            if (player.getRights() > 0) {
                players.getPackets().sendGameMessage(Misc.formatPlayerNameForDisplay(player.getUsername()) + " has issued a assistance request.", true);
            }
        }
        return true;
    }
}

编辑:

以下是getPlayers的代码

public static final EntityList getPlayers() {
    return players;
}

2 个答案:

答案 0 :(得分:0)

确保World.getPlayers()返回实现java.lang.Iterable

答案 1 :(得分:0)

问题出在for循环中。将其更改为

for (Object player : World.getPlayers()) {
     if (((Player)player).getRights() > 0)
         players.getPackets().sendGameMessage(Misc.formatPlayerNameForDisplay(player.getUsername())+" has issued a assistance request.", true);
     }
}

这应该有效。