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

Pressure plate collision hook doesn't work #104

Open
MineBartekSA opened this issue Jan 11, 2023 · 2 comments
Open

Pressure plate collision hook doesn't work #104

MineBartekSA opened this issue Jan 11, 2023 · 2 comments

Comments

@MineBartekSA
Copy link

MineBartekSA commented Jan 11, 2023

Hello!
I've been trying to update my TShock plugin that utilizes the OTAPI.Hooks.Collision.PressurePlate hook and it doesn't seem to work.

To test this out, I've written a simple OTAPI Server that utilizes this hook.
Even on this server, the hook wouldn't trigger.

I've attached my code in the spoiler below.
Other hooks work as one would expect.

Simple OTAPI Server code

Target framework: .net6.0
Built on the latest version of OTAPI (3.1.20) got from NuGet

using OTAPI;
using Terraria;

namespace OTest;

internal abstract class Program
{
    private static void Main(string[] args)
    {
        Console.WriteLine("Starting....");
        On.Terraria.Main.DedServ += OnDedicatedServer;
        Hooks.Collision.PressurePlate += OnPressurePlate;
        Hooks.NetMessage.PlayerAnnounce += OnPlayerAnnounce;
        WindowsLaunch.Main(args);
    }

    private static void OnDedicatedServer(On.Terraria.Main.orig_DedServ orig, Main self)
    {
        Console.WriteLine("Dedicated server start!");
        orig(self);
    }

    private static void OnPressurePlate(object? sender, Hooks.Collision.PressurePlateEventArgs args)
    {
        Console.WriteLine($"Entity {args.Entity.entityId} triggered pressure plate");
    }

    private static void OnPlayerAnnounce(object? sender, Hooks.NetMessage.PlayerAnnounceEventArgs args)
    {
        Console.WriteLine($"Announcing player: {args.Text}");
    }
}
@SignatureBeef
Copy link
Owner

I believe the pressure plate implementation in Terraria has changed over the years, and the current hook likely only works for non players as iirc the client now sends the HitSwitch packet instead, so you likely need to listen on those events instead nowadays.

this could be something that OTAPI can patch to get working through the current hook, but for now ive modified your example below which may help?

using Terraria;
using OTAPI;

Console.WriteLine("Starting....");
On.Terraria.Main.DedServ += OnDedicatedServer;
Hooks.Collision.PressurePlate += OnPressurePlate;
Hooks.NetMessage.PlayerAnnounce += OnPlayerAnnounce;
On.Terraria.GameContent.PressurePlateHelper.PokeLocation += PressurePlateHelper_PokeLocation;
On.Terraria.Wiring.HitSwitch += Wiring_HitSwitch;

void Wiring_HitSwitch(On.Terraria.Wiring.orig_HitSwitch orig, int i, int j)
{
    var player = Wiring.CurrentUser != 255 ? Main.player[Wiring.CurrentUser].name : null;
    var tile = Main.tile[i, j];
    if (tile.type == 135 || tile.type == 442 || tile.type == 428)
    {
        Console.WriteLine($"HitSwitch Pressure Plate. T={tile.type} by {player ?? "??"}");
    }
    orig(i, j);
}

void PressurePlateHelper_PokeLocation(On.Terraria.GameContent.PressurePlateHelper.orig_PokeLocation orig, Microsoft.Xna.Framework.Point location)
{
    var tile = Main.tile[location.X, location.Y];
    if (tile.type == 135 || tile.type == 442 || tile.type == 428)
        Console.WriteLine($"PressurePlateHelper_PokeLocation. T={tile.type}");
    orig(location);
}

static void OnDedicatedServer(On.Terraria.Main.orig_DedServ orig, Main self)
{
    Console.WriteLine("Dedicated server start!");
    orig(self);
}

static void OnPressurePlate(object? sender, Hooks.Collision.PressurePlateEventArgs args)
{
    var tile = Main.tile[args.X, args.Y];
    Console.WriteLine($"Entity {args.Entity.whoAmI} triggered pressure plate. T={tile.type}");
}

static void OnPlayerAnnounce(object? sender, Hooks.NetMessage.PlayerAnnounceEventArgs args)
{
    Console.WriteLine($"Announcing player: {args.Text}");
}

WindowsLaunch.Main(args);

image

@MineBartekSA
Copy link
Author

Ah, thank you.
I also did some investigation on my own and came to the same conclusion.

I also experimented with adding a hook callback when the server receives the HitSwitch packet (I did not notice there is a Wiring.HitSwitch hook) and then checking if the packet did not come from the server and if it actually was a pressure plate.

But as far as I can see, I'll need to write a workaround for my specific case.

Thanks again for your response.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants