0

This is a Bukkit plugin, for Minecraft, using their API.

I'm trying to check if a player is in a specified location, and if they are, then don't teleport a player there. Instead, check another location, and spawn the player there if it's empty.

It may be better explained by the code below:

Player player = (Player) sender;
List< Player> prisoners = new ArrayList< Player>();
for (int i = 0; i < prisoners.size(); i++) {
    //loc(1-12) are coords in the form of a location variable. I just wanted to save some room.
    if (!(loc1 == prisoners.get(i))) {
        player.teleport(loc1);
    } else if (!(loc1 == prisoners.get(i).getLocation())) {
        player.teleport(loc2);
    } else if (!(loc2 == prisoners.get(i).getLocation())) {
        player.teleport(loc3);
    } else if (!(loc3 == prisoners.get(i).getLocation())) {
        player.teleport(loc4);
    } else if (!(loc4 == prisoners.get(i).getLocation())) {
        player.teleport(loc5);
    } else if (!(loc5 == prisoners.get(i).getLocation())) {
        player.teleport(loc6);
    } else if (!(loc6 == prisoners.get(i).getLocation())) {
        player.teleport(loc7);
    } else if (!(loc7 == prisoners.get(i).getLocation())) {
        player.teleport(loc8);
    } else if (!(loc8 == prisoners.get(i).getLocation())) {
        player.teleport(loc9);
    } else if (!(loc9 == prisoners.get(i).getLocation())) {
        player.teleport(loc10);
    } else if (!(loc10 == prisoners.get(i).getLocation())) {
        player.teleport(loc11);
    } else if (!(loc11 == prisoners.get(i).getLocation())) {
        player.teleport(loc12);
    } else {
        player.sendMessage("Sorry, the Prisoner's team is full. Try joining the Guards, or wait until the next round.");
        player.teleport(lobby);
        prisoners.remove(player);
    }

I want to check each location, and teleport the player to the next available location by going through the list of players on the prisoner's team, and checking their location.

But, this doesn't seem to work, as I spawn in the same position, so how would I fix this issue?

I use virtual players to test with multiple players, instead of paying for accounts.

Thanks in advance!

4

1 回答 1

4

这个条件

!(loc1 == prisoners.get(i))

正如您所说的那样,它将始终评估为真loc1坐标并且prisoners.get(i)Player对象。这些永远不会相等,因此您的上述条件评估为

!(false)  // meaning true

所以循环的每次迭代都会执行player.teleport(loc1);

您需要使用.equalsnot == 并确保您正在比较相同类型的两个对象。

于 2013-09-03T20:23:12.560 回答