<commit_before>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.skelril.skree.content.playerstate;

import com.skelril.skree.service.PlayerStateService;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.data.manipulator.mutable.entity.GameModeData;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static org.spongepowered.api.command.args.GenericArguments.*;

public class GameModeCommand implements CommandExecutor {

    @Override
    public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
        Optional<PlayerStateService> optService = Sponge.getServiceManager().provide(PlayerStateService.class);
        if (!optService.isPresent()) {
            src.sendMessage(Text.of(TextColors.DARK_RED, "The player state service is not currently running."));
            return CommandResult.empty();
        }
        PlayerStateService service = optService.get();

        GameMode mode = args.<GameMode>getOne("mode").get();
        Player target = args.<Player>getOne("target").get();

        GameModeData current = target.getGameModeData();
        service.save(target, current.type().get().getId());
        target.offer(current.set(Keys.GAME_MODE, mode));
        service.load(target, current.type().get().getId());

        target.sendMessage(Text.of(TextColors.YELLOW, "Changed game mode to " + mode.getName() + '.'));
        return CommandResult.success();
    }

    public static CommandSpec aquireSpec() {
        Map<String, GameMode> map = new HashMap<>();

        map.put("survival", GameModes.SURVIVAL);
        map.put("creative", GameModes.CREATIVE);
        map.put("adventure", GameModes.ADVENTURE);
        map.put("spectator", GameModes.SPECTATOR);

        return CommandSpec.builder()
                .description(Text.of("Change gamemode"))
                .permission("skree.gamemode")
                .arguments(
                        seq(
                                onlyOne(choices(Text.of("mode"), map)),
                                onlyOne(playerOrSource(Text.of("target")))
                        )<commit_msg>Updated the GameMode command to reset fall distance when switching game modes<commit_after>/*
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

package com.skelril.skree.content.playerstate;

import com.skelril.skree.service.PlayerStateService;
import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandException;
import org.spongepowered.api.command.CommandResult;
import org.spongepowered.api.command.CommandSource;
import org.spongepowered.api.command.args.CommandContext;
import org.spongepowered.api.command.spec.CommandExecutor;
import org.spongepowered.api.command.spec.CommandSpec;
import org.spongepowered.api.data.key.Keys;
import org.spongepowered.api.entity.living.player.Player;
import org.spongepowered.api.entity.living.player.gamemode.GameMode;
import org.spongepowered.api.entity.living.player.gamemode.GameModes;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import static org.spongepowered.api.command.args.GenericArguments.*;

public class GameModeCommand implements CommandExecutor {

    @Override
    public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
        Optional<PlayerStateService> optService = Sponge.getServiceManager().provide(PlayerStateService.class);
        if (!optService.isPresent()) {
            src.sendMessage(Text.of(TextColors.DARK_RED, "The player state service is not currently running."));
            return CommandResult.empty();
        }
        PlayerStateService service = optService.get();

        GameMode mode = args.<GameMode>getOne("mode").get();
        Player target = args.<Player>getOne("target").get();

        service.save(target, target.get(Keys.GAME_MODE).get().getId());
        target.offer(Keys.FALL_DISTANCE, 0F);
        target.offer(Keys.GAME_MODE, mode);
        service.load(target, mode.getId());

        target.sendMessage(Text.of(TextColors.YELLOW, "Changed game mode to " + mode.getName() + '.'));
        return CommandResult.success();
    }

    public static CommandSpec aquireSpec() {
        Map<String, GameMode> map = new HashMap<>();

        map.put("survival", GameModes.SURVIVAL);
        map.put("creative", GameModes.CREATIVE);
        map.put("adventure", GameModes.ADVENTURE);
        map.put("spectator", GameModes.SPECTATOR);

        return CommandSpec.builder()
                .description(Text.of("Change gamemode"))
                .permission("skree.gamemode")
                .arguments(
                        seq(
                                onlyOne(choices(Text.of("mode"), map)),
                                onlyOne(playerOrSource(Text.of("target")))
                        )
{'fraction_non_alphanumeric': 0.11789509192106595, 'fraction_numerical': 0.0015179625569235959, 'mean_word_length': 3.2908827785817656, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5327160', 'n_tokens_mistral': 1753, 'n_tokens_neox': 1678, 'n_words': 326}