Modding:SaveGame Data Storage: Difference between revisions

From Vintage Story Wiki
(Marked this version for translation)
m (Updated to 1.19.3. Now using new command api.)
Line 1: Line 1:
__FORCETOC__
__FORCETOC__
{{GameVersion|1.15}}
{{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.RegisterCommand("lfg", "List or join the lfg list", "[list|join|leave]", OnLfg);
             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 void OnLfg(IServerPlayer player, int groupId, CmdArgs args)
         private TextCommandResult OnLfg(TextCommandCallingArgs args)
         {
         {
             string cmd = args.PopWord();
             string cmd = args[0] as String;
             switch (cmd)
             switch (cmd)
             {
             {
Line 122: Line 127:


                 default:
                 default:
                     player.SendMessage(groupId, "/lfg [list|join|leave]", EnumChatType.CommandError);
                     return TextCommandResult.Error("/lfg [list|join|leave]");
                    break;
             }
             }
         }
         }
</syntaxhighlight>
</syntaxhighlight>


We use the <code>PopWord</code> of our <code>CmdArgs</code> parameter 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.
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(player.PlayerUID))
                     if (lfgList.Contains(args.Caller.Player.PlayerUID))
                     {
                     {
                         player.SendMessage(groupId, "You're already in the list!", EnumChatType.CommandError);
                         return TextCommandResult.Error("You're already in the list!");
                     }
                     }
                     else
                     else
                     {
                     {
                         lfgList.Add(player.PlayerUID);
                         lfgList.Add(args.Caller.Player.PlayerUID);
                         player.SendMessage(groupId, "Successfully joined.", EnumChatType.CommandSuccess);
                         return TextCommandResult.Success("Successfully joined.");
                     }
                     }
                    break;
</syntaxhighlight>
</syntaxhighlight>


Line 154: Line 157:
<syntaxhighlight lang="c#">
<syntaxhighlight lang="c#">
                 case "leave":
                 case "leave":
                     if (!lfgList.Remove(player.PlayerUID))
                     if (!lfgList.Remove(args.Caller.Player.PlayerUID))
                     {
                     {
                         player.SendMessage(groupId, "You're not in the list!", EnumChatType.CommandError);
                         return TextCommandResult.Error("You're not in the list!");
                     }
                     }
                     else
                     else
                     {
                     {
                         player.SendMessage(groupId, "Successfully left.", EnumChatType.CommandSuccess);
                         return TextCommandResult.Success("Successfully left.");
                     }
                     }
                    break;
</syntaxhighlight>
</syntaxhighlight>


Line 173: Line 175:
                     if (lfgList.Count == 0)
                     if (lfgList.Count == 0)
                     {
                     {
                         player.SendMessage(groupId, "Noone is looking for a group.", EnumChatType.CommandSuccess);
                         return TextCommandResult.Success("No one is looking for a group.");
                     }
                     }
                     else
                     else
Line 183: Line 185:
                         });
                         });


                         player.SendMessage(groupId, response, EnumChatType.CommandSuccess);
                         return TextCommandResult.Success(response);
                     }
                     }
                    break;
</syntaxhighlight>
</syntaxhighlight>


Line 213: Line 214:
             api.Event.GameWorldSave += OnSaveGameSaving;
             api.Event.GameWorldSave += OnSaveGameSaving;


             api.RegisterCommand("lfg", "Access the lfg list.", "[list|join|leave]", OnLfg);
             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 void OnLfg(IServerPlayer player, int groupId, CmdArgs args)
         private TextCommandResult OnLfg(TextCommandCallingArgs args)
         {
         {
             string cmd = args.PopWord();
             string cmd = args[0] as String;
             switch (cmd)
             switch (cmd)
             {
             {
                 case "join":
                 case "join":
                     if (lfgList.Contains(player.PlayerUID))
                     if (lfgList.Contains(args.Caller.Player.PlayerUID))
                     {
                     {
                         player.SendMessage(groupId, "You're already in the list!", EnumChatType.CommandError);
                         return TextCommandResult.Error("You're already in the list!");
                     }
                     }
                     else
                     else
                     {
                     {
                         lfgList.Add(player.PlayerUID);
                         lfgList.Add(args.Caller.Player.PlayerUID);
                         player.SendMessage(groupId, "Successfully joined.", EnumChatType.CommandSuccess);
                         return TextCommandResult.Success("Successfully joined.");
                     }
                     }
                    break;


                 case "leave":
                 case "leave":
                     if (!lfgList.Remove(player.PlayerUID))
                     if (!lfgList.Remove(args.Caller.Player.PlayerUID))
                     {
                     {
                         player.SendMessage(groupId, "You're not in the list!", EnumChatType.CommandError);
                         return TextCommandResult.Error("You're not in the list!");
                     }
                     }
                     else
                     else
                     {
                     {
                         player.SendMessage(groupId, "Successfully left.", EnumChatType.CommandSuccess);
                         return TextCommandResult.Success("Successfully left.");
                     }
                     }
                    break;


                 case "list":
                 case "list":
                     if (lfgList.Count == 0)
                     if (lfgList.Count == 0)
                     {
                     {
                         player.SendMessage(groupId, "Noone is looking for a group.", EnumChatType.CommandSuccess);
                         return TextCommandResult.Success("No one is looking for a group.");
                     }
                     }
                     else
                     else
Line 268: Line 272:
                         });
                         });


                         player.SendMessage(groupId, response, EnumChatType.CommandSuccess);
                         return TextCommandResult.Success(response);
                     }
                     }
                    break;
 
                   
                case null:
                 default:
                 default:
                     player.SendMessage(groupId, "/lfg [list|join|leave]", EnumChatType.CommandError);
                     return TextCommandResult.Error("/lfg [list|join|leave]");
                    return;
             }
             }
         }
         }
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>!
== Distribution ==
To distribute this mod, you may run the following command in the modtools cli pack <your mod id>, then copy the .zip file into your VintageStory mods folder.
Here are the official versions:
* for VS v1.12.4: [https://wiki.vintagestory.at/images/8/84/LookingForGroup_vs1.12.4_v1.0.0.zip LookingForGroup_vs1.12.4_v1.0.0.zip]
* for VS v1.6: [https://wiki.vintagestory.at/images/f/f0/LookingForGroup_vs1.6_v1.0.0.zip LookingForGroup_vs1.6_v1.0.0.zip]


{{Navbox/modding|Vintage Story}}
{{Navbox/modding|Vintage Story}}