Fix accessing runtime when not initialized (#32)

Co-authored-by: Relentless <relentless@rlnt.dev>
This commit is contained in:
Relentless 2023-02-08 01:36:49 +01:00
parent 8db0f1d005
commit 59b2d84998
No known key found for this signature in database
GPG key ID: 50C5FD225130D790
9 changed files with 248 additions and 116 deletions

View file

@ -29,7 +29,7 @@ public final class AlmostUnified {
public static AlmostUnifiedRuntime getRuntime() {
if (RUNTIME == null) {
throw new IllegalStateException("AlmostUnifiedRuntime not initialized");
return AlmostUnifiedFallbackRuntime.getInstance();
}
return RUNTIME;
}
@ -39,7 +39,7 @@ public final class AlmostUnified {
throw new IllegalStateException("Internal error. TagManager was not updated correctly");
}
RUNTIME = AlmostUnifiedRuntime.create(TAG_MANGER);
RUNTIME = AlmostUnifiedRuntimeImpl.create(TAG_MANGER);
}
public static void updateTagManager(TagManager tm) {

View file

@ -0,0 +1,81 @@
package com.almostreliable.unified;
import com.almostreliable.unified.api.StoneStrataHandler;
import com.almostreliable.unified.config.Config;
import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.utils.ReplacementMap;
import com.almostreliable.unified.utils.TagMap;
import com.almostreliable.unified.utils.UnifyTag;
import com.google.gson.JsonElement;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
import javax.annotation.Nullable;
import java.util.*;
// TODO: Implement sync so it's not just a fallback
public class AlmostUnifiedFallbackRuntime implements AlmostUnifiedRuntime {
@Nullable private static AlmostUnifiedFallbackRuntime INSTANCE;
@Nullable private UnifyConfig config;
@Nullable private ReplacementMap replacementMap;
@Nullable private TagMap filteredTagMap;
public static AlmostUnifiedFallbackRuntime getInstance() {
if (INSTANCE == null) {
INSTANCE = new AlmostUnifiedFallbackRuntime();
INSTANCE.reload();
}
return INSTANCE;
}
public void reload() {
config = null;
replacementMap = null;
filteredTagMap = null;
build();
}
private UnifyConfig getConfig() {
if (config == null) {
config = Config.load(UnifyConfig.NAME, new UnifyConfig.Serializer());
}
return config;
}
public void build() {
var config = getConfig();
List<UnifyTag<Item>> unifyTags = config.bakeTags();
filteredTagMap = TagMap.create(unifyTags).filtered($ -> true, config::includeItem);
StoneStrataHandler stoneStrataHandler = getStoneStrataHandler(config);
replacementMap = new ReplacementMap(Objects.requireNonNull(filteredTagMap), stoneStrataHandler, config);
}
private static StoneStrataHandler getStoneStrataHandler(UnifyConfig config) {
Set<UnifyTag<Item>> stoneStrataTags = AlmostUnifiedPlatform.INSTANCE.getStoneStrataTags(config.getStoneStrata());
TagMap stoneStrataTagMap = TagMap.create(stoneStrataTags);
return StoneStrataHandler.create(config.getStoneStrata(), stoneStrataTags, stoneStrataTagMap);
}
@Override
public void run(Map<ResourceLocation, JsonElement> recipes, boolean skipClientTracking) {
// no-op
}
@Override
public Optional<TagMap> getFilteredTagMap() {
return Optional.ofNullable(filteredTagMap);
}
@Override
public Optional<ReplacementMap> getReplacementMap() {
return Optional.ofNullable(replacementMap);
}
@Override
public Optional<UnifyConfig> getUnifyConfig() {
return Optional.ofNullable(config);
}
}

View file

@ -26,30 +26,36 @@ public class AlmostUnifiedLookupImpl implements AlmostUnifiedLookup {
@Override
public Item getReplacementForItem(ItemLike itemLike) {
ResourceLocation id = BuiltInRegistries.ITEM.getKey(itemLike.asItem());
ResourceLocation result = AlmostUnified.getRuntime().getReplacementMap().getReplacementForItem(id);
return BuiltInRegistries.ITEM.getOptional(result).orElse(null);
return AlmostUnified
.getRuntime()
.getReplacementMap()
.map(rm -> rm.getReplacementForItem(id))
.flatMap(BuiltInRegistries.ITEM::getOptional)
.orElse(null);
}
@Nullable
@Override
public Item getPreferredItemForTag(TagKey<Item> tag) {
UnifyTag<Item> asUnifyTag = UnifyTag.item(tag.location());
ResourceLocation result = AlmostUnified
return AlmostUnified
.getRuntime()
.getReplacementMap()
.getPreferredItemForTag(asUnifyTag, $ -> true);
return BuiltInRegistries.ITEM.getOptional(result).orElse(null);
.map(rm -> rm.getPreferredItemForTag(asUnifyTag, $ -> true))
.flatMap(BuiltInRegistries.ITEM::getOptional)
.orElse(null);
}
@Nullable
@Override
public TagKey<Item> getPreferredTagForItem(ItemLike itemLike) {
ResourceLocation id = BuiltInRegistries.ITEM.getKey(itemLike.asItem());
UnifyTag<Item> unifyTag = AlmostUnified.getRuntime().getReplacementMap().getPreferredTagForItem(id);
if (unifyTag == null) {
return null;
}
return TagKey.create(Registries.ITEM, unifyTag.location());
return AlmostUnified
.getRuntime()
.getReplacementMap()
.map(rm -> rm.getPreferredTagForItem(id))
.map(ut -> TagKey.create(Registries.ITEM, ut.location()))
.orElse(null);
}
@Override
@ -58,10 +64,12 @@ public class AlmostUnifiedLookupImpl implements AlmostUnifiedLookup {
return AlmostUnified
.getRuntime()
.getFilteredTagMap()
.getItems(asUnifyTag)
.stream()
.flatMap(rl -> BuiltInRegistries.ITEM.getOptional(rl).stream())
.collect(Collectors.toSet());
.map(tagMap -> tagMap
.getItems(asUnifyTag)
.stream()
.flatMap(rl -> BuiltInRegistries.ITEM.getOptional(rl).stream())
.collect(Collectors.toSet()))
.orElseGet(Set::of);
}
@Override
@ -69,9 +77,11 @@ public class AlmostUnifiedLookupImpl implements AlmostUnifiedLookup {
return AlmostUnified
.getRuntime()
.getFilteredTagMap()
.getTags()
.stream()
.map(ut -> TagKey.create(Registries.ITEM, ut.location()))
.collect(Collectors.toSet());
.map(tagMap -> tagMap
.getTags()
.stream()
.map(ut -> TagKey.create(Registries.ITEM, ut.location()))
.collect(Collectors.toSet()))
.orElseGet(Set::of);
}
}

View file

@ -1,97 +1,21 @@
package com.almostreliable.unified;
import com.almostreliable.unified.api.StoneStrataHandler;
import com.almostreliable.unified.config.Config;
import com.almostreliable.unified.config.DebugConfig;
import com.almostreliable.unified.config.DuplicationConfig;
import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.recipe.RecipeDumper;
import com.almostreliable.unified.recipe.RecipeTransformer;
import com.almostreliable.unified.recipe.unifier.RecipeHandlerFactory;
import com.almostreliable.unified.utils.FileUtils;
import com.almostreliable.unified.utils.ReplacementMap;
import com.almostreliable.unified.utils.TagMap;
import com.almostreliable.unified.utils.UnifyTag;
import com.google.gson.JsonElement;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagManager;
import net.minecraft.world.item.Item;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
public final class AlmostUnifiedRuntime {
private final RecipeHandlerFactory recipeHandlerFactory;
private final TagMap filteredTagMap;
private final DuplicationConfig dupConfig;
private final UnifyConfig unifyConfig;
private final DebugConfig debugConfig;
private final ReplacementMap replacementMap;
public interface AlmostUnifiedRuntime {
private AlmostUnifiedRuntime(RecipeHandlerFactory recipeHandlerFactory, TagMap tagMap, DuplicationConfig dupConfig, UnifyConfig unifyConfig, DebugConfig debugConfig) {
this.recipeHandlerFactory = recipeHandlerFactory;
this.dupConfig = dupConfig;
this.unifyConfig = unifyConfig;
this.debugConfig = debugConfig;
List<UnifyTag<Item>> allowedTags = unifyConfig.bakeTags();
this.filteredTagMap = tagMap.filtered(allowedTags::contains, unifyConfig::includeItem);
StoneStrataHandler stoneStrataHandler = StoneStrataHandler.create(unifyConfig.getStoneStrata(),
AlmostUnifiedPlatform.INSTANCE.getStoneStrataTags(unifyConfig.getStoneStrata()),
tagMap);
this.replacementMap = new ReplacementMap(filteredTagMap, stoneStrataHandler, unifyConfig);
}
void run(Map<ResourceLocation, JsonElement> recipes, boolean skipClientTracking);
public static AlmostUnifiedRuntime create(TagManager tagManager) {
Objects.requireNonNull(tagManager);
Optional<TagMap> getFilteredTagMap();
createGitIgnoreIfNotExists();
DuplicationConfig dupConfig = Config.load(DuplicationConfig.NAME, new DuplicationConfig.Serializer());
UnifyConfig unifyConfig = Config.load(UnifyConfig.NAME, new UnifyConfig.Serializer());
DebugConfig debugConfig = Config.load(DebugConfig.NAME, new DebugConfig.Serializer());
Optional<ReplacementMap> getReplacementMap();
RecipeHandlerFactory factory = new RecipeHandlerFactory();
AlmostUnifiedPlatform.INSTANCE.bindRecipeHandlers(factory);
TagMap tagMap = TagMap.create(tagManager);
return new AlmostUnifiedRuntime(factory, tagMap, dupConfig, unifyConfig, debugConfig);
}
private static void createGitIgnoreIfNotExists() {
Path path = AlmostUnifiedPlatform.INSTANCE.getConfigPath();
if (!(Files.exists(path) && Files.isDirectory(path))) {
FileUtils.write(AlmostUnifiedPlatform.INSTANCE.getConfigPath(),
".gitignore",
sb -> sb.append(DebugConfig.NAME).append(".json").append("\n"));
}
}
public void run(Map<ResourceLocation, JsonElement> recipes, boolean skipClientTracking) {
debugConfig.logRecipes(recipes, "recipes_before_unification.txt");
debugConfig.logUnifyTagDump(filteredTagMap);
long startTime = System.currentTimeMillis();
RecipeTransformer.Result result = new RecipeTransformer(recipeHandlerFactory,
replacementMap,
unifyConfig,
dupConfig).transformRecipes(recipes, skipClientTracking);
RecipeDumper dumper = new RecipeDumper(result, startTime, System.currentTimeMillis());
dumper.dump(debugConfig.dumpOverview, debugConfig.dumpUnification, debugConfig.dumpDuplicates);
debugConfig.logRecipes(recipes, "recipes_after_unification.txt");
}
public TagMap getFilteredTagMap() {
return filteredTagMap;
}
public ReplacementMap getReplacementMap() {
return replacementMap;
}
public UnifyConfig getUnifyConfig() {
return unifyConfig;
}
Optional<UnifyConfig> getUnifyConfig();
}

View file

@ -0,0 +1,102 @@
package com.almostreliable.unified;
import com.almostreliable.unified.api.StoneStrataHandler;
import com.almostreliable.unified.config.Config;
import com.almostreliable.unified.config.DebugConfig;
import com.almostreliable.unified.config.DuplicationConfig;
import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.recipe.RecipeDumper;
import com.almostreliable.unified.recipe.RecipeTransformer;
import com.almostreliable.unified.recipe.unifier.RecipeHandlerFactory;
import com.almostreliable.unified.utils.FileUtils;
import com.almostreliable.unified.utils.ReplacementMap;
import com.almostreliable.unified.utils.TagMap;
import com.almostreliable.unified.utils.UnifyTag;
import com.google.gson.JsonElement;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagManager;
import net.minecraft.world.item.Item;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
public final class AlmostUnifiedRuntimeImpl implements AlmostUnifiedRuntime {
private final RecipeHandlerFactory recipeHandlerFactory;
private final TagMap filteredTagMap;
private final DuplicationConfig dupConfig;
private final UnifyConfig unifyConfig;
private final DebugConfig debugConfig;
private final ReplacementMap replacementMap;
private AlmostUnifiedRuntimeImpl(RecipeHandlerFactory recipeHandlerFactory, TagMap tagMap, DuplicationConfig dupConfig, UnifyConfig unifyConfig, DebugConfig debugConfig) {
this.recipeHandlerFactory = recipeHandlerFactory;
this.dupConfig = dupConfig;
this.unifyConfig = unifyConfig;
this.debugConfig = debugConfig;
List<UnifyTag<Item>> allowedTags = unifyConfig.bakeTags();
this.filteredTagMap = tagMap.filtered(allowedTags::contains, unifyConfig::includeItem);
StoneStrataHandler stoneStrataHandler = StoneStrataHandler.create(unifyConfig.getStoneStrata(),
AlmostUnifiedPlatform.INSTANCE.getStoneStrataTags(unifyConfig.getStoneStrata()),
tagMap);
this.replacementMap = new ReplacementMap(filteredTagMap, stoneStrataHandler, unifyConfig);
}
public static AlmostUnifiedRuntimeImpl create(TagManager tagManager) {
Objects.requireNonNull(tagManager);
createGitIgnoreIfNotExists();
DuplicationConfig dupConfig = Config.load(DuplicationConfig.NAME, new DuplicationConfig.Serializer());
UnifyConfig unifyConfig = Config.load(UnifyConfig.NAME, new UnifyConfig.Serializer());
DebugConfig debugConfig = Config.load(DebugConfig.NAME, new DebugConfig.Serializer());
RecipeHandlerFactory factory = new RecipeHandlerFactory();
AlmostUnifiedPlatform.INSTANCE.bindRecipeHandlers(factory);
TagMap tagMap = TagMap.create(tagManager);
return new AlmostUnifiedRuntimeImpl(factory, tagMap, dupConfig, unifyConfig, debugConfig);
}
private static void createGitIgnoreIfNotExists() {
Path path = AlmostUnifiedPlatform.INSTANCE.getConfigPath();
if (!(Files.exists(path) && Files.isDirectory(path))) {
FileUtils.write(AlmostUnifiedPlatform.INSTANCE.getConfigPath(),
".gitignore",
sb -> sb.append(DebugConfig.NAME).append(".json").append("\n"));
}
}
@Override
public void run(Map<ResourceLocation, JsonElement> recipes, boolean skipClientTracking) {
debugConfig.logRecipes(recipes, "recipes_before_unification.txt");
debugConfig.logUnifyTagDump(filteredTagMap);
long startTime = System.currentTimeMillis();
RecipeTransformer.Result result = new RecipeTransformer(recipeHandlerFactory,
replacementMap,
unifyConfig,
dupConfig).transformRecipes(recipes, skipClientTracking);
RecipeDumper dumper = new RecipeDumper(result, startTime, System.currentTimeMillis());
dumper.dump(debugConfig.dumpOverview, debugConfig.dumpUnification, debugConfig.dumpDuplicates);
debugConfig.logRecipes(recipes, "recipes_after_unification.txt");
}
@Override
public Optional<TagMap> getFilteredTagMap() {
return Optional.of(filteredTagMap);
}
@Override
public Optional<ReplacementMap> getReplacementMap() {
return Optional.of(replacementMap);
}
@Override
public Optional<UnifyConfig> getUnifyConfig() {
return Optional.of(unifyConfig);
}
}

View file

@ -1,7 +1,8 @@
package com.almostreliable.unified.compat;
import com.almostreliable.unified.AlmostUnified;
import com.almostreliable.unified.AlmostUnifiedFallbackRuntime;
import com.almostreliable.unified.api.ModConstants;
import com.almostreliable.unified.config.Config;
import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.recipe.CRTLookup;
import com.almostreliable.unified.utils.Utils;
@ -29,10 +30,15 @@ public class AlmostJEI implements IModPlugin {
@Override
public void onRuntimeAvailable(IJeiRuntime jei) {
UnifyConfig config = Config.load(UnifyConfig.NAME, new UnifyConfig.Serializer());
if (config.reiOrJeiDisabled()) return;
AlmostUnifiedFallbackRuntime.getInstance().reload();
Collection<ItemStack> items = HideHelper.createHidingList(config);
Boolean jeiDisabled = AlmostUnified.getRuntime()
.getUnifyConfig()
.map(UnifyConfig::reiOrJeiDisabled)
.orElse(false);
if (jeiDisabled) return;
Collection<ItemStack> items = HideHelper.createHidingList(AlmostUnified.getRuntime());
if (!items.isEmpty()) {
jei.getIngredientManager().removeIngredientsAtRuntime(VanillaTypes.ITEM_STACK, items);
}

View file

@ -1,8 +1,9 @@
package com.almostreliable.unified.compat;
import com.almostreliable.unified.AlmostUnified;
import com.almostreliable.unified.AlmostUnifiedFallbackRuntime;
import com.almostreliable.unified.ClientTagUpdateEvent;
import com.almostreliable.unified.api.ModConstants;
import com.almostreliable.unified.config.Config;
import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.recipe.CRTLookup;
import com.almostreliable.unified.recipe.ClientRecipeTracker.ClientRecipeLink;
@ -46,9 +47,16 @@ public class AlmostREI implements REIClientPlugin {
@Override
public void registerBasicEntryFiltering(BasicFilteringRule<?> rule) {
filterUpdate = rule.hide(() -> {
UnifyConfig config = Config.load(UnifyConfig.NAME, new UnifyConfig.Serializer());
if (config.reiOrJeiDisabled()) return List.of();
return EntryIngredients.ofItemStacks(HideHelper.createHidingList(config));
AlmostUnifiedFallbackRuntime.getInstance().reload();
var reiDisabled = AlmostUnified
.getRuntime()
.getUnifyConfig()
.map(UnifyConfig::reiOrJeiDisabled)
.orElse(false);
if (reiDisabled) return List.of();
return EntryIngredients.ofItemStacks(HideHelper.createHidingList(AlmostUnified.getRuntime()));
});
}

View file

@ -2,6 +2,7 @@ package com.almostreliable.unified.compat;
import com.almostreliable.unified.AlmostUnified;
import com.almostreliable.unified.AlmostUnifiedPlatform;
import com.almostreliable.unified.AlmostUnifiedRuntime;
import com.almostreliable.unified.api.StoneStrataHandler;
import com.almostreliable.unified.config.UnifyConfig;
import com.almostreliable.unified.utils.ReplacementMap;
@ -17,11 +18,11 @@ import java.util.stream.Collectors;
public class HideHelper {
public static Collection<ItemStack> createHidingList(UnifyConfig config) {
List<UnifyTag<Item>> unifyTags = config.bakeTags();
TagMap filteredTagMap = TagMap.create(unifyTags).filtered($ -> true, config::includeItem);
StoneStrataHandler stoneStrataHandler = getStoneStrataHandler(config);
ReplacementMap repMap = new ReplacementMap(filteredTagMap, stoneStrataHandler, config);
public static Collection<ItemStack> createHidingList(AlmostUnifiedRuntime runtime) {
ReplacementMap repMap = runtime.getReplacementMap().orElse(null);
TagMap filteredTagMap = runtime.getFilteredTagMap().orElse(null);
if (repMap == null || filteredTagMap == null) return new ArrayList<>();
return filteredTagMap.getTags().stream().map(unifyTag -> {
Collection<ResourceLocation> itemsByTag = filteredTagMap.getItems(unifyTag);

View file

@ -3,7 +3,7 @@ group = com.almostreliable.mods
license = GNU Lesser General Public License v3.0
enabledPlatforms = fabric,forge
enableAccessWidener = false
sharedRunDir = true
sharedRunDir = false
extraModsDirectory = extra-mods
junitVersion = 5.9.0