Impl in-game server config editing

This commit is contained in:
Stephen Seo 2022-06-10 12:05:19 +09:00
parent bfd584ad69
commit c094f9b690
3 changed files with 230 additions and 56 deletions

View file

@ -961,6 +961,73 @@ public class ClientProxy extends CommonProxy {
parent.append(sub);
}
sub = new TextComponent("creeper_explode_turn ");
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.YELLOW)
.withBold(true)
.withHoverEvent(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
new TextComponent("The number of turns it takes for a creeper to explode")
)));
parent.append(sub);
for (int i = 1; i <= 10; ++i) {
sub = new TextComponent(String.valueOf(i) + ' ');
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.GREEN)
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND,
"/tbm-server-edit creeper_explode_turn " + i)));
parent.append(sub);
}
sub = new TextComponent("creeper_stop_explode_on_leave_battle ");
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.YELLOW)
.withBold(true)
.withHoverEvent(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
new TextComponent("Stops creepers from exploding when they leave battle (during leave battle cooldown)")
)));
parent.append(sub);
sub = new TextComponent("true ");
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.GREEN)
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND,
"/tbm-server-edit creeper_stop_explode_on_leave_battle true")));
parent.append(sub);
sub = new TextComponent("false ");
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.GREEN)
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND,
"/tbm-server-edit creeper_stop_explode_on_leave_battle false")));
parent.append(sub);
sub = new TextComponent("creeper_always_allow_damage ");
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.YELLOW)
.withBold(true)
.withHoverEvent(new HoverEvent(
HoverEvent.Action.SHOW_TEXT,
new TextComponent("Allows creepers to damage anyone who just left battle (in cooldown)")
)));
parent.append(sub);
sub = new TextComponent("true ");
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.GREEN)
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND,
"/tbm-server-edit creeper_always_allow_damage true")));
parent.append(sub);
sub = new TextComponent("false ");
sub.setStyle(sub.getStyle()
.withColor(ChatFormatting.GREEN)
.withClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND,
"/tbm-server-edit creeper_always_allow_damage false")));
parent.append(sub);
TurnBasedMinecraftMod.proxy.displayComponent(parent);
break;
}

View file

@ -1410,7 +1410,24 @@ public class Config
public int getCreeperExplodeTurn() { return creeperExplodeTurn; }
public void setCreeperExplodeTurn(int turns) {
if (turns < 1) {
turns = 1;
} else if (turns > 10) {
turns = 10;
}
creeperExplodeTurn = turns;
}
public boolean getCreeperStopExplodeOnLeaveBattle() { return creeperStopExplodeOnLeaveBattle; }
public void setCreeperStopExplodeOnLeaveBattle(boolean stop_explode_on_leave_battle) {
creeperStopExplodeOnLeaveBattle = stop_explode_on_leave_battle;
}
public boolean getCreeperAlwaysAllowDamage() { return creeperAlwaysAllowDamage; }
public void setCreeperAlwaysAllowDamage(boolean allow_damage) {
creeperAlwaysAllowDamage = allow_damage;
}
}

View file

@ -1367,6 +1367,96 @@ public class TurnBasedMinecraftMod {
}
return 1;
})))
.then(Commands.literal("creeper_explode_turn").executes(c -> {
TextComponent parent = new TextComponent("Use ");
TextComponent sub = new TextComponent("/tbm-server-edit creeper_explode_turn <1-10>");
sub.setStyle(sub.getStyle().withColor(ChatFormatting.YELLOW));
parent.append(sub);
c.getSource().sendSuccess(parent, false);
return 1;
})
.then(Commands.argument("turns", IntegerArgumentType.integer()).executes(c -> {
int turns = IntegerArgumentType.getInteger(c, "turns");
// setCreeperExplodeTurn() in Config validates the value. Set it, then fetch it again.
TurnBasedMinecraftMod.proxy.getConfig().setCreeperExplodeTurn(turns);
turns = TurnBasedMinecraftMod.proxy.getConfig().getCreeperExplodeTurn();
if (!TurnBasedMinecraftMod.proxy.getConfig().updateConfig("server_config.creeper_explode_turn", turns)) {
TurnBasedMinecraftMod.logger.warn(
"Failed to set \"server_config.creeper_explode_turn\" in config file!");
c.getSource().sendFailure(new TextComponent(
"Failed to set creeper_explode_turn to \""
+ turns
+ "\" in config file!"));
} else {
TextComponent response = new TextComponent("Successfully set creeper_explode_turn to: ");
TextComponent subResponse = new TextComponent(String.valueOf(turns));
subResponse.setStyle(subResponse.getStyle().withColor(ChatFormatting.GREEN));
response.append(subResponse);
c.getSource().sendSuccess(response, true);
}
return 1;
})))
.then(Commands.literal("creeper_stop_explode_on_leave_battle").executes(c -> {
TextComponent parent = new TextComponent("Use ");
TextComponent sub = new TextComponent("/tbm-server-edit creeper_stop_explode_on_leave_battle <true/false>");
sub.setStyle(sub.getStyle().withColor(ChatFormatting.YELLOW));
parent.append(sub);
c.getSource().sendSuccess(parent, false);
return 1;
})
.then(Commands.argument("stop_explode_on_leave", BoolArgumentType.bool()).executes(c -> {
boolean stop_explode_on_leave = BoolArgumentType.getBool(c, "stop_explode_on_leave");
TurnBasedMinecraftMod.proxy.getConfig().setCreeperStopExplodeOnLeaveBattle(stop_explode_on_leave);
if (!TurnBasedMinecraftMod.proxy.getConfig().updateConfig("server_config.creeper_stop_explode_on_leave_battle", stop_explode_on_leave)) {
TurnBasedMinecraftMod.logger.warn(
"Failed to set \"server_config.creeper_stop_explode_on_leave_battle\" in config file!"
);
c.getSource().sendFailure(new TextComponent(
"Failed to set creeper_stop_explode_on_leave_battle to \""
+ stop_explode_on_leave
+ "\" in config file!"
));
} else {
TextComponent response = new TextComponent("Successfully set creeper_stop_explode_on_leave_battle to: ");
TextComponent subResponse = new TextComponent(String.valueOf(stop_explode_on_leave));
subResponse.setStyle(subResponse.getStyle().withColor(ChatFormatting.GREEN));
response.append(subResponse);
c.getSource().sendSuccess(response, true);
}
return 1;
})))
.then(Commands.literal("creeper_always_allow_damage").executes(c -> {
TextComponent parent = new TextComponent("Use ");
TextComponent sub = new TextComponent("/tbm-server-edit creeper_always_allow_damage <true/false>");
sub.setStyle(sub.getStyle().withColor(ChatFormatting.YELLOW));
parent.append(sub);
c.getSource().sendSuccess(parent, false);
return 1;
})
.then(Commands.argument("allow_damage", BoolArgumentType.bool()).executes(c -> {
boolean allow_damage = BoolArgumentType.getBool(c, "allow_damage");
TurnBasedMinecraftMod.proxy.getConfig().setCreeperAlwaysAllowDamage(allow_damage);
if (!TurnBasedMinecraftMod.proxy.getConfig().updateConfig("server_config.creeper_always_allow_damage", allow_damage)) {
TurnBasedMinecraftMod.logger.warn(
"Failed to set \"server_config.creeper_always_allow_damage\" in config file!"
);
c.getSource().sendFailure(new TextComponent(
"Failed to set creeper_always_allow_damage to \""
+ allow_damage
+ "\" in config file!"
));
} else {
TextComponent response = new TextComponent("Successfully set creeper_always_allow_damage to: ");
TextComponent subResponse = new TextComponent(String.valueOf(allow_damage));
subResponse.setStyle(subResponse.getStyle().withColor(ChatFormatting.GREEN));
response.append(subResponse);
c.getSource().sendSuccess(response, true);
}
return 1;
})))
);
}