Support id ingredients, and load conditions in datagen.
This commit is contained in:
parent
0bcd2bec03
commit
0845d6618b
4 changed files with 124 additions and 8 deletions
|
@ -203,8 +203,7 @@ allprojects {
|
|||
// TechReborn specific dependencies
|
||||
dependencies {
|
||||
api project(path: ":RebornCore", configuration: "namedElements")
|
||||
clientCompileClasspath project(":RebornCore").sourceSets.client.output
|
||||
clientRuntimeClasspath project(":RebornCore").sourceSets.client.output
|
||||
clientImplementation project(":RebornCore").sourceSets.client.output
|
||||
|
||||
include project(":RebornCore")
|
||||
|
||||
|
|
|
@ -28,13 +28,15 @@ import net.minecraft.item.Item
|
|||
import net.minecraft.item.ItemConvertible
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.tag.TagKey
|
||||
import net.minecraft.util.Identifier
|
||||
import reborncore.common.crafting.ingredient.RebornIngredient
|
||||
import reborncore.common.crafting.ingredient.StackIngredient
|
||||
import reborncore.common.crafting.ingredient.TagIngredient
|
||||
|
||||
class IngredientBuilder {
|
||||
private TagKey<Item> tag
|
||||
private List<ItemStack> stacks = new ArrayList<>()
|
||||
private List<ItemStack> stacks = []
|
||||
private List<Identifier> ids = []
|
||||
|
||||
private IngredientBuilder() {
|
||||
}
|
||||
|
@ -44,9 +46,21 @@ class IngredientBuilder {
|
|||
}
|
||||
|
||||
RebornIngredient build() {
|
||||
if (!ids.isEmpty()) {
|
||||
if (!stacks.isEmpty() || tag != null) {
|
||||
throw new IllegalStateException("Cannot have id ingredient with tag/stack inputs")
|
||||
}
|
||||
|
||||
if (ids.size() != 1) {
|
||||
throw new IllegalStateException()
|
||||
}
|
||||
|
||||
return new StackIdIngredient(ids.get(0))
|
||||
}
|
||||
|
||||
if (tag != null) {
|
||||
if (!stacks.isEmpty()) {
|
||||
throw new IllegalStateException("Cannot have ingredient with tag and stack inputs")
|
||||
if (!stacks.isEmpty() || !ids.isEmpty()) {
|
||||
throw new IllegalStateException("Cannot have ingredient with tag and stack/id inputs")
|
||||
}
|
||||
|
||||
return new TagIngredient(tag, Optional.empty())
|
||||
|
@ -72,4 +86,9 @@ class IngredientBuilder {
|
|||
stacks.add(itemStack)
|
||||
return this
|
||||
}
|
||||
|
||||
def ident(Identifier identifier) {
|
||||
ids.add(identifier)
|
||||
return this
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,6 +25,8 @@
|
|||
package techreborn.datagen.recipes.machine
|
||||
|
||||
import com.google.gson.JsonObject
|
||||
import net.fabricmc.fabric.api.resource.conditions.v1.ConditionJsonProvider
|
||||
import net.fabricmc.fabric.impl.datagen.FabricDataGenHelper
|
||||
import net.minecraft.advancement.Advancement.Builder
|
||||
import net.minecraft.advancement.criterion.CriterionConditions
|
||||
import net.minecraft.data.server.recipe.RecipeJsonProvider
|
||||
|
@ -39,6 +41,7 @@ import reborncore.common.crafting.RebornRecipe
|
|||
import reborncore.common.crafting.RebornRecipeType
|
||||
import reborncore.common.crafting.RecipeUtils
|
||||
import reborncore.common.crafting.ingredient.RebornIngredient
|
||||
import techreborn.datagen.recipes.TechRebornRecipesProvider
|
||||
|
||||
import java.util.function.Consumer
|
||||
|
||||
|
@ -52,6 +55,7 @@ class MachineRecipeJsonFactory<R extends RebornRecipe> {
|
|||
protected int time = -1
|
||||
protected Identifier customId = null
|
||||
protected String source = null
|
||||
protected List<ConditionJsonProvider> conditions = []
|
||||
|
||||
protected MachineRecipeJsonFactory(RebornRecipeType<R> type) {
|
||||
this.type = type
|
||||
|
@ -82,7 +86,14 @@ class MachineRecipeJsonFactory<R extends RebornRecipe> {
|
|||
ingredient {
|
||||
stack object
|
||||
}
|
||||
|
||||
} else if (object instanceof Identifier) {
|
||||
ingredient {
|
||||
ident object
|
||||
}
|
||||
} else if (object instanceof String) {
|
||||
ingredient {
|
||||
ident(new Identifier(object))
|
||||
}
|
||||
} else {
|
||||
throw new IllegalArgumentException()
|
||||
}
|
||||
|
@ -136,6 +147,11 @@ class MachineRecipeJsonFactory<R extends RebornRecipe> {
|
|||
return this
|
||||
}
|
||||
|
||||
def condition(ConditionJsonProvider conditionJsonProvider) {
|
||||
this.conditions.add(conditionJsonProvider)
|
||||
return this
|
||||
}
|
||||
|
||||
@NotNull String getSourceAppendix() {
|
||||
if (source == null)
|
||||
return ""
|
||||
|
@ -181,7 +197,7 @@ class MachineRecipeJsonFactory<R extends RebornRecipe> {
|
|||
Identifier recipeId = getIdentifier()
|
||||
Identifier advancementId = new Identifier(recipeId.getNamespace(), "recipes/" + recipeId.getPath())
|
||||
RecipeUtils.addToastDefaults(builder, recipeId)
|
||||
exporter.accept(new MachineRecipeJsonProvider<R>(type, createRecipe(recipeId), advancementId, builder))
|
||||
exporter.accept(new MachineRecipeJsonProvider<R>(type, createRecipe(recipeId), advancementId, builder, conditions))
|
||||
}
|
||||
|
||||
def getIdentifier() {
|
||||
|
@ -202,16 +218,22 @@ class MachineRecipeJsonFactory<R extends RebornRecipe> {
|
|||
private final R recipe
|
||||
private final Identifier advancementId
|
||||
private final Builder builder
|
||||
private final List<ConditionJsonProvider> conditions
|
||||
|
||||
MachineRecipeJsonProvider(RebornRecipeType<R> type, R recipe, Identifier advancementId = null, Builder builder = null) {
|
||||
MachineRecipeJsonProvider(RebornRecipeType<R> type, R recipe, Identifier advancementId, Builder builder, List<ConditionJsonProvider> conditions) {
|
||||
this.type = type
|
||||
this.recipe = recipe
|
||||
this.advancementId = advancementId
|
||||
this.builder = builder
|
||||
this.conditions = conditions
|
||||
}
|
||||
|
||||
@Override
|
||||
JsonObject toJson() {
|
||||
if (!conditions.isEmpty()) {
|
||||
FabricDataGenHelper.addConditions(this, conditions.toArray() as ConditionJsonProvider[])
|
||||
}
|
||||
|
||||
return type.toJson(recipe, false)
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,76 @@
|
|||
/*
|
||||
* This file is part of TechReborn, licensed under the MIT License (MIT).
|
||||
*
|
||||
* Copyright (c) 2020 TechReborn
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package techreborn.datagen.recipes.machine
|
||||
|
||||
import com.google.gson.JsonObject
|
||||
import net.minecraft.item.ItemStack
|
||||
import net.minecraft.recipe.Ingredient
|
||||
import net.minecraft.util.Identifier
|
||||
import reborncore.common.crafting.ingredient.RebornIngredient
|
||||
|
||||
// Dummy ingredient to create recipes for mods that do not exist at datagen runtime.
|
||||
class StackIdIngredient extends RebornIngredient {
|
||||
final Identifier itemId
|
||||
final int count
|
||||
|
||||
StackIdIngredient(Identifier itemId, int count = -1) {
|
||||
this.itemId = itemId
|
||||
this.count = count
|
||||
}
|
||||
|
||||
@Override
|
||||
JsonObject toJson(boolean networkSync) {
|
||||
assert !networkSync
|
||||
|
||||
def json = new JsonObject()
|
||||
json.addProperty("item", itemId.toString())
|
||||
|
||||
if (count > 0) {
|
||||
json.addProperty("count", count)
|
||||
}
|
||||
|
||||
return json
|
||||
}
|
||||
|
||||
@Override
|
||||
boolean test(ItemStack itemStack) {
|
||||
throw new UnsupportedOperationException()
|
||||
}
|
||||
|
||||
@Override
|
||||
Ingredient getPreview() {
|
||||
throw new UnsupportedOperationException()
|
||||
}
|
||||
|
||||
@Override
|
||||
List<ItemStack> getPreviewStacks() {
|
||||
throw new UnsupportedOperationException()
|
||||
}
|
||||
|
||||
@Override
|
||||
int getCount() {
|
||||
throw new UnsupportedOperationException()
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue