Possible NPE fixes for KubeJS

This commit is contained in:
embeddedt 2023-08-30 18:49:25 -04:00
parent 20a15a587c
commit fcde6104eb
No known key found for this signature in database
GPG Key ID: A69433EC199B5613
2 changed files with 16 additions and 6 deletions

View File

@ -24,8 +24,11 @@ public class IDFilterMixin {
@Overwrite(remap = false) @Overwrite(remap = false)
public boolean test(RecipeJS recipe) { public boolean test(RecipeJS recipe) {
if(!_targetSearched) { if(!_targetSearched) {
_target = KubeUtil.originalRecipesByHash.get(this.id); if(KubeUtil.originalRecipesByHash.size() > 0) {
_targetSearched = true; _target = KubeUtil.originalRecipesByHash.get(this.id);
_targetSearched = true;
} else
return recipe.getOrCreateId().equals(this.id); // fallback
} }
return recipe == _target; return recipe == _target;
} }

View File

@ -4,6 +4,7 @@ import dev.latvian.kubejs.server.TagEventJS;
import dev.latvian.kubejs.util.UtilsJS; import dev.latvian.kubejs.util.UtilsJS;
import me.shedaniel.architectury.registry.Registry; import me.shedaniel.architectury.registry.Registry;
import net.minecraft.resources.ResourceLocation; import net.minecraft.resources.ResourceLocation;
import org.embeddedt.modernfix.ModernFix;
import org.embeddedt.modernfix.annotation.RequiresMod; import org.embeddedt.modernfix.annotation.RequiresMod;
import org.embeddedt.modernfix.forge.util.KubeUtil; import org.embeddedt.modernfix.forge.util.KubeUtil;
import org.spongepowered.asm.mixin.Mixin; import org.spongepowered.asm.mixin.Mixin;
@ -29,15 +30,21 @@ public class TagWrapperMixin<T> {
@Redirect(method = "add", at = @At(value = "INVOKE", target = "Lme/shedaniel/architectury/registry/Registry;getIds()Ljava/util/Set;", ordinal = 0), remap = false) @Redirect(method = "add", at = @At(value = "INVOKE", target = "Lme/shedaniel/architectury/registry/Registry;getIds()Ljava/util/Set;", ordinal = 0), remap = false)
private Set<ResourceLocation> getCachedIds(Registry<T> registryIn) { private Set<ResourceLocation> getCachedIds(Registry<T> registryIn) {
String currentPatternStr = this.currentPatternStr;
if(currentPatternStr == null) if(currentPatternStr == null)
throw new AssertionError(); throw new AssertionError();
Set<ResourceLocation> cachedSet = KubeUtil.matchedIdsForRegex.get(currentPatternStr); Set<ResourceLocation> cachedSet = KubeUtil.matchedIdsForRegex.get(currentPatternStr);
if(cachedSet == null) { if(cachedSet == null) {
Pattern thePattern = UtilsJS.parseRegex(currentPatternStr); Pattern thePattern = UtilsJS.parseRegex(currentPatternStr);
ArrayList<ResourceLocation> locations = new ArrayList<>(registryIn.getIds()); if(thePattern != null) {
cachedSet = locations.parallelStream() ArrayList<ResourceLocation> locations = new ArrayList<>(registryIn.getIds());
.filter(rLoc -> thePattern.matcher(rLoc.toString()).find()) cachedSet = locations.parallelStream()
.collect(Collectors.toSet()); .filter(rLoc -> thePattern.matcher(rLoc.toString()).find())
.collect(Collectors.toSet());
} else {
ModernFix.LOGGER.error("Empty pattern for '{}' somehow... ignoring...", currentPatternStr);
cachedSet = new HashSet<>();
}
KubeUtil.matchedIdsForRegex.put(currentPatternStr, cachedSet); KubeUtil.matchedIdsForRegex.put(currentPatternStr, cachedSet);
} }
return cachedSet; return cachedSet;