Files
docs
src
build.gradle
manifest.kod
Code
37 3,875
src/main/java/org/example/SectorHandler/sectorsPlugin/SectorsPlugin.java
package org.example.SectorHandler.sectorsPlugin;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandSender;
import org.bukkit.plugin.java.JavaPlugin;
import org.example.SectorHandler.sectorsPlugin.api.SectorManager;
import org.example.SectorHandler.sectorsPlugin.border.BorderVisualizer;
import org.example.SectorHandler.sectorsPlugin.border.BuildProtectionListener;
import org.example.SectorHandler.sectorsPlugin.commands.ReloadCommand;
import org.example.SectorHandler.sectorsPlugin.commands.SectorsCommand;
import org.example.SectorHandler.sectorsPlugin.commands.SpawnCommand;
import org.example.SectorHandler.sectorsPlugin.config.SectorsConfig;
import org.example.SectorHandler.sectorsPlugin.status.RedisHandoverService;
import org.example.SectorHandler.sectorsPlugin.listeners.BufferZoneListener;
import org.example.SectorHandler.sectorsPlugin.listeners.CrossSectorTeleportListener;
import org.example.SectorHandler.sectorsPlugin.listeners.NetherPortalListener;
import org.example.SectorHandler.sectorsPlugin.listeners.PlayerJoinListener;
import org.example.SectorHandler.sectorsPlugin.listeners.PlayerMoveListener;
import org.example.SectorHandler.sectorsPlugin.listeners.PvPListener;
import org.example.SectorHandler.sectorsPlugin.status.RedisConsumerService;
import org.example.SectorHandler.sectorsPlugin.status.RedisPublisherService;
public class SectorsPlugin extends JavaPlugin {
private static SectorsPlugin instance;
private SectorsConfig config;
private SectorManager sectorManager;
private BorderVisualizer borderVisualizer;
private RedisPublisherService redisPublisher;
private RedisConsumerService redisConsumer;
private RedisHandoverService handoverService;
@Override
public void onEnable() {
instance = this;
getLogger().info("╔════════════════════════════════════╗");
getLogger().info("║ MinecraftSectors - Loading... ║");
getLogger().info("╚════════════════════════════════════╝");
try {
loadConfiguration();
sectorManager = new SectorManager(this, config);
startRedisServices();
startHandoverService();
registerListeners();
registerCommands();
if (config.isVisualizeBorders()) {
borderVisualizer = new BorderVisualizer(this, sectorManager);
borderVisualizer.start();
getLogger().info("✓ Wizualizacja granic włączona!");
}
getLogger().info("╔════════════════════════════════════╗");
getLogger().info("║ MinecraftSectors - Załadowany! ║");
getLogger().info("║ Sektory: " + String.format("%-20s", config.getServers().size()) + "║");
getLogger().info("╚════════════════════════════════════╝");
} catch (Exception e) {
getLogger().severe("✗ Błąd podczas ładowania pluginu!");
getLogger().log(java.util.logging.Level.SEVERE, "Stack trace:", e);
Bukkit.getPluginManager().disablePlugin(this);
}
}
@Override
public void onDisable() {
getLogger().info("✓ MinecraftSectors wyłączony!");
if (borderVisualizer != null) {
borderVisualizer.stop();
borderVisualizer = null;
}
stopRedisServices();
stopHandoverService();
}
public void reloadAll(CommandSender sender) {
try {
if (borderVisualizer != null) {
borderVisualizer.stop();
borderVisualizer = null;
}
stopRedisServices();
stopHandoverService();
loadConfiguration();
sectorManager = new SectorManager(this, config);
startRedisServices();
startHandoverService();
registerListeners();
if (config.isVisualizeBorders()) {
borderVisualizer = new BorderVisualizer(this, sectorManager);
borderVisualizer.start();
}
if (sender != null) {
sender.sendMessage("§a✓ Konfiguracja sektorów przeładowana.");
}
getLogger().info("✓ Przeładowano konfigurację sektorów.");
} catch (Exception e) {
if (sender != null) {
sender.sendMessage("§c✗ Błąd przeładowania: " + e.getMessage());
}
getLogger().log(java.util.logging.Level.SEVERE, "Reload error:", e);
}
}
private void loadConfiguration() throws Exception {
if (!getDataFolder().exists()) {
getDataFolder().mkdirs();
}
java.io.File configFile = new java.io.File(getDataFolder(), "config.yml");
if (!configFile.exists()) {
getLogger().info("Tworzę nowy plik konfiguracyjny...");
saveDefaultConfig();
}
this.config = new SectorsConfig(this);
this.config.load();
getLogger().info("✓ Konfiguracja załadowana!");
getLogger().info(" - Sektory: " + config.getServers().size());
getLogger().info(" - Worldy: " + config.getWorlds().size());
}
public boolean isHuskSyncEnabled() {
return Bukkit.getPluginManager().getPlugin("HuskSync") != null;
}
public void registerListeners() {
Bukkit.getPluginManager().registerEvents(new PlayerMoveListener(this, sectorManager), this);
Bukkit.getPluginManager().registerEvents(new NetherPortalListener(this, sectorManager), this);
Bukkit.getPluginManager().registerEvents(new CrossSectorTeleportListener(this, sectorManager), this);
Bukkit.getPluginManager().registerEvents(new PvPListener(this, sectorManager), this);
Bukkit.getPluginManager().registerEvents(new BuildProtectionListener(this, sectorManager), this);
Bukkit.getPluginManager().registerEvents(new BufferZoneListener(this, sectorManager), this);
Bukkit.getPluginManager().registerEvents(new PlayerJoinListener(this), this);
getLogger().info("✓ Listenery zarejestrowane!");
}
private void registerCommands() {
if (getCommand("sectors") != null) {
getCommand("sectors").setExecutor(new SectorsCommand(this, sectorManager));
} else {
getLogger().warning("⚠ Komenda 'sectors' nie jest zdefiniowana w plugin.yml");
}
if (getCommand("sectorinfo") != null) {
getCommand("sectorinfo").setExecutor(new SectorsCommand(this, sectorManager));
} else {
getLogger().warning("⚠ Komenda 'sectorinfo' nie jest zdefiniowana w plugin.yml");
}
if (getCommand("sectorsreload") != null) {
getCommand("sectorsreload").setExecutor(new ReloadCommand(this));
} else {
getLogger().warning("⚠ Komenda 'sectorsreload' nie jest zdefiniowana w plugin.yml");
}
if (getCommand("spawn") != null) {
getCommand("spawn").setExecutor(new SpawnCommand(this, sectorManager));
} else {
getLogger().warning("⚠ Komenda 'spawn' nie jest zdefiniowana w plugin.yml");
}
getLogger().info("✓ Komendy zarejestrowane!");
}
private void startRedisServices() {
boolean redisEnabled = false;
String role = "publisher";
try {
redisEnabled = config.getYaml().getBoolean("redis.enabled", false);
role = config.getYaml().getString("redis.role", "publisher");
} catch (Throwable t) {
getLogger().warning("Redis config not found / getYaml() missing, skipping Redis.");
return;
}
if (!redisEnabled) {
getLogger().info("Redis disabled in config - skipping.");
return;
}
try {
if ("publisher".equalsIgnoreCase(role)) {
redisPublisher = new RedisPublisherService(this, sectorManager, config);
boolean ok = redisPublisher.start();
if (!ok) {
getLogger().warning("RedisPublisherService failed to start.");
}
} else {
redisConsumer = new RedisConsumerService(this, sectorManager, config);