Skip to content

Commit

Permalink
[TeleportRequest] Cleaned code.
Browse files Browse the repository at this point in the history
  • Loading branch information
kafeijao committed Jun 25, 2023
1 parent bf8dcc2 commit 441d039
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
69 changes: 41 additions & 28 deletions TeleportRequest/Main.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ABI_RC.Core.Player;
using ABI_RC.Core.Savior;
using ABI_RC.Core.UI;
using ABI_RC.Systems.MovementSystem;
using MelonLoader;
Expand All @@ -10,50 +11,62 @@ public class TeleportRequest : MelonMod {
private static readonly HashSet<string> PendingRequests = new();

public override void OnInitializeMelon() {

ModConfig.InitializeMelonPrefs();
ModConfig.InitializeBTKUI();

// Register this mod on the request lib
RequestLib.API.RegisterMod();
}

private static void OnResponse(RequestLib.API.Request request, RequestLib.API.Response response) {

// Remove from our cache preventing of sending duplicates
if (PendingRequests.Contains(request.TargetPlayerGuid)) PendingRequests.Remove(request.TargetPlayerGuid);

var playerName = CVRPlayerManager.Instance.TryGetPlayerName(request.TargetPlayerGuid);
switch (response.Result) {

case RequestLib.API.RequestResult.Accepted:
var msgAccepted = $"The player {playerName} has <color=green>Accepted</color> the teleport request!";
MelonLogger.Msg(msgAccepted);
CohtmlHud.Instance.ViewDropText(nameof(TeleportRequest), msgAccepted);
var target = CVRPlayerManager.Instance.NetworkPlayers.FirstOrDefault(np => np.Uuid == request.TargetPlayerGuid);
if (target == null) {
MelonLogger.Warning($"The player {playerName} is not in the Instance anymore...");
return;
}
if (!MovementSystem.Instance.canFly) {
MelonLogger.Warning($"This world doesn't allow flight, so we won't be able to teleport.");
return;
}
MovementSystem.Instance.TeleportTo(target.PlayerObject.transform.position, target.PlayerObject.transform.eulerAngles);
break;

case RequestLib.API.RequestResult.Declined:
var msgDeclined = $"The player {playerName} has <color=red>Declined</color> the teleport request!";
MelonLogger.Msg(msgDeclined);
CohtmlHud.Instance.ViewDropText(nameof(TeleportRequest), msgDeclined);
break;

case RequestLib.API.RequestResult.TimedOut:
var msgTimedOut = $"The teleport request to the player {playerName} has <color=yellow>timed out</color>...";
MelonLogger.Msg(msgTimedOut);
CohtmlHud.Instance.ViewDropText(nameof(TeleportRequest), msgTimedOut);
break;
}
}

public static void RequestToTeleport(string playerName, string playerID) {

if (PendingRequests.Contains(playerID)) {
MelonLogger.Warning($"There is already a pending request for {playerName}");
return;
}

PendingRequests.Add(playerID);

MelonLogger.Msg($"Sending teleport request to {playerName}...");

RequestLib.API.SendRequest(playerID, $"{playerName} is requesting to teleport to you.", requestResult => {
if (PendingRequests.Contains(playerID)) PendingRequests.Remove(playerID);
switch (requestResult) {
case RequestLib.API.RequestResult.Accepted:
var msgAccepted = $"The player {playerName} has accepted the teleport request!";
MelonLogger.Msg(msgAccepted);
CohtmlHud.Instance.ViewDropText(nameof(TeleportRequest), msgAccepted);
var target = CVRPlayerManager.Instance.NetworkPlayers.FirstOrDefault(np => np.Uuid == playerID);
if (target == null) {
MelonLogger.Error($"The player {playerName} couldn't be found...");
return;
}
MovementSystem.Instance.TeleportTo(target.PlayerObject.transform.position, target.PlayerObject.transform.eulerAngles);
break;
case RequestLib.API.RequestResult.Declined:
var msgDeclined = $"The player {playerName} has declined the teleport request!";
MelonLogger.Msg(msgDeclined);
CohtmlHud.Instance.ViewDropText(nameof(TeleportRequest), msgDeclined);
break;
case RequestLib.API.RequestResult.TimedOut:
var msgTimedOut = $"The teleport request to the player {playerName} has timed out...";
MelonLogger.Msg(msgTimedOut);
CohtmlHud.Instance.ViewDropText(nameof(TeleportRequest), msgTimedOut);
break;
}
});
RequestLib.API.SendRequest(new RequestLib.API.Request(playerID, $"{MetaPort.Instance.username} is requesting to teleport to you.", OnResponse));
}

}
10 changes: 8 additions & 2 deletions TeleportRequest/ModConfig.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using ABI_RC.Core.InteractionSystem;
using ABI_RC.Systems.MovementSystem;
using MelonLoader;
using UnityEngine;

Expand Down Expand Up @@ -61,8 +62,13 @@ private static void SetupBTKUI(CVR_MenuManager manager) {
BTKUILib.QuickMenuAPI.OnPlayerSelected += (playerName, playerID) => {
playerCat.ClearChildren();
if (RequestLib.API.HasRequestLib(playerID)) {
var teleportButton = playerCat.AddButton("Request to Teleport", "", $"Send a request to teleport to the {playerName}");
teleportButton.OnPress += () => TeleportRequest.RequestToTeleport(playerName, playerID);
if (MovementSystem.Instance.canFly) {
var teleportButton = playerCat.AddButton("Request to Teleport", "", $"Send a request to teleport to the {playerName}");
teleportButton.OnPress += () => TeleportRequest.RequestToTeleport(playerName, playerID);
}
else {
playerCat.AddButton("World doesn't allow Flight", "", $"This world doesn't allow flight, so we can't request :(");
}
}
else {
playerCat.AddButton("Has no RequestLib", "", $"This player doesn't have the RequestLib, so we can't request :(");
Expand Down

0 comments on commit 441d039

Please sign in to comment.