Merge pull request #2946 from AgaSpace/playerdata-changes

PlayerData changes
This commit is contained in:
Lucas Nicodemus 2025-03-10 00:42:59 +09:00 committed by GitHub
commit 1c724901b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 37 additions and 14 deletions

View file

@ -79,7 +79,7 @@ namespace TShockAPI.DB
public PlayerData GetPlayerData(TSPlayer player, int acctid) public PlayerData GetPlayerData(TSPlayer player, int acctid)
{ {
PlayerData playerData = new PlayerData(player); PlayerData playerData = new PlayerData(false);
try try
{ {

View file

@ -2620,9 +2620,9 @@ namespace TShockAPI
private static bool HandleConnecting(GetDataHandlerArgs args) private static bool HandleConnecting(GetDataHandlerArgs args)
{ {
var account = TShock.UserAccounts.GetUserAccountByName(args.Player.Name);// var account = TShock.UserAccounts.GetUserAccountByName(args.Player.Name);//
args.Player.DataWhenJoined = new PlayerData(args.Player); args.Player.DataWhenJoined = new PlayerData(false);
args.Player.DataWhenJoined.CopyCharacter(args.Player); args.Player.DataWhenJoined.CopyCharacter(args.Player);
args.Player.PlayerData = new PlayerData(args.Player); args.Player.PlayerData = new PlayerData(false);
args.Player.PlayerData.CopyCharacter(args.Player); args.Player.PlayerData.CopyCharacter(args.Player);
if (account != null && !TShock.Config.Settings.DisableUUIDLogin) if (account != null && !TShock.Config.Settings.DisableUUIDLogin)

View file

@ -23,6 +23,7 @@ using Terraria.Localization;
using Terraria.GameContent.NetModules; using Terraria.GameContent.NetModules;
using Terraria.Net; using Terraria.Net;
using Terraria.ID; using Terraria.ID;
using System;
namespace TShockAPI namespace TShockAPI
{ {
@ -63,18 +64,27 @@ namespace TShockAPI
public int unlockedSuperCart; public int unlockedSuperCart;
public int enabledSuperCart; public int enabledSuperCart;
public PlayerData(TSPlayer player) /// <summary>
/// Sets the default values for the inventory.
/// </summary>
[Obsolete("The player argument is not used.")]
public PlayerData(TSPlayer player) : this(true) { }
/// <summary>
/// Sets the default values for the inventory.
/// </summary>
/// <param name="includingStarterInventory">Is it necessary to load items from TShock's config</param>
public PlayerData(bool includingStarterInventory = true)
{ {
for (int i = 0; i < NetItem.MaxInventory; i++) for (int i = 0; i < NetItem.MaxInventory; i++)
{
this.inventory[i] = new NetItem(); this.inventory[i] = new NetItem();
}
for (int i = 0; i < TShock.ServerSideCharacterConfig.Settings.StartingInventory.Count; i++) if (includingStarterInventory)
{ for (int i = 0; i < TShock.ServerSideCharacterConfig.Settings.StartingInventory.Count; i++)
var item = TShock.ServerSideCharacterConfig.Settings.StartingInventory[i]; {
StoreSlot(i, item.NetId, item.PrefixId, item.Stack); var item = TShock.ServerSideCharacterConfig.Settings.StartingInventory[i];
} StoreSlot(i, item.NetId, item.PrefixId, item.Stack);
}
} }
/// <summary> /// <summary>
@ -86,12 +96,22 @@ namespace TShockAPI
/// <param name="stack"></param> /// <param name="stack"></param>
public void StoreSlot(int slot, int netID, byte prefix, int stack) public void StoreSlot(int slot, int netID, byte prefix, int stack)
{ {
if (slot > (this.inventory.Length - 1)) //if the slot is out of range then dont save StoreSlot(slot, new NetItem(netID, stack, prefix));
}
/// <summary>
/// Stores an item at the specific storage slot
/// </summary>
/// <param name="slot"></param>
/// <param name="item"></param>
public void StoreSlot(int slot, NetItem item)
{
if (slot > (this.inventory.Length - 1) || slot < 0) //if the slot is out of range then dont save
{ {
return; return;
} }
this.inventory[slot] = new NetItem(netID, stack, prefix); this.inventory[slot] = item;
} }
/// <summary> /// <summary>

View file

@ -1292,7 +1292,7 @@ namespace TShockAPI
} }
} }
PlayerData = new PlayerData(this); PlayerData = new PlayerData();
Group = TShock.Groups.GetGroupByName(TShock.Config.Settings.DefaultGuestGroupName); Group = TShock.Groups.GetGroupByName(TShock.Config.Settings.DefaultGuestGroupName);
tempGroup = null; tempGroup = null;
if (tempGroupTimer != null) if (tempGroupTimer != null)

View file

@ -117,6 +117,9 @@ Use past tense when adding new entries; sign your name off when you add or chang
* Added a property `TSPlayer.Hostile`, which gets pvp player mode. (@AgaSpace) * Added a property `TSPlayer.Hostile`, which gets pvp player mode. (@AgaSpace)
* Fixed bug where when the `UseSqlLogs` config property is true, an empty log file would still get created. (@ZakFahey) * Fixed bug where when the `UseSqlLogs` config property is true, an empty log file would still get created. (@ZakFahey)
* Fixed typo in `/gbuff`. (@sgkoishi, #2955) * Fixed typo in `/gbuff`. (@sgkoishi, #2955)
* Added a constructor for `TShockAPI.PlayerData` that accepts the `includingStarterInventory` parameter, which is responsible for loading the TShock inventory.
* Declared the constructor `TShockAPI.PlayerData` accepting the argument `TShockAPI.TSPlayer` obsolete.
* Updated the `PlayerData.StoreSlot` method: Added an overload that takes `TShockAPI.NetItem`.
* Added `PlayerHooks.PrePlayerCommand` hook, which fired before command execution. (@AgaSpace) * Added `PlayerHooks.PrePlayerCommand` hook, which fired before command execution. (@AgaSpace)
* Added `PlayerHooks.PostPlayerCommand` hook, which fired after command execution. (@AgaSpace) * Added `PlayerHooks.PostPlayerCommand` hook, which fired after command execution. (@AgaSpace)