コード例 #1
0
        public void CommandPermSet(MiNET.Player player, string targetName, string targetGroupName)
        {
            if (!(player is SkyPlayer skyPlayer) || !skyPlayer.PlayerGroup.IsAtLeast(PlayerGroup.Admin))
            {
                player.SendMessage("§c§l(!)§r §cYou do not have permission for this command.");
                return;
            }

            if (String.IsNullOrEmpty(targetName))
            {
                player.SendMessage($"{ChatColors.Red}Enter a valid player name.");
                return;
            }

            SkyPlayer target = _skyCoreApi.GetPlayer(targetName);

            if (target == null || !target.IsConnected)
            {
                player.SendMessage($"{ChatColors.Red}Target player is not online.");
                return;
            }

            //Format as our Enums
            targetGroupName = targetGroupName.ToLower();
            targetGroupName = Char.ToUpper(targetGroupName.ToCharArray()[0]) + targetGroupName.Substring(1);

            if (!PlayerGroup.ValueOf(targetGroupName, out var targetGroup))
            {
                player.SendMessage($"{ChatColors.Red}Unrecognized group name '{targetGroupName}'.");
                string possibleGroups = "";
                foreach (PlayerGroup groupLoop in PlayerGroup.Values)
                {
                    possibleGroups += groupLoop.GroupName + ",";
                }

                player.SendMessage($"Possible Groups: {possibleGroups}");
                return;
            }

            target.SetPlayerGroup(targetGroup);

            RunnableTask.RunTask(() =>
            {
                new DatabaseAction().Execute(
                    "INSERT INTO `player_groups`\n" +
                    "  (`player_xuid`, `group_name`)\n" +
                    "VALUES\n" +
                    "  (@xuid, @group)\n" +
                    "ON DUPLICATE KEY UPDATE\n" +
                    "  `player_xuid`    = VALUES(`player_xuid`),\n" +
                    "  `group_name`     = VALUES(`group_name`);",
                    (command) =>
                {
                    command.Parameters.AddWithValue("@xuid", target.CertificateData.ExtraData.Xuid);
                    command.Parameters.AddWithValue("@group", targetGroup.GroupName);
                },
                    new Action(delegate
                {
                    player.SendMessage($"{ChatColors.Yellow}Updated {target.Username}'s group to {targetGroup.GroupName}");
                })
                    );
            });
        }