Skip to content

Commit

Permalink
modifyコマンドの利便性向上
Browse files Browse the repository at this point in the history
  • Loading branch information
Maru32768 committed Dec 31, 2022
1 parent 943f66b commit a746153
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import net.kunmc.lab.commandlib.Command;
import net.kunmc.lab.configlib.util.ConfigUtil;
import org.bukkit.command.CommandSender;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Set;

class ConfigModifyCommand extends Command {
Expand All @@ -28,7 +30,7 @@ public ConfigModifyCommand(Set<BaseConfig> configSet) {
}
}

private void init(BaseConfig config, Command command) {
private static void init(BaseConfig config, Command command) {
for (Field field : ConfigUtil.getSingleValueFields(config)) {
if (Modifier.isStatic(field.getModifiers())) {
continue;
Expand All @@ -49,7 +51,13 @@ private void init(BaseConfig config, Command command) {
}

command.addChildren(new Command(field.getName()) {{
addChildren(new ModifySetCommand(field, v, config));
execute(ctx -> {
v.sendListMessage(ctx, field.getName());
});
applySet(this, field, v, config);
addChildren(new Command("set") {{
applySet(this, field, v, config);
}});

if (v instanceof NumericValue) {
addChildren(new ModifyIncCommand(field, ((NumericValue<?, ?>) v), config));
Expand Down Expand Up @@ -108,4 +116,36 @@ private void init(BaseConfig config, Command command) {
}});
}
}

private static void applySet(Command command, Field field, SingleValue value, BaseConfig config) {
command.argument(builder -> {
value.appendArgument(builder);

builder.execute(ctx -> {
String entryName = field.getName();

List<Object> argument = ctx.getParsedArgs();
CommandSender sender = ctx.getSender();
if (!value.isCorrectArgument(entryName, argument, sender)) {
ctx.sendFailure(value.incorrectArgumentMessage(entryName, argument, sender));
return;
}

Object newValue = value.argumentToValue(argument, sender);
if (!value.validateOnSet(entryName, newValue, sender)) {
ctx.sendFailure(value.invalidValueMessage(entryName, newValue, sender));
return;
}

if (value.onModifyValue(newValue, ctx)) {
return;
}

value.value(newValue);
ctx.sendSuccess(value.succeedModifyMessage(entryName));

config.saveConfigIfPresent();
});
});
}
}
43 changes: 0 additions & 43 deletions bukkit/src/main/java/net/kunmc/lab/configlib/ModifySetCommand.java

This file was deleted.

10 changes: 5 additions & 5 deletions bukkit/test_plugin/server/server.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#Minecraft server properties
#Sun Jan 01 00:39:18 JST 2023
#Sun Jan 01 01:00:09 JST 2023
spawn-protection=16
max-tick-time=-1
query.port=25565
Expand Down Expand Up @@ -42,12 +42,12 @@ spawn-animals=true
white-list=false
rcon.password=
generate-structures=true
online-mode=false
max-build-height=256
online-mode=false
level-seed=
prevent-proxy-connections=false
use-native-transport=true
prevent-proxy-connections=false
enable-jmx-monitoring=false
rate-limit=0
motd=A Minecraft Server
enable-rcon=false
motd=A Minecraft Server
rate-limit=0
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import net.kunmc.lab.commandlib.Command;
import net.kunmc.lab.configlib.util.ConfigUtil;
import net.minecraft.command.CommandSource;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;
import java.util.List;
import java.util.Set;

class ConfigModifyCommand extends Command {
Expand Down Expand Up @@ -49,7 +51,13 @@ private void init(BaseConfig config, Command command) {
}

command.addChildren(new Command(field.getName()) {{
addChildren(new ModifySetCommand(field, v, config));
execute(ctx -> {
v.sendListMessage(ctx, field.getName());
});
applySet(this, field, v, config);
addChildren(new Command("set") {{
applySet(this, field, v, config);
}});

if (v instanceof NumericValue) {
addChildren(new ModifyIncCommand(field, ((NumericValue<?, ?>) v), config));
Expand Down Expand Up @@ -108,4 +116,36 @@ private void init(BaseConfig config, Command command) {
}});
}
}

private static void applySet(Command command, Field field, SingleValue value, BaseConfig config) {
command.argument(builder -> {
value.appendArgument(builder);

builder.execute(ctx -> {
String entryName = field.getName();

List<Object> argument = ctx.getParsedArgs();
CommandSource sender = ctx.getSender();
if (!value.isCorrectArgument(entryName, argument, sender)) {
ctx.sendFailure(value.incorrectArgumentMessage(entryName, argument, sender));
return;
}

Object newValue = value.argumentToValue(argument, sender);
if (!value.validateOnSet(entryName, newValue, sender)) {
ctx.sendFailure(value.invalidValueMessage(entryName, newValue, sender));
return;
}

if (value.onModifyValue(newValue, ctx)) {
return;
}

value.value(newValue);
ctx.sendSuccess(value.succeedModifyMessage(entryName));

config.saveConfigIfPresent();
});
});
}
}
43 changes: 0 additions & 43 deletions forge/src/main/java/net/kunmc/lab/configlib/ModifySetCommand.java

This file was deleted.

0 comments on commit a746153

Please sign in to comment.