Search in sources :

Example 1 with RuleType

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();
    });
}
Also used : CommandArgs(org.spongepowered.api.command.args.CommandArgs) CommandContext(org.spongepowered.api.command.args.CommandContext) RuleType(org.lanternpowered.server.world.rules.RuleType) StartsWithPredicate(org.spongepowered.api.util.StartsWithPredicate) CommandSource(org.spongepowered.api.command.CommandSource) CommandElement(org.spongepowered.api.command.args.CommandElement) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) LanternWorldProperties(org.lanternpowered.server.world.LanternWorldProperties) LanternWorldProperties(org.lanternpowered.server.world.LanternWorldProperties) WorldProperties(org.spongepowered.api.world.storage.WorldProperties)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 List (java.util.List)1 LanternWorldProperties (org.lanternpowered.server.world.LanternWorldProperties)1 RuleType (org.lanternpowered.server.world.rules.RuleType)1 CommandSource (org.spongepowered.api.command.CommandSource)1 CommandArgs (org.spongepowered.api.command.args.CommandArgs)1 CommandContext (org.spongepowered.api.command.args.CommandContext)1 CommandElement (org.spongepowered.api.command.args.CommandElement)1 StartsWithPredicate (org.spongepowered.api.util.StartsWithPredicate)1 WorldProperties (org.spongepowered.api.world.storage.WorldProperties)1