-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wip: Continue port to Minecraft 1.21.2
- Loading branch information
1 parent
51bfefc
commit 9e25ae9
Showing
4 changed files
with
135 additions
and
38 deletions.
There are no files selected for viewing
32 changes: 0 additions & 32 deletions
32
...src/main/java/net/blay09/mods/farmingforblockheads/client/FarmingForBlockheadsClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 } | ||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
common/src/main/java/net/blay09/mods/farmingforblockheads/menu/ServerPlaceMarketRecipe.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters