options to env handling

This commit is contained in:
2026-06-05 00:38:12 +02:00
parent 37ce941e02
commit e52763c35e
5 changed files with 90 additions and 8 deletions

60
src/cli/env_settings.rs Normal file
View File

@ -0,0 +1,60 @@
use clap::{ Arg, ArgMatches, ArgAction};
pub fn get_args() -> [Arg; 22] {
[
Arg::new("VERSION").short('v').long("version").help("Minecraft version of the server").default_value("LATEST"),
Arg::new("MEMORY").short('m').long("memory").help("How much ram does this server have").default_value("4G"),
Arg::new("MAX_PLAYERS").short('p').long("max-players").help("Max players").default_value("20"),
Arg::new("SPAWN_PROTECTION").short('s').long("spawn-protection").help("spawn protection size").default_value("0"),
Arg::new("ONLINE_MODE").short('o').long("online-mode").help("false to allow cracked accounts to join").default_value("true"),
Arg::new("TYPE").short('t').long("type").help("Server type (VANILLA, FORGE, NEOFORGE, PAPER)").default_value("VANILLA"),
Arg::new("FORGE_VERSION").long("forge-version").help("forge version if set type is FORGE or NEOFORGE").default_value("latest"), //TODO diff handle
Arg::new("MOTD").short('M').long("motd").help("MOTD of the server (only shown when the server is running)"),
Arg::new("DIFFICULTY").short('d').long("difficulty").help("Difficulty of the server").default_value("easy"),
Arg::new("WHITELIST").short('w').long("whitelist").help("comma separated list of the playsrs to whitelist (disabled if not provied)"), //TODO diff handle
Arg::new("OPS").short('O').long("ops").help("comma separated list of the players to give operator permissions to"),
Arg::new("MODE").long("gamemode").default_value("survival"),
Arg::new("CUSTOM_SERVER_PROPERTIES").long("custom-property").help("add a custom property").action(ArgAction::Append), //TODO diff handle
Arg::new("ALLOW_FLIGHT").short('f').long("alllow-flight").help("Allow flight in suvival mode").default_value("false"),
Arg::new("ALLOW_NETHER").short('n').long("allow-nether").help("Enable the nether dimension").default_value("true"),
Arg::new("HARDCORE").short('H').long("hardcore").help("Enable hardcore mode").default_value("false"),
Arg::new("MAX_TICK_TIME").short('T').long("max-tick-time").help("how long should it wait for a tick before crashing").default_value("-1").allow_negative_numbers(true),
Arg::new("PAUSE_WHEN_EMPTY_SECONDS").long("pause-when-empty-seconds").help("pause when empty for more than selected number of seconds").default_value("-1").allow_negative_numbers(true),
Arg::new("PVP").short('P').long("pvp").help("enable pvp").default_value("true"),
Arg::new("SPAWN_ANIMALS").long("spawn-animals").help("spawn animals").default_value("true"),
Arg::new("SPAWN_MONSTERS").long("spawn-monsters").help("spawn monsters").default_value("true"),
Arg::new("SPAWN_NPCS").long("spawn-npcs").help("spawn npcs").default_value("true"),
]
}
pub fn args_to_env(arguments: &ArgMatches) -> String {
let default_handle = vec!["VERSION", "MEMORY", "MAX_PLAYERS", "SPAWN_PROTECTION", "ONLINE_MODE", "TYPE", "MOTD", "DIFFICULTY", "OPS", "MODE", "ALLOW_FLIGHT",
"ALLOW_NETHER", "HARDCORE", "MAX_TICK_TIME", "PAUSE_WHEN_EMPTY_SECONDS", "PVP", "SPAWN_ANIMALS", "SPAWN_MONSTERS", "SPAWN_NPCS"];
let mut res = vec![];
for setting in default_handle {
if let Some(val) = arguments.get_one::<String>(setting) {
res.push((setting.to_string(), val.to_string()));
}
}
if let Some(value) = arguments.get_one::<String>("FORGE_VERSION") {
res.push(("FORGE_VERSION".to_string(), value.to_string()));
res.push(("NEOFORGE_VERSION".to_string(), value.to_string()));
}
if let Some(value) = arguments.get_one::<String>("WHITELIST") {
res.push(("ENABLE_WHITELIST".to_string(), "true".to_string()));
res.push(("WHITELIST".to_string(),value.to_string()));
}
if let Some(value) = arguments.get_many::<String>("CUSTOM_SERVER_PROPERTIES") {
let properties = value.collect::<Vec<&String>>().iter().map(|x| x.to_string()).collect::<Vec<String>>().join("\n");
res.push(("CUSTOM_SERVER_PROPERTIES".to_string(), properties));
}
// println!("\n\n\narguments : {:?}\nignored:{:?}", arguments, ignored);
res.iter()
.map(|x| format!("{}=\"{}\"", x.0, x.1))
.collect::<Vec<String>>().join("\n")
}