-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce a custom recipe type for quartz knives to support damaging …
- Loading branch information
Showing
10 changed files
with
223 additions
and
67 deletions.
There are no files selected for viewing
32 changes: 0 additions & 32 deletions
32
src/generated/resources/data/ae2/advancement/recipes/misc/network/parts/cable_anchor.json
This file was deleted.
Oops, something went wrong.
7 changes: 3 additions & 4 deletions
7
src/generated/resources/data/ae2/recipe/network/parts/cable_anchor.json
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
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
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
38 changes: 38 additions & 0 deletions
38
src/main/java/appeng/datagen/providers/recipes/QuartzCuttingRecipesProvider.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,38 @@ | ||
package appeng.datagen.providers.recipes; | ||
|
||
import java.util.concurrent.CompletableFuture; | ||
|
||
import net.minecraft.core.HolderLookup; | ||
import net.minecraft.core.NonNullList; | ||
import net.minecraft.data.PackOutput; | ||
import net.minecraft.data.recipes.RecipeOutput; | ||
import net.minecraft.world.item.crafting.Ingredient; | ||
|
||
import appeng.api.ids.AETags; | ||
import appeng.core.AppEng; | ||
import appeng.core.definitions.AEParts; | ||
import appeng.datagen.providers.tags.ConventionTags; | ||
import appeng.recipes.quartzcutting.QuartzCuttingRecipe; | ||
|
||
public class QuartzCuttingRecipesProvider extends AE2RecipeProvider { | ||
public QuartzCuttingRecipesProvider(PackOutput output, CompletableFuture<HolderLookup.Provider> registries) { | ||
super(output, registries); | ||
} | ||
|
||
@Override | ||
protected void buildRecipes(RecipeOutput recipeOutput) { | ||
recipeOutput.accept( | ||
AppEng.makeId("network/parts/cable_anchor"), | ||
new QuartzCuttingRecipe( | ||
AEParts.CABLE_ANCHOR.stack(4), | ||
NonNullList.of(Ingredient.EMPTY, | ||
Ingredient.of(ConventionTags.QUARTZ_KNIFE), | ||
Ingredient.of(AETags.METAL_INGOTS))), | ||
null); | ||
} | ||
|
||
@Override | ||
public String getName() { | ||
return "AE2 Quartz Cutting Recipes"; | ||
} | ||
} |
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
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
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
153 changes: 153 additions & 0 deletions
153
src/main/java/appeng/recipes/quartzcutting/QuartzCuttingRecipe.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,153 @@ | ||
package appeng.recipes.quartzcutting; | ||
|
||
import java.util.ArrayList; | ||
|
||
import com.mojang.serialization.DataResult; | ||
import com.mojang.serialization.MapCodec; | ||
import com.mojang.serialization.codecs.RecordCodecBuilder; | ||
|
||
import org.apache.commons.lang3.mutable.MutableBoolean; | ||
|
||
import net.minecraft.core.HolderLookup; | ||
import net.minecraft.core.NonNullList; | ||
import net.minecraft.network.RegistryFriendlyByteBuf; | ||
import net.minecraft.network.codec.StreamCodec; | ||
import net.minecraft.server.level.ServerPlayer; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.crafting.CraftingBookCategory; | ||
import net.minecraft.world.item.crafting.CraftingInput; | ||
import net.minecraft.world.item.crafting.CraftingRecipe; | ||
import net.minecraft.world.item.crafting.Ingredient; | ||
import net.minecraft.world.item.crafting.RecipeSerializer; | ||
import net.minecraft.world.level.Level; | ||
import net.neoforged.neoforge.common.CommonHooks; | ||
import net.neoforged.neoforge.common.util.RecipeMatcher; | ||
import net.neoforged.neoforge.server.ServerLifecycleHooks; | ||
|
||
import appeng.datagen.providers.tags.ConventionTags; | ||
|
||
public class QuartzCuttingRecipe implements CraftingRecipe { | ||
static final int MAX_HEIGHT = 3; | ||
static final int MAX_WIDTH = 3; | ||
public static final MapCodec<QuartzCuttingRecipe> CODEC = RecordCodecBuilder.mapCodec((builder) -> builder.group( | ||
ItemStack.STRICT_CODEC.fieldOf("result").forGetter(QuartzCuttingRecipe::getResult), | ||
Ingredient.CODEC_NONEMPTY.listOf().fieldOf("ingredients").flatXmap((r) -> { | ||
var ingredients = r.toArray(Ingredient[]::new); | ||
if (ingredients.length == 0) { | ||
return DataResult.error(() -> "No ingredients for quartz cutting recipe"); | ||
} else { | ||
return ingredients.length > MAX_HEIGHT * MAX_WIDTH ? DataResult.error(() -> { | ||
return "Too many ingredients for quartz cutting recipe. The maximum is: %s" | ||
.formatted(MAX_HEIGHT * MAX_WIDTH); | ||
}) : DataResult.success(NonNullList.of(Ingredient.EMPTY, ingredients)); | ||
} | ||
}, DataResult::success).forGetter(QuartzCuttingRecipe::getIngredients)) | ||
.apply(builder, QuartzCuttingRecipe::new)); | ||
|
||
public static final StreamCodec<RegistryFriendlyByteBuf, QuartzCuttingRecipe> STREAM_CODEC = StreamCodec.composite( | ||
ItemStack.STREAM_CODEC, QuartzCuttingRecipe::getResult, | ||
StreamCodec.of( | ||
(buffer, value) -> { | ||
buffer.writeVarInt(value.size()); | ||
for (var ingredient : value) { | ||
Ingredient.CONTENTS_STREAM_CODEC.encode(buffer, ingredient); | ||
} | ||
}, | ||
buffer -> { | ||
int count = buffer.readVarInt(); | ||
NonNullList<Ingredient> ingredients = NonNullList.withSize(count, Ingredient.EMPTY); | ||
ingredients.replaceAll(ignored -> Ingredient.CONTENTS_STREAM_CODEC.decode(buffer)); | ||
return ingredients; | ||
}), | ||
QuartzCuttingRecipe::getIngredients, | ||
QuartzCuttingRecipe::new); | ||
|
||
final ItemStack result; | ||
final NonNullList<Ingredient> ingredients; | ||
private final boolean isSimple; | ||
|
||
public QuartzCuttingRecipe(ItemStack result, NonNullList<Ingredient> ingredients) { | ||
this.result = result; | ||
this.ingredients = ingredients; | ||
this.isSimple = ingredients.stream().allMatch(Ingredient::isSimple); | ||
} | ||
|
||
public RecipeSerializer<?> getSerializer() { | ||
return QuartzCuttingRecipeSerializer.INSTANCE; | ||
} | ||
|
||
public CraftingBookCategory category() { | ||
return CraftingBookCategory.MISC; | ||
} | ||
|
||
public ItemStack getResultItem(HolderLookup.Provider registries) { | ||
return this.result; | ||
} | ||
|
||
public NonNullList<Ingredient> getIngredients() { | ||
return this.ingredients; | ||
} | ||
|
||
public boolean matches(CraftingInput input, Level level) { | ||
if (input.ingredientCount() != this.ingredients.size()) { | ||
return false; | ||
} else if (!this.isSimple) { | ||
var nonEmptyItems = new ArrayList<ItemStack>(input.ingredientCount()); | ||
for (var item : input.items()) { | ||
if (!item.isEmpty()) { | ||
nonEmptyItems.add(item); | ||
} | ||
} | ||
|
||
return RecipeMatcher.findMatches(nonEmptyItems, this.ingredients) != null; | ||
} else { | ||
if (input.size() == 1 && this.ingredients.size() == 1) { | ||
return this.ingredients.getFirst().test(input.getItem(0)); | ||
} | ||
return input.stackedContents().canCraft(this, null); | ||
} | ||
} | ||
|
||
public ItemStack assemble(CraftingInput input, HolderLookup.Provider registries) { | ||
return this.result.copy(); | ||
} | ||
|
||
public boolean canCraftInDimensions(int width, int height) { | ||
return width * height >= this.ingredients.size(); | ||
} | ||
|
||
private ItemStack getResult() { | ||
return result; | ||
} | ||
|
||
@Override | ||
public NonNullList<ItemStack> getRemainingItems(CraftingInput input) { | ||
NonNullList<ItemStack> remainingItems = NonNullList.withSize(input.size(), ItemStack.EMPTY); | ||
|
||
boolean damagedKnife = false; | ||
|
||
for (int i = 0; i < remainingItems.size(); i++) { | ||
ItemStack item = input.getItem(i); | ||
|
||
if (!damagedKnife && item.is(ConventionTags.QUARTZ_KNIFE)) { | ||
damagedKnife = true; | ||
var result = item.copy(); | ||
|
||
var broken = new MutableBoolean(false); | ||
if (CommonHooks.getCraftingPlayer() instanceof ServerPlayer serverPlayer) { | ||
result.hurtAndBreak(1, serverPlayer.serverLevel(), serverPlayer, ignored -> broken.setTrue()); | ||
} else { | ||
var currentServer = ServerLifecycleHooks.getCurrentServer(); | ||
if (currentServer != null) { | ||
result.hurtAndBreak(1, currentServer.overworld(), null, ignored -> broken.setTrue()); | ||
} | ||
} | ||
remainingItems.set(i, broken.getValue() ? ItemStack.EMPTY : result); | ||
} else if (item.hasCraftingRemainingItem()) { | ||
remainingItems.set(i, item.getCraftingRemainingItem()); | ||
} | ||
} | ||
|
||
return remainingItems; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/main/java/appeng/recipes/quartzcutting/QuartzCuttingRecipeSerializer.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,23 @@ | ||
package appeng.recipes.quartzcutting; | ||
|
||
import com.mojang.serialization.MapCodec; | ||
|
||
import net.minecraft.network.RegistryFriendlyByteBuf; | ||
import net.minecraft.network.codec.StreamCodec; | ||
import net.minecraft.world.item.crafting.RecipeSerializer; | ||
|
||
public class QuartzCuttingRecipeSerializer implements RecipeSerializer<QuartzCuttingRecipe> { | ||
|
||
public static final QuartzCuttingRecipeSerializer INSTANCE = new QuartzCuttingRecipeSerializer(); | ||
|
||
public QuartzCuttingRecipeSerializer() { | ||
} | ||
|
||
public MapCodec<QuartzCuttingRecipe> codec() { | ||
return QuartzCuttingRecipe.CODEC; | ||
} | ||
|
||
public StreamCodec<RegistryFriendlyByteBuf, QuartzCuttingRecipe> streamCodec() { | ||
return QuartzCuttingRecipe.STREAM_CODEC; | ||
} | ||
} |