1
0
mirror of https://github.com/CyberL1/MyMcRealms.git synced 2024-11-21 05:38:20 -05:00

feat: add to/remove from whitelist using the Players menu

This commit is contained in:
CyberL1 2024-05-14 12:26:14 +02:00
parent 3f0e0c12db
commit ab05b999f8
5 changed files with 121 additions and 5 deletions

View File

@ -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<ActionResult<WorldResponse>> 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<MinecraftPlayerInfo>($"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<Player> 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<ActionResult<bool>> 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<MinecraftPlayerInfo>($"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);
}
}
}

View File

@ -1,6 +1,5 @@
using Microsoft.AspNetCore.Mvc;
using MyMcRealms.Attributes;
using MyMcRealms.Entities;
using MyMcRealms.MyMcAPI.Responses;
using MyMcRealms.Responses;
using Semver;

View File

@ -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;
}
}

View File

@ -1,6 +1,4 @@
using MyMcRealms.Entities;
namespace MyMcRealms.Responses
namespace MyMcRealms.Responses
{
public class ServersResponse
{

View File

@ -1,6 +1,6 @@
using System.Text.Json;
namespace MyMcRealms.Entities
namespace MyMcRealms.Responses
{
public class WorldResponse
{