Skip to content

Commit

Permalink
reworked HideHelper and uncovered recipes dumper
Browse files Browse the repository at this point in the history
  • Loading branch information
rlnt committed Mar 14, 2024
1 parent 09873d3 commit ba959f4
Show file tree
Hide file tree
Showing 9 changed files with 136 additions and 182 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
import com.almostreliable.unified.recipe.RecipeDumper;
import com.almostreliable.unified.recipe.RecipeTransformer;
import com.almostreliable.unified.recipe.unifier.RecipeHandlerFactory;
import com.almostreliable.unified.utils.MissingRecipesDumper;
import com.almostreliable.unified.utils.ReplacementMap;
import com.almostreliable.unified.utils.TagMap;
import com.almostreliable.unified.utils.UncoveredRecipesDumper;
import com.google.gson.JsonElement;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.Item;
Expand Down Expand Up @@ -56,7 +56,10 @@ public void run(Map<ResourceLocation, JsonElement> recipes, boolean skipClientTr
dumper.dump(debugConfig.dumpOverview, debugConfig.dumpUnification, debugConfig.dumpDuplicates);

debugConfig.logRecipes(recipes, "recipes_after_unification.txt");
MissingRecipesDumper.write(this, debugConfig.dumpPotentialMissingRecipes, recipes);

if (debugConfig.dumpUncoveredRecipes) {
UncoveredRecipesDumper.write(recipes);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void register(EmiRegistry registry) {
.orElse(false);
if (emiDisabled) return;

for (ItemStack item : HideHelper.createHidingList(AlmostUnified.getRuntime())) {
for (ItemStack item : HideHelper.getStacksToHide()) {
registry.removeEmiStacks(EmiStack.of(item));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void onRuntimeAvailable(IJeiRuntime jei) {
.orElse(false);
if (jeiDisabled) return;

Collection<ItemStack> items = HideHelper.createHidingList(AlmostUnified.getRuntime());
Collection<ItemStack> items = HideHelper.getStacksToHide();
if (!items.isEmpty()) {
jei.getIngredientManager().removeIngredientsAtRuntime(VanillaTypes.ITEM_STACK, items);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public void registerBasicEntryFiltering(BasicFilteringRule<?> rule) {
.orElse(false);
if (reiDisabled) return List.of();

return EntryIngredients.ofItemStacks(HideHelper.createHidingList(AlmostUnified.getRuntime()));
return EntryIngredients.ofItemStacks(HideHelper.getStacksToHide());
});
}

Expand Down
132 changes: 60 additions & 72 deletions Common/src/main/java/com/almostreliable/unified/compat/HideHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,122 +4,110 @@
import com.almostreliable.unified.AlmostUnifiedRuntime;
import com.almostreliable.unified.utils.ReplacementMap;
import com.almostreliable.unified.utils.TagOwnerships;
import com.almostreliable.unified.utils.UnifyTag;
import com.almostreliable.unified.utils.Utils;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.core.registries.Registries;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import org.jetbrains.annotations.Nullable;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

public final class HideHelper {

private HideHelper() {}

public static Collection<ItemStack> createHidingList(AlmostUnifiedRuntime runtime) {
public static Multimap<UnifyTag<Item>, ResourceLocation> createHidingMap() {
AlmostUnifiedRuntime runtime = AlmostUnified.getRuntime();
ReplacementMap repMap = runtime.getReplacementMap().orElse(null);
var tagMap = runtime.getFilteredTagMap().orElse(null);

if (repMap == null || tagMap == null) return new ArrayList<>();

Set<ResourceLocation> hidingList = new HashSet<>();
Multimap<UnifyTag<Item>, ResourceLocation> hidingMap = HashMultimap.create();
if (repMap == null || tagMap == null) return hidingMap;
TagOwnerships ownerships = repMap.getTagOwnerships();

for (var unifyTag : tagMap.getTags()) {
var itemsByTag = tagMap.getEntriesByTag(unifyTag);

// avoid handling single entries and tags that only contain the same namespace for all items
if (Utils.allSameNamespace(itemsByTag)) continue;

Set<ResourceLocation> replacements = new HashSet<>();
for (ResourceLocation item : itemsByTag) {
replacements.add(getReplacementForItem(repMap, item));
}
ResourceLocation preferredItem = repMap.getPreferredItemForTag(unifyTag, $ -> true);
if (preferredItem == null) continue;

Set<ResourceLocation> toHide = new HashSet<>();
for (ResourceLocation item : itemsByTag) {
if (!replacements.contains(item)) {
toHide.add(item);
}
}
Set<ResourceLocation> itemsToHide = getItemsToHide(unifyTag, itemsByTag, preferredItem);
if (itemsToHide == null) continue;
hidingMap.putAll(unifyTag, itemsToHide);

if (toHide.isEmpty()) continue;
Set<ResourceLocation> refItemsToHide = getRefItemsToHide(unifyTag, ownerships, preferredItem);
hidingMap.putAll(unifyTag, refItemsToHide);
}

AlmostUnified.LOG.info(
"[AutoHiding] Hiding {}/{} items for tag '#{}' -> {}",
toHide.size(),
itemsByTag.size(),
unifyTag.location(),
toHide
);
return hidingMap;
}

hidingList.addAll(toHide);
@Nullable
private static Set<ResourceLocation> getItemsToHide(UnifyTag<Item> unifyTag, Set<ResourceLocation> itemsByTag, ResourceLocation preferredItem) {
Set<ResourceLocation> itemsToHide = new HashSet<>();
for (ResourceLocation item : itemsByTag) {
if (!item.equals(preferredItem)) {
itemsToHide.add(item);
}
}

hidingList.addAll(getRefItems(repMap));
if (itemsToHide.isEmpty()) return null;

return hidingList
.stream()
.flatMap(rl -> BuiltInRegistries.ITEM.getOptional(rl).stream())
.map(ItemStack::new)
.collect(Collectors.toList());
}

/**
* Returns the replacement for the given item, or the item itself if no replacement is found.
* <p>
* Returning the item itself is important for stone strata detection.
*
* @param repMap The replacement map.
* @param item The item to get the replacement for.
* @return The replacement for the given item, or the item itself if no replacement is found.
*/
private static ResourceLocation getReplacementForItem(ReplacementMap repMap, ResourceLocation item) {
var replacement = repMap.getReplacementForItem(item);
if (replacement == null) return item;
return replacement;
AlmostUnified.LOG.info(
"[AutoHiding] Hiding {}/{} items for tag '#{}' -> {}",
itemsToHide.size(),
itemsByTag.size(),
unifyTag.location(),
itemsToHide
);
return itemsToHide;
}

/**
* Returns a set of all items that are contained in the reference tags.
*
* @return A set of all items that are contained in the reference tags.
*/
private static Set<ResourceLocation> getRefItems(ReplacementMap repMap) {
Set<ResourceLocation> hidingList = new HashSet<>();
TagOwnerships ownerships = repMap.getTagOwnerships();

ownerships.getRefs().forEach(ref -> {
var owner = ownerships.getOwnerByTag(ref);
assert owner != null;
private static Set<ResourceLocation> getRefItemsToHide(UnifyTag<Item> unifyTag, TagOwnerships ownerships, ResourceLocation preferredItem) {
var refTags = ownerships.getRefsByOwner(unifyTag);
Set<ResourceLocation> refItemsToHide = new HashSet<>();

var dominantItem = repMap.getPreferredItemForTag(owner, $ -> true);
for (var refTag : refTags) {
var asTagKey = TagKey.create(Registries.ITEM, refTag.location());

TagKey<Item> asTagKey = TagKey.create(Registries.ITEM, ref.location());
Set<ResourceLocation> refItems = new HashSet<>();
BuiltInRegistries.ITEM.getTagOrEmpty(asTagKey).forEach(holder -> {
ResourceLocation item = BuiltInRegistries.ITEM.getKey(holder.value());
if (item.equals(dominantItem)) return; // don't hide if the item is a dominant one
refItems.add(item);
if (item.equals(preferredItem)) return;
refItemsToHide.add(item);
});

if (refItems.isEmpty()) return;
if (refItemsToHide.isEmpty()) continue;

AlmostUnified.LOG.info(
"[AutoHiding] Hiding reference tag '#{}' of owner tag '#{}' -> {}",
ref.location(),
owner.location(),
refItems
refTag.location(),
unifyTag.location(),
refItemsToHide
);
}

return refItemsToHide;
}

hidingList.addAll(refItems);
});
public static Collection<ItemStack> getStacksToHide() {
var hidingMap = createHidingMap();
if (hidingMap.isEmpty()) return List.of();

return hidingList;
return hidingMap
.entries()
.stream()
.flatMap(rl -> BuiltInRegistries.ITEM.getOptional(rl.getValue()).stream())
.map(ItemStack::new)
.toList();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ public class DebugConfig extends Config {
public final boolean dumpUnification;
public final boolean dumpOverview;
public final boolean dumpRecipes;
public final boolean dumpPotentialMissingRecipes;
public final boolean dumpUncoveredRecipes;

public DebugConfig(boolean dumpTagMap, boolean dumpDuplicates, boolean dumpUnification, boolean dumpOverview, boolean dumpRecipes, boolean dumpPotentialMissingRecipes) {
public DebugConfig(boolean dumpTagMap, boolean dumpDuplicates, boolean dumpUnification, boolean dumpOverview, boolean dumpRecipes, boolean dumpUncoveredRecipes) {
this.dumpTagMap = dumpTagMap;
this.dumpDuplicates = dumpDuplicates;
this.dumpUnification = dumpUnification;
this.dumpOverview = dumpOverview;
this.dumpRecipes = dumpRecipes;
this.dumpPotentialMissingRecipes = dumpPotentialMissingRecipes;
this.dumpUncoveredRecipes = dumpUncoveredRecipes;
}

public void logUnifyTagDump(TagMap<Item> tagMap) {
Expand Down Expand Up @@ -73,7 +73,7 @@ public static class Serializer extends Config.Serializer<DebugConfig> {
public static final String DUMP_UNIFICATION = "dumpUnification";
public static final String DUMP_OVERVIEW = "dumpOverview";
public static final String DUMP_RECIPES = "dumpRecipes";
public static final String DUMP_POTENTIAL_MISSING_RECIPES = "dumpPotentialMissingRecipes";
public static final String DUMP_UNCOVERED_RECIPES = "dumpUncoveredRecipes";

@Override
public DebugConfig deserialize(JsonObject json) {
Expand All @@ -83,7 +83,7 @@ public DebugConfig deserialize(JsonObject json) {
safeGet(() -> json.get(DUMP_UNIFICATION).getAsBoolean(), false),
safeGet(() -> json.get(DUMP_OVERVIEW).getAsBoolean(), false),
safeGet(() -> json.get(DUMP_RECIPES).getAsBoolean(), false),
safeGet(() -> json.get(DUMP_POTENTIAL_MISSING_RECIPES).getAsBoolean(), false)
safeGet(() -> json.get(DUMP_UNCOVERED_RECIPES).getAsBoolean(), false)
);
}

Expand All @@ -95,7 +95,7 @@ public JsonObject serialize(DebugConfig src) {
json.addProperty(DUMP_UNIFICATION, src.dumpUnification);
json.addProperty(DUMP_OVERVIEW, src.dumpOverview);
json.addProperty(DUMP_RECIPES, src.dumpRecipes);
json.addProperty(DUMP_POTENTIAL_MISSING_RECIPES, src.dumpPotentialMissingRecipes);
json.addProperty(DUMP_UNCOVERED_RECIPES, src.dumpUncoveredRecipes);
return json;
}
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,12 @@ public UnifyTag<Item> getOwnerByTag(UnifyTag<Item> tag) {
}

/**
* Gets all reference tags for all owner tags.
* Gets all reference tags for the provided owner tag.
*
* @return A set of all reference tags.
* @param tag The owner tag to get the references for.
* @return A collection of all reference tags.
*/
public Set<UnifyTag<Item>> getRefs() {
return refsToOwner.keySet();
public Collection<UnifyTag<Item>> getRefsByOwner(UnifyTag<Item> tag) {
return ownerToRefs.get(tag);
}
}
Loading

0 comments on commit ba959f4

Please sign in to comment.