2022-11-11 21:16:26 +01:00
|
|
|
package wtf.beatrice.limbomanager.listeners;
|
|
|
|
|
2022-11-11 22:50:42 +01:00
|
|
|
import com.sk89q.worldedit.EditSession;
|
|
|
|
import com.sk89q.worldedit.WorldEdit;
|
|
|
|
import com.sk89q.worldedit.WorldEditException;
|
|
|
|
import com.sk89q.worldedit.bukkit.BukkitAdapter;
|
|
|
|
import com.sk89q.worldedit.extent.clipboard.Clipboard;
|
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormat;
|
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardFormats;
|
|
|
|
import com.sk89q.worldedit.extent.clipboard.io.ClipboardReader;
|
|
|
|
import com.sk89q.worldedit.function.operation.Operation;
|
|
|
|
import com.sk89q.worldedit.function.operation.Operations;
|
|
|
|
import com.sk89q.worldedit.math.BlockVector3;
|
|
|
|
import com.sk89q.worldedit.session.ClipboardHolder;
|
|
|
|
import com.sk89q.worldedit.world.World;
|
2022-11-11 21:16:26 +01:00
|
|
|
import org.bukkit.Location;
|
|
|
|
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;
|
2022-11-11 22:50:42 +01:00
|
|
|
import wtf.beatrice.limbomanager.LimboManager;
|
2022-11-11 21:16:26 +01:00
|
|
|
import wtf.beatrice.limbomanager.objects.Coordinates;
|
2022-11-12 01:12:45 +01:00
|
|
|
import wtf.beatrice.limbomanager.utils.LocationUtils;
|
2022-11-11 21:16:26 +01:00
|
|
|
|
2022-11-11 22:50:42 +01:00
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.io.IOException;
|
|
|
|
|
2022-11-11 21:16:26 +01:00
|
|
|
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());
|
|
|
|
|
2022-11-11 22:50:42 +01:00
|
|
|
// load chunk otherwise it will be buggy (no schem load, no tp)
|
|
|
|
islandLocation.getWorld().loadChunk(islandLocation.getChunk().getX(), islandLocation.getChunk().getZ(), true);
|
|
|
|
|
|
|
|
|
|
|
|
String testSchematicPath = LimboManager.getSchematicsFolder().getAbsolutePath() + File.separator + "endcityship.schem";
|
|
|
|
File schematicFile = new File(testSchematicPath);
|
|
|
|
|
|
|
|
if(!schematicFile.exists())
|
|
|
|
System.out.println("no exist");
|
|
|
|
|
|
|
|
// todo: check if exists, but this is just a test
|
|
|
|
|
|
|
|
World islandWEWorld = BukkitAdapter.adapt(islandLocation.getWorld());
|
|
|
|
Clipboard shematicClipboard;
|
|
|
|
|
|
|
|
ClipboardFormat clipboardFormat = ClipboardFormats.findByFile(schematicFile);
|
|
|
|
try (ClipboardReader reader = clipboardFormat.getReader(new FileInputStream(schematicFile))) {
|
|
|
|
shematicClipboard = reader.read();
|
|
|
|
|
|
|
|
try (EditSession editSession = WorldEdit.getInstance().newEditSession(islandWEWorld)) {
|
|
|
|
Operation operation = new ClipboardHolder(shematicClipboard)
|
|
|
|
.createPaste(editSession)
|
|
|
|
.to(BlockVector3.at(islandLocation.getX(), 64, islandLocation.getZ()))
|
|
|
|
// configure here
|
|
|
|
.build();
|
|
|
|
Operations.complete(operation);
|
|
|
|
editSession.flushSession();
|
|
|
|
} catch (WorldEditException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
|
|
|
throw new RuntimeException(e);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-11-12 01:12:45 +01:00
|
|
|
LocationUtils.teleportToOwnIsland(player);
|
2022-11-11 21:16:26 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@EventHandler
|
|
|
|
public void onPlayerQuit(PlayerQuitEvent event)
|
|
|
|
{
|
|
|
|
String playerName = event.getPlayer().getName();
|
|
|
|
|
|
|
|
Cache.playerIslands.remove(playerName);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private Coordinates calcNewCoordinates() {
|
|
|
|
|
|
|
|
/*
|
|
|
|
how are islands laid out?
|
2022-11-12 01:50:27 +01:00
|
|
|
we start from (1000, 1000) and we put islands at <Cache.islandsDistance> blocks distance up to 10000. then we move to a new row.
|
2022-11-11 21:16:26 +01:00
|
|
|
*/
|
|
|
|
|
2022-11-12 01:12:45 +01:00
|
|
|
Coordinates islandCoords = new Coordinates(Cache.baseCoords);
|
2022-11-11 21:16:26 +01:00
|
|
|
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,
|
2022-11-12 01:50:27 +01:00
|
|
|
// we have to either increase X or move to a new row and reset X, in case it's over 10000.
|
2022-11-11 21:16:26 +01:00
|
|
|
if(islandCoords.getX() >= 10000) // if we need to create a new row
|
|
|
|
{
|
|
|
|
islandCoords.setX(1000);
|
2022-11-12 01:50:27 +01:00
|
|
|
islandCoords.setZ(islandCoords.getZ() + Cache.islandsDistance);
|
2022-11-11 21:16:26 +01:00
|
|
|
} else { // if we just need to increase the column
|
2022-11-12 01:50:27 +01:00
|
|
|
islandCoords.setX(islandCoords.getX() + Cache.islandsDistance);
|
2022-11-11 21:16:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return islandCoords;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
player joins -> we look for the first free space -> we add it to the list and generate the island
|
|
|
|
|
|
|
|
|
|
|
|
*/
|