Add support for ad astra (#24)

This commit is contained in:
LLytho 2022-11-27 18:30:24 +01:00 committed by GitHub
parent 7ec34987bb
commit 9478dacbef
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 37 additions and 6 deletions

View file

@ -4,6 +4,7 @@ package com.almostreliable.unified.api;
public final class ModConstants {
public static final String IE = "immersiveengineering";
public static final String AMETHYST_IMBUEMENT = "amethyst_imbuement";
public static final String AD_ASTRA = "ad_astra";
private ModConstants() {}
}

View file

@ -24,6 +24,9 @@ public interface RecipeContext {
@Nullable
JsonElement createResultReplacement(@Nullable JsonElement element);
@Nullable
JsonElement createResultReplacement(@Nullable JsonElement element, boolean includeTagCheck, String... lookupKeys);
ResourceLocation getType();
boolean hasProperty(String property);

View file

@ -0,0 +1,14 @@
package com.almostreliable.unified.compat;
import com.almostreliable.unified.api.recipe.RecipeConstants;
import com.almostreliable.unified.api.recipe.RecipeUnifier;
import com.almostreliable.unified.api.recipe.RecipeUnifierBuilder;
public class AdAstraRecipeUnifier implements RecipeUnifier {
@Override
public void collectUnifier(RecipeUnifierBuilder builder) {
builder.put("output", (json, ctx) -> {
return ctx.createResultReplacement(json, false, RecipeConstants.ITEM, "id");
});
}
}

View file

@ -94,16 +94,22 @@ public class RecipeContextImpl implements RecipeContext {
@Override
@Nullable
public JsonElement createResultReplacement(@Nullable JsonElement element) {
return createResultReplacement(element, true, RecipeConstants.ITEM);
}
@Override
@Nullable
public JsonElement createResultReplacement(@Nullable JsonElement element, boolean includeTagCheck, String... lookupKeys) {
if (element == null) {
return null;
}
JsonElement copy = element.deepCopy();
JsonElement result = tryCreateResultReplacement(copy);
JsonElement result = tryCreateResultReplacement(copy, includeTagCheck, lookupKeys);
return element.equals(result) ? null : result;
}
@Nullable
private JsonElement tryCreateResultReplacement(JsonElement element) {
public JsonElement tryCreateResultReplacement(JsonElement element, boolean lookupForTag, String... keys) {
if (element instanceof JsonPrimitive primitive) {
ResourceLocation item = ResourceLocation.tryParse(primitive.getAsString());
ResourceLocation replacement = getReplacementForItem(item);
@ -113,17 +119,20 @@ public class RecipeContextImpl implements RecipeContext {
return null;
}
if (element instanceof JsonArray array && JsonUtils.replaceOn(array, this::tryCreateResultReplacement)) {
if (element instanceof JsonArray array &&
JsonUtils.replaceOn(array, j -> tryCreateResultReplacement(j, lookupForTag, keys))) {
return element;
}
if (element instanceof JsonObject object) {
if (JsonUtils.replaceOn(object, RecipeConstants.ITEM, this::tryCreateResultReplacement)) {
return element;
for (String key : keys) {
if (JsonUtils.replaceOn(object, key, j -> tryCreateResultReplacement(j, lookupForTag, keys))) {
return element;
}
}
// Some mods have tags for results instead of items. We replace those with the preferred item.
if (object.get(RecipeConstants.TAG) instanceof JsonPrimitive primitive) {
if (lookupForTag && object.get(RecipeConstants.TAG) instanceof JsonPrimitive primitive) {
ResourceLocation item = getPreferredItemForTag(Utils.toItemTag(primitive.getAsString()), $ -> true);
if (item != null) {
object.remove(RecipeConstants.TAG);

View file

@ -1,6 +1,7 @@
package com.almostreliable.unified;
import com.almostreliable.unified.api.ModConstants;
import com.almostreliable.unified.compat.AdAstraRecipeUnifier;
import com.almostreliable.unified.compat.AmethystImbuementRecipeUnifier;
import com.almostreliable.unified.recipe.unifier.RecipeHandlerFactory;
import com.almostreliable.unified.utils.UnifyTag;
@ -48,6 +49,7 @@ public class AlmostUnifiedPlatformFabric implements AlmostUnifiedPlatform {
@Override
public void bindRecipeHandlers(RecipeHandlerFactory factory) {
factory.registerForMod(ModConstants.AD_ASTRA, new AdAstraRecipeUnifier());
factory.registerForMod(ModConstants.AMETHYST_IMBUEMENT, new AmethystImbuementRecipeUnifier());
}

View file

@ -1,6 +1,7 @@
package com.almostreliable.unified;
import com.almostreliable.unified.api.ModConstants;
import com.almostreliable.unified.compat.AdAstraRecipeUnifier;
import com.almostreliable.unified.compat.IERecipeUnifier;
import com.almostreliable.unified.recipe.unifier.RecipeHandlerFactory;
import com.almostreliable.unified.utils.UnifyTag;
@ -57,6 +58,7 @@ public class AlmostUnifiedPlatformForge implements AlmostUnifiedPlatform {
@Override
public void bindRecipeHandlers(RecipeHandlerFactory factory) {
factory.registerForMod(ModConstants.AD_ASTRA, new AdAstraRecipeUnifier());
factory.registerForMod(ModConstants.IE, new IERecipeUnifier());
}