LimboManager/src/main/java/wtf/beatrice/limbomanager/listeners/PlayerTeleporter.java

78 lines
2.3 KiB
Java

package wtf.beatrice.limbomanager.listeners;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import wtf.beatrice.limbomanager.Cache;
import wtf.beatrice.limbomanager.objects.Coordinates;
public class PlayerTeleporter implements Listener
{
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event)
{
Player player = event.getPlayer();
String playerName = player.getName();
Coordinates islandCoords = calcNewCoordinates();
Cache.playerIslands.put(playerName, islandCoords);
Location islandLocation = new Location(player.getWorld(), islandCoords.getX(), 64, islandCoords.getZ());
islandLocation.getBlock().setType(Material.BEDROCK);
islandLocation.setY(islandLocation.getY() + 2);
player.teleport(islandLocation);
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event)
{
String playerName = event.getPlayer().getName();
Cache.playerIslands.remove(playerName);
}
private Coordinates calcNewCoordinates() {
/*
how are islands laid out?
we start from (1000, 1000) and we put islands at 500 blocks distance up to 10000. then we move to a new row.
*/
Coordinates islandCoords = new Coordinates(1000, 1000);
for(Coordinates currentCoords : Cache.playerIslands.values()) {
// if the two are not the same, it means we found a free spot.
// we can thus quit the loop and keep the current coords as valid.
if(!islandCoords.equals(currentCoords)) break;
// else, if they are the same,
// we have to either increase X or move to a new row, in case it's over 10000.
if(islandCoords.getX() >= 10000) // if we need to create a new row
{
islandCoords.setX(1000);
islandCoords.setZ(islandCoords.getZ() + 500);
} else { // if we just need to increase the column
islandCoords.setX(islandCoords.getX() + 500);
}
}
return islandCoords;
}
}
/*
player joins -> we look for the first free space -> we add it to the list and generate the island
*/