Skip to content

Commit 84defbb

Browse files
committed
add registry helper methods (closes #2)
1 parent 50cd476 commit 84defbb

2 files changed

Lines changed: 103 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
**:sparkles: Features**
66
- Added `System.RegisterClasses` virtual method ([#5](https://git.omni.ms/vintage-story-mods/common-mod/issues/5))
7+
- Added registry helper methods ([#2](https://git.omni.ms/vintage-story-mods/common-mod/issues/2))
78

89
**:hammer: Refactors**
910
- Cleanup pass ([#1](https://git.omni.ms/vintage-story-mods/common-mod/issues/1))

Common.Mod/System.cs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using JetBrains.Annotations;
88
using Vintagestory.API.Client;
99
using Vintagestory.API.Common;
10+
using Vintagestory.API.Common.Entities;
1011
using Vintagestory.API.Config;
1112
using Vintagestory.API.Server;
1213
using GamePaths = Common.Mod.Core.GamePaths;
@@ -18,11 +19,15 @@ namespace Common.Mod;
1819
public abstract class System<TSystem> : ModSystem, ISystem
1920
where TSystem : System<TSystem>
2021
{
22+
#region Events
23+
2124
public event ISystem.AssetsLoadedHandler? OnAssetsLoaded;
2225
public event ISystem.AssetsFinalizedHandler? OnAssetsFinalized;
2326
public event ISystem.ServerPlayerJoinedHandler? OnServerPlayerJoined;
2427
public event ISystem.ClientPlayerJoinedHandler? OnClientPlayerJoined;
2528

29+
#endregion Events
30+
2631
private const string ConfigLibModId = "configlib";
2732

2833
[UsedImplicitly(ImplicitUseKindFlags.Access)]
@@ -35,6 +40,8 @@ protected System()
3540
Instance = this as TSystem ?? throw new NullReferenceException();
3641
}
3742

43+
#region Abstracts
44+
3845
public abstract string ModId();
3946
public abstract string ModVersion();
4047
public abstract string ModName();
@@ -43,6 +50,10 @@ protected System()
4350

4451
protected abstract void RegisterConfigs(IConfigSystem configSystem);
4552

53+
#endregion Abstracts
54+
55+
#region StartPre
56+
4657
public override void StartPre(ICoreAPI api)
4758
{
4859
// Core API, side, ISystem
@@ -141,6 +152,10 @@ public override void AssetsFinalize(ICoreAPI api)
141152
OnAssetsFinalized?.Invoke();
142153
}
143154

155+
#endregion StartPre
156+
157+
#region Start
158+
144159
public override void Start(ICoreAPI api)
145160
{
146161
RegisterClasses(api);
@@ -156,6 +171,8 @@ public override void StartClientSide(ICoreClientAPI api)
156171
api.Event.PlayerJoin += _OnClientPlayerJoined;
157172
}
158173

174+
#endregion Start
175+
159176
public override void Dispose()
160177
{
161178
var api = Container.Resolve<ICoreAPI>();
@@ -172,6 +189,8 @@ public override void Dispose()
172189
}
173190
}
174191

192+
#region Virtuals
193+
175194
[UsedImplicitly]
176195
protected virtual void ServerRegisterNetworkMessages(IServerNetworkChannel channel)
177196
{
@@ -199,6 +218,87 @@ protected virtual void RegisterClasses(ICoreAPI api)
199218
{
200219
}
201220

221+
#endregion Virtuals
222+
223+
#region Registry Helpers
224+
225+
[UsedImplicitly]
226+
protected void RegisterEntity<TEntity>(ICoreAPI api, string className)
227+
where TEntity : Entity
228+
{
229+
api.RegisterEntity(PrefixName(className), typeof(TEntity));
230+
}
231+
232+
[UsedImplicitly]
233+
protected void RegisterEntityClass(ICoreAPI api, string className, EntityProperties properties)
234+
{
235+
api.RegisterEntityClass(PrefixName(className), properties);
236+
}
237+
238+
[UsedImplicitly]
239+
protected void RegisterEntityBehaviorClass<TEntityBehavior>(ICoreAPI api, string className)
240+
where TEntityBehavior : EntityBehavior
241+
{
242+
api.RegisterEntityBehaviorClass(PrefixName(className), typeof(TEntityBehavior));
243+
}
244+
245+
[UsedImplicitly]
246+
protected void RegisterBlockClass<TBlock>(ICoreAPI api, string className)
247+
where TBlock : Block
248+
{
249+
api.RegisterBlockClass(PrefixName(className), typeof(TBlock));
250+
}
251+
252+
[UsedImplicitly]
253+
protected void RegisterBlockBehaviorClass<TBlockBehavior>(ICoreAPI api, string className)
254+
where TBlockBehavior : BlockBehavior
255+
{
256+
api.RegisterBlockBehaviorClass(PrefixName(className), typeof(TBlockBehavior));
257+
}
258+
259+
[UsedImplicitly]
260+
protected void RegisterBlockEntityClass<TBlockEntity>(ICoreAPI api, string className)
261+
where TBlockEntity : BlockEntity
262+
{
263+
api.RegisterBlockEntityClass(PrefixName(className), typeof(TBlockEntity));
264+
}
265+
266+
[UsedImplicitly]
267+
protected void RegisterBlockEntityBehaviorClass<TBlockEntityBehavior>(ICoreAPI api, string className)
268+
where TBlockEntityBehavior : BlockEntityBehavior
269+
{
270+
api.RegisterBlockEntityBehaviorClass(PrefixName(className), typeof(TBlockEntityBehavior));
271+
}
272+
273+
[UsedImplicitly]
274+
protected void RegisterCropBehavior<TCropBehavior>(ICoreAPI api, string className)
275+
where TCropBehavior : CropBehavior
276+
{
277+
api.RegisterCropBehavior(PrefixName(className), typeof(TCropBehavior));
278+
}
279+
280+
[UsedImplicitly]
281+
protected void RegisterItemClass<TItem>(ICoreAPI api, string className)
282+
where TItem : Item
283+
{
284+
api.RegisterItemClass(PrefixName(className), typeof(TItem));
285+
}
286+
287+
[UsedImplicitly]
288+
protected void RegisterCollectibleBehaviorClass<TCollectibleBehavior>(ICoreAPI api, string className)
289+
where TCollectibleBehavior : CollectibleBehavior
290+
{
291+
api.RegisterCollectibleBehaviorClass(PrefixName(className), typeof(TCollectibleBehavior));
292+
}
293+
294+
[UsedImplicitly]
295+
protected void RegisterMountable(ICoreAPI api, string className, GetMountableDelegate mountableInstantiator)
296+
{
297+
api.RegisterMountable(PrefixName(className), mountableInstantiator);
298+
}
299+
300+
#endregion Registry Helpers
301+
202302
private void _OnServerPlayerJoined(IServerPlayer player)
203303
{
204304
// Config synchronization
@@ -214,4 +314,6 @@ private void _OnClientPlayerJoined(IClientPlayer player)
214314
{
215315
OnClientPlayerJoined?.Invoke(player);
216316
}
317+
318+
private string PrefixName(string name) => $"{ModId()}:{name}";
217319
}

0 commit comments

Comments
 (0)