Files
src
build.gradle
settings.gradle
Code
5 208
src/main/java/com/example/simplespawn/SimpleSpawn.java
package com.example.simplespawn;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
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.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
public class SimpleSpawn extends JavaPlugin implements CommandExecutor, Listener {
private File playerLocationsFile;
private FileConfiguration playerLocations;
@Override
public void onEnable() {
saveDefaultConfig();
loadPlayerLocations();
getCommand("spawn").setExecutor(this);
getCommand("setspawn").setExecutor(this);
getServer().getPluginManager().registerEvents(this, this);
}
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!(sender instanceof Player)) {
sender.sendMessage(ChatColor.RED + "This command can only be used by a player.");
return true;
}
Player player = (Player) sender;
if (command.getName().equalsIgnoreCase("setspawn")) {
if (!player.hasPermission("simplespawn.setspawn")) {
player.sendMessage(ChatColor.RED + "You don't have permission to set the spawn.");
return true;
}
saveSpawn(player);
player.sendMessage(ChatColor.GREEN + "Server spawn set to your current location.");
return true;
}
if (command.getName().equalsIgnoreCase("spawn")) {
if (!getConfig().getBoolean("spawn.set")) {
player.sendMessage(ChatColor.RED + "Spawn has not been set yet.");
return true;
}
String worldName = getConfig().getString("spawn.world", "");
World world = Bukkit.getWorld(worldName);
if (world == null) {
player.sendMessage(ChatColor.RED + "The saved spawn world is currently unavailable.");
return true;
}
Location spawn = new Location(
world,
getConfig().getDouble("spawn.x"),
getConfig().getDouble("spawn.y"),
getConfig().getDouble("spawn.z"),
(float) getConfig().getDouble("spawn.yaw"),
(float) getConfig().getDouble("spawn.pitch")
);
player.teleport(spawn);
player.sendMessage(ChatColor.GREEN + "Teleported to spawn.");
return true;
}
return false;
}
@EventHandler
public void onPlayerQuit(PlayerQuitEvent event) {
Player player = event.getPlayer();
Location location = player.getLocation();
String path = player.getUniqueId().toString();
playerLocations.set(path + ".world", location.getWorld().getName());
playerLocations.set(path + ".x", location.getX());
playerLocations.set(path + ".y", location.getY());
playerLocations.set(path + ".z", location.getZ());
playerLocations.set(path + ".yaw", location.getYaw());
playerLocations.set(path + ".pitch", location.getPitch());
savePlayerLocations();
}
@EventHandler
public void onPlayerJoin(PlayerJoinEvent event) {
Player player = event.getPlayer();
String path = player.getUniqueId().toString();
if (!playerLocations.contains(path + ".world")) {
return;
}
World world = Bukkit.getWorld(playerLocations.getString(path + ".world", ""));
if (world == null) {
player.sendMessage(ChatColor.RED + "Your saved location's world is currently unavailable.");
return;
}
Location location = new Location(
world,
playerLocations.getDouble(path + ".x"),
playerLocations.getDouble(path + ".y"),
playerLocations.getDouble(path + ".z"),
(float) playerLocations.getDouble(path + ".yaw"),
(float) playerLocations.getDouble(path + ".pitch")
);
player.teleport(location);
}
private void loadPlayerLocations() {
if (!getDataFolder().exists()) {
getDataFolder().mkdirs();
}
playerLocationsFile = new File(getDataFolder(), "players.yml");
if (!playerLocationsFile.exists()) {
try {
playerLocationsFile.createNewFile();
} catch (IOException exception) {
getLogger().severe("Could not create players.yml: " + exception.getMessage());
}
}
playerLocations = YamlConfiguration.loadConfiguration(playerLocationsFile);
}
private void savePlayerLocations() {
try {
playerLocations.save(playerLocationsFile);
} catch (IOException exception) {
getLogger().severe("Could not save players.yml: " + exception.getMessage());
}
}
private void saveSpawn(Player player) {
Location location = player.getLocation();
getConfig().set("spawn.set", true);
getConfig().set("spawn.world", location.getWorld().getName());
getConfig().set("spawn.x", location.getX());
getConfig().set("spawn.y", location.getY());
getConfig().set("spawn.z", location.getZ());
getConfig().set("spawn.yaw", location.getYaw());
getConfig().set("spawn.pitch", location.getPitch());
saveConfig();
}
}