Implement Island layout calculator and teleporter
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
4524070618
commit
9005db7872
13
src/main/java/wtf/beatrice/limbomanager/Cache.java
Normal file
13
src/main/java/wtf/beatrice/limbomanager/Cache.java
Normal file
@ -0,0 +1,13 @@
|
||||
package wtf.beatrice.limbomanager;
|
||||
|
||||
import wtf.beatrice.limbomanager.objects.Coordinates;
|
||||
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Cache
|
||||
{
|
||||
|
||||
public static final HashMap<String, Coordinates> playerIslands = new HashMap<>();
|
||||
|
||||
|
||||
}
|
@ -5,6 +5,7 @@ import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import wtf.beatrice.limbomanager.listeners.CommandCanceller;
|
||||
import wtf.beatrice.limbomanager.listeners.PlayerHider;
|
||||
import wtf.beatrice.limbomanager.listeners.PlayerTeleporter;
|
||||
import wtf.beatrice.limbomanager.listeners.RiskyBlocksHandler;
|
||||
|
||||
public class LimboManager extends JavaPlugin {
|
||||
@ -19,6 +20,7 @@ public class LimboManager extends JavaPlugin {
|
||||
pluginManager = Bukkit.getServer().getPluginManager();
|
||||
|
||||
pluginManager.registerEvents(new PlayerHider(), this);
|
||||
pluginManager.registerEvents(new PlayerTeleporter(), this);
|
||||
pluginManager.registerEvents(new CommandCanceller(), this);
|
||||
pluginManager.registerEvents(new RiskyBlocksHandler(), this);
|
||||
|
||||
|
@ -0,0 +1,78 @@
|
||||
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
|
||||
|
||||
|
||||
*/
|
@ -0,0 +1,25 @@
|
||||
package wtf.beatrice.limbomanager.objects;
|
||||
|
||||
public class Coordinates
|
||||
{
|
||||
|
||||
private int x, z;
|
||||
|
||||
public Coordinates(int x, int z)
|
||||
{
|
||||
this.x = x;
|
||||
this.z = z;
|
||||
}
|
||||
|
||||
public void setX(int x) { this.x = x; }
|
||||
public void setZ(int z) { this.z = z; }
|
||||
|
||||
public int getX() { return x; }
|
||||
public int getZ() { return z; }
|
||||
|
||||
public boolean equals(Coordinates coordinates)
|
||||
{
|
||||
return x == coordinates.x && z == coordinates.z;
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user