Skip to content

Commit b413eb1

Browse files
committed
feat: new Run method in ConsoleCommand that gives the Terminal context. Is the default wired in CommandManager but calls the old Run when not overriden.
1 parent e3647b1 commit b413eb1

8 files changed

Lines changed: 69 additions & 6 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changelog
2-
## Version 2.28.1
2+
## Version 2.29.0
3+
* Added new method in ConsoleCommand to take a Terminal context parameter, so mods can write output to either Console or Chat depending on where the command was executed from. Backwards compatible. For more info on that see https://valheim-modding.github.io/Jotunn/tutorials/console-commands.html
34
* Added TMPro FontAssets shortcuts to the GUIManager
45

56
## Version 2.28.0

JotunnLib/Documentation/tutorials/console-commands.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,26 @@ private void Awake()
5353
{
5454
CommandManager.Instance.AddConsoleCommand(new BetterSpawnCommand());
5555
}
56+
```
57+
58+
### Getting the Terminal context
59+
If you need to know from which Terminal your command was executed from, you can also overwrite `Run(string[] args, Terminal context)` from the base class. This will give you the `Terminal` instance, which is either the Console or the Chat of Valheim. Note that you will have to use `context.AddString()` for output on the calling Terminal instead of directly writing to `Console.instance`.
60+
61+
```cs
62+
public class EchoCommand : ConsoleCommand
63+
{
64+
public override string Name => "echo";
65+
66+
public override string Help => "Echoes all text entered to the console or chat";
67+
68+
public override void Run(string[] args, Terminal context)
69+
{
70+
if (args.Length < 1)
71+
{
72+
context.AddString("Usage: echo <text>");
73+
}
74+
75+
context.AddString(string.Join(" ", args, 0, args.Length));
76+
}
77+
}
5678
```

JotunnLib/Entities/ConsoleCommand.cs

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@ public abstract class ConsoleCommand : CustomEntity
2323
public virtual bool IsCheat => false;
2424

2525
/// <summary>
26-
/// If true, this command will be allowed in networked play.
26+
/// If true, this command will only be allowed in networked play.
2727
/// </summary>
2828
public virtual bool IsNetwork => false;
2929

3030
/// <summary>
31-
/// If true, and IsNetwork is true, this command will be allowed in networked play, but only for the server.
31+
/// If true, and IsNetwork is true, this command will only be allowed in networked play, but only for the server.
3232
/// </summary>
3333
public virtual bool OnlyServer => false;
3434

@@ -41,8 +41,25 @@ public abstract class ConsoleCommand : CustomEntity
4141
/// The function that will be called when the user runs your console command, with space-delimited arguments.
4242
/// </summary>
4343
/// <param name="args">The arguments the user types, with spaces being the delimiter.</param>
44-
public abstract void Run(string[] args);
44+
public virtual void Run(string[] args)
45+
{
46+
47+
}
4548

49+
/// <summary>
50+
/// The function that will be called when the user runs your console command, with space-delimited arguments and
51+
/// a context <see cref="Terminal"/> object (Console or Chat). Use context?.AddString() to write to the respective
52+
/// output.
53+
/// </summary>
54+
/// <param name="args">The arguments the user types, with spaces being the delimiter.</param>
55+
/// <param name="context">Terminal from which this command is run (Console or Chat window)</param>
56+
public virtual void Run(string[] args, Terminal context)
57+
{
58+
// CommandManager always wires this method to the vanilla Terminal commands
59+
// so we call the one without context one for backward compatibility
60+
Run(args);
61+
}
62+
4663
/// <summary>
4764
/// Override this function to return a list of strings that are valid options for your command
4865
/// </summary>

JotunnLib/Main.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class Main : BaseUnityPlugin
2121
/// <summary>
2222
/// The current version of the Jotunn library.
2323
/// </summary>
24-
public const string Version = "2.28.0";
24+
public const string Version = "2.29.0";
2525

2626
/// <summary>
2727
/// The name of the library.

JotunnLib/Managers/CommandManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ private Terminal.ConsoleCommand CreateVanillaCommand(ConsoleCommand command)
129129
{
130130
command.Name,
131131
command.Help,
132-
(Terminal.ConsoleEvent)((args) => command.Run(args.Args.Skip(1).ToArray())),
132+
(Terminal.ConsoleEvent)((args) => command.Run(args.Args.Skip(1).ToArray(), args.Context)),
133133
command.IsCheat,
134134
command.IsNetwork,
135135
command.OnlyServer,
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using Jotunn.Entities;
2+
3+
namespace TestMod.ConsoleCommands
4+
{
5+
public class EchoCommand : ConsoleCommand
6+
{
7+
public override string Name => "echo";
8+
9+
public override string Help => "Echoes all text entered to the console or chat";
10+
11+
public override void Run(string[] args, Terminal context)
12+
{
13+
if (args.Length < 1)
14+
{
15+
context.AddString("Usage: echo <text>");
16+
}
17+
18+
context.AddString(string.Join(" ", args, 0, args.Length));
19+
}
20+
}
21+
}

TestMod/TestMod.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,7 @@ private void AddCommands()
812812
CommandManager.Instance.AddConsoleCommand(new RemoveCategoryTabCommand());
813813
CommandManager.Instance.AddConsoleCommand(new ResetCartographyCommand());
814814
CommandManager.Instance.AddConsoleCommand(new AdminCheckCommand());
815+
CommandManager.Instance.AddConsoleCommand(new EchoCommand());
815816
}
816817

817818
// Register new skills

TestMod/TestMod.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
<ItemGroup>
2828
<Compile Include="ColorChanger.cs" />
2929
<Compile Include="ConsoleCommands\AdminCheckCommand.cs" />
30+
<Compile Include="ConsoleCommands\EchoCommand.cs" />
3031
<Compile Include="ConsoleCommands\RemoveCategoryTabCommand.cs" />
3132
<Compile Include="ConsoleCommands\ResetCartographyCommand.cs" />
3233
<Compile Include="ConsoleCommands\CreateCategoryTabCommand.cs" />

0 commit comments

Comments
 (0)