-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixed converting fishing rod into baited fishing rod repairing the rod
- Loading branch information
Showing
4 changed files
with
190 additions
and
3 deletions.
There are no files selected for viewing
119 changes: 119 additions & 0 deletions
119
...m/minelittlepony/unicopia/datagen/providers/recipe/ItemConversionShapedRecipeBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package com.minelittlepony.unicopia.datagen.providers.recipe; | ||
|
||
import net.minecraft.advancement.criterion.RecipeUnlockedCriterion; | ||
|
||
import java.util.ArrayList; | ||
import java.util.LinkedHashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
|
||
import org.jetbrains.annotations.Nullable; | ||
import org.spongepowered.include.com.google.common.base.Preconditions; | ||
|
||
import com.minelittlepony.unicopia.recipe.ItemConversionShapedRecipe; | ||
|
||
import net.minecraft.advancement.Advancement; | ||
import net.minecraft.advancement.AdvancementCriterion; | ||
import net.minecraft.advancement.AdvancementRequirements; | ||
import net.minecraft.advancement.AdvancementRewards; | ||
import net.minecraft.data.server.recipe.CraftingRecipeJsonBuilder; | ||
import net.minecraft.data.server.recipe.RecipeExporter; | ||
import net.minecraft.item.Item; | ||
import net.minecraft.item.ItemConvertible; | ||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.recipe.Ingredient; | ||
import net.minecraft.recipe.RawShapedRecipe; | ||
import net.minecraft.recipe.book.RecipeCategory; | ||
import net.minecraft.registry.tag.TagKey; | ||
import net.minecraft.util.Identifier; | ||
|
||
public class ItemConversionShapedRecipeBuilder implements CraftingRecipeJsonBuilder { | ||
private final RecipeCategory category; | ||
private final Item base; | ||
private final Item output; | ||
private final int count; | ||
private final List<String> pattern = new ArrayList<>(); | ||
private final Map<Character, Ingredient> inputs = new LinkedHashMap<>(); | ||
private final Map<String, AdvancementCriterion<?>> criteria = new LinkedHashMap<>(); | ||
@Nullable | ||
private String group; | ||
private boolean showNotification = true; | ||
|
||
public ItemConversionShapedRecipeBuilder(RecipeCategory category, ItemConvertible base, ItemConvertible output, int count) { | ||
this.category = category; | ||
this.base = base.asItem(); | ||
this.output = output.asItem(); | ||
this.count = count; | ||
} | ||
|
||
public static ItemConversionShapedRecipeBuilder create(RecipeCategory category, ItemConvertible base, ItemConvertible output) { | ||
return create(category, base, output, 1); | ||
} | ||
|
||
public static ItemConversionShapedRecipeBuilder create(RecipeCategory category, ItemConvertible base, ItemConvertible output, int count) { | ||
return new ItemConversionShapedRecipeBuilder(category, base, output, count); | ||
} | ||
|
||
public ItemConversionShapedRecipeBuilder input(Character c, TagKey<Item> tag) { | ||
return input(c, Ingredient.fromTag(tag)); | ||
} | ||
|
||
public ItemConversionShapedRecipeBuilder input(Character c, ItemConvertible itemProvider) { | ||
return input(c, Ingredient.ofItems(itemProvider)); | ||
} | ||
|
||
public ItemConversionShapedRecipeBuilder input(Character c, Ingredient ingredient) { | ||
Preconditions.checkArgument(!inputs.containsKey(c), "Symbol '" + c + "' is already defined!"); | ||
Preconditions.checkArgument(c != ' ', "Symbol ' ' (whitespace) is reserved and cannot be defined"); | ||
inputs.put(c, ingredient); | ||
return this; | ||
} | ||
|
||
public ItemConversionShapedRecipeBuilder pattern(String patternStr) { | ||
Preconditions.checkArgument(pattern.isEmpty() || patternStr.length() == pattern.get(0).length(), "Pattern must be the same width on every line!"); | ||
pattern.add(patternStr); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ItemConversionShapedRecipeBuilder criterion(String string, AdvancementCriterion<?> advancementCriterion) { | ||
criteria.put(string, advancementCriterion); | ||
return this; | ||
} | ||
|
||
@Override | ||
public ItemConversionShapedRecipeBuilder group(@Nullable String string) { | ||
this.group = string; | ||
return this; | ||
} | ||
|
||
public ItemConversionShapedRecipeBuilder showNotification(boolean showNotification) { | ||
this.showNotification = showNotification; | ||
return this; | ||
} | ||
|
||
@Override | ||
public Item getOutputItem() { | ||
return output; | ||
} | ||
|
||
@Override | ||
public void offerTo(RecipeExporter exporter, Identifier recipeId) { | ||
Preconditions.checkArgument(!criteria.isEmpty(), "No way of obtaining recipe " + recipeId); | ||
Advancement.Builder builder = exporter.getAdvancementBuilder() | ||
.criterion("has_the_recipe", RecipeUnlockedCriterion.create(recipeId)) | ||
.rewards(AdvancementRewards.Builder.recipe(recipeId)) | ||
.criteriaMerger(AdvancementRequirements.CriterionMerger.OR); | ||
criteria.forEach(builder::criterion); | ||
ItemConversionShapedRecipe shapedRecipe = new ItemConversionShapedRecipe( | ||
Objects.requireNonNullElse(group, ""), | ||
CraftingRecipeJsonBuilder.toCraftingCategory(category), | ||
RawShapedRecipe.create(inputs, pattern), | ||
base.getDefaultStack(), | ||
new ItemStack(output, count), | ||
showNotification | ||
); | ||
exporter.accept(recipeId, shapedRecipe, builder.build(recipeId.withPrefixedPath("recipes/" + category.getName() + "/"))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
src/main/java/com/minelittlepony/unicopia/recipe/ItemConversionShapedRecipe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.minelittlepony.unicopia.recipe; | ||
|
||
import com.minelittlepony.unicopia.util.serialization.PacketCodecUtils; | ||
import com.mojang.serialization.Codec; | ||
import com.mojang.serialization.MapCodec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
|
||
import net.minecraft.item.ItemStack; | ||
import net.minecraft.network.RegistryByteBuf; | ||
import net.minecraft.network.codec.PacketCodec; | ||
import net.minecraft.network.codec.PacketCodecs; | ||
import net.minecraft.recipe.RawShapedRecipe; | ||
import net.minecraft.recipe.RecipeSerializer; | ||
import net.minecraft.recipe.ShapedRecipe; | ||
import net.minecraft.recipe.book.CraftingRecipeCategory; | ||
import net.minecraft.recipe.input.CraftingRecipeInput; | ||
import net.minecraft.registry.RegistryWrapper; | ||
|
||
public class ItemConversionShapedRecipe extends ShapedRecipe { | ||
public static final MapCodec<ItemConversionShapedRecipe> CODEC = RecordCodecBuilder.mapCodec(i -> i.group( | ||
Codec.STRING.optionalFieldOf("group", "").forGetter(ItemConversionShapedRecipe::getGroup), | ||
CraftingRecipeCategory.CODEC.fieldOf("category").orElse(CraftingRecipeCategory.MISC).forGetter(ItemConversionShapedRecipe::getCategory), | ||
RawShapedRecipe.CODEC.forGetter(recipe -> recipe.raw), | ||
ItemStack.VALIDATED_CODEC.fieldOf("base").forGetter(recipe -> recipe.base), | ||
ItemStack.VALIDATED_CODEC.fieldOf("result").forGetter(recipe -> recipe.result), | ||
Codec.BOOL.optionalFieldOf("show_notification", true).forGetter(ItemConversionShapedRecipe::showNotification) | ||
).apply(i, ItemConversionShapedRecipe::new)); | ||
public static final PacketCodec<RegistryByteBuf, ItemConversionShapedRecipe> PACKET_CODEC = PacketCodec.tuple( | ||
PacketCodecs.STRING, ItemConversionShapedRecipe::getGroup, | ||
PacketCodecUtils.ofEnum(CraftingRecipeCategory.class), ItemConversionShapedRecipe::getCategory, | ||
RawShapedRecipe.PACKET_CODEC, recipe -> recipe.raw, | ||
ItemStack.PACKET_CODEC, recipe -> recipe.base, | ||
ItemStack.PACKET_CODEC, recipe -> recipe.result, | ||
PacketCodecs.BOOL, ItemConversionShapedRecipe::showNotification, | ||
ItemConversionShapedRecipe::new | ||
); | ||
|
||
final RawShapedRecipe raw; | ||
final ItemStack base; | ||
final ItemStack result; | ||
|
||
public ItemConversionShapedRecipe(String group, CraftingRecipeCategory category, RawShapedRecipe raw, ItemStack base, ItemStack result, boolean showNotification) { | ||
super(group, category, raw, result, showNotification); | ||
this.raw = raw; | ||
this.base = base; | ||
this.result = result; | ||
} | ||
|
||
@Override | ||
public RecipeSerializer<?> getSerializer() { | ||
return URecipes.CONVERSION_SERIALIZER; | ||
} | ||
|
||
@Override | ||
public ItemStack craft(CraftingRecipeInput input, RegistryWrapper.WrapperLookup lookup) { | ||
return input.getStacks().stream() | ||
.filter(i -> { | ||
|
||
return i.isOf(base.getItem()); | ||
}) | ||
.findFirst() | ||
.map(base -> base.withItem(result.getItem())) | ||
.orElseGet(() -> getResult(lookup).copy()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters