Added configurable global server rules

This commit is contained in:
Anuken
2019-11-16 15:51:35 -05:00
parent 1f6ef9d9ae
commit f9ffb78b33
8 changed files with 84 additions and 15 deletions
@@ -8,6 +8,8 @@ import io.anuke.arc.util.*;
import io.anuke.arc.util.Timer;
import io.anuke.arc.util.CommandHandler.*;
import io.anuke.arc.util.Timer.*;
import io.anuke.arc.util.serialization.*;
import io.anuke.arc.util.serialization.JsonValue.*;
import io.anuke.mindustry.*;
import io.anuke.mindustry.core.GameState.*;
import io.anuke.mindustry.core.*;
@@ -59,7 +61,8 @@ public class ServerControl implements ApplicationListener{
"crashreport", false,
"port", port,
"logging", true,
"socket", false
"socket", false,
"globalrules", "{}"
);
Log.setLogger(new LogHandler(){
@@ -250,8 +253,9 @@ public class ServerControl implements ApplicationListener{
logic.reset();
lastMode = preset;
try{
world.loadMap(result, result.applyRules(lastMode));
world.loadMap(result, result.applyRules(lastMode));
state.rules = result.applyRules(preset);
applyRules();
logic.play();
info("Map loaded.");
@@ -348,6 +352,58 @@ public class ServerControl implements ApplicationListener{
}
});
handler.register("rules", "[remove/add] [name] [value...]", "List, remove or add global rules. These will apply regardless of map.", arg -> {
String rules = Core.settings.getString("globalrules");
JsonValue base = JsonIO.json().fromJson(null, rules);
if(arg.length == 0){
Log.info("&lyRules:\n{0}", JsonIO.print(rules));
}else if(arg.length == 1){
Log.err("Invalid usage. Specify which rule to remove or add.");
}else{
if(!(arg[0].equals("remove") || arg[0].equals("add"))){
Log.err("Invalid usage. Either add or remove rules.");
return;
}
boolean remove = arg[0].equals("remove");
if(remove){
if(base.has(arg[1])){
Log.info("Rule &lc'{0}'&lg removed.", arg[1]);
base.remove(arg[1]);
}else{
Log.err("Rule not defined, so not removed.");
return;
}
}else{
if(arg.length < 3){
Log.err("Missing last argument. Specify which value to set the rule to.");
return;
}
try{
JsonValue value = new JsonReader().parse(arg[2]);
value.name = arg[1];
JsonValue parent = new JsonValue(ValueType.object);
parent.addChild(value);
JsonIO.json().readField(state.rules, value.name, parent);
if(base.has(value.name)){
base.remove(value.name);
}
base.addChild(arg[1], value);
Log.info("Changed rule: &ly{0}", value.toString().replace("\n", " "));
}catch(Throwable e){
Log.err("Error parsing rule JSON", e);
}
}
Core.settings.putSave("globalrules", base.toString());
Call.onSetRules(state.rules);
}
});
handler.register("fillitems", "[team]", "Fill the core with items.", arg -> {
if(!state.is(State.playing)){
err("Not playing. Host first.");
@@ -744,6 +800,15 @@ public class ServerControl implements ApplicationListener{
mods.each(p -> p.registerClientCommands(netServer.clientCommands));
}
private void applyRules(){
try{
JsonValue value = JsonIO.json().fromJson(null, Core.settings.getString("globalrules"));
JsonIO.json().readFields(state.rules, value);
}catch(Throwable t){
Log.err("Error applying custom rules, proceeding without them.", t);
}
}
private void displayStatus() {
if(state.is(State.menu)){
info("Status: &rserver closed");
@@ -824,6 +889,7 @@ public class ServerControl implements ApplicationListener{
run.run();
logic.play();
state.rules = world.getMap().applyRules(lastMode);
applyRules();
for(Player p : players){
if(p.con == null) continue;