Skip to content

Commit

Permalink
Nearly fixed Recipe View, but there remains some offset issue. Spent …
Browse files Browse the repository at this point in the history
…hours on this shit.
  • Loading branch information
sakura-ryoko committed Aug 20, 2024
1 parent b3ed4be commit ec7a0c3
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 77 deletions.
11 changes: 8 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,14 @@ sourceCompatibility = JavaVersion.VERSION_21
targetCompatibility = JavaVersion.VERSION_21

repositories {
// maven { url 'https://masa.dy.fi/maven' }
maven { url 'https://maven.terraformersmc.com/releases/' }
maven { url 'https://jitpack.io' }
//maven { url 'https://masa.dy.fi/maven' }
maven {
url 'https://masa.dy.fi/maven'
mavenLocal()
}
maven { url 'https://maven.terraformersmc.com/releases/' }
maven { url 'https://jitpack.io' }
flatDir { dirs 'libs' }
}

loom {
Expand Down
8 changes: 4 additions & 4 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ mod_version = 0.24.999-snap

# Required malilib version
malilib_version = 0.20.999-snap
malilib_id = 0aaeca823e
malilib_id = f4f2de7373

# Minecraft, Fabric Loader and API and mappings versions
minecraft_version_out = 24w33a
minecraft_version = 24w33a
mappings_version = 24w33a+build.20
mappings_version = 24w33a+build.22

fabric_loader_version = 0.15.11
fabric_loader_version = 0.16.2
mod_menu_version = 11.0.1
fabric_api_version = 0.102.0+1.21
fabric_api_version = 0.102.2+1.21.2
112 changes: 52 additions & 60 deletions src/main/java/fi/dy/masa/itemscroller/event/RenderEventHandler.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package fi.dy.masa.itemscroller.event;

import org.joml.Matrix4fStack;
import com.mojang.blaze3d.systems.RenderSystem;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.ingame.HandledScreen;
import net.minecraft.client.render.DiffuseLighting;
import net.minecraft.client.util.math.MatrixStack;
import net.minecraft.item.ItemStack;

import fi.dy.masa.malilib.render.InventoryOverlay;
import fi.dy.masa.malilib.render.RenderUtils;
import fi.dy.masa.malilib.util.GuiUtils;
Expand Down Expand Up @@ -41,22 +39,28 @@ public static RenderEventHandler instance()
return INSTANCE;
}

public void renderRecipeView(DrawContext drawContext)
public void renderRecipeView(MinecraftClient mc, DrawContext drawContext, int mouseX, int mouseY)
{
if (GuiUtils.getCurrentScreen() instanceof HandledScreen && InputUtils.isRecipeViewOpen())
if (GuiUtils.getCurrentScreen() instanceof HandledScreen<?> gui &&
InputUtils.isRecipeViewOpen())
{
HandledScreen<?> gui = (HandledScreen<?>) GuiUtils.getCurrentScreen();
RecipeStorage recipes = RecipeStorage.getInstance();
final int first = recipes.getFirstVisibleRecipeId();
final int countPerPage = recipes.getRecipeCountPerPage();
final int lastOnPage = first + countPerPage - 1;

this.calculateRecipePositions(gui);

drawContext.getMatrices().push();
drawContext.getMatrices().translate(this.recipeListX, this.recipeListY, 0);
drawContext.getMatrices().scale((float) this.scale, (float) this.scale, 1);

/*
Matrix4fStack matrix4fStack = RenderSystem.getModelViewStack();
matrix4fStack.pushMatrix();
matrix4fStack.translate(this.recipeListX, this.recipeListY, 0);
matrix4fStack.scale((float) this.scale, (float) this.scale, 1);
*/

String str = StringUtils.translate("itemscroller.gui.label.recipe_page", (first / countPerPage) + 1, recipes.getTotalRecipeCount() / countPerPage);

Expand All @@ -74,69 +78,58 @@ public void renderRecipeView(DrawContext drawContext)

if (Configs.Generic.CRAFTING_RENDER_RECIPE_ITEMS.getBooleanValue())
{
final int mouseX = fi.dy.masa.malilib.util.InputUtils.getMouseX();
final int mouseY = fi.dy.masa.malilib.util.InputUtils.getMouseY();
final int recipeId = this.getHoveredRecipeId(mouseX, mouseY, recipes, gui);
RecipePattern recipe = recipeId >= 0 ? recipes.getRecipe(recipeId) : recipes.getSelectedRecipe();

this.renderRecipeItems(recipe, recipes.getRecipeCountPerPage(), gui, drawContext);
}

matrix4fStack.popMatrix();
//RenderSystem.applyModelViewMatrix();
RenderSystem.enableBlend(); // Fixes the crafting book icon rendering
drawContext.getMatrices().pop();
//matrix4fStack.popMatrix();
//RenderSystem.enableBlend(); // Fixes the crafting book icon rendering
}
}

public void onDrawScreenPost(MinecraftClient mc, DrawContext drawContext)
public void onDrawScreenPost(MinecraftClient mc, DrawContext drawContext, int mouseX, int mouseY)
{
this.renderRecipeView(drawContext);
this.renderRecipeView(mc, drawContext, mouseX, mouseY);

if (GuiUtils.getCurrentScreen() instanceof HandledScreen)
if (GuiUtils.getCurrentScreen() instanceof HandledScreen<?> gui)
{
HandledScreen<?> gui = (HandledScreen<?>) this.mc.currentScreen;

int bufferedCount = ClickPacketBuffer.getBufferedActionsCount();

if (bufferedCount > 0)
{
drawContext.drawText(mc.textRenderer, "Buffered slot clicks: " + bufferedCount, 10, 10, 0xFFD0D0D0, false);
}

if (InputUtils.isRecipeViewOpen() == false)
if (InputUtils.isRecipeViewOpen())
{
return;
}

RecipeStorage recipes = RecipeStorage.getInstance();
RecipeStorage recipes = RecipeStorage.getInstance();
final int recipeId = this.getHoveredRecipeId(mouseX, mouseY, recipes, gui);
float offset = 300f;

final int mouseX = fi.dy.masa.malilib.util.InputUtils.getMouseX();
final int mouseY = fi.dy.masa.malilib.util.InputUtils.getMouseY();
final int recipeId = this.getHoveredRecipeId(mouseX, mouseY, recipes, gui);
drawContext.getMatrices().push();
drawContext.getMatrices().translate(0, 0, offset);

float offset = 300f;
Matrix4fStack matrix4fStack = RenderSystem.getModelViewStack();
matrix4fStack.pushMatrix();
matrix4fStack.translate(0, 0, offset);

if (recipeId >= 0)
{
RecipePattern recipe = recipes.getRecipe(recipeId);
this.renderHoverTooltip(mouseX, mouseY, recipe, gui, drawContext);
}
else if (Configs.Generic.CRAFTING_RENDER_RECIPE_ITEMS.getBooleanValue())
{
RecipePattern recipe = recipes.getSelectedRecipe();
ItemStack stack = this.getHoveredRecipeIngredient(mouseX, mouseY, recipe, recipes.getRecipeCountPerPage(), gui);

if (InventoryUtils.isStackEmpty(stack) == false)
if (recipeId >= 0)
{
InventoryOverlay.renderStackToolTip(mouseX, mouseY, stack, this.mc, drawContext);
RecipePattern recipe = recipes.getRecipe(recipeId);
this.renderHoverTooltip(mouseX, mouseY, recipe, gui, drawContext);
}
}
else if (Configs.Generic.CRAFTING_RENDER_RECIPE_ITEMS.getBooleanValue())
{
RecipePattern recipe = recipes.getSelectedRecipe();
ItemStack stack = this.getHoveredRecipeIngredient(mouseX, mouseY, recipe, recipes.getRecipeCountPerPage(), gui);

matrix4fStack.popMatrix();
//RenderSystem.applyModelViewMatrix();
if (InventoryUtils.isStackEmpty(stack) == false)
{
InventoryOverlay.renderStackToolTip(mouseX, mouseY, stack, this.mc, drawContext);
}
}

drawContext.getMatrices().pop();
}
}
}

Expand All @@ -146,15 +139,14 @@ private void calculateRecipePositions(HandledScreen<?> gui)
final int gapHorizontal = 2;
final int gapVertical = 2;
final int stackBaseHeight = 16;
final int guiLeft = AccessorUtils.getGuiLeft(gui);

this.recipesPerColumn = 9;
this.columns = (int) Math.ceil((double) recipes.getRecipeCountPerPage() / (double) this.recipesPerColumn);
this.numberTextWidth = 12;
this.gapColumn = 4;

int usableHeight = GuiUtils.getScaledWindowHeight();
int usableWidth = guiLeft;
int usableWidth = AccessorUtils.getGuiLeft(gui);
// Scale the maximum stack size by taking into account the relative gap size
double gapScaleVertical = (1D - (double) gapVertical / (double) (stackBaseHeight + gapVertical));
// the +1.2 is for the gap and page text height on the top and bottom
Expand All @@ -166,7 +158,7 @@ private void calculateRecipePositions(HandledScreen<?> gui)

this.scale = (double) stackDimensions / (double) stackBaseHeight;
this.entryHeight = stackBaseHeight + gapVertical;
this.recipeListX = guiLeft - (int) ((this.columns * (stackBaseHeight + this.numberTextWidth + this.gapColumn) + gapHorizontal) * this.scale);
this.recipeListX = usableWidth - (int) ((this.columns * (stackBaseHeight + this.numberTextWidth + this.gapColumn) + gapHorizontal) * this.scale);
this.recipeListY = (int) (this.entryHeight * this.scale);
this.columnWidth = stackBaseHeight + this.numberTextWidth + this.gapColumn;
}
Expand Down Expand Up @@ -224,15 +216,13 @@ private void renderStoredRecipeStack(ItemStack stack, int recipeId, int row, int
x = x - (int) (font.getWidth(indexStr) * scale) - 2;
y = row * this.entryHeight + this.entryHeight / 2 - font.fontHeight / 2;

// TODO DrawContext still uses the MatrixStack type
MatrixStack matrixStack = drawContext.getMatrices();
matrixStack.push();
matrixStack.translate(x, y, 0);
matrixStack.scale(scale, scale, 1);
drawContext.getMatrices().push();
drawContext.getMatrices().translate(x, y, 0);
drawContext.getMatrices().scale(scale, scale, 1);

drawContext.drawText(font, indexStr, 0, 0, 0xFFC0C0C0, false);

matrixStack.pop();
drawContext.getMatrices().pop();
}

private void renderRecipeItems(RecipePattern recipe, int recipeCountPerPage, HandledScreen<?> gui, DrawContext drawContext)
Expand Down Expand Up @@ -289,14 +279,18 @@ private ItemStack getHoveredRecipeIngredient(int mouseX, int mouseY, RecipePatte
private void renderStackAt(ItemStack stack, int x, int y, boolean border, DrawContext drawContext)
{
final int w = 16;
// FIXME --> TERRIBLE OFFSET! (What values am I missing? I've tried several)
int xAdj = (int) ((x) * this.scale) + 125;
int yAdj = (int) ((y) * this.scale) + 35;
int wAdj = (int) ((w) * this.scale);

if (border)
{
// Draw a light/white border around the stack
RenderUtils.drawOutline(x - 1, y - 1, w + 2, w + 2, 0xFFFFFFFF);
RenderUtils.drawOutline(xAdj - 1, yAdj - 1, wAdj + 2, wAdj + 2, 0xFFFFFFFF);
}

RenderUtils.drawRect(x, y, w, w, 0x20FFFFFF); // light background for the item
RenderUtils.drawRect(xAdj, yAdj, wAdj, wAdj, 0x20FFFFFF); // light background for the item

if (InventoryUtils.isStackEmpty(stack) == false)
{
Expand All @@ -305,14 +299,12 @@ private void renderStackAt(ItemStack stack, int x, int y, boolean border, DrawCo
stack = stack.copy();
InventoryUtils.setStackSize(stack, 1);

// TODO DrawContext still uses the MatrixStack type
MatrixStack matrixStack = drawContext.getMatrices();
matrixStack.push();
matrixStack.translate(0, 0, 100.f);
drawContext.getMatrices().push();
drawContext.getMatrices().translate(0, 0, 100.f);

drawContext.drawItem(stack, x, y);

matrixStack.pop();
drawContext.getMatrices().pop();
}
}

Expand Down
24 changes: 17 additions & 7 deletions src/main/java/fi/dy/masa/itemscroller/mixin/MixinScreen.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,32 @@
package fi.dy.masa.itemscroller.mixin;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;

import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.screen.Screen;

import fi.dy.masa.itemscroller.event.RenderEventHandler;

@Mixin(Screen.class)
public abstract class MixinScreen
{
@Inject(method = "renderWithTooltip", at = @At(value = "RETURN"))
private void onDrawScreenPost(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci)
/*
@Inject(method = "renderWithTooltip",
at = @At(value = "INVOKE",
target = "Lnet/minecraft/client/gui/screen/Screen;render(Lnet/minecraft/client/gui/DrawContext;IIF)V",
shift = At.Shift.AFTER))
private void itemscroller_inDrawScreenPre(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci)
{
RenderEventHandler.instance().onDrawCraftingScreenBackground(MinecraftClient.getInstance(), context, mouseX, mouseY);
}
*/

@Inject(method = "renderWithTooltip", at = @At(value = "TAIL"))
private void itemscroller_onDrawScreenPost(DrawContext context, int mouseX, int mouseY, float delta, CallbackInfo ci)
{
RenderEventHandler.instance().onDrawScreenPost(MinecraftClient.getInstance(), context);
RenderEventHandler.instance().onDrawScreenPost(MinecraftClient.getInstance(), context, mouseX, mouseY);
}
}
4 changes: 2 additions & 2 deletions src/main/resources/fabric.mod.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
],
"accessWidener": "itemscroller.accesswidener",
"depends": {
"minecraft": ">=1.21-alpha.24.33.a",
"malilib": ">=0.20.999-snap"
"minecraft": ">1.21.1",
"malilib": "0.20.999-snap"
}
}
2 changes: 1 addition & 1 deletion src/main/resources/mixins.itemscroller.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"required": true,
"package": "fi.dy.masa.itemscroller.mixin",
"compatibilityLevel": "JAVA_17",
"compatibilityLevel": "JAVA_21",
"minVersion": "0.8",
"client": [
"IMixinCraftingResultSlot",
Expand Down

0 comments on commit ec7a0c3

Please sign in to comment.