Adds homes

This commit is contained in:
zontreck 2025-01-18 14:11:00 -07:00
parent 3f1ce790d9
commit d5dc0d5892
5 changed files with 184 additions and 4 deletions

View file

@ -18,6 +18,8 @@ namespace AriasServerUtils
private static bool bDirty = false;
internal static Dictionary<string, PlayerInventory> backupInventory = new Dictionary<string, PlayerInventory>();
internal static Dictionary<string, PlayerStorage> mPlayerData = new Dictionary<string, PlayerStorage>();
internal static string[] saveInvTypes = new string[] {
GlobalConstants.hotBarInvClassName,
GlobalConstants.backpackInvClassName,
@ -64,6 +66,11 @@ namespace AriasServerUtils
api.Event.ServerRunPhase(EnumServerRunPhase.Shutdown, OnShutdown);
api.Event.Timer(OnCheckModDirty, 20);
api.Event.PlayerDeath += OnPlayerDeath;
api.Event.PlayerJoin += OnPlayerJoin;
api.Event.PlayerDisconnect += OnPlayerDC;
api.Event.PlayerLeave += OnPlayerDC;
api.ChatCommands.Create("setspawn").RequiresPrivilege(Privilege.controlserver).HandleWith(Events.HandleSetSpawn);
api.ChatCommands.Create("spawn").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleSpawn);
@ -73,6 +80,39 @@ namespace AriasServerUtils
api.ChatCommands.Create("test_death").RequiresPlayer().RequiresPrivilege(Privilege.controlserver).HandleWith(TestDeath);
var parsers = api.ChatCommands.Parsers;
api.ChatCommands.Create("restoreinv").RequiresPlayer().WithArgs(parsers.OnlinePlayer("player")).HandleWith(Events.HandleReturnItems).WithDescription("Returns items to a player in the event of a problem").RequiresPrivilege(Privilege.controlserver);
api.ChatCommands.Create("sethome").RequiresPlayer().WithArgs(parsers.OptionalWord("name")).WithDescription("Creates a home").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleSetHome);
api.ChatCommands.Create("home").RequiresPlayer().WithArgs(parsers.OptionalWord("name")).WithDescription("Teleports you to home").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleGoHome);
api.ChatCommands.Create("delhome").RequiresPlayer().WithArgs(parsers.OptionalWord("name")).WithDescription("Deletes a home entry").RequiresPrivilege(Privilege.chat).HandleWith(Events.HandleDelHome);
}
private void OnPlayerDC(IServerPlayer byPlayer)
{
OnCheckModDirty();
mPlayerData.Remove(byPlayer.PlayerName);
}
private void OnPlayerJoin(IServerPlayer byPlayer)
{
API.Logger.Notification($"[ASU] {Lang.Get($"{MOD_ID}:playerjoin")}");
PlayerStorage data = API.LoadModConfig<PlayerStorage>(GetConfigurationFile(byPlayer.PlayerName, ModConfigType.World));
if (data == null) data = new PlayerStorage();
mPlayerData[byPlayer.PlayerName] = data;
}
public static PlayerStorage GetPlayerData(IServerPlayer player)
{
if (mPlayerData.ContainsKey(player.PlayerName))
{
return mPlayerData[player.PlayerName];
}
else
{
return new PlayerStorage();
}
}
private TextCommandResult TestDeath(TextCommandCallingArgs args)
@ -129,6 +169,16 @@ namespace AriasServerUtils
//API.Logger.Notification(Lang.Get($"{MOD_ID}:timer"));
bDirty = false;
SaveGlobalConfig();
SavePlayerData();
}
}
private void SavePlayerData()
{
foreach (var data in mPlayerData)
{
API.StoreModConfig<PlayerStorage>(data.Value, GetConfigurationFile(data.Key, ModConfigType.World));
}
}

View file

@ -15,6 +15,59 @@ namespace AriasServerUtils
return TextCommandResult.Success(Lang.Get($"{ServerUtilities.MOD_ID}:rmspawn"));
}
internal static TextCommandResult HandleDelHome(TextCommandCallingArgs args)
{
string homeName = "default";
if (args.ArgCount > 0)
{
homeName = args[0] as string;
}
if (args.Caller.Player is IServerPlayer isp)
{
PlayerStorage data = ServerUtilities.GetPlayerData(isp);
if (data.Homes.ContainsKey(homeName))
{
data.Homes.Remove(homeName);
ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-del"));
ServerUtilities.MarkDirty();
}
else
{
ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-no"));
}
}
return TextCommandResult.Success();
}
internal static TextCommandResult HandleGoHome(TextCommandCallingArgs args)
{
string homeName = "default";
if (args.ArgCount > 0)
{
homeName = args[0] as string;
}
if (args.Caller.Player is IServerPlayer isp)
{
PlayerStorage data = ServerUtilities.GetPlayerData(isp);
if (data.Homes.ContainsKey(homeName))
{
data.Homes[homeName].Location.Merge(isp.Entity);
ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-tp"));
}
else
{
ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-no"));
}
}
return TextCommandResult.Success();
}
internal static TextCommandResult HandleReturnItems(TextCommandCallingArgs args)
{
IPlayer player = args[0] as IPlayer;
@ -38,6 +91,27 @@ namespace AriasServerUtils
return TextCommandResult.Success();
}
internal static TextCommandResult HandleSetHome(TextCommandCallingArgs args)
{
string homeName = "default";
if (args.ArgCount > 0)
{
homeName = args[0] as string;
}
if (args.Caller.Player is IServerPlayer isp)
{
var data = ServerUtilities.GetPlayerData(isp);
data.Homes[homeName] = Home.MakeHome(args.Caller.Player.Entity, homeName);
ServerUtilities.SendMessageTo(isp, Lang.Get($"{ServerUtilities.MOD_ID}:home-set"));
ServerUtilities.MarkDirty();
}
return TextCommandResult.Success();
}
internal static TextCommandResult HandleSetSpawn(TextCommandCallingArgs args)
{
PlayerPosition pos = PlayerPosition.from(args.Caller.Entity);

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.ConstrainedExecution;
using Vintagestory.API.Common;
using Vintagestory.API.Common.Entities;
using Vintagestory.API.MathTools;
@ -8,7 +9,7 @@ using Vintagestory.GameContent;
namespace AriasServerUtils
{
[Serializable]
public class PlayerPosition
public class PlayerPosition : ICloneable
{
public int X { get; set; } = 0;
public int Y { get; set; } = 0;
@ -16,12 +17,27 @@ namespace AriasServerUtils
public int Dimension { get; set; } = 0;
public float Yaw { get; set; } = 0.0f;
public float Pitch { get; set; } = 0.0f;
public readonly PlayerPosition ZERO = new(0, 0, 0, 0, 0.0f, 0.0f);
public PlayerPosition() { }
internal PlayerPosition(int x, int y, int z, int dim, float yaw, float pitch)
{
this.X = x;
this.Y = y;
this.Z = z;
this.Dimension = dim;
this.Yaw = yaw;
this.Pitch = pitch;
}
public void Merge(Entity entity)
{
entity.TeleportTo(new BlockPos(X, Y, Z, Dimension));
entity.Pos.SetYaw(Yaw);
entity.Pos.Pitch = Pitch;
}
@ -41,13 +57,46 @@ namespace AriasServerUtils
pos.Dimension = playerPos.dimension;
pos.Yaw = entity.Pos.Yaw;
pos.Pitch = entity.Pos.Pitch;
return pos;
}
public object Clone()
{
return new PlayerPosition(this.X, this.Y, this.Z, this.Dimension, this.Yaw, this.Pitch);
}
}
public class PlayerInventory
{
public List<ItemStack> Items = new List<ItemStack>();
}
/// <summary>
/// This class creates a simple storage for all memory within this mod
/// </summary>
[Serializable]
public class PlayerStorage
{
public Dictionary<string, Home> Homes = new Dictionary<string, Home>();
}
/// <summary>
/// This class represents a single home object
/// </summary>
[Serializable]
public class Home
{
public PlayerPosition Location { get; set; }
public static Home MakeHome(Entity player, string homeName)
{
Home home = new Home();
home.Location = PlayerPosition.from(player);
return home;
}
}
}

View file

@ -7,5 +7,12 @@
"rmspawn": "World Spawn cleared. The /spawn command will now be non-functional until a new spawn is set",
"halt": "Aria's Server Utils is shutting down, performing any last minute pending tasks...",
"nobackup": "No backup inventory exists, aborting restore.."
"nobackup": "No backup inventory exists, aborting restore..",
"playerjoin": "A player has joined the server, their data is being loaded, or created",
"home-tp": "You have teleported home",
"home-no": "No such home exists",
"home-max": "You have reached your max number of homes",
"home-set": "Home saved"
}

View file

@ -3,8 +3,8 @@
"modid": "ariasserverutils",
"name": "Aria's Server Utilities",
"authors": ["zontreck"],
"description": "A collection of server utilities\n\nBuild Date: 01-18-2025 @ 01:00 PM",
"version": "1.0.0-dev.4",
"description": "A collection of server utilities\n\nBuild Date: 01-18-2025 @ 02:06 PM",
"version": "1.0.0-dev.5",
"dependencies": {
"game": ""
}