From ab05b999f816e66052ad4c1c2548798c5bc9e042 Mon Sep 17 00:00:00 2001 From: CyberL1 Date: Tue, 14 May 2024 12:26:14 +0200 Subject: [PATCH] feat: add to/remove from whitelist using the `Players` menu --- MyMcRealms/Controllers/InvitesController.cs | 111 ++++++++++++++++++++ MyMcRealms/Controllers/WorldsController.cs | 1 - MyMcRealms/Responses/MinecraftPlayerInfo.cs | 8 ++ MyMcRealms/Responses/ServersResponse.cs | 4 +- MyMcRealms/Responses/WorldResponse.cs | 2 +- 5 files changed, 121 insertions(+), 5 deletions(-) create mode 100644 MyMcRealms/Controllers/InvitesController.cs create mode 100644 MyMcRealms/Responses/MinecraftPlayerInfo.cs diff --git a/MyMcRealms/Controllers/InvitesController.cs b/MyMcRealms/Controllers/InvitesController.cs new file mode 100644 index 0000000..d74f25a --- /dev/null +++ b/MyMcRealms/Controllers/InvitesController.cs @@ -0,0 +1,111 @@ +using Microsoft.AspNetCore.Mvc; +using Minecraft_Realms_Emulator.Responses; +using MyMcRealms.Attributes; +using MyMcRealms.MyMcAPI; +using MyMcRealms.Requests; +using MyMcRealms.Responses; + +namespace Minecraft_Realms_Emulator.Controllers +{ + [Route("[controller]")] + [ApiController] + [RequireMinecraftCookie] + public class InvitesController : ControllerBase + { + [HttpPost("{wId}")] + public async Task> InvitePlayer(int wId, PlayerRequest body) + { + string cookie = Request.Headers.Cookie; + string playerName = cookie.Split(";")[1].Split("=")[1]; + + if (body.Name == playerName) return Forbid("You cannot invite yourself"); + + var _api = new MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY")); + var world = (await _api.GetAllServers()).Servers[wId]; + + if (world == null) return NotFound("World not found"); + + // Get player name + var playerInfo = await new HttpClient().GetFromJsonAsync($"https://api.mojang.com/users/profiles/minecraft/{body.Name}"); + + if (world.Whitelist.Any(p => p.Name == body.Name)) return BadRequest("Player already whitelisted"); + + var api = new MyMcAPI(world.OwnersToken); + api.ExecuteCommand($"whitelist add {body.Name}"); + + List whitelistedPlayers = []; + + foreach (var player in world.Whitelist) + { + Player whitelistedPlayer = new() + { + Name = player.Name, + Uuid = player.Uuid, + Accepted = true, + Online = false, + Operator = world.Ops.Find(p => p.Name == player.Name) != null, + Permission = world.Ops.Find(p => p.Name == player.Name) != null ? "OPERATOR" : "MEMBER", + }; + + whitelistedPlayers.Add(whitelistedPlayer); + } + + Player npl = new() + { + Name = body.Name, + Uuid = playerInfo.Id, + Accepted = true, + Online = false, + Operator = world.Ops.Find(p => p.Name == body.Name) != null, + Permission = world.Ops.Find(p => p.Name == body.Name) != null ? "OPERATOR" : "MEMBER", + }; + + whitelistedPlayers.Add(npl); + + WorldResponse response = new() + { + Id = wId, + Owner = "blank", + OwnerUUID = "blank", + Name = "blank", + Motd = world.Motd, + State = world.WhitelistEnable ? "CLOSED" : "OPEN", + WorldType = "NORMAL", + MaxPlayers = 10, + MinigameId = null, + MinigameName = null, + MinigameImage = null, + ActiveSlot = 1, + Member = false, + Players = whitelistedPlayers, + DaysLeft = 7, + Expired = false, + ExpiredTrial = false, + ActiveVersion = world.GameVersion + }; + + return Ok(response); + } + + [HttpDelete("{wId}/invite/{uuid}")] + public async Task> DeleteInvite(int wId, string uuid) + { + var _api = new MyMcAPI(Environment.GetEnvironmentVariable("MYMC_API_KEY")); + var world = (await _api.GetAllServers()).Servers[wId]; + + if (world == null) return NotFound("World not found"); + + var player = world.Whitelist.Find(p => p.Uuid.Replace("-", "") == uuid); + + // Get player name + var playerInfo = await new HttpClient().GetFromJsonAsync($"https://sessionserver.mojang.com/session/minecraft/profile/{uuid}"); + + if (!world.Whitelist.Any(p => p.Uuid.Replace("-", "") == uuid)) return BadRequest("Player not whitelisted"); + + var api = new MyMcAPI(world.OwnersToken); + api.ExecuteCommand($"whitelist remove {player.Name}"); + + return Ok(true); + } + } +} \ No newline at end of file diff --git a/MyMcRealms/Controllers/WorldsController.cs b/MyMcRealms/Controllers/WorldsController.cs index 9ebe2bf..a2f00e1 100644 --- a/MyMcRealms/Controllers/WorldsController.cs +++ b/MyMcRealms/Controllers/WorldsController.cs @@ -1,6 +1,5 @@ using Microsoft.AspNetCore.Mvc; using MyMcRealms.Attributes; -using MyMcRealms.Entities; using MyMcRealms.MyMcAPI.Responses; using MyMcRealms.Responses; using Semver; diff --git a/MyMcRealms/Responses/MinecraftPlayerInfo.cs b/MyMcRealms/Responses/MinecraftPlayerInfo.cs new file mode 100644 index 0000000..2198d10 --- /dev/null +++ b/MyMcRealms/Responses/MinecraftPlayerInfo.cs @@ -0,0 +1,8 @@ +namespace Minecraft_Realms_Emulator.Responses +{ + public class MinecraftPlayerInfo + { + public string Id { get; set; } = string.Empty; + public string Name { get; set; } = string.Empty; + } +} \ No newline at end of file diff --git a/MyMcRealms/Responses/ServersResponse.cs b/MyMcRealms/Responses/ServersResponse.cs index 229d05e..d46f51d 100644 --- a/MyMcRealms/Responses/ServersResponse.cs +++ b/MyMcRealms/Responses/ServersResponse.cs @@ -1,6 +1,4 @@ -using MyMcRealms.Entities; - -namespace MyMcRealms.Responses +namespace MyMcRealms.Responses { public class ServersResponse { diff --git a/MyMcRealms/Responses/WorldResponse.cs b/MyMcRealms/Responses/WorldResponse.cs index 2396780..728694d 100644 --- a/MyMcRealms/Responses/WorldResponse.cs +++ b/MyMcRealms/Responses/WorldResponse.cs @@ -1,6 +1,6 @@ using System.Text.Json; -namespace MyMcRealms.Entities +namespace MyMcRealms.Responses { public class WorldResponse {