This repository was archived by the owner on Jan 21, 2026. It is now read-only.
forked from Patrikkk/DieMob
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.cs
More file actions
143 lines (123 loc) · 4.95 KB
/
Plugin.cs
File metadata and controls
143 lines (123 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
using Auxiliary;
using Auxiliary.Configuration;
using CSF;
using CSF.TShock;
using DieMob.Api;
using MongoDB.Driver;
using SQLite;
using System;
using System.Collections.Generic;
using System.Reflection;
using Microsoft.Xna.Framework;
using Terraria;
using Terraria.Localization;
using TerrariaApi.Server;
using TShockAPI;
using TShockAPI.DB;
using TShockAPI.Hooks;
namespace DieMob
{
[ApiVersion(2, 1)]
public class Plugin : TerrariaPlugin
{
#region Plugin Metadata
public override string Name
=> "DieMob Regions";
public override string Author
=> "Average";
public override string Description
=> "Removes any entities that decide to enter a region";
public override Version Version
=> new Version(1, 3);
#endregion
#region Plugin Initialization
public Plugin(Main game)
: base(game)
{
Order = 1;
// Define the command standardization framework made for TShock.
_fx = new(new CommandConfiguration()
{
DoAsynchronousExecution = false
});
}
private readonly TSCommandFramework _fx;
private static DateTime lastUpdate = DateTime.UtcNow;
public static DiemobSettings Settings;
public static DiemobApi api = new();
public async override void Initialize()
{
api.SQL = new SQLiteAsyncConnection(api.dbPath);
await api.SQL.CreateTableAsync<DieMobRegion>();
Configuration<DiemobSettings>.Load(nameof(DieMob));
Settings = Configuration<DiemobSettings>.Settings;
await _fx.BuildModulesAsync(typeof(Plugin).Assembly);
GeneralHooks.ReloadEvent += (x) =>
{
Configuration<DiemobSettings>.Load(nameof(DieMob));
x.Player.SendSuccessMessage("[Diemob] Reloaded configuration.");
};
TerrariaApi.Server.ServerApi.Hooks.GameUpdate.Register(this, OnUpdate);
RegionHooks.RegionDeleted += OnRegionDelete;
}
#endregion
#region Hooks
private async void OnRegionDelete(RegionHooks.RegionDeletedEventArgs args)
{
DieMobRegion region = await api.RetrieveRegion(args.Region.Name);
if (region != null)
api.DeleteDiemob(args.Region.Name);
}
private async void OnUpdate(EventArgs e)
{
if ((DateTime.UtcNow - lastUpdate).TotalMilliseconds >= Settings.UpdateInterval)
{
lastUpdate = DateTime.UtcNow;
List<DieMobRegion> regions = await api.SQL.Table<DieMobRegion>().ToListAsync();
foreach (var r in regions)
{
var region = TShock.Regions.GetRegionByName(r.Region);
if (region is null)
continue;
foreach (var npc in Main.npc)
{
if (npc.active && !npc.townNPC)
{
if (!region.InArea((int)(npc.position.X / 16), (int)(npc.position.Y / 16)))
{
continue;
}
if (region.Area.Contains(npc.Center.ToTileCoordinates()))
{
if (r.Type == RegionType.Kill)
{
npc.active = false;
npc.life = 0;
npc.lifeMax = 0;
npc.checkDead();
Main.npc[npc.whoAmI] = new NPC();
NetMessage.SendData((int)PacketTypes.NpcUpdate, -1, -1, NetworkText.Empty,
npc.whoAmI);
}
else if (r.Type == RegionType.Repel)
{
Rectangle area = region.Area;
int yDir = -10;
if (area.Bottom - (int)(npc.position.Y / 16) < area.Height / 2)
yDir = 10;
int xDir = -10;
if (area.Right - (int)(npc.position.X / 16) < area.Width / 2)
xDir = 10;
npc.velocity = new Vector2(xDir * Settings.RepelPowerModifier,
yDir * Settings.RepelPowerModifier);
NetMessage.SendData((int)PacketTypes.NpcUpdate, -1, -1, NetworkText.Empty, npc.whoAmI);
}
}
}
}
}
}
}
#endregion
}
}