user2070394
user2070394

Reputation: 5

For Loop checking player locations

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!

Upvotes: 0

Views: 255

Answers (1)

Java Devil
Java Devil

Reputation: 10959

This condition

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

will always evaluate to true as loc1 as you say is a Co-ordinate and prisoners.get(i) is a Player object. These will never be equal so your above condition evaluates to

!(false)  // meaning true

So every iteration of the loop will execute player.teleport(loc1);

You need to use .equals not == and also make sure you are comparing two objects of the same type.

Upvotes: 4

Related Questions