SignatureBeef / Open-Terraria-API

Open Terraria API - Mac, Linux & Windows
GNU General Public License v3.0
93 stars 36 forks source link

Pressure plate collision hook doesn't work #104

Open MineBartekSA opened 1 year ago

MineBartekSA commented 1 year ago

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 ```cs 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 commented 1 year ago

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 commented 1 year ago

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.