From e3e8b9aaed8f766090541beea9be6ab7107c259e Mon Sep 17 00:00:00 2001 From: BlayTheNinth <1933180+BlayTheNinth@users.noreply.github.com> Date: Tue, 29 Oct 2024 09:30:52 +0100 Subject: [PATCH] feat: Add sortIndex field to market recipes #221 --- .../farmingforblockheads/menu/MarketMenu.java | 1 + .../recipe/MarketRecipe.java | 17 ++++++++++++++--- .../recipe/MarketRecipeDisplay.java | 4 ++++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/common/src/main/java/net/blay09/mods/farmingforblockheads/menu/MarketMenu.java b/common/src/main/java/net/blay09/mods/farmingforblockheads/menu/MarketMenu.java index dd7a1fe..7539216 100644 --- a/common/src/main/java/net/blay09/mods/farmingforblockheads/menu/MarketMenu.java +++ b/common/src/main/java/net/blay09/mods/farmingforblockheads/menu/MarketMenu.java @@ -386,6 +386,7 @@ private Comparator 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()); diff --git a/common/src/main/java/net/blay09/mods/farmingforblockheads/recipe/MarketRecipe.java b/common/src/main/java/net/blay09/mods/farmingforblockheads/recipe/MarketRecipe.java index adbafd7..9dd04f8 100644 --- a/common/src/main/java/net/blay09/mods/farmingforblockheads/recipe/MarketRecipe.java +++ b/common/src/main/java/net/blay09/mods/farmingforblockheads/recipe/MarketRecipe.java @@ -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; @@ -30,12 +31,14 @@ public class MarketRecipe implements Recipe { 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 category, @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional payment) { + public MarketRecipe(ItemStack result, String defaults, @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional category, @SuppressWarnings("OptionalUsedAsFieldOrParameterType") Optional payment, int sortIndex) { this.defaults = defaults; this.category = category.orElse(null); this.result = result; this.payment = payment.orElse(null); + this.sortIndex = sortIndex; } @Override @@ -83,6 +86,7 @@ public List display() { new SlotDisplay.ItemSlotDisplay( ModBlocks.market.asItem()), effectiveCategory, + sortIndex, enabled())); } @@ -119,6 +123,10 @@ public Optional getCategory() { return Optional.ofNullable(category); } + public int getSortIndex() { + return sortIndex; + } + static class Serializer implements RecipeSerializer { private static final MapCodec RESULT_CODEC = RecordCodecBuilder.mapCodec((instance) -> instance.group( @@ -133,7 +141,8 @@ static class Serializer implements RecipeSerializer { 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 STREAM_CODEC = StreamCodec.of(Serializer::toNetwork, Serializer::fromNetwork); @@ -153,7 +162,8 @@ 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) { @@ -161,6 +171,7 @@ public static void toNetwork(RegistryFriendlyByteBuf buf, MarketRecipe recipe) { buf.writeUtf(recipe.defaults); buf.writeResourceLocation(recipe.category); PaymentImpl.toNetwork(buf, recipe.payment); + buf.writeVarInt(recipe.sortIndex); } } diff --git a/common/src/main/java/net/blay09/mods/farmingforblockheads/recipe/MarketRecipeDisplay.java b/common/src/main/java/net/blay09/mods/farmingforblockheads/recipe/MarketRecipeDisplay.java index ddb0e7a..603f27b 100644 --- a/common/src/main/java/net/blay09/mods/farmingforblockheads/recipe/MarketRecipeDisplay.java +++ b/common/src/main/java/net/blay09/mods/farmingforblockheads/recipe/MarketRecipeDisplay.java @@ -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 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 STREAM_CODEC = StreamCodec.composite(SlotDisplay.STREAM_CODEC, @@ -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);