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 20, 2024
1 parent 51bfefc commit 9e25ae9
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 38 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,12 @@
package net.blay09.mods.farmingforblockheads.client;

import net.blay09.mods.balm.api.Balm;
import net.blay09.mods.balm.api.client.BalmClient;
import net.blay09.mods.balm.api.event.client.ItemTooltipEvent;
import net.blay09.mods.balm.mixin.AbstractContainerScreenAccessor;
import net.blay09.mods.farmingforblockheads.client.gui.screen.MarketScreen;
import net.blay09.mods.farmingforblockheads.menu.MarketResultSlot;
import net.blay09.mods.farmingforblockheads.menu.MarketListingSlot;
import net.blay09.mods.farmingforblockheads.recipe.MarketRecipeDisplay;
import net.minecraft.client.Minecraft;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;

public class FarmingForBlockheadsClient {

public static void initialize() {
ModScreens.initialize(BalmClient.getScreens());
ModRenderers.initialize(BalmClient.getRenderers());

Balm.getEvents().onEvent(ItemTooltipEvent.class, event -> {
// TODO if (Minecraft.getInstance().screen instanceof MarketScreen screen) {
// TODO Slot hoverSlot = ((AbstractContainerScreenAccessor) screen).getHoveredSlot();
// TODO if (hoverSlot != null && event.getItemStack() == hoverSlot.getItem()) {
// TODO RecipeDisplayEntry hoverRecipe = null;
// TODO
// TODO if (hoverSlot instanceof MarketListingSlot marketListingSlot) {
// TODO hoverRecipe = marketListingSlot.getRecipeDisplayEntry();
// TODO } else if (hoverSlot instanceof MarketResultSlot) {
// TODO hoverRecipe = screen.getMenu().getSelectedRecipe();
// TODO }
// TODO
// TODO if (hoverRecipe != null && hoverRecipe.display() instanceof MarketRecipeDisplay marketRecipeDisplay) {
// TODO // TODO final var paymentComponent = payment.tooltip().orElseGet(() -> FarmingForBlockheads.getDefaultPaymentComponent(payment));
// TODO // TODO final var tooltipComponent = Component.translatable("tooltip.farmingforblockheads.payment", paymentComponent)
// TODO // TODO .withStyle(ChatFormatting.GREEN);
// TODO // TODO event.getToolTip().add(tooltipComponent);
// TODO }
// TODO }
// TODO }
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,10 @@ public RecipeBookMenu.PostPlaceAction handlePlacement(boolean useMaxItems, boole

RecipeBookMenu.PostPlaceAction postPlaceAction;
try {
final var paymentSlots = List.of(getPaymentSlot());
postPlaceAction = ServerPlaceRecipe.placeRecipe(new ServerPlaceRecipe.CraftingMenuAccess<>() {
postPlaceAction = ServerPlaceMarketRecipe.placeRecipe(new ServerPlaceRecipe.CraftingMenuAccess<>() {
@Override
public void fillCraftSlotsStackedContents(StackedItemContents stackedItemContents) {
MarketMenu.this.fillCraftSlotsStackedContents(stackedItemContents);
MarketMenu.this.fillPaymentSlotsStackedContents(stackedItemContents);
}

@Override
Expand All @@ -144,15 +143,15 @@ public void clearCraftingContent() {
public boolean recipeMatches(RecipeHolder<MarketRecipe> recipe) {
return recipe.value().matches(MarketMenu.this.paymentSlots.asRecipeInput(), MarketMenu.this.owner().level());
}
}, 1, 1, paymentSlots, paymentSlots, inventory, recipeHolder, useMaxItems, creative);
}, getPaymentSlot(), inventory, recipeHolder, useMaxItems, creative);
} finally {
finishPlacingRecipe(level, recipeHolder);
}

return postPlaceAction;
}

private void fillCraftSlotsStackedContents(StackedItemContents stackedItemContents) {
private void fillPaymentSlotsStackedContents(StackedItemContents stackedItemContents) {
paymentSlots.fillStackedContents(stackedItemContents);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package net.blay09.mods.farmingforblockheads.menu;

import net.blay09.mods.farmingforblockheads.recipe.MarketRecipe;
import net.blay09.mods.farmingforblockheads.registry.MarketDefaultsRegistry;
import net.minecraft.core.Holder;
import net.minecraft.recipebook.ServerPlaceRecipe;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.StackedItemContents;
import net.minecraft.world.inventory.RecipeBookMenu;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeHolder;

import java.util.ArrayList;

public class ServerPlaceMarketRecipe {
private final ServerPlaceRecipe.CraftingMenuAccess<MarketRecipe> menuAccess;
private final Slot paymentSlot;
private final Inventory inventory;
private final RecipeHolder<MarketRecipe> recipeHolder;
private final boolean useMaxItems;
private final boolean creative;

public ServerPlaceMarketRecipe(ServerPlaceRecipe.CraftingMenuAccess<MarketRecipe> menuAccess, Slot paymentSlot, Inventory inventory, RecipeHolder<MarketRecipe> recipeHolder, boolean useMaxItems, boolean creative) {
this.menuAccess = menuAccess;
this.paymentSlot = paymentSlot;
this.recipeHolder = recipeHolder;
this.inventory = inventory;
this.useMaxItems = useMaxItems;
this.creative = creative;
}

public static RecipeBookMenu.PostPlaceAction placeRecipe(
ServerPlaceRecipe.CraftingMenuAccess<MarketRecipe> access, Slot paymentSlot, Inventory inventory, RecipeHolder<MarketRecipe> recipeHolder, boolean useMaxItems, boolean creative) {
final var serverPlaceMarketRecipe = new ServerPlaceMarketRecipe(access, paymentSlot, inventory, recipeHolder, useMaxItems, creative);
if (!creative && !serverPlaceMarketRecipe.testClearPayment()) {
return RecipeBookMenu.PostPlaceAction.NOTHING;
} else {
final var stackedItemContents = new StackedItemContents();
inventory.fillStackedContents(stackedItemContents);
access.fillCraftSlotsStackedContents(stackedItemContents);
return serverPlaceMarketRecipe.tryPlaceRecipe(recipeHolder, stackedItemContents);
}
}

private RecipeBookMenu.PostPlaceAction tryPlaceRecipe(RecipeHolder<MarketRecipe> recipeHolder, StackedItemContents stackedItemContents) {
if (stackedItemContents.canCraft(recipeHolder.value(), null)) {
placeRecipe(recipeHolder, stackedItemContents);
inventory.setChanged();
return RecipeBookMenu.PostPlaceAction.NOTHING;
} else {
clearPayment();
inventory.setChanged();
return RecipeBookMenu.PostPlaceAction.PLACE_GHOST_RECIPE;
}
}

private void placeRecipe(RecipeHolder<MarketRecipe> recipeHolder, StackedItemContents stackedItemContents) {
if (!menuAccess.recipeMatches(recipeHolder)) {
clearPayment();
}

final var itemsToUse = new ArrayList<Holder<Item>>();
if (stackedItemContents.canCraft(recipeHolder.value(), itemsToUse::add)) {
final var effectivePayment = MarketDefaultsRegistry.resolvePayment(recipeHolder.value());
var paymentSlotItem = paymentSlot.getItem();
final var desiredCount = Math.min(paymentSlot.getMaxStackSize(), useMaxItems
? stackedItemContents.getBiggestCraftableStack(recipeHolder.value(), null) * effectivePayment.count()
: paymentSlotItem.getCount() + effectivePayment.count());
final var itemToUse = itemsToUse.getFirst();
while (paymentSlotItem.getCount() < desiredCount) {
final var desiredAddition = desiredCount - paymentSlotItem.getCount();
final var foundSlot = inventory.findSlotMatchingCraftingIngredient(itemToUse);
if (foundSlot == -1) {
break;
}

final var slotItem = inventory.getItem(foundSlot);
var toTake = Math.min(slotItem.getCount(), desiredAddition);
if (paymentSlotItem.isEmpty()) {
paymentSlot.set(slotItem.split(toTake));
paymentSlotItem = paymentSlot.getItem();
} else if (ItemStack.isSameItemSameComponents(paymentSlotItem, slotItem)) {
toTake = Math.min(toTake, paymentSlotItem.getMaxStackSize() - paymentSlotItem.getCount());
slotItem.shrink(toTake);
paymentSlotItem.grow(toTake);
} else {
break;
}
}
}
}

private boolean testClearPayment() {
final var restItem = paymentSlot.getItem().copy();
if (restItem.isEmpty()) {
return true;
}

for (int i = 0; i < inventory.getContainerSize(); i++) {
final var slotItem = inventory.getItem(i);
if (slotItem.isEmpty()) {
return true;
} else if (ItemStack.isSameItemSameComponents(restItem, slotItem)) {
final var availableSpace = slotItem.getMaxStackSize() - slotItem.getCount();
if (availableSpace > 0) {
restItem.shrink(availableSpace);
}
if (restItem.isEmpty()) {
return true;
}
}
}

return restItem.isEmpty();
}

private void clearPayment() {
final var itemstack = paymentSlot.getItem().copy();
inventory.placeItemBackInInventory(itemstack, false);
paymentSlot.set(itemstack);

menuAccess.clearCraftingContent();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ public RecipeType<? extends Recipe<RecipeInput>> getType() {
@Override
public PlacementInfo placementInfo() {
final var effectivePayment = MarketDefaultsRegistry.resolvePayment(this);
return PlacementInfo.create(effectivePayment.ingredient());
final var ingredients = new ArrayList<Ingredient>();
for (int i = 0; i < effectivePayment.count(); i++) {
ingredients.add(effectivePayment.ingredient());
}
return PlacementInfo.create(ingredients);
}

@Override
Expand Down

0 comments on commit 9e25ae9

Please sign in to comment.