-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add support for Vanila Recipe Book #56
- Loading branch information
1 parent
b8b4fbb
commit f9af830
Showing
15 changed files
with
243 additions
and
113 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
common/src/main/java/net/blay09/mods/craftingslots/CraftingSlots.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
4 changes: 2 additions & 2 deletions
4
common/src/main/java/net/blay09/mods/craftingslots/addon/CraftingTweaksAddon.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
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
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
17 changes: 12 additions & 5 deletions
17
common/src/main/java/net/blay09/mods/craftingslots/client/screen/PortableCraftingScreen.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
52 changes: 0 additions & 52 deletions
52
common/src/main/java/net/blay09/mods/craftingslots/container/CustomCraftingMenu.java
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
common/src/main/java/net/blay09/mods/craftingslots/item/InventoryCraftingItem.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
2 changes: 1 addition & 1 deletion
2
common/src/main/java/net/blay09/mods/craftingslots/item/PortableCraftingItem.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
124 changes: 124 additions & 0 deletions
124
common/src/main/java/net/blay09/mods/craftingslots/menu/CustomCraftingMenu.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,124 @@ | ||
package net.blay09.mods.craftingslots.menu; | ||
|
||
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket; | ||
import net.minecraft.recipebook.ServerPlaceRecipe; | ||
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.StackedItemContents; | ||
import net.minecraft.world.inventory.*; | ||
import net.minecraft.world.item.ItemStack; | ||
import net.minecraft.world.item.crafting.CraftingRecipe; | ||
import net.minecraft.world.item.crafting.RecipeHolder; | ||
import net.minecraft.world.item.crafting.RecipeType; | ||
|
||
import javax.annotation.Nullable; | ||
import java.util.List; | ||
|
||
public abstract class CustomCraftingMenu extends AbstractCraftingMenu { | ||
|
||
private static final int WIDTH = 3; | ||
private static final int HEIGHT = 3; | ||
private final Inventory playerInventory; | ||
private boolean placingRecipe; | ||
|
||
protected CustomCraftingMenu(MenuType<?> type, int id, Inventory playerInventory) { | ||
super(type, id, WIDTH, HEIGHT); | ||
this.playerInventory = playerInventory; | ||
} | ||
|
||
@Override | ||
public RecipeBookMenu.PostPlaceAction handlePlacement(boolean useMaxItems, boolean b, RecipeHolder<?> recipeHolder, ServerLevel level, Inventory inventory) { | ||
RecipeHolder<CraftingRecipe> craftingRecipeHolder = (RecipeHolder<CraftingRecipe>) recipeHolder; | ||
beginPlacingRecipe(); | ||
|
||
RecipeBookMenu.PostPlaceAction result; | ||
try { | ||
List<Slot> list = getInputGridSlots(); | ||
result = ServerPlaceRecipe.placeRecipe(new ServerPlaceRecipe.CraftingMenuAccess<>() { | ||
public void fillCraftSlotsStackedContents(StackedItemContents stackedItemContents) { | ||
CustomCraftingMenu.this.fillCraftSlotsStackedContents(stackedItemContents); | ||
} | ||
|
||
public void clearCraftingContent() { | ||
CustomCraftingMenu.this.getResultContainer().clearContent(); | ||
CustomCraftingMenu.this.getCraftingContainer().clearContent(); | ||
} | ||
|
||
public boolean recipeMatches(RecipeHolder<CraftingRecipe> recipeHolder) { | ||
return recipeHolder.value().matches(CustomCraftingMenu.this.getCraftingContainer().asCraftInput(), CustomCraftingMenu.this.owner().level()); | ||
} | ||
}, WIDTH, HEIGHT, list, list, inventory, craftingRecipeHolder, useMaxItems, b); | ||
} finally { | ||
finishPlacingRecipe(level, craftingRecipeHolder); | ||
} | ||
|
||
return result; | ||
} | ||
|
||
public abstract Slot getResultSlot(); | ||
|
||
public abstract List<Slot> getInputGridSlots(); | ||
|
||
protected abstract CraftingContainer getCraftingContainer(); | ||
|
||
protected abstract ResultContainer getResultContainer(); | ||
|
||
protected 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 itemStack = 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())) { | ||
itemStack = assembledStack; | ||
} | ||
} | ||
} | ||
|
||
resultContainer.setItem(getResultSlot().getContainerSlot(), itemStack); | ||
menu.setRemoteSlot(getResultSlot().index, itemStack); | ||
serverPlayer.connection.send(new ClientboundContainerSetSlotPacket(menu.containerId, menu.incrementStateId(), getResultSlot().index, itemStack)); | ||
} | ||
|
||
@Override | ||
public void slotsChanged(Container container) { | ||
if (!placingRecipe) { | ||
if (playerInventory.player.level() instanceof ServerLevel serverLevel) { | ||
slotChangedCraftingGrid(this, serverLevel, playerInventory.player, getCraftingContainer(), getResultContainer(), null); | ||
} | ||
} | ||
} | ||
|
||
public void beginPlacingRecipe() { | ||
this.placingRecipe = true; | ||
} | ||
|
||
public void finishPlacingRecipe(ServerLevel level, RecipeHolder<CraftingRecipe> recipeHolder) { | ||
this.placingRecipe = false; | ||
slotChangedCraftingGrid(this, level, playerInventory.player, getCraftingContainer(), getResultContainer(), recipeHolder); | ||
} | ||
|
||
@Override | ||
public boolean canTakeItemForPickAll(ItemStack itemStack, Slot slot) { | ||
return slot.container != getResultContainer() && super.canTakeItemForPickAll(itemStack, slot); | ||
} | ||
|
||
@Override | ||
public RecipeBookType getRecipeBookType() { | ||
return RecipeBookType.CRAFTING; | ||
} | ||
|
||
protected Player owner() { | ||
return playerInventory.player; | ||
} | ||
|
||
} |
5 changes: 1 addition & 4 deletions
5
...container/InventoryCraftingContainer.java → ...lots/menu/InventoryCraftingContainer.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
Oops, something went wrong.