Moves all config related code from Stargate to StargateConfig
This commit is contained in:
@ -3,13 +3,12 @@ package net.knarcraft.stargate;
|
||||
import net.knarcraft.stargate.command.CommandStarGate;
|
||||
import net.knarcraft.stargate.command.StarGateTabCompleter;
|
||||
import net.knarcraft.stargate.config.EconomyConfig;
|
||||
import net.knarcraft.stargate.config.LanguageLoader;
|
||||
import net.knarcraft.stargate.config.MessageSender;
|
||||
import net.knarcraft.stargate.config.StargateConfig;
|
||||
import net.knarcraft.stargate.config.StargateGateConfig;
|
||||
import net.knarcraft.stargate.container.BlockChangeRequest;
|
||||
import net.knarcraft.stargate.container.ChunkUnloadRequest;
|
||||
import net.knarcraft.stargate.listener.BlockEventListener;
|
||||
import net.knarcraft.stargate.listener.BungeeCordListener;
|
||||
import net.knarcraft.stargate.listener.EntityEventListener;
|
||||
import net.knarcraft.stargate.listener.PlayerEventListener;
|
||||
import net.knarcraft.stargate.listener.PluginEventListener;
|
||||
@ -17,34 +16,23 @@ import net.knarcraft.stargate.listener.PortalEventListener;
|
||||
import net.knarcraft.stargate.listener.TeleportEventListener;
|
||||
import net.knarcraft.stargate.listener.VehicleEventListener;
|
||||
import net.knarcraft.stargate.listener.WorldEventListener;
|
||||
import net.knarcraft.stargate.portal.GateHandler;
|
||||
import net.knarcraft.stargate.portal.Portal;
|
||||
import net.knarcraft.stargate.portal.PortalHandler;
|
||||
import net.knarcraft.stargate.portal.PortalRegistry;
|
||||
import net.knarcraft.stargate.thread.BlockChangeThread;
|
||||
import net.knarcraft.stargate.thread.ChunkUnloadThread;
|
||||
import net.knarcraft.stargate.thread.StarGateThread;
|
||||
import net.knarcraft.stargate.utility.FileHelper;
|
||||
import net.knarcraft.stargate.utility.PortalFileHelper;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.block.Sign;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.PluginCommand;
|
||||
import org.bukkit.configuration.file.FileConfiguration;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
import org.bukkit.plugin.PluginManager;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
import org.bukkit.plugin.java.JavaPluginLoader;
|
||||
import org.bukkit.plugin.messaging.Messenger;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.HashSet;
|
||||
import java.util.LinkedList;
|
||||
import java.util.Map;
|
||||
import java.util.PriorityQueue;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.ConcurrentLinkedQueue;
|
||||
@ -63,28 +51,11 @@ public class Stargate extends JavaPlugin {
|
||||
private static Logger logger;
|
||||
public static Server server;
|
||||
public static Stargate stargate;
|
||||
public static LanguageLoader languageLoader;
|
||||
|
||||
private String dataFolderPath;
|
||||
private static StargateGateConfig stargateGateConfig;
|
||||
private static EconomyConfig economyConfig;
|
||||
|
||||
//Used for debug
|
||||
public static boolean debuggingEnabled = false;
|
||||
public static boolean permissionDebuggingEnabled = false;
|
||||
|
||||
//World names that contain stargates
|
||||
public static final HashSet<String> managedWorlds = new HashSet<>();
|
||||
|
||||
private static String pluginVersion;
|
||||
private static String portalFolder;
|
||||
private static String gateFolder;
|
||||
|
||||
private static String languageName = "en";
|
||||
|
||||
private FileConfiguration newConfig;
|
||||
private PluginManager pluginManager;
|
||||
private static MessageSender messageSender;
|
||||
private static PluginManager pluginManager;
|
||||
private static StargateConfig stargateConfig;
|
||||
|
||||
/**
|
||||
* Empty constructor necessary for Spigot
|
||||
@ -111,7 +82,7 @@ public class Stargate extends JavaPlugin {
|
||||
* @return <p>The sender for sending messages to players</p>
|
||||
*/
|
||||
public static MessageSender getMessageSender() {
|
||||
return messageSender;
|
||||
return stargateConfig.getMessageSender();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -120,16 +91,7 @@ public class Stargate extends JavaPlugin {
|
||||
* @return <p>The object containing gate configuration values</p>
|
||||
*/
|
||||
public static StargateGateConfig getGateConfig() {
|
||||
return stargateGateConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the object containing economy config values
|
||||
*
|
||||
* @return <p>The object containing economy config values</p>
|
||||
*/
|
||||
public static EconomyConfig getEconomyConfig() {
|
||||
return economyConfig;
|
||||
return stargateConfig.getStargateGateConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -141,15 +103,6 @@ public class Stargate extends JavaPlugin {
|
||||
return pluginVersion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets whether portals should be destroyed by explosions
|
||||
*
|
||||
* @return <p>True if portals should be destroyed</p>
|
||||
*/
|
||||
public static boolean destroyedByExplosion() {
|
||||
return stargateGateConfig.destroyedByExplosion();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the logger used for logging to the console
|
||||
*
|
||||
@ -166,24 +119,13 @@ public class Stargate extends JavaPlugin {
|
||||
* @param message <p>A message describing what happened</p>
|
||||
*/
|
||||
public static void debug(String route, String message) {
|
||||
if (Stargate.debuggingEnabled) {
|
||||
if (Stargate.stargateConfig.isDebuggingEnabled()) {
|
||||
logger.info("[stargate::" + route + "] " + message);
|
||||
} else {
|
||||
logger.log(Level.FINEST, "[stargate::" + route + "] " + message);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a line on a sign, adding the chosen sign color
|
||||
*
|
||||
* @param sign <p>The sign to update</p>
|
||||
* @param index <p>The index of the sign line to change</p>
|
||||
* @param text <p>The new text on the sign</p>
|
||||
*/
|
||||
public static void setLine(Sign sign, int index, String text) {
|
||||
sign.setLine(index, stargateGateConfig.getSignColor() + text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the folder for saving created portals
|
||||
*
|
||||
@ -191,8 +133,8 @@ public class Stargate extends JavaPlugin {
|
||||
*
|
||||
* @return <p>The folder for storing the portal database</p>
|
||||
*/
|
||||
public static String getSaveLocation() {
|
||||
return portalFolder;
|
||||
public static String getPortalFolder() {
|
||||
return stargateConfig.getPortalFolder();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -203,7 +145,7 @@ public class Stargate extends JavaPlugin {
|
||||
* @return <p>The folder storing gate files</p>
|
||||
*/
|
||||
public static String getGateFolder() {
|
||||
return gateFolder;
|
||||
return stargateConfig.getGateFolder();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -212,7 +154,7 @@ public class Stargate extends JavaPlugin {
|
||||
* @return <p>The default network</p>
|
||||
*/
|
||||
public static String getDefaultNetwork() {
|
||||
return stargateGateConfig.getDefaultPortalNetwork();
|
||||
return stargateConfig.getStargateGateConfig().getDefaultPortalNetwork();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -224,7 +166,7 @@ public class Stargate extends JavaPlugin {
|
||||
* @return <p>The full translated string</p>
|
||||
*/
|
||||
public static String getString(String name) {
|
||||
return languageLoader.getString(name);
|
||||
return stargateConfig.getLanguageLoader().getString(name);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -257,11 +199,29 @@ public class Stargate extends JavaPlugin {
|
||||
return input.replace(search, value);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets this plugin's plugin manager
|
||||
*
|
||||
* @return <p>A plugin manager</p>
|
||||
*/
|
||||
public static PluginManager getPluginManager() {
|
||||
return pluginManager;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the object containing economy config values
|
||||
*
|
||||
* @return <p>The object containing economy config values</p>
|
||||
*/
|
||||
public static EconomyConfig getEconomyConfig() {
|
||||
return stargateConfig.getEconomyConfig();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDisable() {
|
||||
PortalHandler.closeAllPortals();
|
||||
PortalRegistry.clearPortals();
|
||||
managedWorlds.clear();
|
||||
stargateConfig.clearManagedWorlds();
|
||||
getServer().getScheduler().cancelTasks(this);
|
||||
}
|
||||
|
||||
@ -269,52 +229,35 @@ public class Stargate extends JavaPlugin {
|
||||
public void onEnable() {
|
||||
PluginDescriptionFile pluginDescriptionFile = this.getDescription();
|
||||
pluginManager = getServer().getPluginManager();
|
||||
newConfig = this.getConfig();
|
||||
FileConfiguration newConfig = this.getConfig();
|
||||
logger = Logger.getLogger("Minecraft");
|
||||
Stargate.server = getServer();
|
||||
Stargate.stargate = this;
|
||||
|
||||
// Set portalFile and gateFolder to the plugin folder as defaults.
|
||||
dataFolderPath = getDataFolder().getPath().replaceAll("\\\\", "/");
|
||||
portalFolder = dataFolderPath + "/portals/";
|
||||
gateFolder = dataFolderPath + "/gates/";
|
||||
String languageFolder = dataFolderPath + "/lang/";
|
||||
stargateConfig = new StargateConfig(logger);
|
||||
stargateConfig.finishSetup();
|
||||
|
||||
pluginVersion = pluginDescriptionFile.getVersion();
|
||||
|
||||
logger.info(pluginDescriptionFile.getName() + " v." + pluginDescriptionFile.getVersion() + " is enabled.");
|
||||
|
||||
//Register events before loading gates to stop weird things happening.
|
||||
//Register events before loading gates to stop weird things from happening.
|
||||
registerEventListeners();
|
||||
|
||||
this.loadConfig();
|
||||
|
||||
//Enable the required channels for Bungee support
|
||||
if (stargateGateConfig.enableBungee()) {
|
||||
startStopBungeeListener(true);
|
||||
}
|
||||
|
||||
// It is important to load languages here, as they are used during reloadGates()
|
||||
languageLoader = new LanguageLoader(languageFolder, Stargate.languageName);
|
||||
messageSender = new MessageSender(languageLoader);
|
||||
if (debuggingEnabled) {
|
||||
languageLoader.debug();
|
||||
}
|
||||
|
||||
this.createMissingFolders();
|
||||
this.loadGates();
|
||||
this.loadAllPortals();
|
||||
|
||||
//Check to see if Economy is loaded yet.
|
||||
setupVaultEconomy();
|
||||
|
||||
//Run necessary threads
|
||||
runThreads();
|
||||
|
||||
this.registerCommands();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts threads using the bukkit scheduler
|
||||
*/
|
||||
private void runThreads() {
|
||||
BukkitScheduler scheduler = getServer().getScheduler();
|
||||
scheduler.runTaskTimer(this, new StarGateThread(), 0L, 100L);
|
||||
scheduler.runTaskTimer(this, new BlockChangeThread(), 0L, 1L);
|
||||
scheduler.runTaskTimer(this, new ChunkUnloadThread(), 0L, 100L);
|
||||
|
||||
this.registerCommands();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -338,218 +281,11 @@ public class Stargate extends JavaPlugin {
|
||||
private void registerCommands() {
|
||||
PluginCommand stargateCommand = this.getCommand("stargate");
|
||||
if (stargateCommand != null) {
|
||||
stargateCommand.setExecutor(new CommandStarGate(this));
|
||||
stargateCommand.setExecutor(new CommandStarGate());
|
||||
stargateCommand.setTabCompleter(new StarGateTabCompleter());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all config values
|
||||
*/
|
||||
public void loadConfig() {
|
||||
this.reloadConfig();
|
||||
newConfig = this.getConfig();
|
||||
|
||||
boolean isMigrating = false;
|
||||
if (newConfig.getString("lang") != null ||
|
||||
newConfig.getString("gates.integrity.ignoreEntrance") != null ||
|
||||
newConfig.getString("ignoreEntrance") != null) {
|
||||
migrateConfig(newConfig);
|
||||
isMigrating = true;
|
||||
}
|
||||
|
||||
// Copy default values if required
|
||||
newConfig.options().copyDefaults(true);
|
||||
|
||||
//Language
|
||||
languageName = newConfig.getString("language");
|
||||
|
||||
//Folders
|
||||
portalFolder = newConfig.getString("folders.portalFolder");
|
||||
gateFolder = newConfig.getString("folders.gateFolder");
|
||||
|
||||
//Debug
|
||||
debuggingEnabled = newConfig.getBoolean("debugging.debug");
|
||||
permissionDebuggingEnabled = newConfig.getBoolean("debugging.permissionDebug");
|
||||
|
||||
//If users have an outdated config, assume they also need to update their default gates
|
||||
if (isMigrating) {
|
||||
GateHandler.writeDefaultGatesToFolder(gateFolder);
|
||||
}
|
||||
|
||||
//Gates
|
||||
stargateGateConfig = new StargateGateConfig(newConfig);
|
||||
|
||||
//Economy
|
||||
economyConfig = new EconomyConfig(newConfig);
|
||||
|
||||
this.saveConfig();
|
||||
}
|
||||
|
||||
/**
|
||||
* Changes all configuration values from the old name to the new name
|
||||
*
|
||||
* @param newConfig <p>The config to read from and write to</p>
|
||||
*/
|
||||
private void migrateConfig(FileConfiguration newConfig) {
|
||||
try {
|
||||
newConfig.save(dataFolderPath + "/config.old");
|
||||
} catch (IOException e) {
|
||||
Stargate.debug("Stargate::migrateConfig", "Unable to save old backup and do migration");
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
Map<String, String> migrationFields;
|
||||
try {
|
||||
migrationFields = FileHelper.readKeyValuePairs(FileHelper.getBufferedReaderFromInputStream(
|
||||
FileHelper.getInputStreamForInternalFile("/config-migrations.txt")));
|
||||
} catch (IOException e) {
|
||||
Stargate.debug("Stargate::migrateConfig", "Unable to load config migration file");
|
||||
e.printStackTrace();
|
||||
return;
|
||||
}
|
||||
|
||||
//Replace old config names with the new ones
|
||||
for (String key : migrationFields.keySet()) {
|
||||
if (newConfig.contains(key)) {
|
||||
String newPath = migrationFields.get(key);
|
||||
Object oldValue = newConfig.get(key);
|
||||
if (!newPath.trim().isEmpty()) {
|
||||
newConfig.set(newPath, oldValue);
|
||||
}
|
||||
newConfig.set(key, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Forces all open portals to close
|
||||
*/
|
||||
public void closeAllPortals() {
|
||||
// Close all gates prior to reloading
|
||||
for (Portal openPortal : openPortalsQueue) {
|
||||
openPortal.getPortalOpener().closePortal(true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all available gates
|
||||
*/
|
||||
public void loadGates() {
|
||||
GateHandler.loadGates(gateFolder);
|
||||
logger.info(Stargate.getString("prefix") + "Loaded " + GateHandler.getGateCount() + " gate layouts");
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads all portals in all un-managed worlds
|
||||
*/
|
||||
public void loadAllPortals() {
|
||||
for (World world : getServer().getWorlds()) {
|
||||
if (!managedWorlds.contains(world.getName())) {
|
||||
PortalFileHelper.loadAllPortals(world);
|
||||
managedWorlds.add(world.getName());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates missing folders
|
||||
*/
|
||||
private void createMissingFolders() {
|
||||
File newPortalDir = new File(portalFolder);
|
||||
if (!newPortalDir.exists()) {
|
||||
if (!newPortalDir.mkdirs()) {
|
||||
logger.severe("Unable to create portal directory");
|
||||
}
|
||||
}
|
||||
File newFile = new File(portalFolder, getServer().getWorlds().get(0).getName() + ".db");
|
||||
if (!newFile.exists() && !newFile.getParentFile().exists()) {
|
||||
if (!newFile.getParentFile().mkdirs()) {
|
||||
logger.severe("Unable to create portal database folder: " + newFile.getParentFile().getPath());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads all portals and files
|
||||
*
|
||||
* @param sender <p>The sender of the reload request</p>
|
||||
*/
|
||||
public void reload(CommandSender sender) {
|
||||
// Deactivate portals
|
||||
for (Portal activePortal : activePortalsQueue) {
|
||||
activePortal.getPortalActivator().deactivate();
|
||||
}
|
||||
// Close portals
|
||||
closeAllPortals();
|
||||
// Clear all lists
|
||||
activePortalsQueue.clear();
|
||||
openPortalsQueue.clear();
|
||||
managedWorlds.clear();
|
||||
PortalRegistry.clearPortals();
|
||||
GateHandler.clearGates();
|
||||
|
||||
// Store the old Bungee enabled value
|
||||
boolean oldEnableBungee = stargateGateConfig.enableBungee();
|
||||
// Reload data
|
||||
loadConfig();
|
||||
loadGates();
|
||||
loadAllPortals();
|
||||
languageLoader.setChosenLanguage(languageName);
|
||||
languageLoader.reload();
|
||||
|
||||
//Load Economy support if enabled/clear if disabled
|
||||
reloadEconomy();
|
||||
|
||||
//Enable or disable the required channels for Bungee support
|
||||
if (oldEnableBungee != stargateGateConfig.enableBungee()) {
|
||||
startStopBungeeListener(stargateGateConfig.enableBungee());
|
||||
}
|
||||
|
||||
messageSender.sendErrorMessage(sender, "stargate reloaded");
|
||||
}
|
||||
|
||||
/**
|
||||
* Reloads economy by enabling or disabling it as necessary
|
||||
*/
|
||||
private void reloadEconomy() {
|
||||
EconomyConfig economyConfig = Stargate.getEconomyConfig();
|
||||
if (economyConfig.isEconomyEnabled() && economyConfig.getEconomy() == null) {
|
||||
setupVaultEconomy();
|
||||
} else if (!economyConfig.isEconomyEnabled()) {
|
||||
economyConfig.disableEconomy();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Loads economy from Vault
|
||||
*/
|
||||
private void setupVaultEconomy() {
|
||||
EconomyConfig economyConfig = Stargate.getEconomyConfig();
|
||||
if (economyConfig.setupEconomy(pluginManager) && economyConfig.getEconomy() != null) {
|
||||
String vaultVersion = economyConfig.getVault().getDescription().getVersion();
|
||||
logger.info(Stargate.getString("prefix") + Stargate.replaceVars(
|
||||
Stargate.getString("vaultLoaded"), "%version%", vaultVersion));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the listener for listening to BungeeCord messages
|
||||
*/
|
||||
private void startStopBungeeListener(boolean start) {
|
||||
Messenger messenger = Bukkit.getMessenger();
|
||||
String bungeeChannel = "BungeeCord";
|
||||
|
||||
if (start) {
|
||||
messenger.registerOutgoingPluginChannel(this, bungeeChannel);
|
||||
messenger.registerIncomingPluginChannel(this, bungeeChannel, new BungeeCordListener());
|
||||
} else {
|
||||
messenger.unregisterIncomingPluginChannel(this, bungeeChannel);
|
||||
messenger.unregisterOutgoingPluginChannel(this, bungeeChannel);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the chunk unload queue containing chunks to unload
|
||||
*
|
||||
@ -569,4 +305,12 @@ public class Stargate extends JavaPlugin {
|
||||
chunkUnloadQueue.add(request);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the stargate configuration
|
||||
*
|
||||
* @return <p>The stargate configuration</p>
|
||||
*/
|
||||
public static StargateConfig getStargateConfig() {
|
||||
return stargateConfig;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user