diff --git a/src/main/java/com/altnoir/mia/MIA.java b/src/main/java/com/altnoir/mia/MIA.java index 29db7935..9807d4af 100644 --- a/src/main/java/com/altnoir/mia/MIA.java +++ b/src/main/java/com/altnoir/mia/MIA.java @@ -1,11 +1,13 @@ package com.altnoir.mia; +import com.altnoir.mia.client.gui.screens.ArtifactSmithingTableScreen; import com.altnoir.mia.core.curse.CurseManager; import com.altnoir.mia.init.*; import com.altnoir.mia.init.event.EventHandle; import com.altnoir.mia.init.worldgen.MiaBiomeSources; import com.altnoir.mia.init.worldgen.MiaDensityFunctionTypes; import com.altnoir.mia.init.worldgen.MiaFeature; +import com.altnoir.mia.inventory.ArtifactSmithingTableEventHandler; import com.mojang.logging.LogUtils; import net.neoforged.api.distmarker.Dist; import net.neoforged.bus.api.IEventBus; diff --git a/src/main/java/com/altnoir/mia/client/gui/screens/ArtifactSmithingTableScreen.java b/src/main/java/com/altnoir/mia/client/gui/screens/ArtifactSmithingTableScreen.java index e1e6ddcc..52b22978 100644 --- a/src/main/java/com/altnoir/mia/client/gui/screens/ArtifactSmithingTableScreen.java +++ b/src/main/java/com/altnoir/mia/client/gui/screens/ArtifactSmithingTableScreen.java @@ -1,5 +1,6 @@ package com.altnoir.mia.client.gui.screens; +import com.altnoir.mia.MIA; import com.altnoir.mia.client.event.ClientTooltipEvent; import com.altnoir.mia.inventory.ArtifactSmithingTableMenu; import com.altnoir.mia.recipe.ArtifactSmithingRecipe; @@ -20,15 +21,21 @@ import java.util.List; public class ArtifactSmithingTableScreen extends AbstractContainerScreen { - private static final ResourceLocation BACKGROUND = MiaUtil.miaId("textures/gui/container/artifact_smithing_table.png"); - - private static final ResourceLocation RECIPE_SELECTED_SPRITE = MiaUtil.miaId("container/artifact_smithing_table/recipe_selected"); - private static final ResourceLocation RECIPE_HIGHLIGHTED_SPRITE = MiaUtil.miaId("container/artifact_smithing_table/recipe_highlighted"); - private static final ResourceLocation RECIPE_AVAILABLE_SPRITE = MiaUtil.miaId("container/artifact_smithing_table/recipe_available"); - private static final ResourceLocation RECIPE_UNAVAILABLE_SPRITE = MiaUtil.miaId("container/artifact_smithing_table/recipe_unavailable"); + private static final ResourceLocation BACKGROUND = MiaUtil + .miaId("textures/gui/container/artifact_smithing_table.png"); + + private static final ResourceLocation RECIPE_SELECTED_SPRITE = MiaUtil + .miaId("container/artifact_smithing_table/recipe_selected"); + private static final ResourceLocation RECIPE_HIGHLIGHTED_SPRITE = MiaUtil + .miaId("container/artifact_smithing_table/recipe_highlighted"); + private static final ResourceLocation RECIPE_AVAILABLE_SPRITE = MiaUtil + .miaId("container/artifact_smithing_table/recipe_available"); + private static final ResourceLocation RECIPE_UNAVAILABLE_SPRITE = MiaUtil + .miaId("container/artifact_smithing_table/recipe_unavailable"); private static final ResourceLocation SCROLLER_SPRITE = MiaUtil.miaId("container/artifact_smithing_table/scroller"); - private static final ResourceLocation SCROLLER_DISABLED_SPRITE = MiaUtil.miaId("container/artifact_smithing_table/scroller_disabled"); + private static final ResourceLocation SCROLLER_DISABLED_SPRITE = MiaUtil + .miaId("container/artifact_smithing_table/scroller_disabled"); // 每行槽间隔高度为18像素 private static final int RECIPES_COLUMNS = 4; // 配方显示网格列数 @@ -91,12 +98,13 @@ protected void renderBg(GuiGraphics guiGraphics, float partialTick, int mouseX, recipeSelected.value().getAttributeAmount(), recipeSelected.value().getAttributeOperation()); - guiGraphics.drawString(this.font, text, this.leftPos + RECIPES_X, this.topPos + RECIPES_Y - 10, 0x000000, false); + guiGraphics.drawString(this.font, text, this.leftPos + RECIPES_X, this.topPos + RECIPES_Y - 10, 0x000000, + false); } } private void renderButtons(GuiGraphics guiGraphics, int mouseX, int mouseY, int x, int y, - int lastVisibleElementIndex) { + int lastVisibleElementIndex) { int availableRecipeCount = ((ArtifactSmithingTableMenu) this.menu).getAvailableRecipes().size(); int unavailableRecipeCount = ((ArtifactSmithingTableMenu) this.menu).getUnavailableRecipes().size(); for (int i = this.startIndex; i < lastVisibleElementIndex @@ -108,6 +116,8 @@ private void renderButtons(GuiGraphics guiGraphics, int mouseX, int mouseY, int ResourceLocation resourcelocation; if (i == ((ArtifactSmithingTableMenu) this.menu).getSelectedRecipeIndex()) { + MIA.LOGGER.debug("selected " + i); + resourcelocation = RECIPE_SELECTED_SPRITE; } else if (i < availableRecipeCount) { if (mouseX >= k && mouseY >= i1 && mouseX < k + RECIPES_IMAGE_SIZE_WIDTH diff --git a/src/main/java/com/altnoir/mia/inventory/ArtifactSmithingTableEventHandler.java b/src/main/java/com/altnoir/mia/inventory/ArtifactSmithingTableEventHandler.java new file mode 100644 index 00000000..ac59d6a7 --- /dev/null +++ b/src/main/java/com/altnoir/mia/inventory/ArtifactSmithingTableEventHandler.java @@ -0,0 +1,26 @@ +package com.altnoir.mia.inventory; + +import com.altnoir.mia.MIA; + +import net.minecraft.world.entity.player.Player; +import net.neoforged.bus.api.SubscribeEvent; +import net.neoforged.neoforge.event.tick.PlayerTickEvent; + +public class ArtifactSmithingTableEventHandler { + + // onInventoryChanged 检测不到新物品进入背包 + // @SubscribeEvent + // public void onInventoryChanged(PlayerTickEvent.Post event) { + // Player player = event.getEntity(); + // if (!player.level().isClientSide()) { + // if (player.containerMenu instanceof ArtifactSmithingTableMenu menu) { + // if (menu.inventoryTimesChanged != player.getInventory().getTimesChanged()) { + // menu.fetchRecipesForArtifact(); + // menu.tryMatchRecipe(); + // menu.inventoryTimesChanged = player.getInventory().getTimesChanged(); + // } + // } + // } + // } + +} diff --git a/src/main/java/com/altnoir/mia/inventory/ArtifactSmithingTableMenu.java b/src/main/java/com/altnoir/mia/inventory/ArtifactSmithingTableMenu.java index 3201f8b0..c4feb824 100644 --- a/src/main/java/com/altnoir/mia/inventory/ArtifactSmithingTableMenu.java +++ b/src/main/java/com/altnoir/mia/inventory/ArtifactSmithingTableMenu.java @@ -1,5 +1,6 @@ package com.altnoir.mia.inventory; +import com.altnoir.mia.MIA; import com.altnoir.mia.component.ArtifactEnhancementComponent; import com.altnoir.mia.init.MiaBlocks; import com.altnoir.mia.init.MiaComponents; @@ -39,6 +40,7 @@ public class ArtifactSmithingTableMenu extends AbstractContainerMenu { private final Level level; private final Player player; + private final Inventory inventory; protected final ContainerLevelAccess access; private final DataSlot selectedRecipeIndex; @@ -46,7 +48,7 @@ public class ArtifactSmithingTableMenu extends AbstractContainerMenu { private final List> unavailableRecipes; private final List> allRecipes; - + public int inventoryTimesChanged = 0; Runnable slotUpdateListener; public ArtifactSmithingTableMenu(int containerId, Inventory playerInventory) { @@ -58,6 +60,8 @@ public ArtifactSmithingTableMenu(int containerId, Inventory playerInventory, Con this.access = access; this.player = playerInventory.player; this.level = playerInventory.player.level(); + this.inventory = playerInventory; + this.inventoryTimesChanged = playerInventory.getTimesChanged(); this.availableRecipes = new ArrayList<>(); this.unavailableRecipes = new ArrayList<>(); this.selectedRecipeIndex = DataSlot.standalone(); @@ -82,8 +86,7 @@ public int getMaxStackSize() { this.materialSlot = this.addSlot(new Slot(this.materialContainer, 0, 20, 66) { @Override public void onTake(Player player, ItemStack stack) { - selectedRecipeIndex.set(-1); - setupResultSlot(); + tryMatchRecipe(); } }); this.resultSlot = this.addSlot(new Slot(this.resultContainer, 0, 143, 37) { @@ -143,36 +146,49 @@ public boolean clickMenuButton(Player player, int recipeIndex) { public void slotsChanged(Container inventory) { // if input slot changes, reset the recipe list and the result slot if (inventory == this.artifactContainer) { - // clear avaliable recipes and selected recipes - this.availableRecipes.clear(); - this.unavailableRecipes.clear(); - this.selectedRecipeIndex.set(-1); // clear result slot this.resultSlot.set(ItemStack.EMPTY); - // clear material slot - this.clearMaterialSlot(); - // add all possible enhancement recipe - ItemStack artifact = this.artifactSlot.getItem(); - if (inputHasSmithingRecipe()) { - for (RecipeHolder recipe : allRecipes) { - if (recipe.value().isArtifactIngredient(artifact)) { - if (playerHasMaterial(recipe.value())) { - this.availableRecipes.add(recipe); - } else { - this.unavailableRecipes.add(recipe); - } - } - } - } + // fetch artifact recipes + fetchRecipesForArtifact(); + tryMatchRecipe(); } else if (inventory == this.materialContainer) { tryMatchRecipe(); } + // else if (inventory == this.inventory) { + // tryMatchRecipe(); + // } this.slotUpdateListener.run(); } - private void tryMatchRecipe() { + public void fetchRecipesForArtifact() { + // clear avaliable recipes and selected recipes + this.availableRecipes.clear(); + this.unavailableRecipes.clear(); + this.selectedRecipeIndex.set(-1); + // clear material slot + // this.clearMaterialSlot(); + // add all possible enhancement recipe + ItemStack artifact = this.artifactSlot.getItem(); + MIA.LOGGER.debug("fetch"); + + if (!artifact.isEmpty() && inputHasSmithingRecipe()) { + for (RecipeHolder recipe : allRecipes) { + if (recipe.value().isArtifactIngredient(artifact)) { + if (playerHasMaterial(recipe.value())) { + this.availableRecipes.add(recipe); + } else { + this.unavailableRecipes.add(recipe); + } + } + } + } + } + + public void tryMatchRecipe() { ItemStack artifact = this.artifactSlot.getItem(); ItemStack material = this.materialSlot.getItem(); + this.selectedRecipeIndex.set(-1); + this.resultSlot.set(ItemStack.EMPTY); if (!artifact.isEmpty() && !material.isEmpty()) { for (int i = 0; i < this.availableRecipes.size(); i++) { @@ -188,7 +204,8 @@ private void tryMatchRecipe() { private void setupResultSlot() { if (!this.availableRecipes.isEmpty() && this.isValidRecipeIndex(this.selectedRecipeIndex.get())) { - RecipeHolder recipeholder = this.availableRecipes.get(this.selectedRecipeIndex.get()); + RecipeHolder recipeholder = this.availableRecipes + .get(this.selectedRecipeIndex.get()); ItemStack materialStack = this.materialSlot.getItem(); ItemStack requiredMaterial = recipeholder.value().getMaterial(); @@ -337,15 +354,21 @@ private boolean isValidRecipeIndex(int recipeIndex) { @Override public boolean stillValid(Player player) { - return this.access.evaluate((level, pos) -> - level.getBlockState(pos).is(MiaBlocks.ARTIFACT_SMITHING_TABLE) && player.canInteractWithBlock(pos, 4.0), true); + return this.access.evaluate((level, pos) -> level.getBlockState(pos).is(MiaBlocks.ARTIFACT_SMITHING_TABLE) + && player.canInteractWithBlock(pos, 4.0), true); } private boolean playerHasMaterial(ArtifactSmithingRecipe recipe) { ItemStack required = recipe.getMaterial(); int needed = required.getCount(); int found = 0; - + ItemStack material = this.materialSlot.getItem(); + if (!material.isEmpty() && recipe.isMaterialIngredient(material)) { + found += material.getCount(); + if (found >= needed) { + return true; + } + } for (int i = 0; i < player.getInventory().getContainerSize(); i++) { ItemStack stack = player.getInventory().getItem(i); if (!stack.isEmpty() && recipe.isMaterialIngredient(stack)) {