Clear code

This commit is contained in:
LLytho 2022-07-22 19:33:37 +02:00
parent fbbe715447
commit d276f37873
19 changed files with 102 additions and 103 deletions

View file

@ -1,5 +0,0 @@
package com.almostreliable.unified.api.recipe;
public interface RecipeHandler {
void collectTransformations(RecipeTransformationBuilder builder);
}

View file

@ -0,0 +1,5 @@
package com.almostreliable.unified.api.recipe;
public interface RecipeUnifier {
void collectUnifier(RecipeUnifierBuilder builder);
}

View file

@ -5,7 +5,7 @@ import com.google.gson.JsonObject;
import java.util.function.BiFunction;
public interface RecipeTransformationBuilder {
public interface RecipeUnifierBuilder {
void forEachObject(String property, BiFunction<JsonObject, RecipeContext, JsonObject> consumer);

View file

@ -3,7 +3,6 @@ package com.almostreliable.unified.api.recipe;
import com.almostreliable.unified.utils.TagMap;
import com.almostreliable.unified.utils.UnifyTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import javax.annotation.Nullable;

View file

@ -83,7 +83,7 @@ public class RecipeDumper {
.append("\n");
stringBuilder
.append("\t\t Transformed: ")
.append(recipe.getTransformed().toString())
.append(recipe.getUnified().toString())
.append("\n\n");
}
});

View file

@ -13,7 +13,7 @@ public class RecipeLink {
private final ResourceLocation type;
private final JsonObject originalRecipe;
@Nullable private DuplicateLink duplicateLink;
@Nullable private JsonObject transformedRecipe;
@Nullable private JsonObject unifiedRecipe;
public RecipeLink(ResourceLocation id, JsonObject originalRecipe) {
this.id = id;
@ -95,21 +95,21 @@ public class RecipeLink {
}
@Nullable
public JsonObject getTransformed() {
return transformedRecipe;
public JsonObject getUnified() {
return unifiedRecipe;
}
public boolean isTransformed() {
return transformedRecipe != null;
public boolean isUnified() {
return unifiedRecipe != null;
}
public void setTransformed(JsonObject transformedRecipe) {
Objects.requireNonNull(transformedRecipe);
if (isTransformed()) {
throw new IllegalStateException("Recipe already transformed");
void setUnified(JsonObject json) {
Objects.requireNonNull(json);
if (isUnified()) {
throw new IllegalStateException("Recipe already unified");
}
this.transformedRecipe = transformedRecipe;
this.unifiedRecipe = json;
}
private List<String> getIgnoredFields() {
@ -127,8 +127,8 @@ public class RecipeLink {
@Override
public String toString() {
String duplicate = duplicateLink != null ? " (duplicate)" : "";
String transformed = transformedRecipe != null ? " (transformed)" : "";
return String.format("['%s'] %s%s%s", type, id, duplicate, transformed);
String unified = unifiedRecipe != null ? " (unified)" : "";
return String.format("['%s'] %s%s%s", type, id, duplicate, unified);
}
/**
@ -165,7 +165,7 @@ public class RecipeLink {
}
public JsonObject getActual() {
return getTransformed() != null ? getTransformed() : getOriginal();
return getUnified() != null ? getUnified() : getOriginal();
}
public static class DuplicateLink {

View file

@ -45,9 +45,9 @@ public class RecipeTransformer {
byType.forEach((type, recipeLinks) -> {
Set<RecipeLink.DuplicateLink> duplicates = new HashSet<>(recipeLinks.size());
for (RecipeLink curRecipe : recipeLinks) {
transformRecipe(curRecipe);
if (curRecipe.isTransformed()) {
recipes.put(curRecipe.getId(), curRecipe.getTransformed());
unifyRecipe(curRecipe);
if (curRecipe.isUnified()) {
recipes.put(curRecipe.getId(), curRecipe.getUnified());
if (handleDuplicate(curRecipe, recipeLinks)) {
duplicates.add(curRecipe.getDuplicateLink());
}
@ -94,22 +94,22 @@ public class RecipeTransformer {
}
/**
* Transforms a single recipe link. This method will modify the recipe link in-place.
* {@link RecipeHandlerFactory} will apply multiple transformations onto the recipe.
* Unifies a single recipe link. This method will modify the recipe link in-place.
* {@link RecipeHandlerFactory} will apply multiple unification's onto the recipe.
*
* @param recipe The recipe link to transform.
* @param recipe The recipe link to unify.
*/
public void transformRecipe(RecipeLink recipe) {
public void unifyRecipe(RecipeLink recipe) {
try {
RecipeContextImpl ctx = new RecipeContextImpl(recipe.getOriginal(), replacementMap);
RecipeTransformationBuilderImpl builder = new RecipeTransformationBuilderImpl();
factory.fillTransformations(builder, ctx);
JsonObject result = builder.transform(recipe.getOriginal(), ctx);
RecipeUnifierBuilderImpl builder = new RecipeUnifierBuilderImpl();
factory.fillUnifier(builder, ctx);
JsonObject result = builder.unify(recipe.getOriginal(), ctx);
if (result != null) {
recipe.setTransformed(result);
recipe.setUnified(result);
}
} catch (Exception e) {
AlmostUnified.LOG.warn("Error transforming recipe '{}': {}",
AlmostUnified.LOG.warn("Error unifying recipe '{}': {}",
recipe.getId(),
e.getMessage());
e.printStackTrace();
@ -126,7 +126,7 @@ public class RecipeTransformer {
}
allRecipesByType.put(link.getType(), link);
if (link.isTransformed()) {
if (link.isUnified()) {
unifiedRecipesByType.put(link.getType(), link);
}
}

View file

@ -1,7 +1,7 @@
package com.almostreliable.unified.recipe;
import com.almostreliable.unified.api.recipe.RecipeContext;
import com.almostreliable.unified.api.recipe.RecipeTransformationBuilder;
import com.almostreliable.unified.api.recipe.RecipeUnifierBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
@ -11,7 +11,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.function.BiFunction;
public class RecipeTransformationBuilderImpl implements RecipeTransformationBuilder {
public class RecipeUnifierBuilderImpl implements RecipeUnifierBuilder {
private final Map<String, Entry<?>> consumers = new HashMap<>();
@Override
@ -42,7 +42,7 @@ public class RecipeTransformationBuilderImpl implements RecipeTransformationBuil
consumers.put(property, new Entry<>(type, consumer));
}
public JsonObject transform(JsonObject json, RecipeContext context) {
public JsonObject unify(JsonObject json, RecipeContext context) {
JsonObject changedValues = new JsonObject();
for (var e : json.entrySet()) {

View file

@ -4,7 +4,6 @@ import com.almostreliable.unified.api.recipe.ReplacementFallbackStrategy;
import com.almostreliable.unified.utils.TagMap;
import com.almostreliable.unified.utils.UnifyTag;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import java.util.Collection;

View file

@ -1,13 +1,13 @@
package com.almostreliable.unified.recipe.handler;
import com.almostreliable.unified.api.recipe.RecipeConstants;
import com.almostreliable.unified.api.recipe.RecipeHandler;
import com.almostreliable.unified.api.recipe.RecipeTransformationBuilder;
import com.almostreliable.unified.api.recipe.RecipeUnifier;
import com.almostreliable.unified.api.recipe.RecipeUnifierBuilder;
import java.util.Set;
public class GenericRecipeHandler implements RecipeHandler {
public static final GenericRecipeHandler INSTANCE = new GenericRecipeHandler();
public class GenericRecipeUnifier implements RecipeUnifier {
public static final GenericRecipeUnifier INSTANCE = new GenericRecipeUnifier();
private final Set<String> inputKeys = Set.of(RecipeConstants.INPUT,
RecipeConstants.INGREDIENT,
RecipeConstants.INGREDIENTS);
@ -16,7 +16,7 @@ public class GenericRecipeHandler implements RecipeHandler {
RecipeConstants.RESULTS);
@Override
public void collectTransformations(RecipeTransformationBuilder builder) {
public void collectUnifier(RecipeUnifierBuilder builder) {
for (String inputKey : inputKeys) {
builder.put(inputKey, (json, ctx) -> ctx.createIngredientReplacement(json));
}

View file

@ -1,42 +1,42 @@
package com.almostreliable.unified.recipe.handler;
import com.almostreliable.unified.api.recipe.RecipeContext;
import com.almostreliable.unified.api.recipe.RecipeHandler;
import com.almostreliable.unified.api.recipe.RecipeTransformationBuilder;
import com.almostreliable.unified.api.recipe.RecipeUnifier;
import com.almostreliable.unified.api.recipe.RecipeUnifierBuilder;
import net.minecraft.resources.ResourceLocation;
import java.util.HashMap;
import java.util.Map;
public class RecipeHandlerFactory {
private final Map<ResourceLocation, RecipeHandler> transformersByType = new HashMap<>();
private final Map<String, RecipeHandler> transformersByModId = new HashMap<>();
private final Map<ResourceLocation, RecipeUnifier> transformersByType = new HashMap<>();
private final Map<String, RecipeUnifier> transformersByModId = new HashMap<>();
public void fillTransformations(RecipeTransformationBuilder builder, RecipeContext context) {
GenericRecipeHandler.INSTANCE.collectTransformations(builder);
public void fillUnifier(RecipeUnifierBuilder builder, RecipeContext context) {
GenericRecipeUnifier.INSTANCE.collectUnifier(builder);
if (context.hasProperty(ShapedRecipeKeyHandler.PATTERN_PROPERTY) &&
context.hasProperty(ShapedRecipeKeyHandler.KEY_PROPERTY)) {
ShapedRecipeKeyHandler.INSTANCE.collectTransformations(builder);
if (context.hasProperty(ShapedRecipeKeyUnifier.PATTERN_PROPERTY) &&
context.hasProperty(ShapedRecipeKeyUnifier.KEY_PROPERTY)) {
ShapedRecipeKeyUnifier.INSTANCE.collectUnifier(builder);
}
ResourceLocation type = context.getType();
RecipeHandler byMod = transformersByModId.get(type.getNamespace());
RecipeUnifier byMod = transformersByModId.get(type.getNamespace());
if (byMod != null) {
byMod.collectTransformations(builder);
byMod.collectUnifier(builder);
}
RecipeHandler byType = transformersByType.get(type);
RecipeUnifier byType = transformersByType.get(type);
if (byType != null) {
byType.collectTransformations(builder);
byType.collectUnifier(builder);
}
}
public void registerForType(ResourceLocation type, RecipeHandler transformer) {
public void registerForType(ResourceLocation type, RecipeUnifier transformer) {
transformersByType.put(type, transformer);
}
public void registerForMod(String mod, RecipeHandler transformer) {
public void registerForMod(String mod, RecipeUnifier transformer) {
transformersByModId.put(mod, transformer);
}
}

View file

@ -1,17 +1,17 @@
package com.almostreliable.unified.recipe.handler;
import com.almostreliable.unified.api.recipe.RecipeHandler;
import com.almostreliable.unified.api.recipe.RecipeTransformationBuilder;
import com.almostreliable.unified.api.recipe.RecipeUnifier;
import com.almostreliable.unified.api.recipe.RecipeUnifierBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
public class ShapedRecipeKeyHandler implements RecipeHandler {
public static final RecipeHandler INSTANCE = new ShapedRecipeKeyHandler();
public class ShapedRecipeKeyUnifier implements RecipeUnifier {
public static final RecipeUnifier INSTANCE = new ShapedRecipeKeyUnifier();
public static final String PATTERN_PROPERTY = "pattern";
public static final String KEY_PROPERTY = "key";
@Override
public void collectTransformations(RecipeTransformationBuilder builder) {
public void collectUnifier(RecipeUnifierBuilder builder) {
builder.put(KEY_PROPERTY, JsonObject.class, (json, context) -> {
for (var entry : json.entrySet()) {
JsonElement result = context.createIngredientReplacement(entry.getValue());

View file

@ -9,6 +9,10 @@ public class JsonQuery {
private final JsonElement element;
JsonQuery(JsonElement element) {
this.element = element;
}
public static JsonQuery of(JsonElement element) {
return new JsonQuery(element);
}
@ -17,7 +21,7 @@ public class JsonQuery {
String[] parts = path.split("/");
JsonQuery current = of(element);
for (String part : parts) {
if(StringUtils.isNumeric(part)) {
if (StringUtils.isNumeric(part)) {
current = current.get(Integer.parseInt(part));
} else {
current = current.get(part);
@ -26,17 +30,13 @@ public class JsonQuery {
return current;
}
JsonQuery(JsonElement element) {
this.element = element;
}
public JsonQuery get(String identifier) {
if(!element.isJsonObject()) {
if (!element.isJsonObject()) {
throw new IllegalArgumentException("Expected JsonObject, got " + element.getClass());
}
JsonElement child = element.getAsJsonObject().get(identifier);
if(child == null) {
if (child == null) {
return null;
}
@ -44,12 +44,12 @@ public class JsonQuery {
}
public JsonQuery get(int index) {
if(!element.isJsonArray()) {
if (!element.isJsonArray()) {
throw new IllegalArgumentException("Expected JsonArray, got " + element.getClass());
}
JsonElement child = element.getAsJsonArray().get(index);
if(child == null) {
if (child == null) {
return null;
}

View file

@ -1,9 +1,7 @@
package com.almostreliable.unified.utils;
import com.almostreliable.unified.BuildConfig;
import net.minecraft.core.Registry;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.tags.TagKey;
import net.minecraft.world.item.Item;
import javax.annotation.Nullable;

View file

@ -15,7 +15,8 @@ public class FakeResourceKeyRegistry {
c.setAccessible(true);
return (ResourceKey<Registry<T>>) c.newInstance(new ResourceLocation("test_registry"),
new ResourceLocation(name));
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException | IllegalAccessException e) {
} catch (NoSuchMethodException | InvocationTargetException | InstantiationException |
IllegalAccessException e) {
e.printStackTrace();
return null;
}

View file

@ -8,7 +8,8 @@ import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class ReplacementMapTests {
@ -45,6 +46,7 @@ public class ReplacementMapTests {
TestUtils.TEST_MOD_PRIORITIES, ModConfig.DEFAULT_STONE_STRATA);
assertEquals(map.getPreferredTagForItem(TestUtils.mod1RL("bronze_ore")), TestUtils.BRONZE_ORES_TAG);
assertNull(map.getPreferredTagForItem(new ResourceLocation("minecraft:diamond")), "We don't have a tag for diamond");
assertNull(map.getPreferredTagForItem(new ResourceLocation("minecraft:diamond")),
"We don't have a tag for diamond");
}
}

View file

@ -1,7 +1,7 @@
package com.almostreliable.unified;
import com.almostreliable.unified.api.ModConstants;
import com.almostreliable.unified.compat.ie.IERecipeHandler;
import com.almostreliable.unified.compat.ie.IERecipeUnifier;
import com.almostreliable.unified.recipe.handler.RecipeHandlerFactory;
import net.minecraftforge.fml.ModList;
import net.minecraftforge.fml.loading.FMLLoader;
@ -38,7 +38,7 @@ public class AlmostUnifiedPlatformForge implements AlmostUnifiedPlatform {
@Override
public void bindRecipeHandlers(RecipeHandlerFactory factory) {
factory.registerForMod(ModConstants.IE, new IERecipeHandler());
factory.registerForMod(ModConstants.IE, new IERecipeUnifier());
}
@Override

View file

@ -2,8 +2,8 @@ package com.almostreliable.unified.compat.ie;
import com.almostreliable.unified.api.recipe.RecipeConstants;
import com.almostreliable.unified.api.recipe.RecipeContext;
import com.almostreliable.unified.api.recipe.RecipeHandler;
import com.almostreliable.unified.api.recipe.RecipeTransformationBuilder;
import com.almostreliable.unified.api.recipe.RecipeUnifier;
import com.almostreliable.unified.api.recipe.RecipeUnifierBuilder;
import com.almostreliable.unified.utils.JsonUtils;
import com.almostreliable.unified.utils.Utils;
import com.google.gson.JsonArray;
@ -13,14 +13,14 @@ import net.minecraft.resources.ResourceLocation;
import javax.annotation.Nullable;
public class IERecipeHandler implements RecipeHandler {
public class IERecipeUnifier implements RecipeUnifier {
// From IE
protected static final String BASE_KEY = "base_ingredient";
// TODO make it cleaner
@Override
public void collectTransformations(RecipeTransformationBuilder builder) {
public void collectUnifier(RecipeUnifierBuilder builder) {
builder.put("input0", this::replaceIEIngredient); // alloy recipes, refinery
builder.put("input1", this::replaceIEIngredient); // alloy recipes, refinery
builder.put(RecipeConstants.INPUT,

View file

@ -12,7 +12,7 @@ import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
public class IERecipeHandlerTest {
public class IERecipeUnifierTest {
public final Gson gson = new Gson();
private final ResourceLocation defaultRecipeId = new ResourceLocation("default_test_recipe");
@ -29,29 +29,29 @@ public class IERecipeHandlerTest {
@Test
public void notMatching() {
RecipeTransformer transformer = TestUtils.basicTransformer(f -> f.registerForMod(ModConstants.IE,
new IERecipeHandler()));
new IERecipeUnifier()));
JsonObject alloy = gson.fromJson(simpleAlloyRecipe, JsonObject.class);
RecipeLink recipe = new RecipeLink(defaultRecipeId, alloy);
transformer.transformRecipe(recipe);
assertFalse(recipe.isTransformed(), "Nothing to transform, so it should be false");
transformer.unifyRecipe(recipe);
assertFalse(recipe.isUnified(), "Nothing to transform, so it should be false");
}
@Test
public void resultTagMatches() {
RecipeTransformer transformer = TestUtils.basicTransformer(f -> f.registerForMod(ModConstants.IE,
new IERecipeHandler()));
new IERecipeUnifier()));
JsonObject alloy = gson.fromJson(simpleAlloyRecipe, JsonObject.class);
alloy
.getAsJsonObject("result")
.getAsJsonObject("base_ingredient")
.addProperty("tag", TestUtils.BRONZE_ORES_TAG.location().toString());
RecipeLink recipe = new RecipeLink(defaultRecipeId, alloy);
transformer.transformRecipe(recipe);
transformer.unifyRecipe(recipe);
assertNotEquals(recipe.getTransformed(), alloy, "Result should be different");
assertNotNull(recipe.getTransformed(), "Result should not be null");
assertNull(JsonQuery.of(recipe.getTransformed(), "result/base_ingredient/tag"), "Tag key should be removed");
assertEquals(JsonQuery.of(recipe.getTransformed(), "result/base_ingredient/item").asString(),
assertNotEquals(recipe.getUnified(), alloy, "Result should be different");
assertNotNull(recipe.getUnified(), "Result should not be null");
assertNull(JsonQuery.of(recipe.getUnified(), "result/base_ingredient/tag"), "Tag key should be removed");
assertEquals(JsonQuery.of(recipe.getUnified(), "result/base_ingredient/item").asString(),
TestUtils.mod1RL("bronze_ore").toString(),
"Result should be bronze_ore");
}
@ -59,7 +59,7 @@ public class IERecipeHandlerTest {
@Test
public void resultItemMatches() {
RecipeTransformer transformer = TestUtils.basicTransformer(f -> f.registerForMod(ModConstants.IE,
new IERecipeHandler()));
new IERecipeUnifier()));
JsonObject alloy = gson.fromJson(simpleAlloyRecipe, JsonObject.class);
alloy.getAsJsonObject("result").getAsJsonObject("base_ingredient").remove("tag");
alloy
@ -67,11 +67,11 @@ public class IERecipeHandlerTest {
.getAsJsonObject("base_ingredient")
.addProperty("item", TestUtils.mod3RL("bronze_ore").toString());
RecipeLink recipe = new RecipeLink(defaultRecipeId, alloy);
transformer.transformRecipe(recipe);
transformer.unifyRecipe(recipe);
assertNotEquals(recipe.getTransformed(), alloy, "Result should be different");
assertNotNull(recipe.getTransformed(), "Result should not be null");
assertEquals(JsonQuery.of(recipe.getTransformed(), ("result/base_ingredient/item")).asString(),
assertNotEquals(recipe.getUnified(), alloy, "Result should be different");
assertNotNull(recipe.getUnified(), "Result should not be null");
assertEquals(JsonQuery.of(recipe.getUnified(), ("result/base_ingredient/item")).asString(),
TestUtils.mod1RL("bronze_ore").toString(),
"Transformer should replace bronze_ore from mod3 with bronze_ore from mod1");
}
@ -79,7 +79,7 @@ public class IERecipeHandlerTest {
@Test
public void inputAlloyItemMatches() {
RecipeTransformer transformer = TestUtils.basicTransformer(f -> f.registerForMod(ModConstants.IE,
new IERecipeHandler()));
new IERecipeUnifier()));
JsonObject alloy = gson.fromJson(simpleAlloyRecipe, JsonObject.class);
alloy.getAsJsonObject("result").getAsJsonObject("base_ingredient").remove("tag");
alloy
@ -87,11 +87,11 @@ public class IERecipeHandlerTest {
.getAsJsonObject("base_ingredient")
.addProperty("item", TestUtils.mod3RL("bronze_ore").toString());
RecipeLink recipe = new RecipeLink(defaultRecipeId, alloy);
transformer.transformRecipe(recipe);
transformer.unifyRecipe(recipe);
assertNotEquals(recipe.getTransformed(), alloy, "Result should be different");
assertNotNull(recipe.getTransformed(), "Result should not be null");
assertEquals(JsonQuery.of(recipe.getTransformed(), ("result/base_ingredient/item")).asString(),
assertNotEquals(recipe.getUnified(), alloy, "Result should be different");
assertNotNull(recipe.getUnified(), "Result should not be null");
assertEquals(JsonQuery.of(recipe.getUnified(), ("result/base_ingredient/item")).asString(),
TestUtils.mod1RL("bronze_ore").toString(),
"Transformer should replace bronze_ore from mod3 with bronze_ore from mod1");
}