Files
src
build.gradle
settings.gradle
Code
16 1,880
src/main/java/lt/kodari/kristalai/KristalaiPlugin.java
package lt.kodari.kristalai;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.plugin.java.JavaPlugin;
public class KristalaiPlugin extends JavaPlugin {
private DataStore dataStore;
private RaceManager raceManager;
private CrystalEventManager eventManager;
private int passiveEffectsTaskId = -1;
@Override
public void onEnable() {
saveDefaultConfig();
migrateLegacyCrystalZone();
dataStore = new DataStore(this);
dataStore.load();
raceManager = new RaceManager(dataStore, new ServerRaceProvider(this));
CrystalItemFactory itemFactory = new CrystalItemFactory(this);
eventManager = new CrystalEventManager(this, raceManager, itemFactory);
KristalaiListener listener = new KristalaiListener(this, raceManager, itemFactory, eventManager);
getServer().getPluginManager().registerEvents(listener, this);
passiveEffectsTaskId = getServer().getScheduler()
.runTaskTimer(this, () -> getServer().getOnlinePlayers().forEach(listener::applyPassiveEffects),
0L, 1L)
.getTaskId();
KristalaiCommand command = new KristalaiCommand(raceManager, eventManager, itemFactory);
PluginCommand pluginCommand = getCommand("kristalai");
if (pluginCommand != null) {
pluginCommand.setExecutor(command);
pluginCommand.setTabCompleter(command);
}
PluginCommand zoneCommand = getCommand("sandelioriba");
if (zoneCommand != null) {
SandelioRibaCommand executor = new SandelioRibaCommand(this);
zoneCommand.setExecutor(executor);
zoneCommand.setTabCompleter(executor);
}
PluginCommand deleteZoneCommand = getCommand("sandelioribaistrinti");
if (deleteZoneCommand != null) {
SandelioRibaDeleteCommand executor = new SandelioRibaDeleteCommand(this);
deleteZoneCommand.setExecutor(executor);
deleteZoneCommand.setTabCompleter(executor);
}
getLogger().info("Kristalai įjungti.");
}
@Override
public void onDisable() {
if (passiveEffectsTaskId != -1) {
getServer().getScheduler().cancelTask(passiveEffectsTaskId);
passiveEffectsTaskId = -1;
}
if (eventManager != null) {
eventManager.shutdown();
}
if (dataStore != null) {
dataStore.save();
}
}
public int nextCrystalZoneId() {
ConfigurationSection zones = getConfig().getConfigurationSection("crystal-zones");
int maxId = 0;
if (zones != null) {
for (String key : zones.getKeys(false)) {
try {
maxId = Math.max(maxId, Integer.parseInt(key));
} catch (NumberFormatException ignored) {
}
}
}
return maxId + 1;
}
private void migrateLegacyCrystalZone() {
String oldPath = "crystal-zone";
String firstPath = oldPath + ".pos1";
String secondPath = oldPath + ".pos2";
String firstWorld = getConfig().getString(firstPath + ".world", getConfig().getString(oldPath + ".world"));
String secondWorld = getConfig().getString(secondPath + ".world", firstWorld);
double firstX = getConfig().getDouble(firstPath + ".x", Double.NaN);
double firstY = getConfig().getDouble(firstPath + ".y", Double.NaN);
double firstZ = getConfig().getDouble(firstPath + ".z", Double.NaN);
double secondX = getConfig().getDouble(secondPath + ".x", Double.NaN);
double secondY = getConfig().getDouble(secondPath + ".y", Double.NaN);
double secondZ = getConfig().getDouble(secondPath + ".z", Double.NaN);
boolean hasFirst = firstWorld != null && Double.isFinite(firstX) && Double.isFinite(firstY)
&& Double.isFinite(firstZ);
boolean hasSecond = secondWorld != null && Double.isFinite(secondX) && Double.isFinite(secondY)
&& Double.isFinite(secondZ);
if (!hasFirst && !hasSecond) {
return;
}
if (hasFirst && hasSecond && firstWorld.equalsIgnoreCase(secondWorld)) {
int zoneId = nextCrystalZoneId();
String zonePath = "crystal-zones." + zoneId;
getConfig().set(zonePath + ".world", firstWorld);
copyPoint(firstPath, zonePath + ".pos1");
copyPoint(secondPath, zonePath + ".pos2");
} else if (hasFirst) {
copyPoint(firstPath, "crystal-zone-pending.pos1");
}
getConfig().set(oldPath, null);
saveConfig();
}
private void copyPoint(String source, String target) {
getConfig().set(target + ".world", getConfig().getString(source + ".world",
getConfig().getString("crystal-zone.world")));
getConfig().set(target + ".x", getConfig().getInt(source + ".x"));
getConfig().set(target + ".y", getConfig().getInt(source + ".y"));
getConfig().set(target + ".z", getConfig().getInt(source + ".z"));
}
}