Confirmedusers
711
edits
Mirotworez (talk | contribs) (Marked this version for translation) |
m (Updated to 1.19.3. Now using new command api.) |
||
Line 1: | Line 1: | ||
__FORCETOC__ | __FORCETOC__ | ||
{{GameVersion|1. | {{GameVersion|1.19.3}} | ||
<languages/><translate> | <languages/><translate> | ||
<!--T:1--> | <!--T:1--> | ||
Line 50: | Line 50: | ||
api.Event.GameWorldSave += OnSaveGameSaving; | api.Event.GameWorldSave += OnSaveGameSaving; | ||
api. | api.ChatCommands.Create("lfg") | ||
.WithDescription("List or join the lfg list") | |||
.RequiresPrivilege(Privilege.chat) | |||
.RequiresPlayer() | |||
.WithArgs(api.ChatCommands.Parsers.Word("cmd", new string[] { "list", "join", "leave" })) | |||
.HandleWith(new OnCommandDelegate(OnLfg)); | |||
} | } | ||
} | } | ||
Line 107: | Line 112: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
private | private TextCommandResult OnLfg(TextCommandCallingArgs args) | ||
{ | { | ||
string cmd = args | string cmd = args[0] as String; | ||
switch (cmd) | switch (cmd) | ||
{ | { | ||
Line 122: | Line 127: | ||
default: | default: | ||
return TextCommandResult.Error("/lfg [list|join|leave]"); | |||
} | } | ||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
We use | We use <code>args[0]</code> to collect the first argument passed (ignoring anything subsequent). We then start a switch statement for our valid arguments, and default to showing these to the player if none of them match or <code>cmd</code> is null. | ||
Let's handle each of these: | Let's handle each of these: | ||
Line 134: | Line 138: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
case "join": | case "join": | ||
if (lfgList.Contains( | if (lfgList.Contains(args.Caller.Player.PlayerUID)) | ||
{ | { | ||
return TextCommandResult.Error("You're already in the list!"); | |||
} | } | ||
else | else | ||
{ | { | ||
lfgList.Add( | lfgList.Add(args.Caller.Player.PlayerUID); | ||
return TextCommandResult.Success("Successfully joined."); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 154: | Line 157: | ||
<syntaxhighlight lang="c#"> | <syntaxhighlight lang="c#"> | ||
case "leave": | case "leave": | ||
if (!lfgList.Remove( | if (!lfgList.Remove(args.Caller.Player.PlayerUID)) | ||
{ | { | ||
return TextCommandResult.Error("You're not in the list!"); | |||
} | } | ||
else | else | ||
{ | { | ||
return TextCommandResult.Success("Successfully left."); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 173: | Line 175: | ||
if (lfgList.Count == 0) | if (lfgList.Count == 0) | ||
{ | { | ||
return TextCommandResult.Success("No one is looking for a group."); | |||
} | } | ||
else | else | ||
Line 183: | Line 185: | ||
}); | }); | ||
return TextCommandResult.Success(response); | |||
} | } | ||
</syntaxhighlight> | </syntaxhighlight> | ||
Line 213: | Line 214: | ||
api.Event.GameWorldSave += OnSaveGameSaving; | api.Event.GameWorldSave += OnSaveGameSaving; | ||
api. | api.ChatCommands.Create("lfg") | ||
.WithDescription("List or join the lfg list") | |||
.RequiresPrivilege(Privilege.chat) | |||
.RequiresPlayer() | |||
.WithArgs(api.ChatCommands.Parsers.Word("cmd", new string[] { "list", "join", "leave" })) | |||
.HandleWith(new OnCommandDelegate(OnLfg)); | |||
} | } | ||
Line 227: | Line 233: | ||
} | } | ||
private | private TextCommandResult OnLfg(TextCommandCallingArgs args) | ||
{ | { | ||
string cmd = args | string cmd = args[0] as String; | ||
switch (cmd) | switch (cmd) | ||
{ | { | ||
case "join": | case "join": | ||
if (lfgList.Contains( | if (lfgList.Contains(args.Caller.Player.PlayerUID)) | ||
{ | { | ||
return TextCommandResult.Error("You're already in the list!"); | |||
} | } | ||
else | else | ||
{ | { | ||
lfgList.Add( | lfgList.Add(args.Caller.Player.PlayerUID); | ||
return TextCommandResult.Success("Successfully joined."); | |||
} | } | ||
case "leave": | case "leave": | ||
if (!lfgList.Remove( | if (!lfgList.Remove(args.Caller.Player.PlayerUID)) | ||
{ | { | ||
return TextCommandResult.Error("You're not in the list!"); | |||
} | } | ||
else | else | ||
{ | { | ||
return TextCommandResult.Success("Successfully left."); | |||
} | } | ||
case "list": | case "list": | ||
if (lfgList.Count == 0) | if (lfgList.Count == 0) | ||
{ | { | ||
return TextCommandResult.Success("No one is looking for a group."); | |||
} | } | ||
else | else | ||
Line 268: | Line 272: | ||
}); | }); | ||
return TextCommandResult.Success(response); | |||
} | } | ||
default: | default: | ||
return TextCommandResult.Error("/lfg [list|join|leave]"); | |||
} | } | ||
} | } | ||
Line 286: | Line 287: | ||
Let's test our mod. Once you're ingame, try entering <code>/lfg join</code>. Now quit the game and join back in. Upon entering <code>/lfg list</code>, you should see your name on the list; this means that your custom data has persisted in the <code>SaveGame</code>! | Let's test our mod. Once you're ingame, try entering <code>/lfg join</code>. Now quit the game and join back in. Upon entering <code>/lfg list</code>, you should see your name on the list; this means that your custom data has persisted in the <code>SaveGame</code>! | ||
{{Navbox/modding|Vintage Story}} | {{Navbox/modding|Vintage Story}} |