Files
src
build.gradle
settings.gradle
Code
5 303
src/main/java/com/spawnisland/SpawnIslandPlugin.java
package com.spawnisland;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
public final class SpawnIslandPlugin extends JavaPlugin {
private final Map<String, Command> registeredCommands = new HashMap<>();
private CommandMap commandMap;
private Location configuredSpawn;
private boolean spawnConfigured;
private boolean spawnWorldMissing;
@Override
public void onEnable() {
saveDefaultConfig();
loadSpawnLocation();
registerConfiguredCommands();
}
private boolean handleSpawnCommand(CommandSender sender, String[] args) {
if (args.length != 0) {
sendMessage(sender, "usage");
return true;
}
if (!sender.hasPermission("spawn.use")) {
sendMessage(sender, "no-permission");
return true;
}
if (!(sender instanceof org.bukkit.entity.Player player)) {
sendMessage(sender, "player-only");
return true;
}
if (!spawnConfigured) {
sendMessage(sender, "no-spawn");
return true;
}
if (spawnWorldMissing || configuredSpawn == null) {
sendMessage(sender, "invalid-world");
return true;
}
if (!player.teleport(configuredSpawn.clone())) {
sendMessage(sender, "teleport-failed");
return true;
}
sendMessage(sender, "teleported");
return true;
}
private boolean handleSetSpawnCommand(CommandSender sender, String[] args) {
if (!sender.hasPermission("spawn.admin")) {
sendMessage(sender, "no-permission");
return true;
}
if (!(sender instanceof org.bukkit.entity.Player player)) {
sendMessage(sender, "player-only");
return true;
}
if (args.length != 0) {
sendMessage(sender, "usage");
return true;
}
Location location = player.getLocation();
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();
configuredSpawn = location.clone();
spawnConfigured = true;
spawnWorldMissing = false;
sendMessage(sender, "spawn-set");
return true;
}
private boolean handleReloadCommand(CommandSender sender, String[] args) {
if (!sender.hasPermission("spawn.admin")) {
sendMessage(sender, "no-permission");
return true;
}
if (args.length != 0) {
sendMessage(sender, "usage");
return true;
}
unregisterConfiguredCommands();
reloadConfig();
loadSpawnLocation();
registerConfiguredCommands();
sendMessage(sender, "reloaded");
return true;
}
private void loadSpawnLocation() {
spawnConfigured = getConfig().contains("spawn.world")
&& getConfig().contains("spawn.x")
&& getConfig().contains("spawn.y")
&& getConfig().contains("spawn.z")
&& getConfig().contains("spawn.yaw")
&& getConfig().contains("spawn.pitch");
configuredSpawn = null;
spawnWorldMissing = false;
if (!spawnConfigured) {
return;
}
String worldName = getConfig().getString("spawn.world");
if (worldName == null || worldName.isBlank()) {
spawnConfigured = false;
return;
}
World world = Bukkit.getWorld(worldName);
if (world == null) {
spawnWorldMissing = true;
return;
}
configuredSpawn = 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")
);
}
private void registerConfiguredCommands() {
commandMap = getCommandMap();
if (commandMap == null) {
getLogger().severe("Could not access Paper's command map; configured commands were not registered.");
return;
}
registerConfiguredCommand("commands.spawn", "spawn.use", this::handleSpawnCommand);
registerConfiguredCommand("commands.setspawn", "spawn.admin", this::handleSetSpawnCommand);
registerConfiguredCommand("commands.reload", "spawn.admin", this::handleReloadCommand);
}
private void registerConfiguredCommand(String path, String permission, CommandAction action) {
String name = getConfig().getString(path);
if (name == null) {
getLogger().warning("Missing command name at '" + path + "'; command not registered.");
return;
}
name = name.trim().replaceFirst("^/+", "").toLowerCase(Locale.ROOT);
if (!name.matches("[a-z0-9._-]+")) {
getLogger().warning("Invalid command name at '" + path + "'; command not registered.");
return;
}
if (registeredCommands.containsKey(name)) {
getLogger().warning("Command name '" + name + "' is configured more than once; duplicate not registered.");
return;
}
Command command = new ConfiguredCommand(name, permission, action);
boolean registered = commandMap.register(getName().toLowerCase(Locale.ROOT), command);
registeredCommands.put(name, command);
if (!registered) {
getLogger().warning("Command '" + name + "' conflicts with another command and may only be available with the plugin namespace.");
}
}
private void unregisterConfiguredCommands() {
if (commandMap == null || registeredCommands.isEmpty()) {
registeredCommands.clear();
return;
}
Set<Command> commands = new HashSet<>(registeredCommands.values());
try {
Field field = findKnownCommandsField(commandMap.getClass());
field.setAccessible(true);
Object value = field.get(commandMap);
if (value instanceof Map<?, ?> knownCommands) {
knownCommands.entrySet().removeIf(entry -> commands.contains(entry.getValue()));
}
} catch (ReflectiveOperationException | RuntimeException exception) {
getLogger().severe("Could not unregister old configured commands: " + exception.getMessage());
}
for (Command command : commands) {
command.unregister(commandMap);
}
registeredCommands.clear();
}