From 446640e380aa7b08bdc0e01a76f7b09249b89d2c Mon Sep 17 00:00:00 2001
From: Extremelyd1 <10898310+Extremelyd1@users.noreply.github.com>
Date: Wed, 24 Jul 2024 11:33:14 +0200
Subject: [PATCH] Refactor commands to fit new UI
---
HKMP/Game/Client/ClientManager.cs | 2 -
HKMP/Game/Command/Client/AddonCommand.cs | 30 ++++++----
HKMP/Game/Command/Client/ConnectCommand.cs | 61 ---------------------
HKMP/Game/Command/Client/HostCommand.cs | 64 ----------------------
HKMP/Ui/Chat/ChatBox.cs | 46 ++++++++++++----
5 files changed, 53 insertions(+), 150 deletions(-)
delete mode 100644 HKMP/Game/Command/Client/ConnectCommand.cs
delete mode 100644 HKMP/Game/Command/Client/HostCommand.cs
diff --git a/HKMP/Game/Client/ClientManager.cs b/HKMP/Game/Client/ClientManager.cs
index 5fd35e29..9fbc079e 100644
--- a/HKMP/Game/Client/ClientManager.cs
+++ b/HKMP/Game/Client/ClientManager.cs
@@ -278,8 +278,6 @@ ModSettings modSettings
/// Register the default client commands.
///
private void RegisterCommands() {
- _commandManager.RegisterCommand(new ConnectCommand(this));
- _commandManager.RegisterCommand(new HostCommand(_serverManager));
_commandManager.RegisterCommand(new AddonCommand(_addonManager, _netClient));
}
diff --git a/HKMP/Game/Command/Client/AddonCommand.cs b/HKMP/Game/Command/Client/AddonCommand.cs
index 89814b6a..726bae30 100644
--- a/HKMP/Game/Command/Client/AddonCommand.cs
+++ b/HKMP/Game/Command/Client/AddonCommand.cs
@@ -42,18 +42,24 @@ public void Execute(string[] arguments) {
var action = arguments[1];
if (action == "list") {
- var message = "Loaded addons: ";
- message += string.Join(
- ", ",
- _addonManager.GetLoadedAddons().Select(addon => {
- var msg = $"{addon.GetName()} {addon.GetVersion()}";
- if (addon is TogglableClientAddon {Disabled: true }) {
- msg += " (disabled)";
- }
-
- return msg;
- })
- );
+ string message;
+ var addons = _addonManager.GetLoadedAddons();
+ if (addons.Count == 0) {
+ message = "No addons loaded.";
+ } else {
+ message = "Loaded addons: ";
+ message += string.Join(
+ ", ",
+ addons.Select(addon => {
+ var msg = $"{addon.GetName()} {addon.GetVersion()}";
+ if (addon is TogglableClientAddon { Disabled: true }) {
+ msg += " (disabled)";
+ }
+
+ return msg;
+ })
+ );
+ }
UiManager.InternalChatBox.AddMessage(message);
return;
diff --git a/HKMP/Game/Command/Client/ConnectCommand.cs b/HKMP/Game/Command/Client/ConnectCommand.cs
deleted file mode 100644
index 62eb290e..00000000
--- a/HKMP/Game/Command/Client/ConnectCommand.cs
+++ /dev/null
@@ -1,61 +0,0 @@
-using Hkmp.Api.Command.Client;
-using Hkmp.Game.Client;
-using Hkmp.Ui;
-
-namespace Hkmp.Game.Command.Client;
-
-///
-/// Command for connecting the local user to a server with a given address, port and username.
-///
-internal class ConnectCommand : IClientCommand {
- ///
- public string Trigger => "/connect";
-
- ///
- public string[] Aliases => new[] { "/disconnect" };
-
- ///
- /// The client manager instance.
- ///
- private readonly ClientManager _clientManager;
-
- public ConnectCommand(ClientManager clientManager) {
- _clientManager = clientManager;
- }
-
- ///
- public void Execute(string[] arguments) {
- var command = arguments[0];
- if (command == Aliases[0]) {
- _clientManager.Disconnect();
- UiManager.InternalChatBox.AddMessage("You are disconnected from the server");
- return;
- }
-
- if (arguments.Length != 4) {
- SendUsage();
- return;
- }
-
- var address = arguments[1];
-
- var portString = arguments[2];
- var parsedPort = int.TryParse(portString, out var port);
- if (!parsedPort || port < 1 || port > 99999) {
- UiManager.InternalChatBox.AddMessage("Invalid port!");
- return;
- }
-
- var username = arguments[3];
-
- _clientManager.Connect(address, port, username);
- UiManager.InternalChatBox.AddMessage($"Trying to connect to {address}:{port} as {username}...");
- }
-
- ///
- /// Sends the command usage to the chat box.
- ///
- private void SendUsage() {
- UiManager.InternalChatBox.AddMessage($"Invalid usage: {Trigger}
");
- }
-}
diff --git a/HKMP/Game/Command/Client/HostCommand.cs b/HKMP/Game/Command/Client/HostCommand.cs
deleted file mode 100644
index 2b50d70b..00000000
--- a/HKMP/Game/Command/Client/HostCommand.cs
+++ /dev/null
@@ -1,64 +0,0 @@
-using System;
-using Hkmp.Api.Command.Client;
-using Hkmp.Game.Server;
-using Hkmp.Ui;
-
-namespace Hkmp.Game.Command.Client;
-
-///
-/// Command for controlling local server hosting.
-///
-internal class HostCommand : IClientCommand {
- ///
- public string Trigger => "/host";
-
- ///
- public string[] Aliases => Array.Empty();
-
- ///
- /// The server manager instance.
- ///
- private readonly ServerManager _serverManager;
-
- public HostCommand(ServerManager serverManager) {
- _serverManager = serverManager;
- }
-
- ///
- public void Execute(string[] arguments) {
- if (arguments.Length < 2) {
- SendUsage();
- return;
- }
-
- var action = arguments[1];
- if (action == "start") {
- if (arguments.Length != 3) {
- SendUsage();
- return;
- }
-
- var portString = arguments[2];
- var parsedPort = int.TryParse(portString, out var port);
- if (!parsedPort || port < 1 || port > 99999) {
- UiManager.InternalChatBox.AddMessage("Invalid port!");
- return;
- }
-
- _serverManager.Start(port);
- UiManager.InternalChatBox.AddMessage($"Started server on port {port}");
- } else if (action == "stop") {
- _serverManager.Stop();
- UiManager.InternalChatBox.AddMessage("Stopped server");
- } else {
- SendUsage();
- }
- }
-
- ///
- /// Sends the command usage to the chat box.
- ///
- private void SendUsage() {
- UiManager.InternalChatBox.AddMessage($"Invalid usage: {Trigger} [port]");
- }
-}
diff --git a/HKMP/Ui/Chat/ChatBox.cs b/HKMP/Ui/Chat/ChatBox.cs
index 90e5d7c5..50a2f850 100644
--- a/HKMP/Ui/Chat/ChatBox.cs
+++ b/HKMP/Ui/Chat/ChatBox.cs
@@ -7,6 +7,7 @@
using Hkmp.Ui.Resources;
using Hkmp.Util;
using UnityEngine;
+using Logger = Hkmp.Logging.Logger;
using Object = UnityEngine.Object;
namespace Hkmp.Ui.Chat;
@@ -171,19 +172,42 @@ private void CheckKeyBinds(ModSettings modSettings) {
}
} else if (Input.GetKeyDown(modSettings.OpenChatKey)) {
var gameManager = GameManager.instance;
+ if (gameManager == null) {
+ Logger.Debug("Could not open chat, GM is null");
+ return;
+ }
+
+ var gameState = gameManager.gameState;
+ if (gameState != GameState.PLAYING && gameState != GameState.MAIN_MENU) {
+ Logger.Debug($"Could not open chat, game state is incorrect: {gameState}");
+ return;
+ }
+
var uiManager = UIManager.instance;
+ if (uiManager == null) {
+ Logger.Debug("Could not open chat, UIM is null");
+ return;
+ }
+
+ var uiState = uiManager.uiState;
+ if (uiState != UIState.PLAYING && uiState != UIState.MAIN_MENU_HOME) {
+ Logger.Debug($"Could not open chat, UI state is incorrect: {uiState}");
+ return;
+ }
+
var heroController = HeroController.instance;
- if (gameManager == null
- || uiManager == null
- || gameManager.gameState != GameState.PLAYING
- || uiManager.uiState != UIState.PLAYING
- // If the hero is charging their nail and chat opens, it will cause a flashing effect
- || (heroController != null && heroController.cState.nailCharging)
- // If we are in the inventory, opening the chat has side-effects, such as floating
- || IsInventoryOpen()
- // If we are in a godhome menu, we will soft-lock opening the chat
- || IsGodHomeMenuOpen()
- ) {
+ if (heroController != null && heroController.cState.nailCharging) {
+ Logger.Debug("Could not open chat, player is charging nail");
+ return;
+ }
+
+ if (gameState == GameState.PLAYING && IsInventoryOpen()) {
+ Logger.Debug("Could not open chat, inventory is open");
+ return;
+ }
+
+ if (IsGodHomeMenuOpen()) {
+ Logger.Debug("Could not open chat, GodHome menu is open");
return;
}