Skip to content

Commit

Permalink
Change how disk assembler main ingredients work into a list and not s…
Browse files Browse the repository at this point in the history
…eparated ingredients from one another
  • Loading branch information
wolfieboy09 committed Nov 6, 2024
1 parent 6260dfb commit f1535b1
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
{
"type": "qstorage:disk_assembly",
"casing": {
"item": "qstorage:steel_casing"
},
"energy": 2000,
"extras": [
[
Expand All @@ -14,15 +11,22 @@
}
]
],
"port": {
"item": "qstorage:item_port"
},
"ingredients": [
[
{
"item": "qstorage:item_port"
},
{
"item": "qstorage:steel_casing"
},
{
"item": "qstorage:steel_screw"
}
]
],
"result": {
"count": 1,
"id": "qstorage:basic_storage_disk"
},
"screws": {
"item": "qstorage:steel_screw"
},
"ticks": 20
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,14 @@
@NothingNullByDefault
public class DiskAssemblerBuilder implements RecipeBuilder {
protected final ItemStack result;
protected final Ingredient diskPort;
protected final Ingredient diskCasing;
protected final Ingredient screws;
protected final List<Ingredient> mainIngredients;
protected final List<Ingredient> extras;
protected final int energyCost;
protected final int timeInTicks;
protected final Map<String, Criterion<?>> criteria = new LinkedHashMap<>();

public DiskAssemblerBuilder(Ingredient diskPort, Ingredient diskCasing, Ingredient screws, List<Ingredient> extras, int energyCost, int timeInTicks, ItemStack result) {
this.diskPort = diskPort;
this.diskCasing = diskCasing;
this.screws = screws;
public DiskAssemblerBuilder(List<Ingredient> mainIngredients, List<Ingredient> extras, int energyCost, int timeInTicks, ItemStack result) {
this.mainIngredients = mainIngredients;
this.extras = extras;
this.energyCost = energyCost;
this.timeInTicks = timeInTicks;
Expand Down Expand Up @@ -64,11 +60,11 @@ public void save(RecipeOutput output, ResourceLocation id) {
.rewards(AdvancementRewards.Builder.recipe(id))
.requirements(AdvancementRequirements.Strategy.OR);
this.criteria.forEach(advancement::addCriterion);
DiskAssemblerRecipe recipe = new DiskAssemblerRecipe(this.diskPort, this.diskCasing, this.screws, this.extras, this.energyCost, this.timeInTicks, this.result);
DiskAssemblerRecipe recipe = new DiskAssemblerRecipe(this.mainIngredients, this.extras, this.energyCost, this.timeInTicks, this.result);
output.accept(id, recipe, advancement.build(id.withPrefix("recipes/")));
}

public static DiskAssemblerBuilder create(Ingredient diskPort, Ingredient diskCasing, Ingredient screws, List<Ingredient> extras, int energyCost, int timeInTicks, ItemStack result) {
return new DiskAssemblerBuilder(diskPort, diskCasing, screws, extras, energyCost, timeInTicks, result);
public static DiskAssemblerBuilder create(List<Ingredient> mainIngredients, List<Ingredient> extras, int energyCost, int timeInTicks, ItemStack result) {
return new DiskAssemblerBuilder(mainIngredients, extras, energyCost, timeInTicks, result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.level.Level;
import net.neoforged.neoforge.items.wrapper.RecipeWrapper;
import net.neoforged.neoforge.network.codec.NeoForgeStreamCodecs;
import org.jetbrains.annotations.NotNull;

import javax.annotation.ParametersAreNonnullByDefault;
import java.util.List;

@ParametersAreNonnullByDefault
public record DiskAssemblerRecipe(
Ingredient diskPort,
Ingredient diskCasing,
Ingredient screws,
List<Ingredient> mainItems,
List<Ingredient> extras,
int energyCost,
int timeInTicks,
Expand All @@ -33,6 +30,7 @@ public record DiskAssemblerRecipe(
@Override
public boolean matches(RecipeWrapper input, Level level) {
boolean extrasMatch = !this.extras.isEmpty();
boolean ingredientsMatch = !this.mainItems.isEmpty();
for (Ingredient extra : this.extras) {
if (!extra.test(input.getItem(DiskAssemblerBlockEntity.DiskAssemblerSlot.EXTRA_SLOT_1))
&& !extra.test(input.getItem(DiskAssemblerBlockEntity.DiskAssemblerSlot.EXTRA_SLOT_2))
Expand All @@ -41,11 +39,16 @@ public boolean matches(RecipeWrapper input, Level level) {
extrasMatch = false;
}
}

for (Ingredient extra : this.mainItems) {
if (!extra.test(input.getItem(DiskAssemblerBlockEntity.DiskAssemblerSlot.MAIN_SLOT_1))
&& !extra.test(input.getItem(DiskAssemblerBlockEntity.DiskAssemblerSlot.MAIN_SLOT_2))
&& !extra.test(input.getItem(DiskAssemblerBlockEntity.DiskAssemblerSlot.MAIN_SLOT_3))) {
ingredientsMatch = false;
}
}

return this.diskPort.test(input.getItem(DiskAssemblerBlockEntity.DiskAssemblerSlot.MAIN_SLOT_1))
&& this.diskCasing.test(input.getItem(DiskAssemblerBlockEntity.DiskAssemblerSlot.MAIN_SLOT_2))
&& this.screws.test(input.getItem(DiskAssemblerBlockEntity.DiskAssemblerSlot.MAIN_SLOT_3))
&& extrasMatch;
return extrasMatch && ingredientsMatch;
}

@Override
Expand Down Expand Up @@ -76,20 +79,16 @@ public boolean canCraftInDimensions(int i, int i1) {
public static class Serializer implements RecipeSerializer<DiskAssemblerRecipe> {
private static final MapCodec<DiskAssemblerRecipe> CODEC = RecordCodecBuilder.mapCodec(
builder -> builder.group(
Ingredient.CODEC.fieldOf("port").forGetter(DiskAssemblerRecipe::diskPort),
Ingredient.CODEC.fieldOf("casing").forGetter(DiskAssemblerRecipe::diskCasing),
Ingredient.CODEC.fieldOf("screws").forGetter(DiskAssemblerRecipe::screws),
Ingredient.CODEC.listOf().fieldOf("ingredients").forGetter(DiskAssemblerRecipe::mainItems),
Ingredient.CODEC.listOf().fieldOf("extras").forGetter(DiskAssemblerRecipe::extras),
Codec.INT.fieldOf("energy").forGetter(DiskAssemblerRecipe::energyCost),
Codec.INT.fieldOf("ticks").forGetter(DiskAssemblerRecipe::timeInTicks),
ItemStack.CODEC.fieldOf("result").forGetter(DiskAssemblerRecipe::result)
).apply(builder, DiskAssemblerRecipe::new)
);

private static final StreamCodec<RegistryFriendlyByteBuf, DiskAssemblerRecipe> STREAM_CODEC = NeoForgeStreamCodecs.composite(
Ingredient.CONTENTS_STREAM_CODEC, DiskAssemblerRecipe::diskPort,
Ingredient.CONTENTS_STREAM_CODEC, DiskAssemblerRecipe::diskCasing,
Ingredient.CONTENTS_STREAM_CODEC, DiskAssemblerRecipe::screws,
private static final StreamCodec<RegistryFriendlyByteBuf, DiskAssemblerRecipe> STREAM_CODEC = StreamCodec.composite(
Ingredient.CONTENTS_STREAM_CODEC.apply(ByteBufCodecs.list(3)), DiskAssemblerRecipe::mainItems,
Ingredient.CONTENTS_STREAM_CODEC.apply(ByteBufCodecs.list(4)), DiskAssemblerRecipe::extras,
ByteBufCodecs.INT, DiskAssemblerRecipe::energyCost,
ByteBufCodecs.INT, DiskAssemblerRecipe::timeInTicks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,15 @@ public QSRecipeProvider(PackOutput output, CompletableFuture<HolderLookup.Provid
super(output, registries);
}

private static @NotNull @Unmodifiable List<Ingredient> extras(ItemLike @NotNull ... ingredients) {
// Might be really useless, but oh well
if (ingredients.length > 4) {
throw new IllegalArgumentException("More than four ingredients provided.");
}
private static @NotNull @Unmodifiable List<Ingredient> listedIngredients(ItemLike @NotNull ... ingredients) {
return List.of(Ingredient.of(ingredients));
}

@Override
protected void buildRecipes(@NotNull RecipeOutput output) {
DiskAssemblerBuilder.create(
Ingredient.of(QSItems.ITEM_PORT),
Ingredient.of(QSItems.STEEL_CASING),
Ingredient.of(QSItems.STEEL_SCREW),
extras(QSItems.DATA_CRYSTAL, QSItems.BASIC_CIRCUIT),
listedIngredients(QSItems.ITEM_PORT, QSItems.STEEL_CASING, QSItems.STEEL_SCREW),
listedIngredients(QSItems.DATA_CRYSTAL, QSItems.BASIC_CIRCUIT),
2000,
20,
new ItemStack(QSItems.BASIC_ITEM_DISK.get())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ public IDrawable getBackground() {

@Override
public void setRecipe(IRecipeLayoutBuilder builder, DiskAssemblerRecipe recipe, IFocusGroup focuses) {
builder.addSlot(RecipeIngredientRole.INPUT, 17 - guiUOffset, 27 - guiVOffset).addIngredients(recipe.diskPort());
builder.addSlot(RecipeIngredientRole.INPUT, 17 - guiUOffset, 45 - guiVOffset).addIngredients(recipe.diskCasing());
builder.addSlot(RecipeIngredientRole.INPUT, 35 - guiUOffset, 36 - guiVOffset).addIngredients(recipe.screws());
builder.addSlot(RecipeIngredientRole.INPUT, 17 - guiUOffset, 27 - guiVOffset).addIngredients(recipe.mainItems().getFirst());
builder.addSlot(RecipeIngredientRole.INPUT, 17 - guiUOffset, 45 - guiVOffset).addIngredients(recipe.mainItems().get(1));
builder.addSlot(RecipeIngredientRole.INPUT, 35 - guiUOffset, 36 - guiVOffset).addIngredients(recipe.mainItems().get(2));

int i = 0;
for (Ingredient extra : recipe.extras()) {
Expand Down

0 comments on commit f1535b1

Please sign in to comment.