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.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.EntityEventListener; import net.knarcraft.stargate.listener.PlayerEventListener; import net.knarcraft.stargate.listener.PluginEventListener; 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.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 org.bukkit.Server; 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.scheduler.BukkitScheduler; import java.io.File; import java.util.LinkedList; import java.util.PriorityQueue; import java.util.Queue; import java.util.logging.Level; import java.util.logging.Logger; /** * The main class of the Stargate plugin */ @SuppressWarnings("unused") public class Stargate extends JavaPlugin { //Used for changing gate open/closed material. private static final Queue blockChangeRequestQueue = new LinkedList<>(); private static final Queue chunkUnloadQueue = new PriorityQueue<>(); private static Logger logger; public static Server server; public static Stargate stargate; private static String pluginVersion; private static PluginManager pluginManager; private static StargateConfig stargateConfig; /** * Empty constructor necessary for Spigot */ public Stargate() { super(); } /** * Special constructor used for MockBukkit * * @param loader

The plugin loader to be used.

* @param descriptionFile

The description file to be used.

* @param dataFolder

The data folder to be used.

* @param file

The file to be used

*/ protected Stargate(JavaPluginLoader loader, PluginDescriptionFile descriptionFile, File dataFolder, File file) { super(loader, descriptionFile, dataFolder, file); } /** * Adds a block change request to the request queue * * @param request

The request to add

*/ public static void addBlockChangeRequest(BlockChangeRequest request) { if (request != null) { blockChangeRequestQueue.add(request); } } /** * Gets the queue containing block change requests * * @return

A block change request queue

*/ public static Queue getBlockChangeRequestQueue() { return blockChangeRequestQueue; } /** * Gets the sender for sending messages to players * * @return

The sender for sending messages to players

*/ public static MessageSender getMessageSender() { return stargateConfig.getMessageSender(); } /** * Gets the object containing gate configuration values * * @return

The object containing gate configuration values

*/ public static StargateGateConfig getGateConfig() { return stargateConfig.getStargateGateConfig(); } /** * Gets the version of this plugin * * @return

This plugin's version

*/ public static String getPluginVersion() { return pluginVersion; } /** * Gets the logger used for logging to the console * * @return

The logger

*/ public static Logger getConsoleLogger() { return logger; } /** * Sends a debug message * * @param route

The class name/route where something happened

* @param message

A message describing what happened

*/ public static void debug(String route, String message) { if (stargateConfig == null || stargateConfig.isDebuggingEnabled()) { logger.info("[Stargate::" + route + "] " + message); } else { logger.log(Level.FINEST, "[Stargate::" + route + "] " + message); } } /** * Logs an info message to the console * * @param message

The message to log

*/ public static void logInfo(String message) { logger.info(getBackupString("prefix") + message); } /** * Logs a severe error message to the console * * @param message

The message to log

*/ public static void logSevere(String message) { log(Level.SEVERE, message); } /** * Logs a warning message to the console * * @param message

The message to log

*/ public static void logWarning(String message) { log(Level.WARNING, message); } /** * Logs a message to the console * * @param severity

The severity of the event triggering the message

* @param message

The message to log

*/ private static void log(Level severity, String message) { logger.log(severity, getBackupString("prefix") + message); } /** * Gets the folder for saving created portals * *

The returned String path is the full path to the folder

* * @return

The folder for storing the portal database

*/ public static String getPortalFolder() { return stargateConfig.getPortalFolder(); } /** * Gets the folder storing gate files * *

The returned String path is the full path to the folder

* * @return

The folder storing gate files

*/ public static String getGateFolder() { return stargateConfig.getGateFolder(); } /** * Gets the default network for gates where a network is not specified * * @return

The default network

*/ public static String getDefaultNetwork() { return stargateConfig.getStargateGateConfig().getDefaultPortalNetwork(); } /** * Gets a translated string given its string key * *

The name/key is the string before the equals sign in the language files

* * @param name

The name/key of the string to get

* @return

The full translated string

*/ public static String getString(String name) { return stargateConfig.getLanguageLoader().getString(name); } /** * Gets a backup string given its string key * *

The name/key is the string before the equals sign in the language files

* * @param name

The name/key of the string to get

* @return

The full string in the backup language (English)

*/ public static String getBackupString(String name) { return stargateConfig.getLanguageLoader().getBackupString(name); } /** * Replaces a list of variables in a string in the order they are given * * @param input

The input containing the variables

* @param search

The variables to replace

* @param values

The replacement values

* @return

The input string with the search values replaced with the given values

*/ public static String replaceVars(String input, String[] search, String[] values) { if (search.length != values.length) { throw new IllegalArgumentException("The number of search values and replace values do not match."); } for (int i = 0; i < search.length; i++) { input = replaceVars(input, search[i], values[i]); } return input; } /** * Replaces a variable in a string * * @param input

The input containing the variables

* @param search

The variable to replace

* @param value

The replacement value

* @return

The input string with the search replaced with value

*/ public static String replaceVars(String input, String search, String value) { return input.replace(search, value); } /** * Gets this plugin's plugin manager * * @return

A plugin manager

*/ public static PluginManager getPluginManager() { return pluginManager; } /** * Gets the object containing economy config values * * @return

The object containing economy config values

*/ public static EconomyConfig getEconomyConfig() { return stargateConfig.getEconomyConfig(); } @Override public void onDisable() { PortalHandler.closeAllPortals(); PortalRegistry.clearPortals(); stargateConfig.clearManagedWorlds(); getServer().getScheduler().cancelTasks(this); } @Override public void onEnable() { PluginDescriptionFile pluginDescriptionFile = this.getDescription(); pluginManager = getServer().getPluginManager(); FileConfiguration newConfig = this.getConfig(); logger = Logger.getLogger("Minecraft"); server = getServer(); stargate = this; 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 from happening. registerEventListeners(); //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); } /** * Registers all event listeners */ private void registerEventListeners() { pluginManager.registerEvents(new PlayerEventListener(), this); pluginManager.registerEvents(new BlockEventListener(), this); pluginManager.registerEvents(new VehicleEventListener(), this); pluginManager.registerEvents(new EntityEventListener(), this); pluginManager.registerEvents(new PortalEventListener(), this); pluginManager.registerEvents(new WorldEventListener(), this); pluginManager.registerEvents(new PluginEventListener(this), this); pluginManager.registerEvents(new TeleportEventListener(), this); } /** * Registers a command for this plugin */ private void registerCommands() { PluginCommand stargateCommand = this.getCommand("stargate"); if (stargateCommand != null) { stargateCommand.setExecutor(new CommandStarGate()); stargateCommand.setTabCompleter(new StarGateTabCompleter()); } } /** * Gets the chunk unload queue containing chunks to unload * * @return

The chunk unload queue

*/ public static Queue getChunkUnloadQueue() { return chunkUnloadQueue; } /** * Adds a new chunk unload request to the chunk unload queue * * @param request

The new chunk unload request to add

*/ public static void addChunkUnloadRequest(ChunkUnloadRequest request) { chunkUnloadQueue.removeIf((item) -> item.getChunkToUnload().equals(request.getChunkToUnload())); chunkUnloadQueue.add(request); } /** * Gets the stargate configuration * * @return

The stargate configuration

*/ public static StargateConfig getStargateConfig() { return stargateConfig; } }