Corosauce / OutOfSight

Minecraft Forge Mod - Helps improve frame rates by limiting how far away tile entities and entities can render, with configurable ranges.
Other
6 stars 2 forks source link

Add null check in `OutOfSight#getCanonicalName` #14

Open SuperMartijn642 opened 1 year ago

SuperMartijn642 commented 1 year ago

The OutOfSight#getCanonicalName method returns the canonical name of the given class. Specifically, Class#getCanonicalName is cached and returned. As per the javadoc, the result of Class#getCanonicalName may be null for local, anonymous, or hidden classes. This means that the result of OutOfSight#getCanonicalName will also be null for those cases.

Everywhere OutOfSight#getCanonicalName is called, String#startsWith is immediately called on its return value. This leads to an exception whenever the result of OutOfSight#getCanonicalName is null.

Currently, the only use case for OutOfSight#getCanonicalName is to check if a certain class is in the net.minecraft package. Since the cases mentioned above don't occur for any of Minecraft's classes anyways, I've opted to return an empty string whenever Class#getCanonicalName is null.

-         cacheClassToCanonicalName.put(clazz, clazz.getCanonicalName());
+         String canonicalName = clazz.getCanonicalName();
+         if (canonicalName == null) {
+             canonicalName = "";
+         }
+         cacheClassToCanonicalName.put(clazz, canonicalName);

Relevant issues: This seems to cause issues for nearly all my mods (#13, #12, https://github.com/SuperMartijn642/WirelessChargers/issues/21) and also for some other mods, such as ElementalCraft (#6). I keep getting issue reports from this, so hopefully it can be solved either through this PR or through some other solution 🙂