Skip to content

Commit

Permalink
getting bored...
Browse files Browse the repository at this point in the history
  • Loading branch information
GrahamKracker committed Dec 30, 2024
1 parent b85fba3 commit 33ea56e
Show file tree
Hide file tree
Showing 10 changed files with 503 additions and 174 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,14 @@
package codes.cookies.mod.features.crafthelper;

import codes.cookies.mod.config.data.ListData;
import codes.cookies.mod.features.crafthelper.ui.CraftHelperComponent;
import codes.cookies.mod.features.crafthelper.ui.CraftHelperPanelLine;
import codes.cookies.mod.features.crafthelper.ui.components.RecipeComponent;
import codes.cookies.mod.features.crafthelper.ui.components.TextComponent;
import codes.cookies.mod.features.crafthelper.ui.RecipeListLine;
import codes.cookies.mod.repository.Ingredient;
import codes.cookies.mod.repository.RepositoryItem;
import codes.cookies.mod.repository.recipes.calculations.RecipeCalculationResult;
import codes.cookies.mod.repository.recipes.calculations.RecipeCalculator;
import codes.cookies.mod.repository.recipes.calculations.RecipePrinter;
import codes.cookies.mod.repository.recipes.calculations.RecipeResult;
import codes.cookies.mod.utils.cookies.Constants;
import codes.cookies.mod.utils.cookies.CookiesUtils;

import codes.cookies.mod.utils.minecraft.TextBuilder;
import lombok.Getter;

import net.minecraft.text.Text;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
Expand All @@ -34,7 +24,8 @@ public class CraftHelperItem {
final RepositoryItem repositoryItem;
private final int amount;
private boolean recalculated = false;
private final List<CraftHelperPanelLine> lines = new ArrayList<>();
private final List<RecipeListLine> recipeLines = new ArrayList<>();


public CraftHelperItem(RepositoryItem repositoryItem, int amount) {
this.repositoryItem = repositoryItem;
Expand All @@ -55,19 +46,19 @@ public void recalculate() {
}

private void finishRecalculation(RecipeCalculationResult recipeCalculationResult) {
lines.clear();
lines.add(new CraftHelperPanelLine(new RecipeComponent(recipeCalculationResult, 0)));
recipeCalculationResult = recipeCalculationResult.multiply(amount);
addSubRecipe(recipeCalculationResult, null, 0);
}

private void getRecipe(RecipeResult<?> calculationResult, int depth) {

if (calculationResult instanceof RecipeCalculationResult subResult) {
//lines.add(new CraftHelperPanelLine(new RecipeComponent(subResult, depth)));
subResult.getRequired().forEach(recipeResult -> {
getRecipe(recipeResult, depth + 1);
});
} else if (calculationResult instanceof Ingredient ingredient) {
//lines.add(new CraftHelperPanelLine(new IngredientComponent(ingredient, depth)));
}
private void addSubRecipe(RecipeCalculationResult subRecipe, RecipeListLine parent, int depth) {
var recipeLine = new RecipeListLine(parent, depth, subRecipe.getIngredient());
recipeLines.add(recipeLine);
subRecipe.getRequired().forEach(recipeResult -> {
if (recipeResult instanceof RecipeCalculationResult subRecipe2) {
addSubRecipe(subRecipe2, recipeLine, depth + 1);
} else if (recipeResult instanceof Ingredient ingredient) {
recipeLines.add(new RecipeListLine(recipeLine, depth + 1, ingredient));
}
});
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
package codes.cookies.mod.features.crafthelper;

import codes.cookies.mod.CookiesMod;
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.features.crafthelper.ui.RecipeListLine;
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 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;
Expand All @@ -20,14 +24,20 @@
import org.joml.Vector2i;
import org.joml.Vector2ic;

import java.lang.ref.WeakReference;
import java.util.Stack;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

public class CraftHelperManager {
@Getter
private static final Stack<CraftHelperItem> items = new Stack<>();
@Getter
private static CraftHelperLocation location;

private static WeakReference<CraftHelperPanel> panelRef = new WeakReference<>(null);

public static void init() {
location = ConfigManager.getConfig().helpersConfig.craftHelper.craftHelperLocation.getValue();
ConfigManager.getConfig().helpersConfig.craftHelper.craftHelperLocation.withCallback((oldValue, newValue) -> location = newValue);
Expand All @@ -37,22 +47,28 @@ public static void init() {
}

if (!SkyblockUtils.isCurrentlyInSkyblock()) {
//return; //todo
return;
}
if (!ConfigManager.getConfig().helpersConfig.craftHelper.craftHelper.getValue()) {
return;
}

if (items.isEmpty()) {
//return;
items.push(new CraftHelperItem(RepositoryItem.of("TERMINATOR"), 2)); //WAND_OF_RESTORATION
items.push(new CraftHelperItem(RepositoryItem.of("WAND_OF_RESTORATION"), 2)); //WAND_OF_RESTORATION
}

var panel = inventoryScreenAccessor.cookies$addDrawableChild(new CraftHelperPanel((screen.height - 166), calculateWidth(inventoryScreenAccessor)));

for (var line : items.peek().getLines()) {
for (var line : items.peek().getRecipeLines()) {
panel.addLine(line);
}
panelRef = new WeakReference<>(panel);

for (CraftHelperPanelLine line : panel.getLines()) {
if (line instanceof RecipeListLine recipeListLine) {
recipeListLine.update();
}
}

ScreenEvents.beforeRender(screen).register((screen1, drawContext, i, i1, v) -> onRender(inventoryScreenAccessor, panel, i, i1, v));
ScreenMouseEvents.allowMouseClick(screen).register(CraftHelperManager::onMouseClick);
Expand All @@ -62,11 +78,25 @@ public static void init() {
ScreenKeyEvents.getExtension(screen).cookies$allowCharTyped().register(CraftHelperManager::onCharTyped);
items.clear();
});

AtomicInteger ticksSinceLastUpdate = new AtomicInteger(0);
ClientTickEvents.END_CLIENT_TICK.register(client -> {
var panel = panelRef.get();
if (panel == null || ticksSinceLastUpdate.getAndIncrement() < 40) {
return;
}
ticksSinceLastUpdate.set(0);
panel.children().forEach(child -> {
if (child instanceof RecipeListLine recipeListLine) {
recipeListLine.update();
}
});
});
}

public static void onRender(InventoryScreenAccessor inventoryScreenAccessor, CraftHelperPanel panel, int i, int i1, float v) {
panel.setWidth(calculateWidth(inventoryScreenAccessor));
var position = getPosition(inventoryScreenAccessor);
var position = getPosition(inventoryScreenAccessor, panel);
panel.setX(position.x());
panel.setY(position.y());
}
Expand All @@ -93,12 +123,20 @@ private static int calculateX(InventoryScreenAccessor screen) {
};
}

private static Vector2ic getPosition(InventoryScreenAccessor screen) {
private static Vector2ic getPosition(InventoryScreenAccessor screen, CraftHelperPanel panel) {
int x = calculateX(screen);
int y = screen.cookies$getY() + screen.cookies$getBackgroundHeight() / 2;
preventOverflow(MinecraftClient.getInstance().getWindow().getScaledHeight(), new Vector2i(x, y), panel.getHeight());
return new Vector2i(x, y);
}

private static void preventOverflow(int screenHeight, Vector2i pos, int height) {
int i = height + 3;
if (pos.y + i > screenHeight) {
pos.y = screenHeight - i;
}
}

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

0 comments on commit 33ea56e

Please sign in to comment.