mirror of
https://github.com/GTNewHorizons/Mantle.git
synced 2026-11-28 07:35:44 +00:00
Port two data gen utilities from Inspirations
* Utility for adding config to recipes * Utility for getting names and generic conditions in recipes
This commit is contained in:
1 parent
5543aa202a
commit
99571cafba
4 files changed
+213
-1
No files matched your search
+1
-1
@@ -1,4 +1,4 @@
|
||||
package slimeknights.mantle.recipe;
|
||||
package slimeknights.mantle.recipe.data;
|
||||
|
||||
import net.minecraft.advancements.Advancement;
|
||||
import net.minecraft.advancements.AdvancementRewards;
|
||||
@@ -0,0 +1,113 @@
|
||||
package slimeknights.mantle.recipe.data;
|
||||
|
||||
import com.google.gson.JsonArray;
|
||||
import com.google.gson.JsonObject;
|
||||
import net.minecraft.data.IFinishedRecipe;
|
||||
import net.minecraft.item.crafting.IRecipeSerializer;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.CraftingHelper;
|
||||
import net.minecraftforge.common.crafting.conditions.ICondition;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Builds a recipe consumer wrapper, which adds some extra properties to wrap the result of another recipe
|
||||
*/
|
||||
public class ConsumerWrapperBuilder {
|
||||
private final List<ICondition> conditions = new ArrayList<>();
|
||||
@Nullable
|
||||
private final IRecipeSerializer<?> override;
|
||||
|
||||
private ConsumerWrapperBuilder(@Nullable IRecipeSerializer<?> override) {
|
||||
this.override = override;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a wrapper builder with the default serializer
|
||||
* @return Default serializer builder
|
||||
*/
|
||||
public static ConsumerWrapperBuilder wrap() {
|
||||
return new ConsumerWrapperBuilder(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a wrapper builder with a serializer override
|
||||
* @param override Serializer override
|
||||
* @return Default serializer builder
|
||||
*/
|
||||
public static ConsumerWrapperBuilder wrap(IRecipeSerializer<?> override) {
|
||||
return new ConsumerWrapperBuilder(override);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a conditional to the consumer
|
||||
* @param condition Condition to add
|
||||
* @return Added condition
|
||||
*/
|
||||
public ConsumerWrapperBuilder addCondition(ICondition condition) {
|
||||
conditions.add(condition);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Builds the consumer for the wrapper builder
|
||||
* @param consumer Base consumer
|
||||
* @return Built wrapper consumer
|
||||
*/
|
||||
public Consumer<IFinishedRecipe> build(Consumer<IFinishedRecipe> consumer) {
|
||||
return (recipe) -> consumer.accept(new Wrapped(recipe, conditions, override));
|
||||
}
|
||||
|
||||
private static class Wrapped implements IFinishedRecipe {
|
||||
private final IFinishedRecipe original;
|
||||
private final List<ICondition> conditions;
|
||||
@Nullable
|
||||
private final IRecipeSerializer<?> override;
|
||||
|
||||
private Wrapped(IFinishedRecipe original, List<ICondition> conditions, @Nullable IRecipeSerializer<?> override) {
|
||||
this.original = original;
|
||||
this.conditions = conditions;
|
||||
this.override = override;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serialize(JsonObject json) {
|
||||
// add conditions on top
|
||||
JsonArray conditionsArray = new JsonArray();
|
||||
for (ICondition condition : conditions) {
|
||||
conditionsArray.add(CraftingHelper.serialize(condition));
|
||||
}
|
||||
json.add("conditions", conditionsArray);
|
||||
// serialize the normal recipe
|
||||
original.serialize(json);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ResourceLocation getID() {
|
||||
return original.getID();
|
||||
}
|
||||
|
||||
@Override
|
||||
public IRecipeSerializer<?> getSerializer() {
|
||||
if (override != null) {
|
||||
return override;
|
||||
}
|
||||
return original.getSerializer();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public JsonObject getAdvancementJson() {
|
||||
return original.getAdvancementJson();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public ResourceLocation getAdvancementID() {
|
||||
return original.getAdvancementID();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
package slimeknights.mantle.recipe.data;
|
||||
|
||||
import net.minecraft.data.IFinishedRecipe;
|
||||
import net.minecraft.util.IItemProvider;
|
||||
import net.minecraft.util.ResourceLocation;
|
||||
import net.minecraftforge.common.crafting.conditions.ICondition;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
/**
|
||||
* Utilities to help in the creation of recipes
|
||||
*/
|
||||
@SuppressWarnings("unused")
|
||||
public interface IRecipeBuilderUtils {
|
||||
/**
|
||||
* Gets the mod ID for this recipe builder
|
||||
* @return Mod ID
|
||||
*/
|
||||
String getModId();
|
||||
|
||||
/**
|
||||
* Gets the base recipe consumer
|
||||
* @return Base recipe consumer
|
||||
*/
|
||||
Consumer<IFinishedRecipe> getConsumer();
|
||||
|
||||
/**
|
||||
* Gets the base condition for the condition utility
|
||||
*/
|
||||
@Nullable
|
||||
default ICondition baseCondition() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a resource location under the Inspirations mod ID
|
||||
* @param name Resource path
|
||||
* @return Resource location for Inspirations
|
||||
*/
|
||||
default ResourceLocation resource(String name) {
|
||||
return new ResourceLocation(getModId(), name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a resource location string for the given path
|
||||
* @param name Resource path
|
||||
* @return Resource location string Inspirations
|
||||
*/
|
||||
default String resourceName(String name) {
|
||||
return String.format("%s:%s", getModId(), name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Prefixes an items resource location with the given folder
|
||||
* @param item Item to fetch resource location from
|
||||
* @param prefix Name to prefix location with
|
||||
* @return Prefixed resource location
|
||||
*/
|
||||
default ResourceLocation prefix(IItemProvider item, String prefix) {
|
||||
return resource(prefix + Objects.requireNonNull(item.asItem().getRegistryName()).getPath());
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps an items resource location with the given folder and suffix
|
||||
* @param item Item to fetch resource location from
|
||||
* @param prefix Name to prefix location with
|
||||
* @param suffix Suffix for location
|
||||
* @return Prefixed resource location
|
||||
*/
|
||||
default ResourceLocation wrap(IItemProvider item, String prefix, String suffix) {
|
||||
return resource(prefix + Objects.requireNonNull(item.asItem().getRegistryName()).getPath() + suffix);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a consumer with the given condition, plus the module condition
|
||||
* @param conditions Conditions to add
|
||||
* @return Consumer with condition
|
||||
*/
|
||||
default Consumer<IFinishedRecipe> withCondition(ICondition... conditions) {
|
||||
ConsumerWrapperBuilder builder = ConsumerWrapperBuilder.wrap();
|
||||
ICondition base = baseCondition();
|
||||
if (base != null) {
|
||||
builder.addCondition(base);
|
||||
}
|
||||
for (ICondition condition : conditions) {
|
||||
builder.addCondition(condition);
|
||||
}
|
||||
return builder.build(Objects.requireNonNull(getConsumer()));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
@ParametersAreNonnullByDefault
|
||||
@MethodsReturnNonnullByDefault
|
||||
package slimeknights.mantle.recipe.data;
|
||||
|
||||
import mcp.MethodsReturnNonnullByDefault;
|
||||
|
||||
import javax.annotation.ParametersAreNonnullByDefault;
|
||||
Reference in new issue
Block a user