Skip to content

Commit

Permalink
feat: Add sortIndex field to market recipes #221
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Oct 29, 2024
1 parent e082b07 commit e3e8b9a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,7 @@ private Comparator<RecipeDisplayEntry> sorting() {
(RecipeDisplayEntry recipe) -> recipe.display() instanceof MarketRecipeDisplay marketRecipeDisplay ? resolveMarketCategory(marketRecipeDisplay.category())
.map(MarketCategory::sortIndex)
.orElse(0) : 0)
.thenComparing(recipe -> recipe.display() instanceof MarketRecipeDisplay marketRecipeDisplay ? marketRecipeDisplay.sortIndex() : 0)
.thenComparing(recipe -> recipe.display().result().resolveForFirstStack(contextMap)
.getDisplayName()
.getString());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.blay09.mods.farmingforblockheads.recipe;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.blay09.mods.farmingforblockheads.api.Payment;
Expand Down Expand Up @@ -30,12 +31,14 @@ public class MarketRecipe implements Recipe<RecipeInput> {
private final ResourceLocation category;
private final ItemStack result;
private final Payment payment;
private final int sortIndex;

public MarketRecipe(ItemStack result, String defaults, @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<ResourceLocation> category, @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<Payment> payment) {
public MarketRecipe(ItemStack result, String defaults, @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<ResourceLocation> category, @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional<Payment> payment, int sortIndex) {
this.defaults = defaults;
this.category = category.orElse(null);
this.result = result;
this.payment = payment.orElse(null);
this.sortIndex = sortIndex;
}

@Override
Expand Down Expand Up @@ -83,6 +86,7 @@ public List<RecipeDisplay> display() {
new SlotDisplay.ItemSlotDisplay(
ModBlocks.market.asItem()),
effectiveCategory,
sortIndex,
enabled()));
}

Expand Down Expand Up @@ -119,6 +123,10 @@ public Optional<ResourceLocation> getCategory() {
return Optional.ofNullable(category);
}

public int getSortIndex() {
return sortIndex;
}

static class Serializer implements RecipeSerializer<MarketRecipe> {

private static final MapCodec<ItemStack> RESULT_CODEC = RecordCodecBuilder.mapCodec((instance) -> instance.group(
Expand All @@ -133,7 +141,8 @@ static class Serializer implements RecipeSerializer<MarketRecipe> {
RESULT_CODEC.fieldOf("result").forGetter(recipe -> recipe.result),
ExtraCodecs.NON_EMPTY_STRING.fieldOf("defaults").forGetter(recipe -> recipe.defaults),
ResourceLocation.CODEC.optionalFieldOf("category").forGetter(MarketRecipe::getCategory),
PaymentImpl.CODEC.optionalFieldOf("payment").forGetter(MarketRecipe::getPayment)
PaymentImpl.CODEC.optionalFieldOf("payment").forGetter(MarketRecipe::getPayment),
Codec.INT.fieldOf("sortIndex").orElse(0).forGetter(MarketRecipe::getSortIndex)
).apply(instance, MarketRecipe::new));

public static final StreamCodec<RegistryFriendlyByteBuf, MarketRecipe> STREAM_CODEC = StreamCodec.of(Serializer::toNetwork, Serializer::fromNetwork);
Expand All @@ -153,14 +162,16 @@ public static MarketRecipe fromNetwork(RegistryFriendlyByteBuf buf) {
final var defaults = buf.readUtf();
final var category = buf.readResourceLocation();
final var payment = PaymentImpl.fromNetwork(buf);
return new MarketRecipe(resultItem, defaults, Optional.of(category), Optional.of(payment));
final var sortIndex = buf.readVarInt();
return new MarketRecipe(resultItem, defaults, Optional.of(category), Optional.of(payment), sortIndex);
}

public static void toNetwork(RegistryFriendlyByteBuf buf, MarketRecipe recipe) {
ItemStack.OPTIONAL_STREAM_CODEC.encode(buf, recipe.result);
buf.writeUtf(recipe.defaults);
buf.writeResourceLocation(recipe.category);
PaymentImpl.toNetwork(buf, recipe.payment);
buf.writeVarInt(recipe.sortIndex);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@
import net.minecraft.world.item.crafting.display.SlotDisplay;

public record MarketRecipeDisplay(SlotDisplay payment, SlotDisplay result, SlotDisplay craftingStation, ResourceLocation category,
int sortIndex,
boolean enabled) implements RecipeDisplay {
public static final MapCodec<MarketRecipeDisplay> MAP_CODEC = RecordCodecBuilder.mapCodec((instance) -> instance.group(
SlotDisplay.CODEC.fieldOf("payment").forGetter(MarketRecipeDisplay::payment),
SlotDisplay.CODEC.fieldOf("result").forGetter(MarketRecipeDisplay::result),
SlotDisplay.CODEC.fieldOf("crafting_station").forGetter(MarketRecipeDisplay::craftingStation),
ResourceLocation.CODEC.fieldOf("category").forGetter(MarketRecipeDisplay::category),
Codec.INT.fieldOf("sort_index").forGetter(MarketRecipeDisplay::sortIndex),
Codec.BOOL.fieldOf("enabled").forGetter(MarketRecipeDisplay::enabled)
).apply(instance, MarketRecipeDisplay::new));
public static final StreamCodec<RegistryFriendlyByteBuf, MarketRecipeDisplay> STREAM_CODEC = StreamCodec.composite(SlotDisplay.STREAM_CODEC,
Expand All @@ -28,6 +30,8 @@ public record MarketRecipeDisplay(SlotDisplay payment, SlotDisplay result, SlotD
MarketRecipeDisplay::craftingStation,
ResourceLocation.STREAM_CODEC,
MarketRecipeDisplay::category,
ByteBufCodecs.INT,
MarketRecipeDisplay::sortIndex,
ByteBufCodecs.BOOL,
MarketRecipeDisplay::enabled,
MarketRecipeDisplay::new);
Expand Down

0 comments on commit e3e8b9a

Please sign in to comment.