Spigot Configuration

Provides the basic infrastructure to load and save the Spigot configuration file, spigot.yml

By: md_5 <git@md-5.net>
This commit is contained in:
CraftBukkit/Spigot
2013-07-07 09:32:53 +10:00
parent 6b0bae2fb6
commit cba0d1f1ec
7 changed files with 318 additions and 27 deletions

View File

@@ -945,6 +945,7 @@ public final class CraftServer implements Server {
this.logger.log(Level.WARNING, "Failed to load banned-players.json, " + ex.getMessage());
}
org.spigotmc.SpigotConfig.init((File) this.console.options.valueOf("spigot-settings")); // Spigot
for (ServerLevel world : this.console.getAllLevels()) {
world.serverLevelData.setDifficulty(config.difficulty);
world.setSpawnSettings(config.spawnMonsters);
@@ -959,11 +960,13 @@ public final class CraftServer implements Server {
}
}
}
world.spigotConfig.init(); // Spigot
}
this.pluginManager.clearPlugins();
this.commandMap.clearCommands();
this.reloadData();
org.spigotmc.SpigotConfig.registerCommands(); // Spigot
this.overrideAllCommandBlockCommands = this.commandsConfiguration.getStringList("command-block-overrides").contains("*");
this.ignoreVanillaPermissions = this.commandsConfiguration.getBoolean("ignore-vanilla-permissions");

View File

@@ -134,6 +134,14 @@ public class Main {
this.acceptsAll(Main.asList("demo"), "Demo mode");
this.acceptsAll(Main.asList("initSettings"), "Only create configuration files and then exit"); // SPIGOT-5761: Add initSettings option
// Spigot Start
this.acceptsAll(Main.asList("S", "spigot-settings"), "File for spigot settings")
.withRequiredArg()
.ofType(File.class)
.defaultsTo(new File("spigot.yml"))
.describedAs("Yml file");
// Spigot End
}
};

View File

@@ -0,0 +1,44 @@
package org.spigotmc;
import java.io.File;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerLevel;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
public class SpigotCommand extends Command {
public SpigotCommand(String name) {
super(name);
this.description = "Spigot related commands";
this.usageMessage = "/spigot reload";
this.setPermission("bukkit.command.spigot");
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
if (!this.testPermission(sender)) return true;
if (args.length != 1) {
sender.sendMessage(ChatColor.RED + "Usage: " + this.usageMessage);
return false;
}
if (args[0].equals("reload")) {
Command.broadcastCommandMessage(sender, ChatColor.RED + "Please note that this command is not supported and may cause issues.");
Command.broadcastCommandMessage(sender, ChatColor.RED + "If you encounter any issues please use the /stop command to restart your server.");
MinecraftServer console = MinecraftServer.getServer();
org.spigotmc.SpigotConfig.init((File) console.options.valueOf("spigot-settings"));
for (ServerLevel world : console.getAllLevels()) {
world.spigotConfig.init();
}
console.server.reloadCount++;
Command.broadcastCommandMessage(sender, ChatColor.GREEN + "Reload complete.");
}
return true;
}
}

View File

@@ -0,0 +1,140 @@
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.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import net.minecraft.server.MinecraftServer;
import org.bukkit.Bukkit;
import org.bukkit.command.Command;
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<String, Command> commands;
/*========================================================================*/
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<String, Command>();
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<String, Command> entry : SpigotConfig.commands.entrySet() )
{
MinecraftServer.getServer().server.getCommandMap().register( entry.getKey(), "Spigot", entry.getValue() );
}
}
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 <T> List getList(String path, T def)
{
SpigotConfig.config.addDefault( path, def );
return (List<T>) 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 ) );
}
}

View File

@@ -0,0 +1,82 @@
package org.spigotmc;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.configuration.file.YamlConfiguration;
public class SpigotWorldConfig
{
private final String worldName;
private final YamlConfiguration config;
private boolean verbose;
public SpigotWorldConfig(String worldName)
{
this.worldName = worldName;
this.config = SpigotConfig.config;
this.init();
}
public void init()
{
this.verbose = this.getBoolean( "verbose", true );
this.log( "-------- World Settings For [" + this.worldName + "] --------" );
SpigotConfig.readConfig( SpigotWorldConfig.class, this );
}
private void log(String s)
{
if ( this.verbose )
{
Bukkit.getLogger().info( s );
}
}
private void set(String path, Object val)
{
this.config.set( "world-settings.default." + path, val );
}
public boolean getBoolean(String path, boolean def)
{
this.config.addDefault( "world-settings.default." + path, def );
return this.config.getBoolean( "world-settings." + this.worldName + "." + path, this.config.getBoolean( "world-settings.default." + path ) );
}
public double getDouble(String path, double def)
{
this.config.addDefault( "world-settings.default." + path, def );
return this.config.getDouble( "world-settings." + this.worldName + "." + path, this.config.getDouble( "world-settings.default." + path ) );
}
public int getInt(String path)
{
return this.config.getInt( "world-settings." + this.worldName + "." + path );
}
public int getInt(String path, int def)
{
this.config.addDefault( "world-settings.default." + path, def );
return this.config.getInt( "world-settings." + this.worldName + "." + path, this.config.getInt( "world-settings.default." + path ) );
}
public <T> List getList(String path, T def)
{
this.config.addDefault( "world-settings.default." + path, def );
return (List<T>) this.config.getList( "world-settings." + this.worldName + "." + path, this.config.getList( "world-settings.default." + path ) );
}
public String getString(String path, String def)
{
this.config.addDefault( "world-settings.default." + path, def );
return this.config.getString( "world-settings." + this.worldName + "." + path, this.config.getString( "world-settings.default." + path ) );
}
private Object get(String path, Object def)
{
this.config.addDefault( "world-settings.default." + path, def );
return this.config.get( "world-settings." + this.worldName + "." + path, this.config.get( "world-settings.default." + path ) );
}
}