Update v1.1
PoketDimension now Loads Worlds on Load automatically and does not need a world managing plugin to function after a server restart. Additionally some Names were renamed.
This commit is contained in:
@@ -0,0 +1,101 @@
|
||||
package dev.siskel.poketDimensions.managers;
|
||||
|
||||
import dev.siskel.poketDimensions.classes.PlayerLocation;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Location;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.WorldCreator;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.configuration.file.YamlConfiguration;
|
||||
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 org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
public class PlayerLocationManager extends JavaPlugin implements Listener {
|
||||
|
||||
private File dataFile;
|
||||
private FileConfiguration dataConfig;
|
||||
|
||||
@Override
|
||||
public void onEnable() {
|
||||
dataFile = new File(getDataFolder(), "player-locations.yml");
|
||||
if (!dataFile.exists()) {
|
||||
dataFile.getParentFile().mkdirs();
|
||||
saveResource("player-locations.yml", false);
|
||||
}
|
||||
dataConfig = YamlConfiguration.loadConfiguration(dataFile);
|
||||
|
||||
Bukkit.getPluginManager().registerEvents(this, this);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerQuit(PlayerQuitEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
savePlayerLocation(player);
|
||||
}
|
||||
|
||||
@EventHandler
|
||||
public void onPlayerJoin(PlayerJoinEvent event) {
|
||||
Player player = event.getPlayer();
|
||||
PlayerLocation savedLoc = loadPlayerLocation(player);
|
||||
|
||||
if (savedLoc != null) {
|
||||
World world = Bukkit.getWorld(savedLoc.getWorld());
|
||||
if (world == null) {
|
||||
// World is not loaded, so load it
|
||||
world = Bukkit.createWorld(new WorldCreator(savedLoc.getWorld()));
|
||||
}
|
||||
|
||||
// Now teleport the player to their last location
|
||||
Location loc = savedLoc.toLocation(Bukkit.getServer());
|
||||
if (loc != null) {
|
||||
// Delay teleport by 1 tick to ensure player is fully loaded in the world
|
||||
Bukkit.getScheduler().runTaskLater(this, () -> player.teleport(loc), 1L);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Save player location to file
|
||||
public void savePlayerLocation(Player player) {
|
||||
Location loc = player.getLocation();
|
||||
String path = "players." + player.getUniqueId();
|
||||
|
||||
dataConfig.set(path + ".world", loc.getWorld().getName());
|
||||
dataConfig.set(path + ".x", loc.getX());
|
||||
dataConfig.set(path + ".y", loc.getY());
|
||||
dataConfig.set(path + ".z", loc.getZ());
|
||||
dataConfig.set(path + ".yaw", loc.getYaw());
|
||||
dataConfig.set(path + ".pitch", loc.getPitch());
|
||||
|
||||
try {
|
||||
dataConfig.save(dataFile);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
// Load player location from file as PlayerLocation object
|
||||
public PlayerLocation loadPlayerLocation(Player player) {
|
||||
String path = "players." + player.getUniqueId();
|
||||
|
||||
if (!dataConfig.contains(path)) {
|
||||
return null; // No saved location
|
||||
}
|
||||
|
||||
String world = dataConfig.getString(path + ".world");
|
||||
double x = dataConfig.getDouble(path + ".x");
|
||||
double y = dataConfig.getDouble(path + ".y");
|
||||
double z = dataConfig.getDouble(path + ".z");
|
||||
float yaw = (float) dataConfig.getDouble(path + ".yaw");
|
||||
float pitch = (float) dataConfig.getDouble(path + ".pitch");
|
||||
|
||||
return new PlayerLocation(world, x, y, z, yaw, pitch);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user