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 21, 2024
1 parent 0599404 commit fa33d40
Show file tree
Hide file tree
Showing 11 changed files with 164 additions and 151 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ public ItemStack restore(ItemStack itemStack) {
private record MilkJarItemProvider(MilkJarBlockEntity milkJar) implements KitchenItemProvider {
@Override
public IngredientToken findIngredient(Ingredient ingredient, Collection<IngredientToken> ingredientTokens, CacheHint cacheHint) {
for (final var itemStack : ingredient.getItems()) {
final var found = findIngredient(itemStack, ingredientTokens, cacheHint);
for (final var item : ingredient.items()) {
final var found = findIngredient(new ItemStack(item), ingredientTokens, cacheHint);
if (found != null) {
return found;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ public ItemStack getSmeltingResult(ItemStack itemStack) {
}

public <T extends RecipeInput> ItemStack getSmeltingResult(RecipeType<? extends Recipe<T>> recipeType, T container) {
RecipeHolder<?> recipe = level.getRecipeManager().getRecipeFor(recipeType, container, this.level).orElse(null);
RecipeHolder<?> recipe = level.getServer().getRecipeManager().getRecipeFor(recipeType, container, this.level).orElse(null);
if (recipe != null) {
final var result = recipe.value().getResultItem(level.registryAccess());
final var result = recipe.value().assemble(level.registryAccess());
if (!result.isEmpty() && result.has(DataComponents.FOOD)) {
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ public ItemStack restore(ItemStack itemStack) {
private record SinkItemProvider(SinkBlockEntity sink) implements KitchenItemProvider {
@Override
public IngredientToken findIngredient(Ingredient ingredient, Collection<IngredientToken> ingredientTokens, CacheHint cacheHint) {
for (final var itemStack : ingredient.getItems()) {
final var found = findIngredient(itemStack, ingredientTokens, cacheHint);
for (final var item : ingredient.items()) {
final var found = findIngredient(new ItemStack(item), ingredientTokens, cacheHint);
if (found != null) {
return found;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import net.minecraft.core.component.DataComponents;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.network.chat.Component;
import net.minecraft.server.MinecraftServer;
import net.minecraft.sounds.SoundSource;
import net.minecraft.util.Unit;
import net.minecraft.world.Container;
Expand Down Expand Up @@ -153,19 +154,13 @@ public boolean isBurningToast() {
}

private ItemStack toastItem(ItemStack itemStack) {
// TODO fire a toaster event so addons can add custom handling
final var recipeInput = new SingleRecipeInput(itemStack);
final var toastRecipes = level.getRecipeManager().getRecipesFor(ModRecipes.toasterRecipeType, recipeInput, level);
for (final var toastRecipe : toastRecipes) {
final var assembled = toastRecipe.value().assemble(recipeInput, level.registryAccess());
if (!assembled.isEmpty()) {
return assembled;
}
}
if (itemStack.is(Items.BREAD)) {
return toastBread(itemStack);
final var toastRecipe = level.getServer().getRecipeManager().getRecipeFor(ModRecipes.toasterRecipeType, recipeInput, level);
final var outputItem = toastRecipe.map(recipeHolder -> recipeHolder.value().assemble(recipeInput, level.registryAccess())).orElse(itemStack);
if (outputItem.is(Items.BREAD)) {
return toastBread(outputItem);
} else {
return itemStack;
return outputItem;
}
}

Expand All @@ -188,8 +183,12 @@ private ItemStack toastBread(ItemStack itemStack) {
}

public boolean canToast(ItemStack itemStack) {
// TODO not sure how to best do this one with the toaster event
return level.getRecipeManager()
final var server = level.getServer();
if (server == null) {
return false;
}

return server.getRecipeManager()
.getRecipeFor(ModRecipes.toasterRecipeType, new SingleRecipeInput(itemStack), level)
.map(it -> true)
.orElseGet(() -> itemStack.is(Items.BREAD));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,18 @@ public CraftingOperation prepare() {
final var recipeMapper = CookingForBlockheadsAPI.getKitchenRecipeHandler(recipe);
final var ingredients = recipeMapper.getIngredients(recipe);
for (int i = 0; i < ingredients.size(); i++) {
final var ingredient = ingredients.get(i);
if (ingredient.isEmpty()) {
if (ingredients.get(i).isEmpty()) {
ingredientTokens.add(IngredientToken.EMPTY);
continue;
}

final var ingredient = ingredients.get(i).get();
final var lockedInput = lockedInputs != null ? lockedInputs.get(i) : ItemStack.EMPTY;
final var ingredientToken = accountForIngredient(ingredient, lockedInput);
if (ingredientToken != null) {
if (ingredient.getItems().length > 1) {
if (ingredient.items().size() > 1) {
if (lockedInputs == null) {
lockedInputs = NonNullList.withSize(recipe.getIngredients().size(), ItemStack.EMPTY);
lockedInputs = NonNullList.withSize(ingredients.size(), ItemStack.EMPTY);
}
lockedInputs.set(i, ingredientToken.peek());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,129 +2,117 @@

import net.blay09.mods.cookingforblockheads.block.ModBlocks;
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.Container;
import net.minecraft.world.entity.player.Inventory;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.player.StackedContents;
import net.minecraft.world.inventory.*;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.*;
import net.minecraft.world.level.Level;

public class CuttingBoardMenu extends RecipeBookMenu {
private final CraftingContainer craftSlots = new TransientCraftingContainer(this, 3, 3);
private final ResultContainer resultSlots = new ResultContainer();
import javax.annotation.Nullable;
import java.util.List;

public class CuttingBoardMenu extends AbstractCraftingMenu {
private static final int CRAFTING_GRID_WIDTH = 3;
private static final int CRAFTING_GRID_HEIGHT = 3;
public static final int RESULT_SLOT = 0;
private static final int CRAFT_SLOT_START = 1;
private static final int CRAFT_SLOT_END = 10;
private static final int INV_SLOT_START = 10;
private static final int INV_SLOT_END = 37;
private static final int USE_ROW_SLOT_START = 37;
private static final int USE_ROW_SLOT_END = 46;

private final ContainerLevelAccess access;
private final Player player;
private boolean placingRecipe;

public CuttingBoardMenu(int windowId, Inventory inventory) {
this(windowId, inventory, ContainerLevelAccess.NULL);
}

public CuttingBoardMenu(int windowId, Inventory inventory, ContainerLevelAccess access) {
super(MenuType.CRAFTING, windowId);
public CuttingBoardMenu(int containerId, Inventory inventory, ContainerLevelAccess access) {
super(ModMenus.cuttingBoard.get(), containerId, CRAFTING_GRID_WIDTH, CRAFTING_GRID_HEIGHT);
this.access = access;
this.player = inventory.player;
addSlot(new ResultSlot(inventory.player, craftSlots, resultSlots, 0, 124, 35));

for (int x = 0; x < 3; x++) {
for (int y = 0; y < 3; y++) {
addSlot(new Slot(craftSlots, y + x * 3, 30 + y * 18, 17 + x * 18));
}
}

for (int x = 0; x < 3; x++) {
for (int y = 0; y < 9; y++) {
addSlot(new Slot(inventory, y + x * 9 + 9, 8 + y * 18, 84 + x * 18));
addResultSlot(player, 124, 35);
addCraftingGridSlots(30, 17);
addStandardInventorySlots(inventory, 8, 84);
}

protected static void slotChangedCraftingGrid(AbstractContainerMenu menu, ServerLevel level, Player player, CraftingContainer craftingContainer, ResultContainer resultContainer, @Nullable RecipeHolder<CraftingRecipe> recipeHolder) {
final var craftInput = craftingContainer.asCraftInput();
final var serverPlayer = (ServerPlayer) player;
var resultStack = ItemStack.EMPTY;
final var foundRecipe = level.getServer()
.getRecipeManager()
.getRecipeFor(RecipeType.CRAFTING, craftInput, level, recipeHolder);
if (foundRecipe.isPresent()) {
final var foundRecipeHolder = foundRecipe.get();
final var craftingRecipe = foundRecipeHolder.value();
if (resultContainer.setRecipeUsed(serverPlayer, foundRecipeHolder)) {
final var assembledStack = craftingRecipe.assemble(craftInput, level.registryAccess());
if (assembledStack.isItemEnabled(level.enabledFeatures())) {
resultStack = assembledStack;
}
}
}

for (int i = 0; i < 9; i++) {
addSlot(new Slot(inventory, i, 8 + i * 18, 142));
}
resultContainer.setItem(0, resultStack);
menu.setRemoteSlot(RESULT_SLOT, resultStack);
serverPlayer.connection.send(new ClientboundContainerSetSlotPacket(menu.containerId, menu.incrementStateId(), RESULT_SLOT, resultStack));
}

protected static void slotChangedCraftingGrid(AbstractContainerMenu menu, Level level, Player player, CraftingContainer craftingContainer, ResultContainer resultContainer) {
if (!level.isClientSide) {
final var recipeInput = craftingContainer.asCraftInput();
final var serverPlayer = (ServerPlayer) player;
var itemStack = ItemStack.EMPTY;
final var optionalRecipe = level.getServer().getRecipeManager().getRecipeFor(RecipeType.CRAFTING, recipeInput, level);
if (optionalRecipe.isPresent()) {
final var recipeHolder = optionalRecipe.get();
final var recipe = recipeHolder.value();
if (resultContainer.setRecipeUsed(serverPlayer, recipeHolder)) {
final var assembledStack = recipe.assemble(recipeInput, level.registryAccess());
if (assembledStack.isItemEnabled(level.enabledFeatures())) {
itemStack = assembledStack;
}
public void slotsChanged(Container container) {
if (!placingRecipe) {
access.execute((level, pos) -> {
if (level instanceof ServerLevel serverlevel) {
slotChangedCraftingGrid(this, serverlevel, player, craftSlots, resultSlots, null);
}
}

resultContainer.setItem(0, itemStack);
menu.setRemoteSlot(0, itemStack);
serverPlayer.connection.send(new ClientboundContainerSetSlotPacket(menu.containerId, menu.incrementStateId(), 0, itemStack));
});
}
}

@Override
public void slotsChanged(Container container) {
access.execute((level, pos) -> slotChangedCraftingGrid(this, level, player, craftSlots, resultSlots));
}

@Override
public void fillCraftSlotsStackedContents(StackedContents stackedContents) {
craftSlots.fillStackedContents(stackedContents);
public void beginPlacingRecipe() {
this.placingRecipe = true;
}

@Override
public void clearCraftingContent() {
craftSlots.clearContent();
resultSlots.clearContent();
public void finishPlacingRecipe(ServerLevel level, RecipeHolder<CraftingRecipe> recipeHolder) {
this.placingRecipe = false;
slotChangedCraftingGrid(this, level, player, craftSlots, resultSlots, recipeHolder);
}

@Override
public boolean recipeMatches(RecipeHolder<CraftingRecipe> recipe) {
return recipe.value().matches(craftSlots.asCraftInput(), player.level());
}

@Override
public void removed(Player player) {
super.removed(player);
access.execute((level, pos) -> clearContainer(player, craftSlots));
this.access.execute((level, pos) -> clearContainer(player, craftSlots));
}

@Override
public boolean stillValid(Player player) {
return stillValid(access, player, ModBlocks.cuttingBoard);
}

@Override
public ItemStack quickMoveStack(Player player, int slotId) {
var itemStack = ItemStack.EMPTY;
final var slot = slots.get(slotId);
public ItemStack quickMoveStack(Player player, int index) {
var itemstack = ItemStack.EMPTY;
final var slot = slots.get(index);
if (slot != null && slot.hasItem()) {
final var slotStack = slot.getItem();
itemStack = slotStack.copy();
if (slotId == 0) {
this.access.execute(($$2x, $$3x) -> slotStack.getItem().onCraftedBy(slotStack, $$2x, player));
if (!moveItemStackTo(slotStack, 10, 46, true)) {
itemstack = slotStack.copy();
if (index == RESULT_SLOT) {
access.execute((level, pos) -> slotStack.getItem().onCraftedBy(slotStack, level, player));
if (!moveItemStackTo(slotStack, INV_SLOT_START, USE_ROW_SLOT_END, true)) {
return ItemStack.EMPTY;
}

slot.onQuickCraft(slotStack, itemStack);
} else if (slotId >= 10 && slotId < 46) {
if (!moveItemStackTo(slotStack, 1, 10, false)) {
if (slotId < 37) {
if (!moveItemStackTo(slotStack, 37, 46, false)) {
slot.onQuickCraft(slotStack, itemstack);
} else if (index >= INV_SLOT_START && index < USE_ROW_SLOT_END) {
if (!moveItemStackTo(slotStack, CRAFT_SLOT_START, CRAFT_SLOT_END, false)) {
if (index < INV_SLOT_END) {
if (!moveItemStackTo(slotStack, USE_ROW_SLOT_START, USE_ROW_SLOT_END, false)) {
return ItemStack.EMPTY;
}
} else if (!moveItemStackTo(slotStack, 10, 37, false)) {
} else if (!moveItemStackTo(slotStack, INV_SLOT_START, INV_SLOT_END, false)) {
return ItemStack.EMPTY;
}
}
} else if (!moveItemStackTo(slotStack, 10, 46, false)) {
} else if (!moveItemStackTo(slotStack, INV_SLOT_START, USE_ROW_SLOT_END, false)) {
return ItemStack.EMPTY;
}

Expand All @@ -134,51 +122,37 @@ public ItemStack quickMoveStack(Player player, int slotId) {
slot.setChanged();
}

if (slotStack.getCount() == itemStack.getCount()) {
if (slotStack.getCount() == itemstack.getCount()) {
return ItemStack.EMPTY;
}

slot.onTake(player, slotStack);
if (slotId == 0) {
if (index == RESULT_SLOT) {
player.drop(slotStack, false);
}
}

return itemStack;
return itemstack;
}

@Override
public boolean canTakeItemForPickAll(ItemStack itemStack, Slot slot) {
return slot.container != resultSlots && super.canTakeItemForPickAll(itemStack, slot);
}

@Override
public int getResultSlotIndex() {
return 0;
}

@Override
public int getGridWidth() {
return craftSlots.getWidth();
}

@Override
public int getGridHeight() {
return craftSlots.getHeight();
public Slot getResultSlot() {
//noinspection SequencedCollectionMethodCanBeUsed
return this.slots.get(RESULT_SLOT);
}

@Override
public int getSize() {
return 10;
public List<Slot> getInputGridSlots() {
return this.slots.subList(CRAFT_SLOT_START, CRAFT_SLOT_END);
}

@Override
public RecipeBookType getRecipeBookType() {
return RecipeBookType.CRAFTING;
}

@Override
public boolean shouldMoveToInventory(int slot) {
return slot != getResultSlotIndex();
protected Player owner() {
return this.player;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import net.blay09.mods.balm.api.recipe.BalmRecipes;
import net.blay09.mods.cookingforblockheads.CookingForBlockheads;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.item.crafting.RecipeBookCategory;
import net.minecraft.world.item.crafting.RecipeSerializer;
import net.minecraft.world.item.crafting.RecipeType;

Expand All @@ -13,10 +14,13 @@ public class ModRecipes {
public static final ResourceLocation TOASTER_RECIPE_ID = ResourceLocation.fromNamespaceAndPath(CookingForBlockheads.MOD_ID, TOASTER_RECIPE_GROUP);
public static final ResourceLocation OVEN_RECIPE_ID = ResourceLocation.fromNamespaceAndPath(CookingForBlockheads.MOD_ID, OVEN_RECIPE_GROUP);

public static RecipeBookCategory toasterRecipeBookCategory;
public static RecipeType<ToasterRecipe> toasterRecipeType;
public static RecipeSerializer<OvenRecipe> ovenRecipeSerializer;
public static RecipeSerializer<ToasterRecipe> toasterRecipeSerializer;

public static RecipeBookCategory ovenRecipeBookCategory;
public static RecipeType<OvenRecipe> ovenRecipeType;
public static RecipeSerializer<OvenRecipe> ovenRecipeSerializer;

public static void initialize(BalmRecipes registry) {
registry.registerRecipeType(() -> toasterRecipeType = new RecipeType<>() {
Expand Down
Loading

0 comments on commit fa33d40

Please sign in to comment.