package org.spigotmc; import com.google.common.base.Throwables; import java.io.File; import java.io.IOException; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import java.util.logging.Level; import net.minecraft.core.registries.BuiltInRegistries; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import org.bukkit.Bukkit; import org.bukkit.ChatColor; import org.bukkit.command.Command; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.InvalidConfigurationException; import org.bukkit.configuration.file.YamlConfiguration; public class SpigotConfig { private static File CONFIG_FILE; private static final String HEADER = "This is the main configuration file for Spigot.\n" + "As you can see, there's tons to configure. Some options may impact gameplay, so use\n" + "with caution, and make sure you know what each option does before configuring.\n" + "For a reference for any variable inside this file, check out the Spigot wiki at\n" + "http://www.spigotmc.org/wiki/spigot-configuration/\n" + "\n" + "If you need help with the configuration or have any questions related to Spigot,\n" + "join us at the Discord or drop by our forums and leave a post.\n" + "\n" + "Discord: https://www.spigotmc.org/go/discord\n" + "Forums: http://www.spigotmc.org/\n"; /*========================================================================*/ public static YamlConfiguration config; static int version; static Map commands; /*========================================================================*/ private static Metrics metrics; public static void init(File configFile) { SpigotConfig.CONFIG_FILE = configFile; SpigotConfig.config = new YamlConfiguration(); try { SpigotConfig.config.load( SpigotConfig.CONFIG_FILE ); } catch ( IOException ex ) { } catch ( InvalidConfigurationException ex ) { Bukkit.getLogger().log( Level.SEVERE, "Could not load spigot.yml, please correct your syntax errors", ex ); throw Throwables.propagate( ex ); } SpigotConfig.config.options().header( SpigotConfig.HEADER ); SpigotConfig.config.options().copyDefaults( true ); SpigotConfig.commands = new HashMap(); SpigotConfig.commands.put( "spigot", new SpigotCommand( "spigot" ) ); SpigotConfig.version = SpigotConfig.getInt( "config-version", 12 ); SpigotConfig.set( "config-version", 12 ); SpigotConfig.readConfig( SpigotConfig.class, null ); } public static void registerCommands() { for ( Map.Entry entry : SpigotConfig.commands.entrySet() ) { MinecraftServer.getServer().server.getCommandMap().register( entry.getKey(), "Spigot", entry.getValue() ); } if ( SpigotConfig.metrics == null ) { try { SpigotConfig.metrics = new Metrics(); SpigotConfig.metrics.start(); } catch ( IOException ex ) { Bukkit.getServer().getLogger().log( Level.SEVERE, "Could not start metrics service", ex ); } } } static void readConfig(Class clazz, Object instance) { for ( Method method : clazz.getDeclaredMethods() ) { if ( Modifier.isPrivate( method.getModifiers() ) ) { if ( method.getParameterTypes().length == 0 && method.getReturnType() == Void.TYPE ) { try { method.setAccessible( true ); method.invoke( instance ); } catch ( InvocationTargetException ex ) { throw Throwables.propagate( ex.getCause() ); } catch ( Exception ex ) { Bukkit.getLogger().log( Level.SEVERE, "Error invoking " + method, ex ); } } } } try { SpigotConfig.config.save( SpigotConfig.CONFIG_FILE ); } catch ( IOException ex ) { Bukkit.getLogger().log( Level.SEVERE, "Could not save " + SpigotConfig.CONFIG_FILE, ex ); } } private static void set(String path, Object val) { SpigotConfig.config.set( path, val ); } private static boolean getBoolean(String path, boolean def) { SpigotConfig.config.addDefault( path, def ); return SpigotConfig.config.getBoolean( path, SpigotConfig.config.getBoolean( path ) ); } private static int getInt(String path, int def) { SpigotConfig.config.addDefault( path, def ); return SpigotConfig.config.getInt( path, SpigotConfig.config.getInt( path ) ); } private static List getList(String path, T def) { SpigotConfig.config.addDefault( path, def ); return (List) SpigotConfig.config.getList( path, SpigotConfig.config.getList( path ) ); } private static String getString(String path, String def) { SpigotConfig.config.addDefault( path, def ); return SpigotConfig.config.getString( path, SpigotConfig.config.getString( path ) ); } private static double getDouble(String path, double def) { SpigotConfig.config.addDefault( path, def ); return SpigotConfig.config.getDouble( path, SpigotConfig.config.getDouble( path ) ); } public static boolean logCommands; private static void logCommands() { SpigotConfig.logCommands = SpigotConfig.getBoolean( "commands.log", true ); } public static int tabComplete; public static boolean sendNamespaced; private static void tabComplete() { if ( SpigotConfig.version < 6 ) { boolean oldValue = SpigotConfig.getBoolean( "commands.tab-complete", true ); if ( oldValue ) { SpigotConfig.set( "commands.tab-complete", 0 ); } else { SpigotConfig.set( "commands.tab-complete", -1 ); } } SpigotConfig.tabComplete = SpigotConfig.getInt( "commands.tab-complete", 0 ); SpigotConfig.sendNamespaced = SpigotConfig.getBoolean( "commands.send-namespaced", true ); } public static String whitelistMessage; public static String unknownCommandMessage; public static String serverFullMessage; public static String outdatedClientMessage = "Outdated client! Please use {0}"; public static String outdatedServerMessage = "Outdated server! I\'m still on {0}"; private static String transform(String s) { return ChatColor.translateAlternateColorCodes( '&', s ).replaceAll( "\\\\n", "\n" ); } private static void messages() { if (SpigotConfig.version < 8) { SpigotConfig.set( "messages.outdated-client", SpigotConfig.outdatedClientMessage ); SpigotConfig.set( "messages.outdated-server", SpigotConfig.outdatedServerMessage ); } SpigotConfig.whitelistMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.whitelist", "You are not whitelisted on this server!" ) ); SpigotConfig.unknownCommandMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.unknown-command", "Unknown command. Type \"/help\" for help." ) ); SpigotConfig.serverFullMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.server-full", "The server is full!" ) ); SpigotConfig.outdatedClientMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.outdated-client", SpigotConfig.outdatedClientMessage ) ); SpigotConfig.outdatedServerMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.outdated-server", SpigotConfig.outdatedServerMessage ) ); } public static int timeoutTime = 60; public static boolean restartOnCrash = true; public static String restartScript = "./start.sh"; public static String restartMessage; private static void watchdog() { SpigotConfig.timeoutTime = SpigotConfig.getInt( "settings.timeout-time", SpigotConfig.timeoutTime ); SpigotConfig.restartOnCrash = SpigotConfig.getBoolean( "settings.restart-on-crash", SpigotConfig.restartOnCrash ); SpigotConfig.restartScript = SpigotConfig.getString( "settings.restart-script", SpigotConfig.restartScript ); SpigotConfig.restartMessage = SpigotConfig.transform( SpigotConfig.getString( "messages.restart", "Server is restarting" ) ); SpigotConfig.commands.put( "restart", new RestartCommand( "restart" ) ); WatchdogThread.doStart( SpigotConfig.timeoutTime, SpigotConfig.restartOnCrash ); } public static boolean bungee; private static void bungee() { if ( SpigotConfig.version < 4 ) { SpigotConfig.set( "settings.bungeecord", false ); System.out.println( "Outdated config, disabling BungeeCord support!" ); } SpigotConfig.bungee = SpigotConfig.getBoolean( "settings.bungeecord", false ); } private static void nettyThreads() { int count = SpigotConfig.getInt( "settings.netty-threads", 4 ); System.setProperty( "io.netty.eventLoopThreads", Integer.toString( count ) ); Bukkit.getLogger().log( Level.INFO, "Using {0} threads for Netty based IO", count ); } public static boolean disableStatSaving; public static Map forcedStats = new HashMap<>(); private static void stats() { SpigotConfig.disableStatSaving = SpigotConfig.getBoolean( "stats.disable-saving", false ); if ( !SpigotConfig.config.contains( "stats.forced-stats" ) ) { SpigotConfig.config.createSection( "stats.forced-stats" ); } ConfigurationSection section = SpigotConfig.config.getConfigurationSection( "stats.forced-stats" ); for ( String name : section.getKeys( true ) ) { if ( section.isInt( name ) ) { try { ResourceLocation key = ResourceLocation.parse( name ); if ( BuiltInRegistries.CUSTOM_STAT.get( key ) == null ) { Bukkit.getLogger().log(Level.WARNING, "Ignoring non existent stats.forced-stats " + name); continue; } SpigotConfig.forcedStats.put( key, section.getInt( name ) ); } catch (Exception ex) { Bukkit.getLogger().log(Level.WARNING, "Ignoring invalid stats.forced-stats " + name); } } } } private static void tpsCommand() { SpigotConfig.commands.put( "tps", new TicksPerSecondCommand( "tps" ) ); } public static int playerSample; private static void playerSample() { SpigotConfig.playerSample = SpigotConfig.getInt( "settings.sample-count", 12 ); System.out.println( "Server Ping Player Sample Count: " + SpigotConfig.playerSample ); } public static int playerShuffle; private static void playerShuffle() { SpigotConfig.playerShuffle = SpigotConfig.getInt( "settings.player-shuffle", 0 ); } public static List spamExclusions; private static void spamExclusions() { SpigotConfig.spamExclusions = SpigotConfig.getList( "commands.spam-exclusions", Arrays.asList( new String[] { "/skill" } ) ); } public static boolean silentCommandBlocks; private static void silentCommandBlocks() { SpigotConfig.silentCommandBlocks = SpigotConfig.getBoolean( "commands.silent-commandblock-console", false ); } public static Set replaceCommands; private static void replaceCommands() { if ( SpigotConfig.config.contains( "replace-commands" ) ) { SpigotConfig.set( "commands.replace-commands", SpigotConfig.config.getStringList( "replace-commands" ) ); SpigotConfig.config.set( "replace-commands", null ); } SpigotConfig.replaceCommands = new HashSet( (List) SpigotConfig.getList( "commands.replace-commands", Arrays.asList( "setblock", "summon", "testforblock", "tellraw" ) ) ); } public static int userCacheCap; private static void userCacheCap() { SpigotConfig.userCacheCap = SpigotConfig.getInt( "settings.user-cache-size", 1000 ); } }