/*
 * This file is part of Sponge, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered <https://www.spongepowered.org>
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.test;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.args.GenericArguments;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.event.Listener;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.service.pagination.PaginationService;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.world.storage.WorldProperties;

import java.util.Optional;
import java.util.stream.Collectors;

@Plugin(id = "gamerulestest", name = "Gamerules Test", description = "A plugin to view and set gamerules.", version = "0.0.0")
public class GamerulesTest {

    @Listener
    public void onInit(GameInitializationEvent event) {
        Sponge.getCommandManager().register(
                this,
                CommandSpec.builder()
                    .arguments(
                            GenericArguments.world(Text.of("world")),
                            GenericArguments.optional(GenericArguments.string(Text.of("gamerule"))))
                    .executor((src, args) -> {
                        WorldProperties worldProperties = args.requireOne("world");
                        Optional<String> rule = args.getOne("gamerule");
                        if (rule.isPresent()) {
                            String value = worldProperties.getGameRule(rule.get()).orElse("<not set>");
                            src.sendMessage(Text.of("World: ", worldProperties.getWorldName(), " - Gamerule: ", rule.get(), " - Value: ", value));
                        } else {
                            Sponge.getServiceManager().provideUnchecked(PaginationService.class)
                                    .builder()
                                    .title(Text.of("Gamerules for world ", worldProperties.getWorldName()))
                                    .contents(
                                            worldProperties.getGameRules().entrySet().stream()
                                                    .map(x -> Text.of(x.getKey(), " = ", x.getValue()))
                                                    .collect(Collectors.toList())
                                    )
                                    .sendTo(src);
                        }

                        return CommandResult.success();
                    })
                    .build(),
                "viewgamerules"
        );

        Sponge.getCommandManager().register(
                this,
                CommandSpec.builder()
                        .arguments(
                                GenericArguments.world(Text.of("world")),
                                GenericArguments.optional(GenericArguments.string(Text.of("gamerule"))))
                        .executor((src, args) -> {
                            WorldProperties worldProperties = args.requireOne("world");
                            Optional<String> rule = args.getOne("gamerule");
                            if (rule.isPresent()) {
                                String value = worldProperties.getGameRule(rule.get()).orElse("<not set>");
                                src.sendMessage(Text.of("World: ", worldProperties.getWorldName(), " - Gamerule: ", rule, " - Value: ", value));
                            } else {
                                Sponge.getServiceManager().provideUnchecked(PaginationService.class)
                                        .builder()
                                        .title(Text.of("Gamerules for world ", worldProperties.getWorldName()))
                                        .contents(
                                                worldProperties.getGameRules().entrySet().stream()
                                                        .map(x -> Text.of(x.getKey(), " = ", x.getValue()))
                                                        .collect(Collectors.toList())
                                        )
                                        .sendTo(src);
                            }

                            return CommandResult.success();
                        })
                        .build(),
                "viewgamerules"
        );

        Sponge.getCommandManager().register(this,
                CommandSpec.builder()
                        .arguments(
                                GenericArguments.world(Text.of("world")),
                                GenericArguments.string(Text.of("gamerule")),
                                GenericArguments.string(Text.of("value")))
                        .executor((src, args) -> {
                            WorldProperties worldProperties = args.requireOne("world");
                            String key = args.requireOne("gamerule");
                            String value = args.requireOne("value");

                            worldProperties.setGameRule(key, value);
                            src.sendMessage(Text.of("Set gamerule ", key, " to value ", value, " on world ", worldProperties.getWorldName()));
                            return CommandResult.success();
                        }).build(),
                "setgamerule");
    }

}

@@ -3793,1786 +3793,8 @@
 );%0A%0A
-        Sponge.getCommandManager().register(%0A                this,%0A                CommandSpec.builder()%0A                        .arguments(%0A                                GenericArguments.world(Text.of(%22world%22)),%0A                                GenericArguments.optional(GenericArguments.string(Text.of(%22gamerule%22))))%0A                        .executor((src, args) -%3E %7B%0A                            WorldProperties worldProperties = args.requireOne(%22world%22);%0A                            Optional%3CString%3E rule = args.getOne(%22gamerule%22);%0A                            if (rule.isPresent()) %7B%0A                                String value = worldProperties.getGameRule(rule.get()).orElse(%22%3Cnot set%3E%22);%0A                                src.sendMessage(Text.of(%22World: %22, worldProperties.getWorldName(), %22 - Gamerule: %22, rule, %22 - Value: %22, value));%0A                            %7D else %7B%0A                                Sponge.getServiceManager().provideUnchecked(PaginationService.class)%0A                                        .builder()%0A                                        .title(Text.of(%22Gamerules for world %22, worldProperties.getWorldName()))%0A                                        .contents(%0A                                                worldProperties.getGameRules().entrySet().stream()%0A                                                        .map(x -%3E Text.of(x.getKey(), %22 = %22, x.getValue()))%0A                                                        .collect(Collectors.toList())%0A                                        )%0A                                        .sendTo(src);%0A                            %7D%0A%0A                            return CommandResult.success();%0A                        %7D)%0A                        .build(),%0A                %22viewgamerules%22%0A        );%0A%0A
     

Delete duplicated command in test plugin