Skip to content

Commit

Permalink
wip: Continue port to Minecraft 1.21.2
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Oct 13, 2024
1 parent 2623b11 commit 9eae29f
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import net.blay09.mods.farmingforblockheads.network.ModNetworking;
import net.blay09.mods.farmingforblockheads.recipe.ModRecipes;
import net.blay09.mods.farmingforblockheads.registry.MarketCategoryLoader;
import net.blay09.mods.farmingforblockheads.registry.MarketCategoryRegistry;
import net.blay09.mods.farmingforblockheads.registry.MarketPresetLoader;
import net.blay09.mods.farmingforblockheads.sound.ModSounds;
import net.minecraft.network.chat.Component;
Expand Down Expand Up @@ -42,7 +41,6 @@ public static void initialize() {
Balm.addServerReloadListener(ResourceLocation.fromNamespaceAndPath(MOD_ID, "market_category_loader"), new MarketCategoryLoader());
Balm.addServerReloadListener(ResourceLocation.fromNamespaceAndPath(MOD_ID, "market_preset_loader"), new MarketPresetLoader());

Balm.getEvents().onEvent(PlayerLoginEvent.class, MarketCategoryRegistry.INSTANCE::onLogin);
Balm.getEvents().onEvent(CropGrowEvent.Post.class, FarmlandHandler::onGrowEvent);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,8 @@ protected InteractionResult useItemOn(ItemStack itemStack, BlockState state, Lev

private void use(BlockState state, Level level, BlockPos pos, Player player) {
if (!level.isClientSide) {
BlockEntity tileEntity = level.getBlockEntity(pos);
if (tileEntity instanceof MarketBlockEntity market) {
Balm.getNetworking().openGui(player, market);
if (level.getBlockEntity(pos) instanceof MarketBlockEntity market) {
market.openMenu(player);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package net.blay09.mods.farmingforblockheads.block.entity;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.menu.BalmMenuProvider;
import net.blay09.mods.balm.common.BalmBlockEntity;
import net.blay09.mods.farmingforblockheads.menu.MarketMenu;
Expand Down Expand Up @@ -39,4 +40,8 @@ public BlockPos getScreenOpeningData(ServerPlayer serverPlayer) {
public StreamCodec<RegistryFriendlyByteBuf, BlockPos> getScreenStreamCodec() {
return BlockPos.STREAM_CODEC.cast();
}

public void openMenu(Player player) {
Balm.getNetworking().openGui(player, this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ public static AttributeSupplier.Builder createAttributes() {

@Override
protected InteractionResult mobInteract(Player player, InteractionHand hand) {
MarketBlockEntity market = getMarketTileEntity();
final var market = getMarketBlockEntity();
if (market != null) {
Balm.getNetworking().openGui(player, market);
market.openMenu(player);
return InteractionResult.SUCCESS;
}

Expand Down Expand Up @@ -279,7 +279,7 @@ public boolean isAtMarket() {
}

@Nullable
private MarketBlockEntity getMarketTileEntity() {
private MarketBlockEntity getMarketBlockEntity() {
if (marketPos == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
import net.blay09.mods.farmingforblockheads.network.MarketPutInBasketMessage;
import net.blay09.mods.farmingforblockheads.recipe.MarketRecipe;
import net.blay09.mods.farmingforblockheads.recipe.MarketRecipeDisplay;
import net.blay09.mods.farmingforblockheads.registry.MarketCategoryRegistry;
import net.blay09.mods.farmingforblockheads.registry.SimpleHolder;
import net.minecraft.core.BlockPos;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
Expand All @@ -37,8 +37,8 @@ public class MarketMenu extends AbstractContainerMenu {
private final List<MarketListingSlot> marketSlots = new ArrayList<>();
private final MarketPaymentSlot paymentSlot;

private List<SimpleHolder<MarketCategory>> categories;
private List<RecipeDisplayEntry> recipes;
private List<SimpleHolder<MarketCategory>> categories = List.of();
private List<RecipeDisplayEntry> recipes = List.of();

private String currentSearch;
private SimpleHolder<MarketCategory> currentCategory;
Expand Down Expand Up @@ -378,11 +378,14 @@ public void setCategories(List<SimpleHolder<MarketCategory>> categories) {
this.categories = categories;
}

public Comparator<RecipeDisplayEntry> sorting(Level level) {
private Optional<MarketCategory> resolveMarketCategory(ResourceLocation identifier) {
return categories.stream().filter(it -> it.id().equals(identifier)).findFirst().map(SimpleHolder::value);
}

private Comparator<RecipeDisplayEntry> sorting(Level level) {
final var contextMap = SlotDisplayContext.fromLevel(player.level());
return Comparator.comparingInt(
(RecipeDisplayEntry recipe) -> recipe.display() instanceof MarketRecipeDisplay marketRecipeDisplay ? MarketCategoryRegistry.INSTANCE.get(
marketRecipeDisplay.category())
(RecipeDisplayEntry recipe) -> recipe.display() instanceof MarketRecipeDisplay marketRecipeDisplay ? resolveMarketCategory(marketRecipeDisplay.category())
.map(MarketCategory::sortIndex)
.orElse(0) : 0)
.thenComparing(recipe -> recipe.display().result().resolveForFirstStack(contextMap)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import net.blay09.mods.farmingforblockheads.FarmingForBlockheads;
import net.blay09.mods.farmingforblockheads.api.MarketCategory;
import net.blay09.mods.farmingforblockheads.registry.MarketCategoryImpl;
import net.blay09.mods.farmingforblockheads.registry.MarketCategoryRegistry;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.chat.ComponentSerialization;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
Expand Down Expand Up @@ -50,7 +49,7 @@ public static void encode(RegistryFriendlyByteBuf buf, MarketCategoriesMessage m
}

public static void handle(Player player, MarketCategoriesMessage message) {
MarketCategoryRegistry.INSTANCE.load(message.categories);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,4 @@ public void load(Map<ResourceLocation, MarketCategory> categories) {
this.categories.clear();
categories.forEach(this::register);
}

public void onLogin(PlayerLoginEvent event) {
Balm.getNetworking().sendTo(event.getPlayer(), new MarketCategoriesMessage(categories));
}
}

0 comments on commit 9eae29f

Please sign in to comment.