Skip to content

Commit

Permalink
fix possible null references
Browse files Browse the repository at this point in the history
  • Loading branch information
BillyGalbreath committed Mar 16, 2024
1 parent 6d4b671 commit a757d68
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
18 changes: 10 additions & 8 deletions src/GlowingArrows.cs
@@ -1,4 +1,6 @@
using System.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using Vintagestory.API.Client;
using Vintagestory.API.Common;
using Vintagestory.API.Common.Entities;
Expand All @@ -14,13 +16,13 @@ public class GlowingArrows : ModSystem {

public override void StartClientSide(ICoreClientAPI api) {
api.Event.RegisterCallback(_ => {
foreach (EntityProperties properties in from properties in api.World.EntityTypes
let mapping = ((ClassRegistryAPI)api.ClassRegistry).GetField<ClassRegistry>("registry").entityClassNameToTypeMapping
where mapping[properties.Class].IsAssignableFrom(typeof(EntityProjectile))
|| properties.Class.ToLower().Contains("projectile")
|| (properties.Code.ToString()?.ToLower().Contains("projectile") ?? false)
select properties) {
properties.Client.GlowLevel = 0xFF;
Dictionary<string, Type>? mapping = ((ClassRegistryAPI)api.ClassRegistry).GetField<ClassRegistry>("registry")?.entityClassNameToTypeMapping;
foreach (EntityProperties entityProperties in api.World.EntityTypes.Where(properties =>
(mapping != null && mapping[properties.Class].IsAssignableFrom(typeof(EntityProjectile)))
|| properties.Class.ToLower().Contains("projectile")
|| (properties.Code.ToString()?.ToLower().Contains("projectile") ?? false)
)) {
entityProperties.Client.GlowLevel = 0xFF;
}
}, 1000);
}
Expand Down
4 changes: 2 additions & 2 deletions src/ReflectionHelper.cs
Expand Up @@ -3,7 +3,7 @@
namespace GlowingArrows;

public static class ReflectionHelper {
public static T GetField<T>(this object obj, string name) {
return (T)obj.GetType().GetField(name, BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(obj);
public static T? GetField<T>(this object obj, string name) where T : class {
return obj.GetType().GetField(name, BindingFlags.NonPublic | BindingFlags.Instance)?.GetValue(obj) as T;
}
}

0 comments on commit a757d68

Please sign in to comment.