Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide a way for plugins to override TShock SSC #3022

Closed
wants to merge 1 commit into from

Conversation

ZakFahey
Copy link
Contributor

@ZakFahey ZakFahey commented Feb 18, 2024

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.

Context: this functionality is in the internal Penguin Games server fork of TShock that we have, but I'm trying to merge everything from there upstream because I don't want to have to deal with maintaining a fork. Especially with 1.4.5 coming out any day now, I don't want to create more work for myself. We use SSC but allow logged-out users to play, and I'm sure that that's the case for other public servers as well.

Not all instances of Main.ServerSideCharacter were replaced because this is the code our server uses, and it works. I could understand wanting to change most if not all instances of it, though.

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.
@ZakFahey
Copy link
Contributor Author

That config value combines two things: enabling SSC mode for Terraria clients, which lets you directly set their inventory, and TShock's SSC system, which saves players' inventories to TShock's database and disables logged out characters. I want one but not the other.

I'd acknowledge that this PR may be redundant if it's trivial to just have the config flag be off but override Terraria's SSC checks in my own plugin.

@sgkoishi
Copy link
Member

sgkoishi commented Feb 22, 2024

There are three fields related: Main.ServerSideCharacter, the newly introduced TShock.UsingTShockSSC, and the (almost) unused SscSettings.Enabled. I guess he is proposing moving the function of the new TShock.UsingTShockSSC to replace the unused SscSettings.Enabled (since the latter is only used once for Main.ServerSideCharacter = SscSettings.Enabled)?

@Arthri
Copy link
Contributor

Arthri commented Feb 22, 2024

Yeah, that's what I did on my fork Arthri@a1fce06. I replaced the parts relevant to TShock's SSC and left the others untouched(like /rocket)

@ZakFahey
Copy link
Contributor Author

ZakFahey commented Mar 1, 2024

Hmm, I'm noticing that if I turn off TShock's SSC just in the config on a normal version of it, while Main.ServerSideCharacter is false, players can still have their inventory slots set directly. Looking at the code, it does seem like it being false affects some things, but I can't immediately see them when joining a server.

@ZakFahey
Copy link
Contributor Author

I am going to close this PR because I have found a workaround. You just need to

  • Disable SSC in the config
  • Spoof Main.ServerSideCharacter as true in packets sent to clients.
  • GiveItem does not give items directly anymore due to this line
private void GiveItemDirectly(int type, int stack, int prefix)
{
    if (ItemID.Sets.IsAPickup[type] || !Main.ServerSideCharacter || this.IsDisabledForSSC)
    {
        GiveItemByDrop(type, stack, prefix);
        return;
    }

so as a workaround to that problem I have added this command hook

TShockAPI.Hooks.PlayerHooks.PlayerCommand += OnCommand;
...
private void OnCommand(PlayerCommandEventArgs args)
{
    switch (args.CommandName)
    {
        case "item":
        case "i":
        case "give":
        case "g":
            // If Main.ServerSideCharacter is true, we're in the override for this and shouldn't
            // run anything. Otherwise we'd go in an infinite loop.
            if (!Main.ServerSideCharacter)
                break;
            Main.ServerSideCharacter = true;
            try
            {
                TShockAPI.Commands.HandleCommand(args.Player, $"/{args.CommandText}");
            }
            finally
            {
                Main.ServerSideCharacter = false;
            }
            args.Handled = true;
            break;
    }
}

which will make Main.ServerSideCharacter be true when I need it. Then any instance of GiveItem in my own plugin needs to be replaced with a util that does something similar.

@ZakFahey ZakFahey closed this Jul 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants