Skip to content

Commit

Permalink
Provide a way for plugins to override TShock SSC
Browse files Browse the repository at this point in the history
Add `TShock.UseSSCInventory`, which can be set to false by another plugin on initialization to override TShock's SSC implementation while keeping SSC on. This fixes the use case where you want to be able to override players' inventories from the server, but you don't want to use TShock's SSC system or force players to stay logged in.
  • Loading branch information
ZakFahey committed Feb 18, 2024
1 parent 5d585bb commit a3b2fe8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
12 changes: 6 additions & 6 deletions TShockAPI/Commands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ private static void AttemptLogin(CommandArgs args)
args.Player.IsLoggedIn = true;
args.Player.IsDisabledForSSC = false;

if (Main.ServerSideCharacter)
if (TShock.UsingTShockSSC)
{
if (args.Player.HasPermission(Permissions.bypassssc))
{
Expand Down Expand Up @@ -954,7 +954,7 @@ private static void Logout(CommandArgs args)

args.Player.Logout();
args.Player.SendSuccessMessage(GetString("You have been successfully logged out of your account."));
if (Main.ServerSideCharacter)
if (TShock.UsingTShockSSC)
{
args.Player.SendWarningMessage(GetString("Server side characters are enabled. You need to be logged-in to play."));
}
Expand Down Expand Up @@ -1753,7 +1753,7 @@ private static void DisplayLogs(CommandArgs args)

private static void SaveSSC(CommandArgs args)
{
if (Main.ServerSideCharacter)
if (TShock.UsingTShockSSC)
{
args.Player.SendSuccessMessage(GetString("Your server-side character data has been saved."));
foreach (TSPlayer player in TShock.Players)
Expand All @@ -1768,9 +1768,9 @@ private static void SaveSSC(CommandArgs args)

private static void OverrideSSC(CommandArgs args)
{
if (!Main.ServerSideCharacter)
if (!TShock.UsingTShockSSC)
{
args.Player.SendErrorMessage(GetString("Server-side characters is disabled."));
args.Player.SendErrorMessage(GetString("Server-side characters are disabled."));
return;
}
if (args.Parameters.Count < 1)
Expand Down Expand Up @@ -2026,7 +2026,7 @@ private static void Broadcast(CommandArgs args)
private static void Off(CommandArgs args)
{

if (Main.ServerSideCharacter)
if (TShock.UsingTShockSSC)
{
foreach (TSPlayer player in TShock.Players)
{
Expand Down
8 changes: 4 additions & 4 deletions TShockAPI/GetDataHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2600,7 +2600,7 @@ private static bool HandlePlayerSlot(GetDataHandlerArgs args)
{
args.Player.PlayerData.StoreSlot(slot, type, prefix, stack);
}
else if (Main.ServerSideCharacter && TShock.Config.Settings.DisableLoginBeforeJoin && !bypassTrashCanCheck &&
else if (TShock.UsingTShockSSC && TShock.Config.Settings.DisableLoginBeforeJoin && !bypassTrashCanCheck &&
args.Player.HasSentInventory && !args.Player.HasPermission(Permissions.bypassssc))
{
// The player might have moved an item to their trash can before they performed a single login attempt yet.
Expand Down Expand Up @@ -2645,7 +2645,7 @@ private static bool HandleConnecting(GetDataHandlerArgs args)
args.Player.IsLoggedIn = true;
args.Player.IsDisabledForSSC = false;

if (Main.ServerSideCharacter)
if (TShock.UsingTShockSSC)
{
if (args.Player.HasPermission(Permissions.bypassssc))
{
Expand Down Expand Up @@ -3230,7 +3230,7 @@ private static bool HandlePassword(GetDataHandlerArgs args)
args.Player.IsLoggedIn = true;
args.Player.IsDisabledForSSC = false;

if (Main.ServerSideCharacter)
if (TShock.UsingTShockSSC)
{
if (args.Player.HasPermission(Permissions.bypassssc))
{
Expand Down Expand Up @@ -4263,7 +4263,7 @@ private static bool HandlePlayerKillMeV2(GetDataHandlerArgs args)
}
}

if (args.TPlayer.difficulty == 2 && Main.ServerSideCharacter && args.Player.IsLoggedIn)
if (args.TPlayer.difficulty == 2 && TShock.UsingTShockSSC && args.Player.IsLoggedIn)
{
if (TShock.CharacterDB.RemovePlayer(args.Player.Account.ID))
{
Expand Down
19 changes: 15 additions & 4 deletions TShockAPI/TShock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,17 @@ public class TShock : TerrariaPlugin
/// </summary>
public static Dictionary<string, SecureRest.TokenData> RESTStartupTokens = new Dictionary<string, SecureRest.TokenData>();

/// <summary>
/// Whether to set a player's inventory with SSC and force them to log in to play. Set this to false to enable
/// server-side inventory setting while overriding TShock's implementation of it.
/// </summary>
public static bool UseSSCInventory { get; set; } = true;

/// <summary>
/// Whether TShock's server-side character system is enabled.
/// </summary>
public static bool UsingTShockSSC => Main.ServerSideCharacter && UseSSCInventory;

/// <summary>The TShock anti-cheat/anti-exploit system.</summary>
internal Bouncer Bouncer;

Expand Down Expand Up @@ -1100,7 +1111,7 @@ private void OnUpdate(EventArgs args)
LastCheck = DateTime.UtcNow;
}

if (Main.ServerSideCharacter && (DateTime.UtcNow - LastSave).TotalMinutes >= ServerSideCharacterConfig.Settings.ServerSideCharacterSave)
if (UsingTShockSSC && (DateTime.UtcNow - LastSave).TotalMinutes >= ServerSideCharacterConfig.Settings.ServerSideCharacterSave)
{
foreach (TSPlayer player in Players)
{
Expand Down Expand Up @@ -1433,7 +1444,7 @@ private void OnLeave(LeaveEventArgs args)
Utils.Broadcast(GetString("{0} has left.", tsplr.Name), Color.Yellow);
Log.Info(GetString("{0} disconnected.", tsplr.Name));

if (tsplr.IsLoggedIn && !tsplr.IsDisabledPendingTrashRemoval && Main.ServerSideCharacter && (!tsplr.Dead || tsplr.TPlayer.difficulty != 2))
if (tsplr.IsLoggedIn && !tsplr.IsDisabledPendingTrashRemoval && UsingTShockSSC && (!tsplr.Dead || tsplr.TPlayer.difficulty != 2))
{
tsplr.PlayerData.CopyCharacter(tsplr);
CharacterDB.InsertPlayerData(tsplr);
Expand Down Expand Up @@ -1721,10 +1732,10 @@ private void OnGreetPlayer(GreetPlayerEventArgs args)

if (!player.IsLoggedIn)
{
if (Main.ServerSideCharacter)
if (UsingTShockSSC)
{
player.IsDisabledForSSC = true;
player.SendErrorMessage(GetString("Server side characters is enabled! Please {0}register or {0}login to play!", Commands.Specifier));
player.SendErrorMessage(GetString("Server side characters are enabled! Please {0}register or {0}login to play!", Commands.Specifier));
player.LoginHarassed = true;
}
else if (Config.Settings.RequireLogin)
Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ 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)
* Fixed typo in `/gbuff`. (@sgkoishi, #2955)
* Rewrote the `.dockerignore` file into a denylist. (@timschumi)
* Added the property `TShock.UseSSCInventory` for plugin developers to override TShock's default SSC system. (@ZakFahey)

## TShock 5.2
* An additional option `pvpwithnoteam` is added at `PvPMode` to enable PVP with no team. (@CelestialAnarchy, #2617, @ATFGK)
Expand Down

0 comments on commit a3b2fe8

Please sign in to comment.