Files
maven-tests
src
manifest.kod
pom.xml
README.md
Code
22 2,012
src/main/java/com/morales/smpcore/SmpCorePlugin.java
package com.morales.smpcore;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.IllegalPluginAccessException;
import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public final class SmpCorePlugin extends JavaPlugin {
private OwnerSecurity ownerSecurity;
private SqliteStore store;
private FeatureRegistry featureRegistry;
private PlayerStatsFeature playerStatsFeature;
private TeleportRequestsFeature teleportRequestsFeature;
private OwnerGui ownerGui;
private YamlConfiguration messages;
private YamlConfiguration guiConfig;
private YamlConfiguration databaseConfig;
private YamlConfiguration warps;
private String databaseFileName;
@Override
public void onEnable() {
saveDefaultConfig();
for (String resource : List.of("features.yml", "messages.yml", "gui.yml", "database.yml", "owner.yml", "warps.yml")) {
saveBundledIfMissing(resource);
}
messages = loadYaml("messages.yml");
guiConfig = loadYaml("gui.yml");
databaseConfig = loadYaml("database.yml");
warps = loadYaml("warps.yml");
if (!"sqlite".equalsIgnoreCase(databaseConfig.getString("type", "sqlite"))) {
getLogger().severe("Only SQLite is implemented. Set database.yml type to sqlite.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
databaseFileName = databaseConfig.getString("file", "smpcore.db");
if (databaseFileName == null || databaseFileName.isBlank()) {
getLogger().severe("database.yml file must be a non-empty SQLite file name.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
try {
ownerSecurity = new OwnerSecurity(new File(getDataFolder(), "owner.yml"));
} catch (IOException exception) {
getLogger().severe("Could not load owner.yml; owner-protected actions will not be available.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
store = new SqliteStore(new File(getDataFolder(), databaseFileName));
store.ready().whenComplete((ignored, failure) -> {
if (failure != null && isEnabled()) {
logDatabaseFailure("opening the SQLite database");
} else if (failure == null && isEnabled()) {
getLogger().info("SQLite database initialized.");
}
});
featureRegistry = new FeatureRegistry(new File(getDataFolder(), "features.yml"), getLogger(), ownerSecurity);
playerStatsFeature = new PlayerStatsFeature(this, store);
teleportRequestsFeature = new TeleportRequestsFeature(this);
featureRegistry.register("core_diagnostics", "Core", "Core diagnostics",
"Feature status and configuration validation reports.", "BOOK", Map.of(), () -> { }, () -> { });
featureRegistry.register("player_stats", "Players", "Player statistics",
"UUID-based playtime, kills, deaths, and player statistics.", "CLOCK",
Map.of("autosave-seconds", new FeatureRegistry.IntSetting(300, 30, 3600)),
playerStatsFeature::enable, playerStatsFeature::disable);
featureRegistry.register("homes", "Travel", "Player homes",
"Persistent UUID-owned homes with limits and destination safety checks.", "OAK_DOOR",
Map.of("max-homes", new FeatureRegistry.IntSetting(3, 1, 25)), () -> { }, () -> { });
featureRegistry.register("spawn_warps", "Travel", "Spawn and server warps",
"Safe server spawn and named server warp management.", "COMPASS",
Map.of("max-warps", new FeatureRegistry.IntSetting(20, 1, 100)), () -> { }, () -> { });
featureRegistry.register("teleport_requests", "Travel", "Teleport requests",
"Expiring player-to-player teleport requests with quit cleanup.", "ENDER_PEARL",
Map.of("request-timeout-seconds", new FeatureRegistry.IntSetting(60, 10, 300)),
teleportRequestsFeature::enable, teleportRequestsFeature::disable);
featureRegistry.reloadFromDisk();
ownerGui = new OwnerGui(this);
Bukkit.getPluginManager().registerEvents(ownerGui, this);
SmpCoreCommand command = new SmpCoreCommand(this);
if (getCommand("smpcore") == null) {
getLogger().severe("plugin.yml does not declare the smpcore command.");
Bukkit.getPluginManager().disablePlugin(this);
return;
}
getCommand("smpcore").setExecutor(command);
getCommand("smpcore").setTabCompleter(command);
getLogger().info("Morales's SMPCore enabled with " + featureRegistry.all().size() + " registered features.");
}
@Override
public void onDisable() {
if (featureRegistry != null) {
featureRegistry.disableAll();
}
if (store != null) {
store.close();
}
}
public OwnerSecurity getOwnerSecurity() {
return ownerSecurity;
}
public SqliteStore getStore() {
return store;
}
public FeatureRegistry getFeatureRegistry() {
return featureRegistry;
}
public TeleportRequestsFeature getTeleportRequestsFeature() {
return teleportRequestsFeature;
}
public OwnerGui getOwnerGui() {
return ownerGui;
}
public YamlConfiguration getGuiConfig() {
return guiConfig;
}
public YamlConfiguration getWarps() {
return warps;
}
public void send(CommandSender sender, String key, String... replacements) {
String text = messages.getString(key, "&cMissing message: " + key);
for (int i = 0; i + 1 < replacements.length; i += 2) {
text = text.replace("%" + replacements[i] + "%", replacements[i + 1]);
}
String prefix = messages.getString("prefix", "");
sender.sendMessage(colorize(prefix + text));
}
public String colorize(String text) {
return ChatColor.translateAlternateColorCodes('&', text);
}
public boolean isSafeLocation(Location location) {
if (location.getWorld() == null) {
return false;
}
Location feet = location.clone();
feet.setY(Math.floor(location.getY()));
return feet.getBlock().isPassable()
&& feet.clone().add(0, 1, 0).getBlock().isPassable()
&& feet.clone().subtract(0, 1, 0).getBlock().isSolid();
}
public Location loadLocation(String path) {
String worldName = warps.getString(path + ".world");
if (worldName == null) {
return null;
}
World world = Bukkit.getWorld(worldName);
if (world == null) {
return null;
}
return new Location(world, warps.getDouble(path + ".x"), warps.getDouble(path + ".y"),
warps.getDouble(path + ".z"), (float) warps.getDouble(path + ".yaw"),
(float) warps.getDouble(path + ".pitch"));
}
public void saveLocation(String path, Location location) {
warps.set(path + ".world", location.getWorld().getName());
warps.set(path + ".x", location.getX());
warps.set(path + ".y", location.getY());
warps.set(path + ".z", location.getZ());
warps.set(path + ".yaw", location.getYaw());
warps.set(path + ".pitch", location.getPitch());
}
public boolean saveWarps() {
try {
warps.save(new File(getDataFolder(), "warps.yml"));
return true;
} catch (IOException exception) {
getLogger().warning("Could not save warps.yml.");
return false;
}
}
public List<String> validateConfiguration() {
List<String> issues = new ArrayList<>(featureRegistry.validateConfiguration());
if (!"sqlite".equalsIgnoreCase(databaseConfig.getString("type", ""))) {
issues.add("database.yml type must be sqlite; MySQL is not implemented.");