Skip to content

Commit

Permalink
wip: Technically works but not great so gonna refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
BlayTheNinth committed Oct 19, 2024
1 parent 666d007 commit acacdb6
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;

import java.util.Optional;

public class MarketBasketSlot extends Slot {

private final MarketMenu menu;
Expand Down Expand Up @@ -32,4 +34,31 @@ public void onTake(Player player, ItemStack stack) {
}
}

@Override
public ItemStack getItem() {
return super.getItem().copy();
}

@Override
public void setByPlayer(ItemStack pStack) {
}

@Override
public void setByPlayer(ItemStack pNewStack, ItemStack pOldStack) {
}

@Override
public ItemStack remove(int pAmount) {
return super.getItem().copyWithCount(pAmount);
}

@Override
public ItemStack safeTake(int pCount, int pDecrement, Player pPlayer) {
return super.getItem().copyWithCount(pCount);
}

@Override
public Optional<ItemStack> tryRemove(int pCount, int pDecrement, Player pPlayer) {
return super.tryRemove(pCount, pDecrement, pPlayer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import net.blay09.mods.farmingforblockheads.registry.MarketDefaultsRegistry;
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;
Expand All @@ -20,6 +19,7 @@
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.minecraft.world.item.crafting.SingleRecipeInput;
import net.minecraft.world.item.crafting.display.RecipeDisplayEntry;
import net.minecraft.world.item.crafting.display.RecipeDisplayId;
import net.minecraft.world.item.crafting.display.SlotDisplayContext;
Expand All @@ -37,6 +37,7 @@ public class MarketMenu extends AbstractContainerMenu {
private final DefaultContainer marketOutputBuffer = new DefaultContainer(1);
private final List<MarketListingSlot> marketSlots = new ArrayList<>();
private final MarketPaymentSlot paymentSlot;
private final MarketBasketSlot basketSlot;

private List<SimpleHolder<MarketCategory>> categories = List.of();
private List<RecipeDisplayEntry> recipes = List.of();
Expand All @@ -58,7 +59,7 @@ public MarketMenu(int windowId, Inventory playerInventory, BlockPos pos) {
this.pos = pos;

addSlot(paymentSlot = new MarketPaymentSlot(marketInputBuffer, 0, 23, 39));
addSlot(new MarketBasketSlot(this, marketOutputBuffer, 0, 61, 39));
addSlot(basketSlot = new MarketBasketSlot(this, marketOutputBuffer, 0, 61, 39));

final var fakeInventory = new DefaultContainer(4 * 3);
for (int i = 0; i < 4; i++) {
Expand Down Expand Up @@ -116,7 +117,7 @@ public ItemStack quickMoveStack(Player player, int index) {
}

if (slotStack.isEmpty()) {
slot.set(ItemStack.EMPTY);
slot.setByPlayer(ItemStack.EMPTY);
} else {
slot.setChanged();
}
Expand Down Expand Up @@ -172,7 +173,7 @@ public void selectMarketEntry(RecipeDisplayId recipeDisplayId, boolean stack) {

final var recipe = resolveRecipe(selectedRecipe);
if (recipe != null) {
marketOutputBuffer.setItem(0, recipe.assemble(new TransientCraftingContainer(this, 0, 0).asCraftInput(), RegistryAccess.EMPTY));
basketSlot.set(recipe.assemble(new SingleRecipeInput(marketInputBuffer.getItem(0)), player.level().registryAccess()));
quickMovePayment(recipe, stack);
}
slotsChanged(marketInputBuffer);
Expand All @@ -183,7 +184,12 @@ private MarketRecipe resolveRecipe(@Nullable RecipeDisplayEntry recipeDisplayEnt
return null;
}

final var recipeManager = player.getServer().getRecipeManager();
final var server = player.getServer();
if (server == null) {
return null;
}

final var recipeManager = server.getRecipeManager();
var serverDisplayInfo = recipeManager.getRecipeFromDisplay(recipeDisplayEntry.id());
if (serverDisplayInfo != null) {
return (MarketRecipe) serverDisplayInfo.parent().value();
Expand Down Expand Up @@ -246,14 +252,16 @@ public boolean verifyPayment() {

@Override
public void slotsChanged(Container container) {
canBuy.set(verifyPayment() ? 1 : 0);
if (!player.level().isClientSide) {
canBuy.set(verifyPayment() ? 1 : 0);
}
}

public void onItemBought() {
final var recipe = resolveRecipe(selectedRecipe);
if (recipe != null) {
final var payment = MarketDefaultsRegistry.resolvePayment(recipe);
marketInputBuffer.removeItem(0, payment.count());
if (selectedRecipe.display() instanceof MarketRecipeDisplay marketRecipeDisplay) {
final var contextMap = SlotDisplayContext.fromLevel(player.level());
final var payment = marketRecipeDisplay.payment().resolveForFirstStack(contextMap);
marketInputBuffer.removeItem(0, payment.getCount());
slotsChanged(marketInputBuffer);
}
}
Expand Down

0 comments on commit acacdb6

Please sign in to comment.