use of org.lanternpowered.server.world.rules.RuleType in project LanternServer by LanternPowered.
the class CommandGameRule method completeSpec.
@Override
public void completeSpec(PluginContainer pluginContainer, CommandSpec.Builder specBuilder) {
final Collection<String> defaultRules = Sponge.getRegistry().getDefaultGameRules();
final ThreadLocal<RuleType<?>> currentRule = new ThreadLocal<>();
specBuilder.arguments(GenericArguments.flags().valueFlag(GenericArguments.world(CommandHelper.WORLD_KEY), "-world", "w").buildWith(GenericArguments.none()), new CommandElement(Text.of("rule")) {
@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
RuleType<?> ruleType = RuleType.getOrCreate(args.next(), RuleDataTypes.STRING, "");
currentRule.set(ruleType);
return ruleType;
}
@Override
public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
final String prefix = args.nextIfPresent().orElse("");
return defaultRules.stream().filter(new StartsWithPredicate(prefix)).collect(ImmutableList.toImmutableList());
}
}, new CommandElement(Text.of("value")) {
private final List<String> booleanRuleSuggestions = ImmutableList.of("true", "false");
@Nullable
@Override
protected Object parseValue(CommandSource source, CommandArgs args) throws ArgumentParseException {
RuleType<?> ruleType = currentRule.get();
currentRule.remove();
try {
return ruleType.getDataType().parse(args.next());
} catch (IllegalArgumentException e) {
throw args.createError(t(e.getMessage()));
}
}
@Override
public List<String> complete(CommandSource src, CommandArgs args, CommandContext context) {
RuleType<?> ruleType = context.<RuleType<?>>getOne("rule").get();
if (ruleType.getDataType() == RuleDataTypes.BOOLEAN) {
// match the first part of the string
return this.booleanRuleSuggestions;
}
return Collections.emptyList();
}
}).executor((src, args) -> {
WorldProperties world = CommandHelper.getWorldProperties(src, args);
Object value = args.getOne("value").get();
RuleType ruleType = args.<RuleType>getOne("rule").get();
((LanternWorldProperties) world).getRules().getOrCreateRule(ruleType).setValue(value);
src.sendMessage(t("commands.gamerule.success", ruleType.getName(), ruleType.getDataType().serialize(value)));
return CommandResult.success();
});
}
Aggregations