Skip to content

Commit

Permalink
修复:配方书点击材料可以正常放置了
Browse files Browse the repository at this point in the history
  • Loading branch information
MCZME committed Aug 16, 2024
1 parent 1d7a086 commit d601007
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 1.21 2024-08-16T22:31:06.6934004 Recipes
// 1.21 2024-08-16T23:38:40.1170951 Recipes
30317ddefa8c34feeae2fb9b47b40dae9a2c1335 data/lingshi/advancement/recipes/skillet/rice_apple.json
60b5c5b8536660e2fc3758499991b48002d68196 data/lingshi/advancement/recipes/skillet/test01.json
24367c601743c0d2f9df76c4a1e59f6650f0de4a data/lingshi/advancement/recipes/skillet/test02.json
3e41620c4ec5b19f87046dad7fe5c7ed6ede21c2 data/lingshi/recipe/chopping_board/rice.json
c3a004889c418afc4a46152a5ff63cff2d17ad4a data/lingshi/recipe/skillet/rice_apple.json
1268fc69304435f6b1c56650c7e2f8fcc9fe7e6e data/lingshi/recipe/skillet/rice_apple.json
c80602ecff2ea09399aff222a6d36c7b28d0164f data/lingshi/recipe/skillet/test01.json
89bc0e33a11e930f7b69c4d726d92b4f9446b3d3 data/lingshi/recipe/skillet/test02.json
27 changes: 21 additions & 6 deletions src/main/java/mczme/lingshi/client/menu/SkilletMenu.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import net.neoforged.neoforge.items.ItemStackHandler;
import net.neoforged.neoforge.items.SlotItemHandler;

import java.util.ArrayList;
import java.util.List;

import static mczme.lingshi.client.recipebook.ModRecipeBookType.SKILLET;
Expand Down Expand Up @@ -46,7 +47,7 @@ private void addWorkSlot(List<ItemStack> itemStacks) {
for (int i = itemStacks.size(); i < blockEntity.getMAX(); i++)
itemStacks.add(ItemStack.EMPTY);
}
int[] X = {42, 60, 33, 51, 69, 98, 127};
int[] X = {42, 60, 33, 51, 69, 93, 127};
int[] Y = {29, 29, 47, 47, 47, 29, 47};
for (int i = 0; i < blockEntity.getMAX(); i++) {
SlotItemHandler slot = new SlotItemHandler(inventory, i, X[i], Y[i]);
Expand Down Expand Up @@ -123,18 +124,24 @@ public boolean stillValid(Player pPlayer) {

@Override
public void fillCraftSlotsStackedContents(StackedContents pItemHelper) {

for (ItemStack itemstack : blockEntity.getItemStacks()) {
pItemHelper.accountSimpleStack(itemstack);
}
}

@Override
public void clearCraftingContent() {
this.getSlot(0).set(ItemStack.EMPTY);
this.getSlot(1).set(ItemStack.EMPTY);
this.getSlot(2).set(ItemStack.EMPTY);
this.getSlot(3).set(ItemStack.EMPTY);
this.getSlot(4).set(ItemStack.EMPTY);
this.getSlot(5).set(ItemStack.EMPTY);
}

@Override
public boolean recipeMatches(RecipeHolder<SkilletRecipe> pRecipe) {
return true;
return pRecipe.value().matches(new SkilletRecipeInput(getInputSlotItem(),blockEntity.getFluid(),null), this.level);
}

@Override
Expand All @@ -144,12 +151,12 @@ public int getResultSlotIndex() {

@Override
public int getGridWidth() {
return 1;
return 5;
}

@Override
public int getGridHeight() {
return 7;
return 1;
}

@Override
Expand All @@ -164,6 +171,14 @@ public RecipeBookType getRecipeBookType() {

@Override
public boolean shouldMoveToInventory(int pSlotIndex) {
return pSlotIndex != 1;
return pSlotIndex != this.getResultSlotIndex();
}

public List<ItemStack>getInputSlotItem(){
List<ItemStack> inputSlotItem = new ArrayList<>();
for (int i = 0; i < 5; i++) {
inputSlotItem.add(this.getSlot(i).getItem());
}
return inputSlotItem;
}
}
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@
package mczme.lingshi.client.recipebook;

import mczme.lingshi.common.recipe.SkilletRecipe;
import net.minecraft.client.gui.screens.recipebook.RecipeBookComponent;
import net.minecraft.core.NonNullList;
import net.minecraft.world.inventory.Slot;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.Ingredient;
import net.minecraft.world.item.crafting.RecipeHolder;

import javax.annotation.Nullable;
import java.util.Iterator;
import java.util.List;

public class SkilletRecipeBookComponent extends RecipeBookComponent {

@Override
public void slotClicked(@Nullable Slot pSlot) {
super.slotClicked(pSlot);
if (pSlot != null && pSlot.index < this.menu.getSize()) {
this.ghostRecipe.clear();
}
}

@Override
public void setupGhostRecipe(RecipeHolder<?> pRecipe, List<Slot> pSlots) {
ItemStack itemstack = pRecipe.value().getResultItem(this.minecraft.level.registryAccess());
SkilletRecipe skilletRecipe = (SkilletRecipe) pRecipe.value();
ItemStack resultitem = pRecipe.value().getResultItem(this.minecraft.level.registryAccess());
this.ghostRecipe.setRecipe(pRecipe);
this.ghostRecipe.addIngredient(Ingredient.of(itemstack), pSlots.get(2).x, pSlots.get(2).y);
this.ghostRecipe.addIngredient(Ingredient.of(resultitem), pSlots.get(6).x, pSlots.get(6).y);
if(skilletRecipe.getContainer() != ItemStack.EMPTY){
this.ghostRecipe.addIngredient(Ingredient.of(skilletRecipe.getContainer()), pSlots.get(5).x, pSlots.get(5).y);
}

NonNullList<Ingredient> nonnulllist = pRecipe.value().getIngredients();

Iterator<Ingredient> iterator = nonnulllist.iterator();

for (int i = 0; i < 2; i++) {
for (int i = 0; i < 5; i++) {
if (!iterator.hasNext()) {
return;
}

Ingredient ingredient = iterator.next();
if (!ingredient.isEmpty()) {
Slot slot1 = pSlots.get(i);
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/mczme/lingshi/client/screen/SkilletScreen.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,15 @@ public SkilletScreen(SkilletMenu pMenu, Inventory pPlayerInventory, Component pT

@Override
public void init(){
super.init();
this.leftPos = (this.width - this.imageWidth) / 2;
this.topPos = (this.height - this.imageHeight) / 2;
this.widthTooNarrow = this.width < 379;
this.recipeBookComponent.init(this.width, this.height, this.minecraft, this.widthTooNarrow, this.menu);
this.leftPos = this.recipeBookComponent.updateScreenPosition(this.width, this.imageWidth);
this.addRenderableWidget(new ImageButton(this.leftPos + 20, this.height / 2 - 49, 20, 18, RecipeBookComponent.RECIPE_BUTTON_SPRITES, p_313431_ -> {
this.addRenderableWidget(new ImageButton(this.leftPos+6, this.height / 2 - 49, 20, 18, RecipeBookComponent.RECIPE_BUTTON_SPRITES, p_313431_ -> {
this.recipeBookComponent.toggleVisibility();
this.leftPos = this.recipeBookComponent.updateScreenPosition(this.width, this.imageWidth);
p_313431_.setPosition(this.leftPos + 20, this.height / 2 - 49);
p_313431_.setPosition(this.leftPos + 6, this.height / 2 - 49);
}));
this.titleLabelX = (this.imageWidth - this.font.width(this.title)) / 2;
}
Expand All @@ -57,7 +58,7 @@ public void render(GuiGraphics pGuiGraphics, int pMouseX, int pMouseY, float pPa

this.renderTooltip(pGuiGraphics, pMouseX, pMouseY);
this.recipeBookComponent.renderTooltip(pGuiGraphics, this.leftPos, this.topPos, pMouseX, pMouseY);
super.render(pGuiGraphics, pMouseX, pMouseY, pPartialTick);
// super.render(pGuiGraphics,pMouseX,pMouseY,pPartialTick);
}

@Override
Expand Down
9 changes: 7 additions & 2 deletions src/main/java/mczme/lingshi/common/block/SkilletBlock.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ protected MapCodec<? extends BaseEntityBlock> codec() {
public ItemInteractionResult useItemOn(ItemStack pStack, BlockState pState, Level pLevel, BlockPos pPos, Player pPlayer, InteractionHand pHand, BlockHitResult pHitResult) {
if(pLevel.getBlockEntity(pPos) instanceof SkilletBlockEntity blockEntity){
if(!blockEntity.isFull() && !pStack.isEmpty()){
blockEntity.setItem(pStack);
pStack.consume(pStack.getCount(),pPlayer);
blockEntity.setItem(pStack.consumeAndReturn(1, pPlayer));
blockEntity.setChanged();
return ItemInteractionResult.SUCCESS;
}
Expand Down Expand Up @@ -84,6 +83,12 @@ public BlockEntity newBlockEntity(BlockPos pPos, BlockState pState) {
return new SkilletBlockEntity(pPos, pState);
}

// @Nullable
// @Override
// public <T extends BlockEntity> BlockEntityTicker<T> getTicker(Level level, BlockState state, BlockEntityType<T> type) {
// return type == BlockEntityTypes.SKILLET_BLOCKENTITY ? SkilletBlockEntity::tick : null;
// }

@Override
protected VoxelShape getShape(BlockState pState, BlockGetter pLevel, BlockPos pPos, CollisionContext pContext) {
return SHAPE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.inventory.AbstractContainerMenu;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.entity.BlockEntity;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.material.Fluids;
import net.neoforged.neoforge.fluids.FluidStack;
import net.neoforged.neoforge.items.ItemStackHandler;
import org.jetbrains.annotations.Nullable;
Expand All @@ -29,7 +29,7 @@
public class SkilletBlockEntity extends BlockEntity implements MenuProvider {

private List<ItemStack> itemStacks = new ArrayList<>();
private FluidStack fluidStacks = new FluidStack(Fluids.WATER, 1000);
private FluidStack fluidStacks = FluidStack.EMPTY;

private final int MAX_SLOT = 5;

Expand Down Expand Up @@ -164,4 +164,8 @@ public Component getDisplayName() {
public AbstractContainerMenu createMenu(int pContainerId, Inventory pPlayerInventory, Player pPlayer) {
return new SkilletMenu(pContainerId, pPlayerInventory, this);
}

public static void tick(Level level, BlockPos pos, BlockState state, SkilletBlockEntity blockEntity) {

}
}
15 changes: 9 additions & 6 deletions src/main/java/mczme/lingshi/common/recipe/SkilletRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,17 @@ public NonNullList<Ingredient> getIngredients() {
public boolean matches(SkilletRecipeInput pInput, Level pLevel) {
List<ItemStack> inputs = new ArrayList<>();
int i = 0;
for (int j = 0; j < MAX_SLOT; ++j) {
ItemStack itemstack = pInput.getItem(j);
if (!itemstack.isEmpty()) {
++i;
inputs.add(itemstack);
if(!pInput.isEmpty()){
for (int j = 0; j < pInput.size(); ++j) {
ItemStack itemstack = pInput.getItem(j);
if (!itemstack.isEmpty()) {
++i;
inputs.add(itemstack);
}
}
}
return i == this.items.size() && findMatches(inputs, this.items) != null;

return i == this.items.size() && findMatches(inputs, this.items) != null && pInput.getFluid()==FluidStack.EMPTY;
}

@Override
Expand Down
1 change: 0 additions & 1 deletion src/main/java/mczme/lingshi/common/registry/ModItems.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class ModItems {

public static final Supplier<Item> IRON_KNIFE = registerWithCreateTab("iron_knife", () -> new KnifeItem(Tiers.IRON,new Item.Properties().attributes(SwordItem.createAttributes(Tiers.IRON, 3, -2.4F))));

public static final Supplier<Item> EMPTY_BOWL = register("empty_bowl",()-> new Item(new Item.Properties()));

private static Supplier<Item> registerWithCreateTab(String item_name, Supplier<Item> itemSupplier) {
Supplier<Item> item = ITEMS.register(item_name, itemSupplier);
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit d601007

Please sign in to comment.