<commit_before>package io.github.icohedron.adminmode;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandResult;
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.event.Listener;
import org.spongepowered.api.event.entity.DamageEntityEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@Plugin(id = "adminmode", name = "Admin Mode", version = "1.0.0-S5.1",
        description = "Admin mode for survival servers")
public class AdminMode {

    private Set<UUID> active;

    @Listener
    public void onInitialization(GameInitializationEvent event) {
        active = new HashSet<>();

        CommandSpec command = CommandSpec.builder()
                .permission("adminmode.command")
                .executor((src, args) -> {
                    if (!(src instanceof Player)) {
                        src.sendMessage(Text.of(TextColors.RED, "Only a player may execute this command"));
                        return CommandResult.empty();
                    }

                    Player player = ((Player) src);
                    UUID uuid = player.getUniqueId();
                    if (active.contains(uuid)) {
                        return disableAdminMode(player);
                    } else {
                        return enableAdminMode(player);
                    }
                })
                .build();

        Sponge.getCommandManager().register(this, command, "adminmode", "am");
    }

    private CommandResult enableAdminMode(Player player) {
        if (player.supports(Keys.CAN_FLY)) {
            active.add(player.getUniqueId());
            player.offer(Keys.CAN_FLY, true);
            player.sendMessage(Text.of(TextColors.YELLOW, "You are now in admin mode"));
            return CommandResult.success();
        }
        player.sendMessage(Text.of(TextColors.RED, "Unable to enable admin mode"));
        return CommandResult.empty();
    }

    private CommandResult disableAdminMode(Player player) {
        if (player.supports(Keys.CAN_FLY)) {
            active.remove(player.getUniqueId());
            player.offer(Keys.CAN_FLY, false);
            player.sendMessage(Text.of(TextColors.YELLOW, "You are no longer in admin mode"));
            return CommandResult.success();
        }
        player.sendMessage(Text.of(TextColors.RED, "Unable to disable admin mode"));
        return CommandResult.empty();
    }

    @Listener
    public void onDamagePlayer(DamageEntityEvent event) {
        if (event.getTargetEntity() instanceof Player) {
            Player player = (Player) event.getTargetEntity();
            if (active.contains(player.getUniqueId())) {
                event.setCancelled(true);
            }
        }
    }

    @Listener
    public void onPlayerDisconnect(ClientConnectionEvent.Disconnect event, @First Player player) {
        disableAdminMode(player);
    }
}<commit_msg>Fix small bug Fix a bug in which a player was not put out of flight mode when they disable admin mode while in flight.<commit_after>package io.github.icohedron.adminmode;

import org.spongepowered.api.Sponge;
import org.spongepowered.api.command.CommandResult;
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.event.Listener;
import org.spongepowered.api.event.entity.DamageEntityEvent;
import org.spongepowered.api.event.filter.cause.First;
import org.spongepowered.api.event.game.state.GameInitializationEvent;
import org.spongepowered.api.event.network.ClientConnectionEvent;
import org.spongepowered.api.plugin.Plugin;
import org.spongepowered.api.text.Text;
import org.spongepowered.api.text.format.TextColors;

import java.util.HashSet;
import java.util.Set;
import java.util.UUID;

@Plugin(id = "adminmode", name = "Admin Mode", version = "1.0.0-S5.1",
        description = "Admin mode for survival servers")
public class AdminMode {

    private Set<UUID> active;

    @Listener
    public void onInitialization(GameInitializationEvent event) {
        active = new HashSet<>();

        CommandSpec command = CommandSpec.builder()
                .permission("adminmode.command")
                .executor((src, args) -> {
                    if (!(src instanceof Player)) {
                        src.sendMessage(Text.of(TextColors.RED, "Only a player may execute this command"));
                        return CommandResult.empty();
                    }

                    Player player = ((Player) src);
                    UUID uuid = player.getUniqueId();
                    if (active.contains(uuid)) {
                        return disableAdminMode(player);
                    } else {
                        return enableAdminMode(player);
                    }
                })
                .build();

        Sponge.getCommandManager().register(this, command, "adminmode", "am");
    }

    private CommandResult enableAdminMode(Player player) {
        if (player.supports(Keys.CAN_FLY)) {
            active.add(player.getUniqueId());
            player.offer(Keys.CAN_FLY, true);
            player.sendMessage(Text.of(TextColors.YELLOW, "You are now in admin mode"));
            return CommandResult.success();
        }
        player.sendMessage(Text.of(TextColors.RED, "Unable to enable admin mode"));
        return CommandResult.empty();
    }

    private CommandResult disableAdminMode(Player player) {
        if (player.supports(Keys.CAN_FLY) && player.supports(Keys.IS_FLYING) ) {
            active.remove(player.getUniqueId());
            player.offer(Keys.CAN_FLY, false);
            player.offer(Keys.IS_FLYING, false);
            player.sendMessage(Text.of(TextColors.YELLOW, "You are no longer in admin mode"));
            return CommandResult.success();
        }
        player.sendMessage(Text.of(TextColors.RED, "Unable to disable admin mode"));
        return CommandResult.empty();
    }

    @Listener
    public void onDamagePlayer(DamageEntityEvent event) {
        if (event.getTargetEntity() instanceof Player) {
            Player player = (Player) event.getTargetEntity();
            if (active.contains(player.getUniqueId())) {
                event.setCancelled(true);
            }
        }
    }

    @Listener
    public void onPlayerDisconnect(ClientConnectionEvent.Disconnect event, @First Player player) {
        disableAdminMode(player);
    }
}
{'fraction_non_alphanumeric': 0.10818208009219245, 'fraction_numerical': 0.0014405070584845865, 'mean_word_length': 2.4802005012531327, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '4041524', 'n_tokens_mistral': 1935, 'n_tokens_neox': 1854, 'n_words': 383}