|
1 | | -using Microsoft.Xna.Framework; |
2 | | -using OTAPI; |
| 1 | +using OTAPI; |
3 | 2 |
|
4 | | -namespace TerrariaApi.Server.Hooking |
| 3 | +namespace TerrariaApi.Server.Hooking; |
| 4 | + |
| 5 | +internal static class GameHooks |
5 | 6 | { |
6 | | - internal static class GameHooks |
7 | | - { |
8 | | - private static HookManager _hookManager; |
| 7 | + private static HookManager _hookManager; |
9 | 8 |
|
10 | | - /// <summary> |
11 | | - /// Attaches any of the OTAPI Game hooks to the existing <see cref="HookManager"/> implementation |
12 | | - /// </summary> |
13 | | - /// <param name="hookManager">HookManager instance which will receive the events</param> |
14 | | - public static void AttachTo(HookManager hookManager) |
15 | | - { |
16 | | - _hookManager = hookManager; |
| 9 | + /// <summary> |
| 10 | + /// Attaches any of the OTAPI Game hooks to the existing <see cref="HookManager"/> implementation |
| 11 | + /// </summary> |
| 12 | + /// <param name="hookManager">HookManager instance which will receive the events</param> |
| 13 | + public static void AttachTo(HookManager hookManager) |
| 14 | + { |
| 15 | + _hookManager = hookManager; |
17 | 16 |
|
18 | | - On.Terraria.Main.Update += OnUpdate; |
19 | | - On.Terraria.Main.Initialize += OnInitialize; |
20 | | - On.Terraria.Netplay.StartServer += OnStartServer; |
| 17 | + HookEvents.Terraria.Main.Update += OnUpdate; |
| 18 | + HookEvents.Terraria.Main.Initialize += OnInitialize; |
| 19 | + HookEvents.Terraria.Netplay.StartServer += OnStartServer; |
21 | 20 |
|
22 | | - Hooks.WorldGen.HardmodeTilePlace += OnHardmodeTilePlace; |
23 | | - Hooks.WorldGen.HardmodeTileUpdate += OnHardmodeTileUpdate; |
24 | | - Hooks.Item.MechSpawn += OnItemMechSpawn; |
25 | | - Hooks.NPC.MechSpawn += OnNpcMechSpawn; |
26 | | - } |
| 21 | + Hooks.WorldGen.HardmodeTilePlace += OnHardmodeTilePlace; |
| 22 | + Hooks.WorldGen.HardmodeTileUpdate += OnHardmodeTileUpdate; |
| 23 | + Hooks.Item.MechSpawn += OnItemMechSpawn; |
| 24 | + Hooks.NPC.MechSpawn += OnNpcMechSpawn; |
| 25 | + } |
27 | 26 |
|
28 | | - private static void OnUpdate(On.Terraria.Main.orig_Update orig, Terraria.Main instance, GameTime gameTime) |
29 | | - { |
30 | | - _hookManager.InvokeGameUpdate(); |
31 | | - orig(instance, gameTime); |
32 | | - _hookManager.InvokeGamePostUpdate(); |
33 | | - } |
| 27 | + private static void OnUpdate(Terraria.Main instance, HookEvents.Terraria.Main.UpdateEventArgs args) |
| 28 | + { |
| 29 | + if (!args.ContinueExecution) return; |
| 30 | + args.ContinueExecution = false; |
| 31 | + _hookManager.InvokeGameUpdate(); |
| 32 | + args.OriginalMethod(args.gameTime); |
| 33 | + _hookManager.InvokeGamePostUpdate(); |
| 34 | + } |
34 | 35 |
|
35 | | - private static void OnHardmodeTileUpdate(object sender, Hooks.WorldGen.HardmodeTileUpdateEventArgs e) |
| 36 | + private static void OnHardmodeTileUpdate(object sender, Hooks.WorldGen.HardmodeTileUpdateEventArgs e) |
| 37 | + { |
| 38 | + if (_hookManager.InvokeGameHardmodeTileUpdate(e.X, e.Y, e.Type)) |
36 | 39 | { |
37 | | - if (_hookManager.InvokeGameHardmodeTileUpdate(e.X, e.Y, e.Type)) |
38 | | - { |
39 | | - e.Result = HookResult.Cancel; |
40 | | - } |
| 40 | + e.Result = HookResult.Cancel; |
41 | 41 | } |
| 42 | + } |
42 | 43 |
|
43 | | - private static void OnHardmodeTilePlace(object sender, Hooks.WorldGen.HardmodeTilePlaceEventArgs e) |
| 44 | + private static void OnHardmodeTilePlace(object sender, Hooks.WorldGen.HardmodeTilePlaceEventArgs e) |
| 45 | + { |
| 46 | + if (_hookManager.InvokeGameHardmodeTileUpdate(e.X, e.Y, e.Type)) |
44 | 47 | { |
45 | | - if (_hookManager.InvokeGameHardmodeTileUpdate(e.X, e.Y, e.Type)) |
46 | | - { |
47 | | - e.Result = HardmodeTileUpdateResult.Cancel; |
48 | | - } |
| 48 | + e.Result = HardmodeTileUpdateResult.Cancel; |
49 | 49 | } |
| 50 | + } |
50 | 51 |
|
51 | | - private static void OnInitialize(On.Terraria.Main.orig_Initialize orig, Terraria.Main instance) |
52 | | - { |
53 | | - HookManager.InitialiseAPI(); |
54 | | - _hookManager.InvokeGameInitialize(); |
55 | | - orig(instance); |
56 | | - } |
| 52 | + private static void OnInitialize(Terraria.Main instance, HookEvents.Terraria.Main.InitializeEventArgs args) |
| 53 | + { |
| 54 | + if (!args.ContinueExecution) return; |
| 55 | + HookManager.InitialiseAPI(); |
| 56 | + _hookManager.InvokeGameInitialize(); |
| 57 | + } |
57 | 58 |
|
58 | | - private static void OnStartServer(On.Terraria.Netplay.orig_StartServer orig) |
59 | | - { |
60 | | - _hookManager.InvokeGamePostInitialize(); |
61 | | - orig(); |
62 | | - } |
| 59 | + private static void OnStartServer(object? sender, HookEvents.Terraria.Netplay.StartServerEventArgs args) |
| 60 | + { |
| 61 | + if (!args.ContinueExecution) return; |
| 62 | + _hookManager.InvokeGamePostInitialize(); |
| 63 | + } |
63 | 64 |
|
64 | | - private static void OnItemMechSpawn(object sender, Hooks.Item.MechSpawnEventArgs e) |
| 65 | + private static void OnItemMechSpawn(object sender, Hooks.Item.MechSpawnEventArgs e) |
| 66 | + { |
| 67 | + if (!_hookManager.InvokeGameStatueSpawn(e.Num2, e.Num3, e.Num, (int)(e.X / 16f), (int)(e.Y / 16f), e.Type, false)) |
65 | 68 | { |
66 | | - if (!_hookManager.InvokeGameStatueSpawn(e.Num2, e.Num3, e.Num, (int)(e.X / 16f), (int)(e.Y / 16f), e.Type, false)) |
67 | | - { |
68 | | - e.Result = HookResult.Cancel; |
69 | | - } |
| 69 | + e.Result = HookResult.Cancel; |
70 | 70 | } |
| 71 | + } |
71 | 72 |
|
72 | | - private static void OnNpcMechSpawn(object sender, Hooks.NPC.MechSpawnEventArgs e) |
| 73 | + private static void OnNpcMechSpawn(object sender, Hooks.NPC.MechSpawnEventArgs e) |
| 74 | + { |
| 75 | + if (!_hookManager.InvokeGameStatueSpawn(e.Num2, e.Num3, e.Num, (int)(e.X / 16f), (int)(e.Y / 16f), e.Type, true)) |
73 | 76 | { |
74 | | - if (!_hookManager.InvokeGameStatueSpawn(e.Num2, e.Num3, e.Num, (int)(e.X / 16f), (int)(e.Y / 16f), e.Type, true)) |
75 | | - { |
76 | | - e.Result = HookResult.Cancel; |
77 | | - } |
| 77 | + e.Result = HookResult.Cancel; |
78 | 78 | } |
79 | 79 | } |
80 | 80 | } |
0 commit comments