Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/main/java/com/altnoir/mia/MIA.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,15 +21,21 @@
import java.util.List;

public class ArtifactSmithingTableScreen extends AbstractContainerScreen<ArtifactSmithingTableMenu> {
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; // 配方显示网格列数
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
// }
// }
// }
// }

}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -39,14 +40,15 @@ 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;
private final List<RecipeHolder<ArtifactSmithingRecipe>> availableRecipes;
private final List<RecipeHolder<ArtifactSmithingRecipe>> unavailableRecipes;

private final List<RecipeHolder<ArtifactSmithingRecipe>> allRecipes;

public int inventoryTimesChanged = 0;
Runnable slotUpdateListener;

public ArtifactSmithingTableMenu(int containerId, Inventory playerInventory) {
Expand All @@ -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();
Expand All @@ -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) {
Expand Down Expand Up @@ -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<ArtifactSmithingRecipe> 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<ArtifactSmithingRecipe> 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++) {
Expand All @@ -188,7 +204,8 @@ private void tryMatchRecipe() {

private void setupResultSlot() {
if (!this.availableRecipes.isEmpty() && this.isValidRecipeIndex(this.selectedRecipeIndex.get())) {
RecipeHolder<ArtifactSmithingRecipe> recipeholder = this.availableRecipes.get(this.selectedRecipeIndex.get());
RecipeHolder<ArtifactSmithingRecipe> recipeholder = this.availableRecipes
.get(this.selectedRecipeIndex.get());
ItemStack materialStack = this.materialSlot.getItem();
ItemStack requiredMaterial = recipeholder.value().getMaterial();

Expand Down Expand Up @@ -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)) {
Expand Down