Skip to content

Commit

Permalink
improved compatibility with the recipe book, added horizontal scrolli…
Browse files Browse the repository at this point in the history
…ng, updated base game version to 1.21.4
  • Loading branch information
GrahamKracker committed Feb 18, 2025
1 parent 887f139 commit a336da6
Show file tree
Hide file tree
Showing 8 changed files with 113 additions and 213 deletions.
11 changes: 4 additions & 7 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G
org.gradle.parallel=true

# Fabric Properties
# check these on https://fabricmc.net/develop
minecraft_version=1.21.3
yarn_mappings=1.21.3+build.2
loader_version=0.16.9

minecraft_version=1.21.4
yarn_mappings=1.21.4+build.8
loader_version=0.16.10
# Mod Properties
mod_version=1.3.4
maven_group=cookiesmod
archives_base_name=cookies-mod

# Dependencies
fabric_version=0.110.0+1.21.3
fabric_version=0.117.0+1.21.4
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Getter
Expand Down Expand Up @@ -48,15 +47,15 @@ public void recalculate() {
}

private void finishRecalculation(RecipeCalculationResult recipeCalculationResult) {
addSubRecipe(recipeCalculationResult, null, 0, recipeCalculationResult);
addSubRecipe(recipeCalculationResult, null, 0);
}

private void addSubRecipe(RecipeCalculationResult subRecipe, RecipeListLine parent, int depth, RecipeCalculationResult topRecipe) {
private void addSubRecipe(RecipeCalculationResult subRecipe, RecipeListLine parent, int depth) {
var recipeLine = new RecipeListLine(parent, depth, subRecipe.getIngredient(), amount);
recipeLines.add(recipeLine);
subRecipe.getRequired().forEach(recipeResult -> {
if (recipeResult instanceof RecipeCalculationResult subRecipe2) {
addSubRecipe(subRecipe2, recipeLine, depth + 1, topRecipe);
addSubRecipe(subRecipe2, recipeLine, depth + 1);
} else if (recipeResult instanceof Ingredient ingredient) {
recipeLines.add(new RecipeListLine(recipeLine, depth + 1, ingredient, amount));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,38 +1,31 @@
package codes.cookies.mod.features.crafthelper;

import codes.cookies.mod.config.ConfigManager;
import codes.cookies.mod.events.api.ScreenKeyEvents;
import codes.cookies.mod.features.crafthelper.ui.CraftHelperPanel;
import codes.cookies.mod.features.crafthelper.ui.CraftHelperPanelLine;
import codes.cookies.mod.repository.RepositoryItem;
import codes.cookies.mod.utils.SkyblockUtils;
import codes.cookies.mod.utils.accessors.InventoryScreenAccessor;

import codes.cookies.mod.utils.cookies.CookiesUtils;
import com.mojang.logging.LogUtils;
import lombok.Getter;

import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientTickEvents;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.fabricmc.fabric.api.client.screen.v1.ScreenKeyboardEvents;
import net.fabricmc.fabric.api.client.screen.v1.ScreenMouseEvents;

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

import net.minecraft.client.gui.screen.ingame.RecipeBookScreen;

import org.jetbrains.annotations.Nullable;
import org.joml.Vector2i;
import org.joml.Vector2ic;

import java.lang.ref.SoftReference;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

public class CraftHelperManager {
private static int currentItemIndex = 0;
public static int scrollDelta = -100;

@Nullable
public static CraftHelperItem getCurrentItem() {
Expand All @@ -55,53 +48,40 @@ public static void init() {
location = ConfigManager.getConfig().helpersConfig.craftHelper.craftHelperLocation.getValue();
ConfigManager.getConfig().helpersConfig.craftHelper.craftHelperLocation.withCallback((oldValue, newValue) -> location = newValue);
ScreenEvents.AFTER_INIT.register((client, screen, scaledWidth, scaledHeight) -> {
if(panel == null) {
panel = new CraftHelperPanel(0);
}
if (!(screen instanceof InventoryScreenAccessor inventoryScreenAccessor)) {
return;
}

if (!SkyblockUtils.isCurrentlyInSkyblock()) {
return;
}

if (!ConfigManager.getConfig().helpersConfig.craftHelper.craftHelper.getValue()) {
return;
}

var currentItem = getCurrentItem();

if (currentItem == null) {
pushNewCraftHelperItem(new CraftHelperItem(RepositoryItem.of("TITANIUM_DRILL_3"), 1)); //WAND_OF_RESTORATION
pushNewCraftHelperItem(new CraftHelperItem(RepositoryItem.of("TITANIUM_DRILL_4"), 1)); //WAND_OF_RESTORATION
currentItem = getCurrentItem();
}

panel.getLines().clear();
panel.createHeader(currentItem);
panel.addLine(panel.Spacer);
//if (panel == null) {
panel = new CraftHelperPanel(calculateWidth(inventoryScreenAccessor));
/*} else {
panel.init(currentItem);
}*/

screen.addDrawableChild(panel);

for (var line : currentItem.getRecipeLines()) {
panel.addLine(line);
}

for (CraftHelperPanelLine line : panel.getLines()) {
line.update();
}

ScreenEvents.beforeRender(screen).register((screen1, drawContext, i, i1, v) -> {
panel.setWidth(calculateWidth(inventoryScreenAccessor));
var position = getPosition(inventoryScreenAccessor, panel);
panel.setX(position.x());
panel.setY(position.y());
});

ScreenMouseEvents.allowMouseClick(screen).register(CraftHelperManager::onMouseClick);
ScreenMouseEvents.allowMouseScroll(screen).register(CraftHelperManager::onMouseScroll);
ScreenKeyboardEvents.allowKeyPress(screen).register(CraftHelperManager::onKeyPressed);
ScreenKeyboardEvents.allowKeyRelease(screen).register(CraftHelperManager::onKeyReleased);
ScreenKeyEvents.getExtension(screen).cookies$allowCharTyped().register(CraftHelperManager::onCharTyped);

ScreenEvents.afterTick(screen).register((screen1) -> {
if (ticksSinceLastUpdate.getAndIncrement() > 60) {
panel.getLines().forEach(CraftHelperPanelLine::update);
Expand All @@ -119,8 +99,12 @@ private static int calculateRightEdge(InventoryScreenAccessor screen) {
return screen.cookies$getBackgroundWidth() + screen.cookies$getX();
}

private static int calculateLeftEdge(InventoryScreenAccessor screen) {//todo special case for recipe book
private static int calculateLeftEdge(InventoryScreenAccessor screen) {//todo special case for recipe book, right edge works
if (screen instanceof RecipeBookScreen<?> recipeBookScreen) {
return recipeBookScreen.recipeBook.getLeft() - 35; //magic number is the width of the tabs on the left, 35 is the default
}
return (((Screen) screen).width - screen.cookies$getBackgroundWidth()) / 2;

}

private static int calculateWidth(InventoryScreenAccessor screen) {
Expand Down Expand Up @@ -151,26 +135,6 @@ private static void preventOverflow(int screenHeight, Vector2i pos, int height)
}
}

public static boolean onMouseClick(Screen screen, double v, double v1, int i) {
return true;
}

public static boolean onMouseScroll(Screen screen, double v, double v1, double v2, double v3) {
return true;
}

public static boolean onKeyPressed(Screen screen, int i, int i1, int i2) {
return true;
}

public static boolean onKeyReleased(Screen screen, int i, int i1, int i2) {
return true;
}

public static boolean onCharTyped(Screen screen, char c, int i) {
return true;
}

public static void pushNewCraftHelperItem(CraftHelperItem item) {
items.addFirst(item);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,23 @@ public class CraftHelperPanel extends ContainerWidget implements Element, Select

public CraftHelperPanel(int width) {
super(0, 0, width, 0, Text.of(""));
var item = CraftHelperManager.getCurrentItem();
if (item != null) {
createHeader(item);
init(CraftHelperManager.getCurrentItem());
}

public void init(CraftHelperItem item) {
if (item == null) {
return;
}
lines.clear();
createHeader(item);
addLine(Spacer);

addLine(Spacer);
for (var line : item.getRecipeLines()) {
addLine(line);
}

for (CraftHelperPanelLine line : getLines()) {
line.update();
}
}

Expand All @@ -58,7 +70,7 @@ public void addLine(CraftHelperPanelLine line) {

@Override
protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) {
setY(getY() + scrollDelta);
setY(getY() + CraftHelperManager.scrollDelta);
int totalHeight = 14;
for (CraftHelperPanelLine craftHelperPanelLine : lines) {
int line1Height = craftHelperPanelLine.getHeight();
Expand All @@ -75,20 +87,22 @@ protected void renderWidget(DrawContext context, int mouseX, int mouseY, float d
context.drawGuiTexture(RenderLayer::getGuiTextured, Identifier.ofVanilla("tooltip/background"), getX(), getY(), this.width, totalHeight);
context.drawGuiTexture(RenderLayer::getGuiTextured, Identifier.ofVanilla("tooltip/frame"), getX(), getY(), this.width, totalHeight);

context.enableScissor(getX() + 15, 0, this.width - 10, MinecraftClient.getInstance().getWindow().getHeight());
context.getMatrices().push();
{
context.getMatrices().translate(0, 0, 200);

AtomicInteger widgetY = new AtomicInteger(getY() + 14);
for (CraftHelperPanelLine line : lines) {
line.setX(this.getX() + 15);
line.setX(this.getX() + 15 - horizontalScroll);
var lineHeight = line.getHeight();
line.setY(widgetY.getAndAdd(lineHeight == 0 ? 0 : lineHeight + 1));
line.renderWidget(context, mouseX, mouseY, delta);
}

context.getMatrices().pop();
}
context.disableScissor();
context.getMatrices().pop();
}
}
Expand Down Expand Up @@ -128,18 +142,29 @@ public boolean isMouseOver(double mouseX, double mouseY) {
return mouseX >= getX() && mouseX <= getX() + width;
}

private int scrollDelta = -100;
private int horizontalScroll = 0;

@Override
public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) {
if (!isMouseOver(mouseX, mouseY)) {
return super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount);
}

scrollDelta += 2 * (int) verticalAmount;
CraftHelperManager.scrollDelta += 2 * (int) verticalAmount;
horizontalScroll += (int) horizontalAmount;
return true;
}

@Override
protected int getContentsHeightWithPadding() {
return this.height;
}

@Override
protected double getDeltaYPerScroll() {
return 0;
}

public void createHeader(CraftHelperItem item) {
var line = new CraftHelperPanelLine();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,14 @@ protected void appendClickableNarrations(NarrationMessageBuilder builder) {
public List<CraftHelperComponent> children() {
return children;
}

@Override
protected int getContentsHeightWithPadding() {
return this.height;
}

@Override
protected double getDeltaYPerScroll() {
return 0;
}
}
Loading

0 comments on commit a336da6

Please sign in to comment.