Skip to content

Commit

Permalink
Fixed converting fishing rod into baited fishing rod repairing the rod
Browse files Browse the repository at this point in the history
  • Loading branch information
Sollace committed Dec 20, 2024
1 parent 3dcc96f commit 14685d8
Show file tree
Hide file tree
Showing 4 changed files with 190 additions and 3 deletions.
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() + "/")));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -509,9 +509,11 @@ private void offerEarthPonyRecipes(RecipeExporter exporter) {
// worms
offerReversibleCompactingRecipes(exporter, RecipeCategory.BUILDING_BLOCKS, UItems.WHEAT_WORMS, RecipeCategory.BUILDING_BLOCKS, UBlocks.WORM_BLOCK);
// fishing
ShapelessRecipeJsonBuilder.create(RecipeCategory.TOOLS, UItems.BAITED_FISHING_ROD)
.input(Items.FISHING_ROD).criterion(hasItem(Items.FISHING_ROD), conditionsFromItem(Items.FISHING_ROD))
.input(UItems.WHEAT_WORMS)
ItemConversionShapedRecipeBuilder.create(RecipeCategory.MISC, Items.FISHING_ROD, UItems.BAITED_FISHING_ROD)
.input('#', Items.FISHING_ROD).criterion(hasItem(Items.FISHING_ROD), conditionsFromItem(Items.FISHING_ROD))
.input('*', UItems.WHEAT_WORMS).criterion("has_wheat_worms", conditionsFromItem(UItems.WHEAT_WORMS))
.pattern("# ")
.pattern(" *")
.group("fishing_rod")
.offerTo(exporter);

Expand Down
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public interface URecipes {
RecipeType<TransformCropsRecipe> GROWING = register("growing");

RecipeSerializer<ZapAppleRecipe> ZAP_APPLE_SERIALIZER = register("crafting_zap_apple", ZapAppleRecipe.CODEC, ZapAppleRecipe.PACKET_CODEC);
RecipeSerializer<ItemConversionShapedRecipe> CONVERSION_SERIALIZER = register("conversion", ItemConversionShapedRecipe.CODEC, ItemConversionShapedRecipe.PACKET_CODEC);
RecipeSerializer<GlowingRecipe> GLOWING_SERIALIZER = register("crafting_glowing", new SpecialRecipeSerializer<>(GlowingRecipe::new));
RecipeSerializer<JarInsertRecipe> JAR_INSERT_SERIALIZER = register("jar_insert", new SpecialRecipeSerializer<>(JarInsertRecipe::new));
RecipeSerializer<JarExtractRecipe> JAR_EXTRACT_SERIALIZER = register("jar_extract", new SpecialRecipeSerializer<>(JarExtractRecipe::new));
Expand Down

0 comments on commit 14685d8

Please sign in to comment.