From b85fba36e6ba7ab2c90cb4dfc8ded3b2f23aa509 Mon Sep 17 00:00:00 2001 From: GrahamKracker Date: Sat, 28 Dec 2024 18:30:30 -0500 Subject: [PATCH] backup cause i almost lost it that was terrifying --- .../CraftHelperContext.java | 0 .../CraftHelperFormatter.java | 0 .../CraftHelperInstance.java | 6 +- .../CraftHelperManager.java | 15 +- .../EvaluationContext.java | 0 .../ItemTracker.java | 0 .../ToolTipContext.java | 0 .../tooltips/CraftHelperComponent.java | 0 .../tooltips/CraftHelperComponentPart.java | 0 .../tooltips/CraftHelperText.java | 0 .../tooltips/DebugComponent.java | 0 .../tooltips/HeadingPart.java | 6 +- .../tooltips/NormalComponent.java | 0 .../tooltips/SpacerComponent.java | 0 .../tooltips/TooltipFieldPart.java | 0 .../debug/OpenCraftHelperPlacementScreen.java | 18 +- ...etSelectedCraftHelperItemDebugCommand.java | 64 +-- .../mod/config/categories/HelpersConfig.java | 40 +- .../mod/data/profile/sub/CraftHelperData.java | 14 +- .../features/crafthelper/CraftHelperItem.java | 73 +++ .../crafthelper/CraftHelperLocation.java | 6 + .../crafthelper/CraftHelperManager.java | 132 ++++++ .../CraftHelperPlacementScreen.java} | 40 +- .../crafthelper/ui/CraftHelperComponent.java | 80 ++++ .../crafthelper/ui/CraftHelperPanel.java | 132 ++++++ .../crafthelper/ui/CraftHelperPanelLine.java | 108 +++++ .../ui/components/GroupedComponent.java | 50 ++ .../ui/components/ItemStackComponent.java | 37 ++ .../ui/components/MultiLineComponent.java | 50 ++ .../ui/components/RecipeComponent.java | 44 ++ .../components/ScrollableTextComponent.java | 25 + .../ui/components/SpacerComponent.java | 17 + .../ui/components/TextComponent.java | 92 ++++ .../farming/garden/PlotPriceBreakdown.java | 326 ++++++------- .../misc/utils/ModifyRecipeScreen.java | 78 ++-- .../features/misc/utils/UtilsFeatures.java | 15 +- .../crafthelper/CraftHelperLocation.java | 7 - .../CraftHelperTooltipPositioner.java | 37 -- .../mod/render/utils/RenderHelper.java | 97 ++-- .../screen/inventory/ForgeRecipeScreen.java | 200 ++++---- .../mod/services/item/ItemSearchService.java | 2 +- .../codes/cookies/mod/utils/RenderUtils.java | 432 ++++++++++-------- .../accessors/InventoryScreenAccessor.java | 70 +-- .../mod/utils/mixins/HandledScreenMixin.java | 36 +- .../inventories/ClientSideInventory.java | 19 +- 45 files changed, 1625 insertions(+), 743 deletions(-) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/CraftHelperContext.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/CraftHelperFormatter.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/CraftHelperInstance.java (99%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/CraftHelperManager.java (93%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/EvaluationContext.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/ItemTracker.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/ToolTipContext.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/tooltips/CraftHelperComponent.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/tooltips/CraftHelperComponentPart.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/tooltips/CraftHelperText.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/tooltips/DebugComponent.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/tooltips/HeadingPart.java (97%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/tooltips/NormalComponent.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/tooltips/SpacerComponent.java (100%) rename {src/main/java/codes/cookies/mod/features/misc/utils/crafthelper => crafthelper}/tooltips/TooltipFieldPart.java (100%) create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperItem.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperLocation.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperManager.java rename src/main/java/codes/cookies/mod/features/{misc/utils/crafthelper/CraftHelperPlacement.java => crafthelper/CraftHelperPlacementScreen.java} (72%) create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperComponent.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperPanel.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperPanelLine.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/ui/components/GroupedComponent.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/ui/components/ItemStackComponent.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/ui/components/MultiLineComponent.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/ui/components/RecipeComponent.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/ui/components/ScrollableTextComponent.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/ui/components/SpacerComponent.java create mode 100644 src/main/java/codes/cookies/mod/features/crafthelper/ui/components/TextComponent.java delete mode 100644 src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperLocation.java delete mode 100644 src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperTooltipPositioner.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperContext.java b/crafthelper/CraftHelperContext.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperContext.java rename to crafthelper/CraftHelperContext.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperFormatter.java b/crafthelper/CraftHelperFormatter.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperFormatter.java rename to crafthelper/CraftHelperFormatter.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperInstance.java b/crafthelper/CraftHelperInstance.java similarity index 99% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperInstance.java rename to crafthelper/CraftHelperInstance.java index 361cdac6..c0ccfce3 100644 --- a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperInstance.java +++ b/crafthelper/CraftHelperInstance.java @@ -71,7 +71,7 @@ public void recalculate() { calculate.ifSuccess(this::finishRecalculation).ifError(errorMessage -> { CookiesUtils.sendFailedMessage(Optional.ofNullable(errorMessage) .orElse("An error occurred while evaluating the recipe tree.")); - CraftHelperManager.remove(); + //CraftHelperManager.remove(); }); } @@ -131,13 +131,13 @@ public void render(DrawContext drawContext, int x, int y, int mouseX, int mouseY final ArrayList craftHelperComponents = new ArrayList<>(list); craftHelperComponents.addFirst(new SpacerComponent(0, width)); - drawContext.drawTooltip( + /*drawContext.drawTooltip( MinecraftClient.getInstance().textRenderer, craftHelperComponents, lastX, lastY, CraftHelperTooltipPositioner.INSTANCE, - null); + null);*/ final Optional> textUnder = getTextUnder(mouseX, mouseY, list); if (textUnder.isPresent()) { diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperManager.java b/crafthelper/CraftHelperManager.java similarity index 93% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperManager.java rename to crafthelper/CraftHelperManager.java index d0b5ee86..991cda10 100644 --- a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperManager.java +++ b/crafthelper/CraftHelperManager.java @@ -6,12 +6,14 @@ import codes.cookies.mod.config.ConfigManager; import codes.cookies.mod.events.api.ScreenKeyEvents; +import codes.cookies.mod.features.crafthelper.CraftHelperLocation; import codes.cookies.mod.repository.RepositoryItem; import codes.cookies.mod.utils.SkyblockUtils; import codes.cookies.mod.utils.accessors.InventoryScreenAccessor; import codes.cookies.mod.utils.compatibility.legendarytooltips.LegendaryTooltips; import lombok.Getter; import lombok.Setter; + import org.jetbrains.annotations.NotNull; import org.joml.Vector2ic; @@ -29,12 +31,13 @@ public class CraftHelperManager { @NotNull @Getter private static CraftHelperInstance active = CraftHelperInstance.EMPTY; - private static CraftHelperLocation location; + public static CraftHelperLocation location; 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) -> { + active = CraftHelperInstance.EMPTY; if (!(screen instanceof InventoryScreenAccessor)) { return; } @@ -48,16 +51,16 @@ public static void init() { active.recalculate(); } - ScreenEvents.afterRender(screen).register(CraftHelperListener::onRender); + /*ScreenEvents.afterRender(screen).register(CraftHelperListener::onRender); ScreenMouseEvents.allowMouseClick(screen).register(CraftHelperListener::onMouseClick); ScreenMouseEvents.allowMouseScroll(screen).register(CraftHelperListener::onMouseScroll); ScreenKeyboardEvents.allowKeyPress(screen).register(CraftHelperListener::onKeyPressed); ScreenKeyboardEvents.allowKeyRelease(screen).register(CraftHelperListener::onKeyReleased); - ScreenKeyEvents.getExtension(screen).cookies$allowCharTyped().register(CraftHelperListener::onCharTyped); + ScreenKeyEvents.getExtension(screen).cookies$allowCharTyped().register(CraftHelperListener::onCharTyped);*/ }); } - public static void pushNewCraftHelperItem(RepositoryItem repositoryItem, int amount) { + /*public static void pushNewCraftHelperItem(RepositoryItem repositoryItem, int amount) { active = new CraftHelperInstance(repositoryItem, amount, new ArrayList<>()); active.recalculate(); } @@ -72,8 +75,6 @@ private interface CraftHelperListener { private static int calculateX(Screen screen) { return switch (CraftHelperManager.location) { case LEFT -> 0; - case LEFT_INVENTORY -> InventoryScreenAccessor.getX(screen) - 1; - case RIGHT_INVENTORY -> InventoryScreenAccessor.getX(screen) + 1; case RIGHT -> screen.width; }; } @@ -154,5 +155,5 @@ static boolean onCharTyped(Screen screen, char chr, int modifiers) { return getCurrent().map(instance -> !instance.onCharTyped(chr, modifiers)).orElse(true); } } - +*/ } diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/EvaluationContext.java b/crafthelper/EvaluationContext.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/EvaluationContext.java rename to crafthelper/EvaluationContext.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/ItemTracker.java b/crafthelper/ItemTracker.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/ItemTracker.java rename to crafthelper/ItemTracker.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/ToolTipContext.java b/crafthelper/ToolTipContext.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/ToolTipContext.java rename to crafthelper/ToolTipContext.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/CraftHelperComponent.java b/crafthelper/tooltips/CraftHelperComponent.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/CraftHelperComponent.java rename to crafthelper/tooltips/CraftHelperComponent.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/CraftHelperComponentPart.java b/crafthelper/tooltips/CraftHelperComponentPart.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/CraftHelperComponentPart.java rename to crafthelper/tooltips/CraftHelperComponentPart.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/CraftHelperText.java b/crafthelper/tooltips/CraftHelperText.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/CraftHelperText.java rename to crafthelper/tooltips/CraftHelperText.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/DebugComponent.java b/crafthelper/tooltips/DebugComponent.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/DebugComponent.java rename to crafthelper/tooltips/DebugComponent.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/HeadingPart.java b/crafthelper/tooltips/HeadingPart.java similarity index 97% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/HeadingPart.java rename to crafthelper/tooltips/HeadingPart.java index f07e0f44..18dfe476 100644 --- a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/HeadingPart.java +++ b/crafthelper/tooltips/HeadingPart.java @@ -7,7 +7,7 @@ import codes.cookies.mod.CookiesMod; import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperInstance; import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperManager; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperPlacement; +import codes.cookies.mod.features.crafthelper.CraftHelperPlacement; import codes.cookies.mod.repository.RepositoryItem; import codes.cookies.mod.utils.RenderUtils; import codes.cookies.mod.utils.cookies.Constants; @@ -49,7 +49,7 @@ public HeadingPart( this.formattedName = CraftHelperText.of(createHeading()); this.close = CraftHelperText.of(Text.empty().append(createMove()).append(" ").append(createClose())); this.amountText = CraftHelperText.of(createAmount()); - this.tooltipField = new TooltipFieldPart(5,5, 18, 18); + this.tooltipField = new TooltipFieldPart(5, 5, 18, 18); this.tooltipField.setHoverText(getLore()); } @@ -64,7 +64,7 @@ public HeadingPart(RepositoryItem repositoryItem, CraftHelperInstance craftHelpe private void pressedClose() { SoundUtils.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP); - CraftHelperManager.remove(); + //CraftHelperManager.remove(); } @Override diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/NormalComponent.java b/crafthelper/tooltips/NormalComponent.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/NormalComponent.java rename to crafthelper/tooltips/NormalComponent.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/SpacerComponent.java b/crafthelper/tooltips/SpacerComponent.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/SpacerComponent.java rename to crafthelper/tooltips/SpacerComponent.java diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/TooltipFieldPart.java b/crafthelper/tooltips/TooltipFieldPart.java similarity index 100% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/tooltips/TooltipFieldPart.java rename to crafthelper/tooltips/TooltipFieldPart.java diff --git a/src/main/java/codes/cookies/mod/commands/dev/debug/OpenCraftHelperPlacementScreen.java b/src/main/java/codes/cookies/mod/commands/dev/debug/OpenCraftHelperPlacementScreen.java index 589854a2..7610a9f5 100644 --- a/src/main/java/codes/cookies/mod/commands/dev/debug/OpenCraftHelperPlacementScreen.java +++ b/src/main/java/codes/cookies/mod/commands/dev/debug/OpenCraftHelperPlacementScreen.java @@ -1,20 +1,22 @@ package codes.cookies.mod.commands.dev.debug; +import codes.cookies.mod.features.crafthelper.CraftHelperPlacementScreen; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import codes.cookies.mod.CookiesMod; import codes.cookies.mod.commands.system.ClientCommand; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperPlacement; + import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; + import org.jetbrains.annotations.NotNull; public class OpenCraftHelperPlacementScreen extends ClientCommand { - @Override - public @NotNull LiteralArgumentBuilder getCommand() { - return literal("openCraftHelperPlacementScreen").executes(run(this::openScreen)); - } + @Override + public @NotNull LiteralArgumentBuilder getCommand() { + return literal("openCraftHelperPlacementScreen").executes(run(this::openScreen)); + } - private void openScreen() { - CookiesMod.openScreen(new CraftHelperPlacement()); - } + private void openScreen() { + CookiesMod.openScreen(new CraftHelperPlacementScreen()); + } } diff --git a/src/main/java/codes/cookies/mod/commands/dev/debug/SetSelectedCraftHelperItemDebugCommand.java b/src/main/java/codes/cookies/mod/commands/dev/debug/SetSelectedCraftHelperItemDebugCommand.java index bca90f3b..47a815a0 100644 --- a/src/main/java/codes/cookies/mod/commands/dev/debug/SetSelectedCraftHelperItemDebugCommand.java +++ b/src/main/java/codes/cookies/mod/commands/dev/debug/SetSelectedCraftHelperItemDebugCommand.java @@ -1,49 +1,53 @@ package codes.cookies.mod.commands.dev.debug; +import codes.cookies.mod.features.crafthelper.CraftHelperManager; import com.mojang.brigadier.builder.LiteralArgumentBuilder; import com.mojang.brigadier.context.CommandContext; import codes.cookies.mod.commands.arguments.RealIdentifierArgument; import codes.cookies.mod.commands.system.ClientCommand; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperManager; import codes.cookies.mod.repository.RepositoryItem; + import java.util.ArrayList; import java.util.List; import java.util.Locale; + import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource; + import net.minecraft.util.Identifier; + import org.jetbrains.annotations.NotNull; /** * */ public class SetSelectedCraftHelperItemDebugCommand extends ClientCommand { - @Override - public @NotNull LiteralArgumentBuilder getCommand() { - List identifiers = new ArrayList<>(); - RepositoryItem.getItemMap().keySet().forEach( - item -> identifiers.add( - Identifier.of("skyblock", item.replace(":", "/").replace("-", "/").replace(";", "/").toLowerCase(Locale.ROOT)) - ) - ); - - return literal("setSelectedCraftHelperItem") - .then( - argument( - "item", - new RealIdentifierArgument(identifiers) - ).executes(run(this::setSelectedItem)) - ); - } - - private void setSelectedItem(CommandContext context) { - final Identifier item = context.getArgument("item", Identifier.class); - final RepositoryItem repositoryItem = RepositoryItem.of(item.getPath()); - - if (repositoryItem != null) { - CraftHelperManager.pushNewCraftHelperItem(repositoryItem, 1); - sendSuccessMessage("Switched to item " + item); - } else { - sendFailedMessage("Could not find item " + item.getPath()); - } - } + @Override + public @NotNull LiteralArgumentBuilder getCommand() { + List identifiers = new ArrayList<>(); + RepositoryItem.getItemMap().keySet().forEach( + item -> identifiers.add( + Identifier.of("skyblock", item.replace(":", "/").replace("-", "/").replace(";", "/").toLowerCase(Locale.ROOT)) + ) + ); + + return literal("setSelectedCraftHelperItem") + .then( + argument( + "item", + new RealIdentifierArgument(identifiers) + ).executes(run(this::setSelectedItem)) + ); + } + + private void setSelectedItem(CommandContext context) { + final Identifier item = context.getArgument("item", Identifier.class); + final RepositoryItem repositoryItem = RepositoryItem.of(item.getPath()); + + if (repositoryItem != null) { + CraftHelperManager.pushNewCraftHelperItem(repositoryItem, 1); + sendSuccessMessage("Switched to item " + item); + } else { + sendFailedMessage("Could not find item " + item.getPath()); + } + } } diff --git a/src/main/java/codes/cookies/mod/config/categories/HelpersConfig.java b/src/main/java/codes/cookies/mod/config/categories/HelpersConfig.java index a204af9f..334920b4 100644 --- a/src/main/java/codes/cookies/mod/config/categories/HelpersConfig.java +++ b/src/main/java/codes/cookies/mod/config/categories/HelpersConfig.java @@ -10,8 +10,8 @@ import codes.cookies.mod.config.system.options.EnumCycleOption; import codes.cookies.mod.config.system.options.SliderOption; import codes.cookies.mod.data.profile.items.ItemSources; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperLocation; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperPlacement; +import codes.cookies.mod.features.crafthelper.CraftHelperLocation; +import codes.cookies.mod.features.crafthelper.CraftHelperPlacementScreen; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; @@ -26,21 +26,21 @@ public class HelpersConfig extends Category { - public BooleanOption anvilHelper = new BooleanOption(CONFIG_HELPERS_ANVIL_HELPER, false); + public BooleanOption anvilHelper = new BooleanOption(CONFIG_HELPERS_ANVIL_HELPER, false); - public BooleanOption itemChestTracker = new BooleanOption(CONFIG_HELPERS_CHEST_TRACKER, true); + public BooleanOption itemChestTracker = new BooleanOption(CONFIG_HELPERS_CHEST_TRACKER, true); public CraftHelperFoldable craftHelper = new CraftHelperFoldable(); - @Hidden - public SliderOption craftHelperSlot = SliderOption.integerOption("", 14); + @Hidden + public SliderOption craftHelperSlot = SliderOption.integerOption("", 14); public class CraftHelperFoldable extends Foldable { public BooleanOption craftHelper = new BooleanOption(CONFIG_HELPERS_CRAFT_HELPER_SETTING, true); @Hidden public EnumCycleOption craftHelperLocation = - new EnumCycleOption<>("", CraftHelperLocation.RIGHT_INVENTORY); + new EnumCycleOption<>("", CraftHelperLocation.RIGHT); public ButtonOption openCraftHelperLocationEditor = new ButtonOption(CONFIG_HELPERS_CRAFT_HELPER_LOCATIONS, this::openCraftHelperEditor, @@ -99,7 +99,7 @@ public String getName() { } private void openCraftHelperEditor() { - CookiesMod.openScreen(new CraftHelperPlacement()); + CookiesMod.openScreen(new CraftHelperPlacementScreen()); } @Override @@ -110,16 +110,16 @@ public String getName() { public HelpersConfig() { - super(new ItemStack(Items.SCAFFOLDING), CONFIG_HELPERS); - } - - @Override - public Row getRow() { - return Row.BOTTOM; - } - - @Override - public int getColumn() { - return 0; - } + super(new ItemStack(Items.SCAFFOLDING), CONFIG_HELPERS); + } + + @Override + public Row getRow() { + return Row.BOTTOM; + } + + @Override + public int getColumn() { + return 0; + } } diff --git a/src/main/java/codes/cookies/mod/data/profile/sub/CraftHelperData.java b/src/main/java/codes/cookies/mod/data/profile/sub/CraftHelperData.java index bce4152a..62c56991 100644 --- a/src/main/java/codes/cookies/mod/data/profile/sub/CraftHelperData.java +++ b/src/main/java/codes/cookies/mod/data/profile/sub/CraftHelperData.java @@ -3,17 +3,16 @@ import java.util.Optional; import java.util.function.Predicate; -import com.mojang.serialization.Codec; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperInstance; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperManager; +import codes.cookies.mod.features.crafthelper.CraftHelperItem; import codes.cookies.mod.utils.json.CodecJsonSerializable; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -public class CraftHelperData implements CodecJsonSerializable { +//todo +public class CraftHelperData { /*implements CodecJsonSerializable { public CraftHelperData() { - CraftHelperManager.remove(); + CraftHelperManager.setActive(CraftHelperInstance.EMPTY); } private static final Logger LOGGER = LoggerFactory.getLogger(CraftHelperData.class); @@ -25,7 +24,8 @@ public Codec getCodec() { @Override public void load(CraftHelperInstance value) { - CraftHelperManager.setActive(value); + codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperManager.setActive(value); + codes.cookies.mod.features.crafthelper.CraftHelperManager.pushNewCraftHelperItem(new CraftHelperItem(value.getRepositoryItem())); } @Override @@ -38,5 +38,5 @@ public CraftHelperInstance getValue() { @Override public Logger getLogger() { return LOGGER; - } + }*/ } diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperItem.java b/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperItem.java new file mode 100644 index 00000000..ca0848e1 --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperItem.java @@ -0,0 +1,73 @@ +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.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; + +@Getter +public class CraftHelperItem { + /*public static final Codec CODEC = RecordCodecBuilder.create(instance -> instance.group( + RepositoryItem.ID_CODEC.fieldOf("id").forGetter(CraftHelperInstance::getRepositoryItem), + Codecs.NON_NEGATIVE_INT.fieldOf("amount").forGetter(CraftHelperInstance::getAmount), + Codec.STRING.listOf().fieldOf("collapsed").forGetter(CraftHelperInstance::getCollapsed)) + .apply(instance, CraftHelperInstance::new));*/ + + final RepositoryItem repositoryItem; + private final int amount; + private boolean recalculated = false; + private final List lines = new ArrayList<>(); + + public CraftHelperItem(RepositoryItem repositoryItem, int amount) { + this.repositoryItem = repositoryItem; + this.amount = amount; + recalculate(); + } + + public void recalculate() { + if (this.recalculated) { + return; + } + this.recalculated = true; + final var calculate = RecipeCalculator.calculate(repositoryItem); + calculate.ifSuccess(this::finishRecalculation).ifError(errorMessage -> { + CookiesUtils.sendFailedMessage(Optional.ofNullable(errorMessage) + .orElse("An error occurred while evaluating the recipe tree.")); + }); + } + + private void finishRecalculation(RecipeCalculationResult recipeCalculationResult) { + lines.clear(); + lines.add(new CraftHelperPanelLine(new RecipeComponent(recipeCalculationResult, 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))); + } + } +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperLocation.java b/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperLocation.java new file mode 100644 index 00000000..0ab04e1b --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperLocation.java @@ -0,0 +1,6 @@ +package codes.cookies.mod.features.crafthelper; + +public enum CraftHelperLocation { + LEFT, RIGHT + //todo migrate config/make corrupted config not crash the game +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperManager.java b/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperManager.java new file mode 100644 index 00000000..d640ddcb --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperManager.java @@ -0,0 +1,132 @@ +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 lombok.Getter; + +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 org.joml.Vector2i; +import org.joml.Vector2ic; + +import java.util.Stack; + +public class CraftHelperManager { + @Getter + private static final Stack items = new Stack<>(); + @Getter + private static CraftHelperLocation location; + + 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 (!(screen instanceof InventoryScreenAccessor inventoryScreenAccessor)) { + return; + } + + if (!SkyblockUtils.isCurrentlyInSkyblock()) { + //return; //todo + } + if (!ConfigManager.getConfig().helpersConfig.craftHelper.craftHelper.getValue()) { + return; + } + + if (items.isEmpty()) { + //return; + items.push(new CraftHelperItem(RepositoryItem.of("TERMINATOR"), 2)); //WAND_OF_RESTORATION + } + + var panel = inventoryScreenAccessor.cookies$addDrawableChild(new CraftHelperPanel((screen.height - 166), calculateWidth(inventoryScreenAccessor))); + + for (var line : items.peek().getLines()) { + panel.addLine(line); + } + + ScreenEvents.beforeRender(screen).register((screen1, drawContext, i, i1, v) -> onRender(inventoryScreenAccessor, panel, i, i1, v)); + 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); + items.clear(); + }); + } + + public static void onRender(InventoryScreenAccessor inventoryScreenAccessor, CraftHelperPanel panel, int i, int i1, float v) { + panel.setWidth(calculateWidth(inventoryScreenAccessor)); + var position = getPosition(inventoryScreenAccessor); + panel.setX(position.x()); + panel.setY(position.y()); + } + + 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 + return (((Screen) screen).width - screen.cookies$getBackgroundWidth()) / 2; + } + + private static int calculateWidth(InventoryScreenAccessor screen) { + return switch (location) { + case LEFT -> calculateLeftEdge(screen) - 3; + case RIGHT -> MinecraftClient.getInstance().getWindow().getScaledWidth() - calculateRightEdge(screen) - 3; + }; + } + + private static int calculateX(InventoryScreenAccessor screen) { + return switch (location) { + case LEFT -> 0; + case RIGHT -> calculateRightEdge(screen); + }; + } + + private static Vector2ic getPosition(InventoryScreenAccessor screen) { + int x = calculateX(screen); + int y = screen.cookies$getY() + screen.cookies$getBackgroundHeight() / 2; + return new Vector2i(x, y); + } + + 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.push(item); + } + + public static void pushNewCraftHelperItem(RepositoryItem repositoryItemNotNull, int i) { + //todo just cause im lazy + } + + public static void close() { + } +} diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperPlacement.java b/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperPlacementScreen.java similarity index 72% rename from src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperPlacement.java rename to src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperPlacementScreen.java index 4d24b46c..dde8d67a 100644 --- a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperPlacement.java +++ b/src/main/java/codes/cookies/mod/features/crafthelper/CraftHelperPlacementScreen.java @@ -1,6 +1,4 @@ -package codes.cookies.mod.features.misc.utils.crafthelper; - -import java.util.ArrayList; +package codes.cookies.mod.features.crafthelper; import codes.cookies.mod.config.ConfigManager; import codes.cookies.mod.repository.RepositoryItem; @@ -10,33 +8,36 @@ import codes.cookies.mod.utils.skyblock.inventories.ClientSideInventory; import codes.cookies.mod.utils.skyblock.inventories.ItemBuilder; +import net.minecraft.client.gui.Drawable; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; import net.minecraft.item.Items; import net.minecraft.text.Text; -public class CraftHelperPlacement extends ClientSideInventory { +public class CraftHelperPlacementScreen extends ClientSideInventory { private static final int UNSELECTED_COLOR = Constants.SUCCESS_COLOR; private final PressableField[] fields = new PressableField[CraftHelperLocation.values().length]; boolean removeItemAfterwards = false; private CraftHelperLocation selected; - CraftHelperInstance defaultInstance; + CraftHelperItem defaultInstance; boolean isClosed = false; - public CraftHelperPlacement() { + public CraftHelperPlacementScreen() { super(Text.translatable(TranslationKeys.CRAFT_HELPER_PLACEMENT), 6); this.getContents().fill(new ItemBuilder(Items.BLACK_STAINED_GLASS_PANE).hideTooltips().build()); this.selected = ConfigManager.getConfig().helpersConfig.craftHelper.craftHelperLocation.getValue(); - this.defaultInstance = new CraftHelperInstance(RepositoryItem.of("TERMINATOR"), 1, new ArrayList<>()); + this.defaultInstance = new CraftHelperItem(RepositoryItem.of("TERMINATOR"), 1); } @Override public void tick() { - if (CraftHelperManager.getActive() == CraftHelperInstance.EMPTY && !isClosed) { - CraftHelperManager.setActive(defaultInstance); + if (CraftHelperManager.getItems().isEmpty() && !isClosed) { + CraftHelperManager.getItems().push(defaultInstance); removeItemAfterwards = true; - if (!defaultInstance.hasCalculated) { - defaultInstance.recalculate(); - } + //if (!defaultInstance.isHasCalculated()) { + defaultInstance.recalculate(); + //} } } @@ -46,18 +47,6 @@ protected void init() { this.add( new PressableField(0, this.getY() + 10, 10, this.cookies$getBackgroundHeight() - 20, UNSELECTED_COLOR), CraftHelperLocation.LEFT); - this.add(new PressableField( - this.getX() - 9, - this.getY() + 10, - 10, - this.cookies$getBackgroundHeight() - 20, - UNSELECTED_COLOR), CraftHelperLocation.LEFT_INVENTORY); - this.add(new PressableField( - this.getX() + this.cookies$getBackgroundWidth() - 1, - this.getY() + 10, - 10, - this.cookies$getBackgroundHeight() - 20, - UNSELECTED_COLOR), CraftHelperLocation.RIGHT_INVENTORY); this.add(new PressableField( this.width - 10, this.getY() + 10, @@ -98,8 +87,9 @@ public void close() { super.close(); isClosed = true; if (this.removeItemAfterwards) { - CraftHelperManager.remove(); + CraftHelperManager.getItems().pop(); } ConfigManager.saveConfig(true, "edit-craft-helper-location"); } + } diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperComponent.java b/src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperComponent.java new file mode 100644 index 00000000..ee2966f9 --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperComponent.java @@ -0,0 +1,80 @@ +package codes.cookies.mod.features.crafthelper.ui; + +import codes.cookies.mod.screen.CookiesScreen; +import codes.cookies.mod.utils.cookies.CookiesUtils; +import lombok.Getter; + +import lombok.Setter; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.Drawable; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; + +@Getter +@Setter +public abstract class CraftHelperComponent implements Element, Selectable, Drawable { + protected int height; + + protected int x; + protected int y; + protected int width; + private boolean focused; + + public int getWidth() { + return leftOffset + width + rightOffset; + } + + protected int leftOffset = 0; + protected int rightOffset = 0; + + protected static TextRenderer getTextRenderer() { + return MinecraftClient.getInstance().textRenderer; + } + + public CraftHelperComponent(int width, int height) { + this.width = width; + this.height = height; + } + + public CraftHelperComponent withLeftOffset(int leftOffset) { + this.leftOffset = leftOffset; + return this; + } + + public CraftHelperComponent withRightOffset(int rightOffset) { + this.rightOffset = rightOffset; + return this; + } + + @Override + public void setFocused(boolean focused) { + this.focused = focused; + } + + @Override + public boolean isFocused() { + return focused; + } + + @Override + public SelectionType getType() { + return null; + } + + @Override + public boolean isMouseOver(double mouseX, double mouseY) { + return CookiesScreen.isInBound((int) mouseX, (int) mouseY, x, y, width, height); + } + + @Override + public void appendNarrations(NarrationMessageBuilder builder) { + } + + @Override + public boolean mouseScrolled(double mouseX, double mouseY, double horizontalAmount, double verticalAmount) { + return Element.super.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount); + } +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperPanel.java b/src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperPanel.java new file mode 100644 index 00000000..89a4f4d1 --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperPanel.java @@ -0,0 +1,132 @@ +package codes.cookies.mod.features.crafthelper.ui; + +import codes.cookies.mod.features.crafthelper.CraftHelperItem; + +import codes.cookies.mod.features.crafthelper.CraftHelperManager; + +import codes.cookies.mod.features.crafthelper.ui.components.SpacerComponent; +import codes.cookies.mod.features.crafthelper.ui.components.TextComponent; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; +import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; +import net.minecraft.client.gui.widget.ContainerWidget; +import net.minecraft.client.render.RenderLayer; +import net.minecraft.text.Text; +import net.minecraft.util.Identifier; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +public class CraftHelperPanel extends ContainerWidget implements Element, Selectable { + private final List lines = new ArrayList<>(); + + private final CraftHelperPanelLine Spacer = new CraftHelperPanelLine() {{ + addChildren(new SpacerComponent(15, MinecraftClient.getInstance().textRenderer.fontHeight)); + }}; + + private boolean focused; + + public CraftHelperPanel(int height, int width) { + super(0, 0, width, height, Text.of("")); + createWidgets(CraftHelperManager.getItems().peek()); + } + + public void createWidgets(CraftHelperItem item) { + lines.clear(); + addLine(CraftHelperPanelLine.createHeader(item, new CraftHelperPanelLine(), this)); + + addLine(Spacer); + } + + public void addLine(CraftHelperPanelLine line) { + addLine(line, false); + } + + public void addLine(CraftHelperPanelLine line, boolean spacer) { + if (spacer) { + line.children().addFirst(new SpacerComponent(15, 0)); + } + lines.add(line); + } + + @Override + protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) { + setY(getY() + scrollDelta); + int totalHeight = 14; + for (CraftHelperPanelLine craftHelperPanelLine : lines) { + int line1Height = craftHelperPanelLine.getHeight(); + totalHeight += line1Height + 1; + } + totalHeight += 14; + + context.getMatrices().push(); + context.getMatrices().translate(0, 0, 100); + 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.getMatrices().pop(); + + context.getMatrices().push(); + context.getMatrices().translate(0, 0, 200); + + AtomicInteger widgetY = new AtomicInteger(getY() + 14); + lines.forEach(line -> { + line.setX(this.getX() + 15); + line.setY(widgetY.getAndAdd(line.getHeight() + 1)); + line.setWidth(this.width); + line.render(context, mouseX, mouseY, delta); + }); + + context.getMatrices().pop(); + } + + @Override + protected void appendClickableNarrations(NarrationMessageBuilder builder) { + } + + @Override + public List children() { + return lines; + } + + @Override + public void setFocused(boolean focused) { + this.focused = focused; + } + + @Override + public boolean isFocused() { + return focused; + } + + @Override + public SelectionType getType() { + if (isFocused()) { + return SelectionType.FOCUSED; + } + if (isMouseOver(MinecraftClient.getInstance().mouse.getX(), MinecraftClient.getInstance().mouse.getY())) { + return SelectionType.HOVERED; + } + return SelectionType.NONE; + } + + @Override + public boolean isMouseOver(double mouseX, double mouseY) { + return mouseX >= getX() && mouseX <= getX() + width; + } + + private int scrollDelta = -100; + + @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; + return true; + } +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperPanelLine.java b/src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperPanelLine.java new file mode 100644 index 00000000..8a43b1b7 --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/ui/CraftHelperPanelLine.java @@ -0,0 +1,108 @@ +package codes.cookies.mod.features.crafthelper.ui; + +import codes.cookies.mod.features.crafthelper.CraftHelperItem; +import codes.cookies.mod.features.crafthelper.CraftHelperManager; +import codes.cookies.mod.features.crafthelper.CraftHelperPlacementScreen; +import codes.cookies.mod.features.crafthelper.ui.components.GroupedComponent; +import codes.cookies.mod.features.crafthelper.ui.components.ItemStackComponent; +import codes.cookies.mod.features.crafthelper.ui.components.ScrollableTextComponent; +import codes.cookies.mod.features.crafthelper.ui.components.TextComponent; +import codes.cookies.mod.utils.cookies.Constants; +import codes.cookies.mod.utils.minecraft.TextBuilder; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.screen.narration.NarrationMessageBuilder; +import net.minecraft.client.gui.widget.ContainerWidget; +import net.minecraft.text.Text; +import net.minecraft.util.Formatting; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +public class CraftHelperPanelLine extends ContainerWidget { + + public CraftHelperPanelLine withHeight(int height) { + this.height = height; + return this; + } + + public static CraftHelperPanelLine createHeader(CraftHelperItem item, CraftHelperPanelLine line, CraftHelperPanel parent) { + var stack = item.getRepositoryItem().constructItemStack(); + line.addChildren(new ItemStackComponent(stack).withRightOffset(5)); + + var moveLeftComponent = new TextComponent(Text.literal("<")).withRightOffset(5); + var scrollableNameComponent = new ScrollableTextComponent(Text.of(item.getRepositoryItem().getFormattedName()), 95).withRightOffset(5); + var moveRightComponent = new TextComponent(Text.literal(">")).withRightOffset(5); + var centerGroup = new GroupedComponent(moveLeftComponent, scrollableNameComponent, moveRightComponent); + line.addChildren(centerGroup); + + centerGroup.setLeftOffset(((parent.getWidth() - centerGroup.getWidth()) / 2) - centerGroup.x); + line.updateChildren(); + + + var moveButton = new TextComponent(new TextBuilder(Constants.Emojis.MOVE) + .setRunnable(() -> { + if (MinecraftClient.getInstance().currentScreen instanceof CraftHelperPlacementScreen) { + return; + } + + MinecraftClient.getInstance().setScreenAndRender(new CraftHelperPlacementScreen()); + }) + .onHover(List.of(Text.literal("Click to move").formatted(Formatting.AQUA))) + .formatted(Formatting.AQUA).build()); + + var closeButton = new TextComponent(new TextBuilder(Constants.Emojis.NO).formatted(Formatting.RED, Formatting.BOLD) + .setRunnable(CraftHelperManager::close) + .onHover(List.of(Text.literal("Close").formatted(Formatting.RED))) + .build()).withLeftOffset(2); + var rightGroup = new GroupedComponent(moveButton, closeButton); + + line.addChildren(rightGroup); + rightGroup.setLeftOffset(parent.getWidth() - line.getWidth() - rightGroup.width - 10); + line.updateChildren(); + + return line; + } + + private final List children = new ArrayList<>(); + + public CraftHelperPanelLine(CraftHelperComponent... children) { + super(0, 0, 0, 0, Text.of("")); + addChildren(children); + } + + @Override + protected void renderWidget(DrawContext context, int mouseX, int mouseY, float delta) { + updateChildren(); + children.forEach(component -> { + component.render(context, mouseX, mouseY, delta); + }); + } + + private void updateChildren() { + AtomicInteger xOffset = new AtomicInteger(getX()); + children.forEach(component -> { + component.setX(xOffset.addAndGet(component.getLeftOffset())); + xOffset.addAndGet(component.getWidth() - component.getLeftOffset()); + component.setY(this.getY()); + }); + this.height = this.children.stream().mapToInt(CraftHelperComponent::getHeight).max().orElse(0); + this.width = this.children.stream().mapToInt(CraftHelperComponent::getWidth).sum(); + } + + public void addChildren(CraftHelperComponent... children) { + this.children.addAll(List.of(children)); + updateChildren(); + } + + @Override + protected void appendClickableNarrations(NarrationMessageBuilder builder) { + } + + @Override + public List children() { + return children; + } +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/GroupedComponent.java b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/GroupedComponent.java new file mode 100644 index 00000000..bc6fa80e --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/GroupedComponent.java @@ -0,0 +1,50 @@ +package codes.cookies.mod.features.crafthelper.ui.components; + +import codes.cookies.mod.features.crafthelper.ui.CraftHelperComponent; + +import com.mojang.logging.LogUtils; + +import net.minecraft.client.gui.DrawContext; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + +public class GroupedComponent extends CraftHelperComponent { + + protected final List children = new ArrayList<>(); + + public GroupedComponent(CraftHelperComponent... children) { + super(0, 0); + this.children.addAll(List.of(children)); + this.recalculate(); + } + + protected void recalculate() { + this.width = children.stream().mapToInt(CraftHelperComponent::getWidth).sum(); + this.height = children.stream().mapToInt(CraftHelperComponent::getHeight).max().orElse(0); + + } + + public void add(CraftHelperComponent... component) { + children.addAll(Arrays.stream(component).toList()); + recalculate(); + } + + public void remove(CraftHelperComponent component) { + children.remove(component); + recalculate(); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + int x = getX(); + for (CraftHelperComponent child : children) { + x += child.getLeftOffset(); + child.setX(x); + child.setY(getY()); + child.render(context, mouseX, mouseY, delta); + x += child.getWidth() + child.getRightOffset() - child.getLeftOffset(); + } + } +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/ItemStackComponent.java b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/ItemStackComponent.java new file mode 100644 index 00000000..09cb6c28 --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/ItemStackComponent.java @@ -0,0 +1,37 @@ +package codes.cookies.mod.features.crafthelper.ui.components; + +import codes.cookies.mod.features.crafthelper.ui.CraftHelperComponent; + +import codes.cookies.mod.screen.CookiesScreen; +import codes.cookies.mod.utils.RenderUtils; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.render.RenderLayer; +import net.minecraft.item.ItemStack; +import net.minecraft.util.Identifier; + +public class ItemStackComponent extends CraftHelperComponent { + private final ItemStack stack; + + public ItemStackComponent(ItemStack stack) { + super(26, 26); + this.stack = stack; + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + var textRenderer = MinecraftClient.getInstance().textRenderer; + RenderUtils.renderBackgroundBox(context, x, y, 26, 26, -1); + + context.drawTexture(RenderLayer::getGuiTextured, Identifier.of("cookies-mod", "textures/gui/slot.png"), x + 4, y + 4, 0, 0, 18, 18, 18, 18); + context.getMatrices().push(); + context.getMatrices().translate(0, 0, -100); + context.drawItem(stack, x + 5, y + 5, (int) System.currentTimeMillis()); + context.drawStackOverlay(textRenderer, stack, x + 1, y + 1); + if (CookiesScreen.isInBound(mouseX, mouseY, x, y, 26, 26)) { + context.drawItemTooltip(textRenderer, stack, mouseX, mouseY); + } + context.getMatrices().pop(); + } +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/MultiLineComponent.java b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/MultiLineComponent.java new file mode 100644 index 00000000..aaf50d02 --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/MultiLineComponent.java @@ -0,0 +1,50 @@ +package codes.cookies.mod.features.crafthelper.ui.components; + +import codes.cookies.mod.features.crafthelper.ui.CraftHelperComponent; + +import net.minecraft.client.gui.DrawContext; + +import java.util.ArrayList; +import java.util.List; + +public class MultiLineComponent extends GroupedComponent { + protected final List children = new ArrayList<>(); + + public MultiLineComponent(CraftHelperComponent... children) { + super(children); + } + + @Override + protected void recalculate() { + this.width = children.stream().mapToInt(CraftHelperComponent::getWidth).max().orElse(0); + this.height = children.stream().mapToInt(CraftHelperComponent::getHeight).sum(); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + int x = getX(); + int y = getY(); + for (CraftHelperComponent child : children) { + x += child.getLeftOffset(); + child.setX(x); + if (child instanceof NewLineComponent) { + x = getX(); + y += child.getHeight(); + continue; + } + child.setY(getY()); + child.render(context, mouseX, mouseY, delta); + x += child.getWidth() + child.getRightOffset() - child.getLeftOffset(); + } + } + + public static class NewLineComponent extends CraftHelperComponent { + public NewLineComponent(int height) { + super(0, height); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + } + } +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/RecipeComponent.java b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/RecipeComponent.java new file mode 100644 index 00000000..3f3ab1e8 --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/RecipeComponent.java @@ -0,0 +1,44 @@ +package codes.cookies.mod.features.crafthelper.ui.components; + +import codes.cookies.mod.features.crafthelper.ui.CraftHelperComponent; +import codes.cookies.mod.repository.Ingredient; +import codes.cookies.mod.repository.recipes.calculations.RecipeCalculationResult; +import codes.cookies.mod.repository.recipes.calculations.RecipeResult; +import codes.cookies.mod.utils.cookies.Constants; + +public class RecipeComponent extends MultiLineComponent { + private final RecipeResult recipe; + private final int depth; + + public RecipeComponent(RecipeResult recipe, int depth, CraftHelperComponent... children) { + super(children); + this.recipe = recipe; + this.depth = depth; + StringBuilder builder = new StringBuilder(); + if (depth == 0) { + builder.append(Constants.Emojis.FLAG_EMPTY + " "); + } else { + builder.append("|"); + } + builder.append(" ".repeat(depth)); + if (recipe instanceof RecipeCalculationResult subResult) { + builder.append(subResult.getIngredient().getNameSafe()); + + /*subResult.getRequired().forEach(recipeResult -> { + getRecipe(recipeResult, depth + 1); + });*/ + } else if (recipe instanceof Ingredient ingredient) { + this.add(new TextComponent(" ".repeat(depth + 1) + Constants.Emojis.FLAG_EMPTY + " " + ingredient.getNameSafe())); + } + this.add(new TextComponent(builder.toString())); + } + + private void addRecipe(RecipeCalculationResult recipe, int depth) { + this.add(new RecipeComponent(recipe, depth)); + } + + private void addIngredient(Ingredient ingredient, int depth) { + this.add(new TextComponent(" ".repeat(depth + 1) + Constants.Emojis.FLAG_EMPTY + " " + ingredient.getNameSafe())); + } + +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/ScrollableTextComponent.java b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/ScrollableTextComponent.java new file mode 100644 index 00000000..50fae83d --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/ScrollableTextComponent.java @@ -0,0 +1,25 @@ +package codes.cookies.mod.features.crafthelper.ui.components; + +import codes.cookies.mod.features.crafthelper.ui.CraftHelperComponent; + +import codes.cookies.mod.utils.RenderUtils; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.text.Text; +import net.minecraft.util.Colors; + +public class ScrollableTextComponent extends CraftHelperComponent { + private final Text text; + + public ScrollableTextComponent(Text text, int width) { + super(width, MinecraftClient.getInstance().textRenderer.fontHeight); + this.text = text; + this.width = Math.min(width, MinecraftClient.getInstance().textRenderer.getWidth(text)); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + RenderUtils.drawScrollableText(context, MinecraftClient.getInstance().textRenderer, this.text, x, y - 1, x + width, y + MinecraftClient.getInstance().textRenderer.fontHeight, Colors.WHITE); + } +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/SpacerComponent.java b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/SpacerComponent.java new file mode 100644 index 00000000..63557e3a --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/SpacerComponent.java @@ -0,0 +1,17 @@ +package codes.cookies.mod.features.crafthelper.ui.components; + + +import codes.cookies.mod.features.crafthelper.ui.CraftHelperComponent; + +import net.minecraft.client.gui.DrawContext; + +public class SpacerComponent extends CraftHelperComponent { + public SpacerComponent(int width, int height) { + super(width, height); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + + } +} diff --git a/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/TextComponent.java b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/TextComponent.java new file mode 100644 index 00000000..1d43bff2 --- /dev/null +++ b/src/main/java/codes/cookies/mod/features/crafthelper/ui/components/TextComponent.java @@ -0,0 +1,92 @@ +package codes.cookies.mod.features.crafthelper.ui.components; + +import codes.cookies.mod.features.crafthelper.ui.CraftHelperComponent; + +import codes.cookies.mod.screen.CookiesScreen; + +import codes.cookies.mod.utils.accessors.ClickEventAccessor; +import codes.cookies.mod.utils.accessors.HoverEventAccessor; + +import net.minecraft.client.MinecraftClient; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.tooltip.HoveredTooltipPositioner; +import net.minecraft.text.OrderedText; +import net.minecraft.text.Style; +import net.minecraft.text.Text; +import net.minecraft.util.Colors; + +import org.lwjgl.glfw.GLFW; + +import java.util.List; +import java.util.Optional; + +public class TextComponent extends CraftHelperComponent { + protected OrderedText text; + + public TextComponent(Text text) { + super(getTextRenderer().getWidth(text), MinecraftClient.getInstance().textRenderer.fontHeight); + this.text = text.asOrderedText(); + } + + public TextComponent(String text) { + this(Text.of(text)); + } + + @Override + public void render(DrawContext context, int mouseX, int mouseY, float delta) { + context.drawText(getTextRenderer(), text, x, y, Colors.WHITE, true); + + final Style styleAt = MinecraftClient.getInstance().textRenderer.getTextHandler() + .getStyleAt(text, x - mouseX); + + if (styleAt == null) { + return; + } + + if (CookiesScreen.isInBound(mouseX, mouseY, x + (x - mouseX), y, width, height)) { + if (GLFW.glfwGetMouseButton(MinecraftClient.getInstance().getWindow().getHandle(), GLFW.GLFW_MOUSE_BUTTON_LEFT) == GLFW.GLFW_PRESS && styleAt.getClickEvent() != null) { + final Optional runnable = ClickEventAccessor.getRunnable(styleAt.getClickEvent()); + if (runnable.isPresent()) { + runnable.ifPresent(Runnable::run); + } + } + if (styleAt.getHoverEvent() != null) { + final Optional> hoverText = HoverEventAccessor.getText(styleAt.getHoverEvent()); + hoverText.ifPresent(texts -> context.drawTooltip( + MinecraftClient.getInstance().textRenderer, + texts.stream().map(Text::asOrderedText).toList(), + HoveredTooltipPositioner.INSTANCE, + mouseX, + mouseY)); + } + } + + } + + @Override + public boolean mouseClicked(double mouseX, double mouseY, int button) { + /*final Style styleAt = MinecraftClient.getInstance().textRenderer.getTextHandler() + .getStyleAt(text, (int) (x - mouseX)); + + LogUtils.getLogger().error("mouseClicked: " + (int) (x - mouseX)); + + if (styleAt == null || styleAt.isEmpty()) { + LogUtils.getLogger().error("mouseClicked empty"); + return super.mouseClicked(mouseX, mouseY, button); + } + + if (styleAt.getClickEvent() != null) { + LogUtils.getLogger().error("mouseClicked"); + if (CookiesScreen.isInBound((int) mouseX, (int) mouseY, (int) (x + (x - mouseX)), y, width, height)) { + //if (CookiesScreen.isInBound((int) mouseX, (int) mouseY, x, y, width, height)) { + final Optional runnable = ClickEventAccessor.getRunnable(styleAt.getClickEvent()); + if (runnable.isPresent()) { + runnable.ifPresent(Runnable::run); + return true; + } + } + }*/ //todo ??? + + return super.mouseClicked(mouseX, mouseY, button); + } +} diff --git a/src/main/java/codes/cookies/mod/features/farming/garden/PlotPriceBreakdown.java b/src/main/java/codes/cookies/mod/features/farming/garden/PlotPriceBreakdown.java index 99a779f6..22c5ffe5 100644 --- a/src/main/java/codes/cookies/mod/features/farming/garden/PlotPriceBreakdown.java +++ b/src/main/java/codes/cookies/mod/features/farming/garden/PlotPriceBreakdown.java @@ -4,7 +4,7 @@ import codes.cookies.mod.config.ConfigKeys; import codes.cookies.mod.config.ConfigManager; import codes.cookies.mod.events.api.InventoryContentUpdateEvent; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperLocation; +import codes.cookies.mod.features.crafthelper.CraftHelperLocation; import codes.cookies.mod.repository.constants.PlotPrice; import codes.cookies.mod.repository.constants.RepositoryConstants; import codes.cookies.mod.translations.TranslationKeys; @@ -12,12 +12,15 @@ import codes.cookies.mod.utils.accessors.InventoryScreenAccessor; import codes.cookies.mod.utils.compatibility.legendarytooltips.LegendaryTooltips; import codes.cookies.mod.utils.skyblock.LocationUtils; + import java.text.NumberFormat; import java.util.Collections; import java.util.LinkedList; import java.util.List; import java.util.Locale; + import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; import net.minecraft.client.gui.screen.Screen; @@ -29,6 +32,7 @@ import net.minecraft.text.OrderedText; import net.minecraft.text.Text; import net.minecraft.util.Formatting; + import org.apache.commons.lang3.StringUtils; /** @@ -36,167 +40,167 @@ */ public class PlotPriceBreakdown { - private static final int[][] plotIndex = {{13, 21, 23, 31}, // inner - {12, 14, 30, 32}, // middle - {3, 4, 5, 11, 15, 20, 24, 29, 33, 39, 40, 41}, // edges - {2, 6, 38, 42} // corner - }; - - private final NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH); - - private List lines; - private int lineWidth = 0; - - @SuppressWarnings("MissingJavadoc") - public PlotPriceBreakdown() { - ScreenEvents.AFTER_INIT.register(this::handleScreen); - } - - private void handleScreen(MinecraftClient minecraftClient, Screen screen, int scaledWidth, int scaledHeight) { - if (!SkyblockUtils.isCurrentlyInSkyblock()) { - return; - } - if (!LocationUtils.Island.GARDEN.isActive()) { - return; - } - if (!ConfigKeys.FARMING_PLOT_PRICE.get()) { - return; - } - if (!(screen instanceof HandledScreen handledScreen)) { - return; - } - if (!handledScreen.getTitle().getString().equals("Configure Plots")) { - return; - } - if (RepositoryConstants.plotPrice == null) { - return; - } - final CraftHelperLocation value = ConfigManager.getConfig().helpersConfig.craftHelper.craftHelperLocation.getValue(); - if (value == CraftHelperLocation.LEFT_INVENTORY || value == CraftHelperLocation.LEFT) { - InventoryScreenAccessor.setDisabled(screen, InventoryScreenAccessor.Disabled.CRAFT_HELPER); - } - - ScreenEvents.afterRender(screen).register(this::draw); - this.updateList(handledScreen); - InventoryContentUpdateEvent.register(handledScreen.getScreenHandler(), (slot, item) -> this.updateList(handledScreen)); - } - - private void draw(Screen screen, DrawContext drawContext, int mouseX, int mouseY, float tickDelta) { - HandledScreen handledScreen = (HandledScreen) screen; + private static final int[][] plotIndex = {{13, 21, 23, 31}, // inner + {12, 14, 30, 32}, // middle + {3, 4, 5, 11, 15, 20, 24, 29, 33, 39, 40, 41}, // edges + {2, 6, 38, 42} // corner + }; + + private final NumberFormat numberFormat = NumberFormat.getInstance(Locale.ENGLISH); + + private List lines; + private int lineWidth = 0; + + @SuppressWarnings("MissingJavadoc") + public PlotPriceBreakdown() { + ScreenEvents.AFTER_INIT.register(this::handleScreen); + } + + private void handleScreen(MinecraftClient minecraftClient, Screen screen, int scaledWidth, int scaledHeight) { + if (!SkyblockUtils.isCurrentlyInSkyblock()) { + return; + } + if (!LocationUtils.Island.GARDEN.isActive()) { + return; + } + if (!ConfigKeys.FARMING_PLOT_PRICE.get()) { + return; + } + if (!(screen instanceof HandledScreen handledScreen)) { + return; + } + if (!handledScreen.getTitle().getString().equals("Configure Plots")) { + return; + } + if (RepositoryConstants.plotPrice == null) { + return; + } + final CraftHelperLocation value = ConfigManager.getConfig().helpersConfig.craftHelper.craftHelperLocation.getValue(); + if (value == CraftHelperLocation.LEFT) { + InventoryScreenAccessor.setDisabled(screen, InventoryScreenAccessor.Disabled.CRAFT_HELPER); + } + + ScreenEvents.afterRender(screen).register(this::draw); + this.updateList(handledScreen); + InventoryContentUpdateEvent.register(handledScreen.getScreenHandler(), (slot, item) -> this.updateList(handledScreen)); + } + + private void draw(Screen screen, DrawContext drawContext, int mouseX, int mouseY, float tickDelta) { + HandledScreen handledScreen = (HandledScreen) screen; LegendaryTooltips.getInstance().beforeTooltipRender(screen, drawContext); - drawContext.drawTooltip( - MinecraftClient.getInstance().textRenderer, - this.lines, - HoveredTooltipPositioner.INSTANCE, - handledScreen.x - this.lineWidth - 32, - handledScreen.y + 16); + drawContext.drawTooltip( + MinecraftClient.getInstance().textRenderer, + this.lines, + HoveredTooltipPositioner.INSTANCE, + handledScreen.x - this.lineWidth - 32, + handledScreen.y + 16); LegendaryTooltips.getInstance().afterTooltipRender(screen); - } - - private void updateList(HandledScreen handledScreen) { - int missingCompost = 0; - int missingBundles = 0; - int missingPlots = 0; - int ownedPlots = 0; - for (int index = 0; index < 4; index++) { - List cost = RepositoryConstants.plotPrice.getByIndex(index); - int amountBought = 0; - int amountMissing = 0; - for (int groupIndex = 0; groupIndex < plotIndex[index].length; groupIndex++) { - Slot slot = handledScreen.getScreenHandler().slots.get(plotIndex[index][groupIndex]); - if (slot.getStack().isEmpty()) { - continue; - } - if (slot.getStack().getItem() == Items.OAK_BUTTON || - slot.getStack().getItem() == Items.RED_STAINED_GLASS_PANE) { - amountMissing++; - } else { - amountBought++; - } - } - missingPlots += amountMissing; - ownedPlots += amountBought; - - for (int compostIndex = 0; compostIndex < amountMissing; compostIndex++) { - PlotPrice.Cost slotCost = cost.get(amountBought + compostIndex); - if (slotCost.bundle()) { - missingBundles += slotCost.amount(); - } else { - missingCompost += slotCost.amount(); - } - } - } - - if (missingCompost == 0 && missingBundles == 0) { - this.lines = Collections.emptyList(); - return; - } - - int regularCompost = missingCompost + missingBundles * 160; - - List breakdown = new LinkedList<>(); - - breakdown.add(Text.empty()); - breakdown.add(Text.translatable(TranslationKeys.PLOT_PRICE_BREAKDOWN_PLOTS_MISSING) - .append(": ") - .formatted(Formatting.GRAY) - .append(Text.literal(String.valueOf(missingPlots)).formatted(Formatting.GOLD))); - breakdown.add(Text.translatable(TranslationKeys.PLOT_PRICE_BREAKDOWN_PLOTS_OWNED) - .append(": ") - .formatted(Formatting.GRAY) - .append(Text.literal(String.valueOf(ownedPlots)).formatted(Formatting.GOLD))); - breakdown.add(Text.empty()); - breakdown.add(Text.empty()); - if (missingCompost > 0) { - breakdown.add(Text.empty() - .append(Text.literal(this.numberFormat.format(missingCompost)).formatted(Formatting.GOLD).append("x ")) - .append(Text.literal("Compost").formatted(Formatting.GREEN))); - } - breakdown.add(Text.empty() - .append(Text.literal(this.numberFormat.format(missingBundles)).formatted(Formatting.GOLD).append("x ")) - .append(Text.literal("Compost Bundle").formatted(Formatting.BLUE))); - breakdown.add(Text.literal(" -> ") - .append(Text.literal(this.numberFormat.format(missingBundles * 160L)).formatted(Formatting.GOLD).append("x ")) - .append(Text.literal("Compost").formatted(Formatting.GREEN)) - .formatted(Formatting.DARK_GRAY)); - breakdown.add(Text.translatable(TranslationKeys.PLOT_PRICE_BREAKDOWN_MISSING) - .append(": ") - .formatted(Formatting.GRAY) - .append(Text.literal(this.numberFormat.format(regularCompost)).formatted(Formatting.GOLD).append("x ")) - .append(Text.literal("Compost").formatted(Formatting.GREEN))); - - - int maxLineWidth = 0; - for (MutableText mutableText : breakdown) { - maxLineWidth = Math.max(maxLineWidth, MinecraftClient.getInstance().textRenderer.getWidth(mutableText)); - } - - int equalsWidth = MinecraftClient.getInstance().textRenderer.getWidth(" "); - breakdown.add( - breakdown.size() - 1, - Text.literal(StringUtils.leftPad("", maxLineWidth / equalsWidth, " ")) - .formatted(Formatting.STRIKETHROUGH, Formatting.BLUE)); - - Text breakdownLine = Text.translatable(TranslationKeys.PLOT_PRICE_BREAKDOWN) - .append(": ") - .formatted(Formatting.BOLD, Formatting.BLUE); - int breakdownWidth = MinecraftClient.getInstance().textRenderer.getWidth(breakdownLine); - - breakdown.addFirst(Text.literal(StringUtils.leftPad("", ((maxLineWidth - breakdownWidth) / 2) / equalsWidth)) - .append(breakdownLine)); - - Text compostBreakdownLine = Text.translatable(TranslationKeys.PLOT_PRICE_BREAKDOWN_COMPOST_BREAKDOWN) - .append(": ") - .formatted(Formatting.BOLD, Formatting.BLUE); - int compostBreakdownWidth = MinecraftClient.getInstance().textRenderer.getWidth(compostBreakdownLine); - breakdown.add( - 5, - Text.literal(StringUtils.leftPad("", ((maxLineWidth - compostBreakdownWidth) / 2) / equalsWidth)) - .append(compostBreakdownLine)); - - - this.lines = Lists.transform(breakdown, Text::asOrderedText); - this.lineWidth = maxLineWidth; - } + } + + private void updateList(HandledScreen handledScreen) { + int missingCompost = 0; + int missingBundles = 0; + int missingPlots = 0; + int ownedPlots = 0; + for (int index = 0; index < 4; index++) { + List cost = RepositoryConstants.plotPrice.getByIndex(index); + int amountBought = 0; + int amountMissing = 0; + for (int groupIndex = 0; groupIndex < plotIndex[index].length; groupIndex++) { + Slot slot = handledScreen.getScreenHandler().slots.get(plotIndex[index][groupIndex]); + if (slot.getStack().isEmpty()) { + continue; + } + if (slot.getStack().getItem() == Items.OAK_BUTTON || + slot.getStack().getItem() == Items.RED_STAINED_GLASS_PANE) { + amountMissing++; + } else { + amountBought++; + } + } + missingPlots += amountMissing; + ownedPlots += amountBought; + + for (int compostIndex = 0; compostIndex < amountMissing; compostIndex++) { + PlotPrice.Cost slotCost = cost.get(amountBought + compostIndex); + if (slotCost.bundle()) { + missingBundles += slotCost.amount(); + } else { + missingCompost += slotCost.amount(); + } + } + } + + if (missingCompost == 0 && missingBundles == 0) { + this.lines = Collections.emptyList(); + return; + } + + int regularCompost = missingCompost + missingBundles * 160; + + List breakdown = new LinkedList<>(); + + breakdown.add(Text.empty()); + breakdown.add(Text.translatable(TranslationKeys.PLOT_PRICE_BREAKDOWN_PLOTS_MISSING) + .append(": ") + .formatted(Formatting.GRAY) + .append(Text.literal(String.valueOf(missingPlots)).formatted(Formatting.GOLD))); + breakdown.add(Text.translatable(TranslationKeys.PLOT_PRICE_BREAKDOWN_PLOTS_OWNED) + .append(": ") + .formatted(Formatting.GRAY) + .append(Text.literal(String.valueOf(ownedPlots)).formatted(Formatting.GOLD))); + breakdown.add(Text.empty()); + breakdown.add(Text.empty()); + if (missingCompost > 0) { + breakdown.add(Text.empty() + .append(Text.literal(this.numberFormat.format(missingCompost)).formatted(Formatting.GOLD).append("x ")) + .append(Text.literal("Compost").formatted(Formatting.GREEN))); + } + breakdown.add(Text.empty() + .append(Text.literal(this.numberFormat.format(missingBundles)).formatted(Formatting.GOLD).append("x ")) + .append(Text.literal("Compost Bundle").formatted(Formatting.BLUE))); + breakdown.add(Text.literal(" -> ") + .append(Text.literal(this.numberFormat.format(missingBundles * 160L)).formatted(Formatting.GOLD).append("x ")) + .append(Text.literal("Compost").formatted(Formatting.GREEN)) + .formatted(Formatting.DARK_GRAY)); + breakdown.add(Text.translatable(TranslationKeys.PLOT_PRICE_BREAKDOWN_MISSING) + .append(": ") + .formatted(Formatting.GRAY) + .append(Text.literal(this.numberFormat.format(regularCompost)).formatted(Formatting.GOLD).append("x ")) + .append(Text.literal("Compost").formatted(Formatting.GREEN))); + + + int maxLineWidth = 0; + for (MutableText mutableText : breakdown) { + maxLineWidth = Math.max(maxLineWidth, MinecraftClient.getInstance().textRenderer.getWidth(mutableText)); + } + + int equalsWidth = MinecraftClient.getInstance().textRenderer.getWidth(" "); + breakdown.add( + breakdown.size() - 1, + Text.literal(StringUtils.leftPad("", maxLineWidth / equalsWidth, " ")) + .formatted(Formatting.STRIKETHROUGH, Formatting.BLUE)); + + Text breakdownLine = Text.translatable(TranslationKeys.PLOT_PRICE_BREAKDOWN) + .append(": ") + .formatted(Formatting.BOLD, Formatting.BLUE); + int breakdownWidth = MinecraftClient.getInstance().textRenderer.getWidth(breakdownLine); + + breakdown.addFirst(Text.literal(StringUtils.leftPad("", ((maxLineWidth - breakdownWidth) / 2) / equalsWidth)) + .append(breakdownLine)); + + Text compostBreakdownLine = Text.translatable(TranslationKeys.PLOT_PRICE_BREAKDOWN_COMPOST_BREAKDOWN) + .append(": ") + .formatted(Formatting.BOLD, Formatting.BLUE); + int compostBreakdownWidth = MinecraftClient.getInstance().textRenderer.getWidth(compostBreakdownLine); + breakdown.add( + 5, + Text.literal(StringUtils.leftPad("", ((maxLineWidth - compostBreakdownWidth) / 2) / equalsWidth)) + .append(compostBreakdownLine)); + + + this.lines = Lists.transform(breakdown, Text::asOrderedText); + this.lineWidth = maxLineWidth; + } } diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/ModifyRecipeScreen.java b/src/main/java/codes/cookies/mod/features/misc/utils/ModifyRecipeScreen.java index 0481bb84..ce65326d 100644 --- a/src/main/java/codes/cookies/mod/features/misc/utils/ModifyRecipeScreen.java +++ b/src/main/java/codes/cookies/mod/features/misc/utils/ModifyRecipeScreen.java @@ -2,7 +2,7 @@ import codes.cookies.mod.config.ConfigKey; import codes.cookies.mod.config.ConfigKeys; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperManager; +import codes.cookies.mod.features.crafthelper.CraftHelperManager; import codes.cookies.mod.repository.RepositoryItem; import codes.cookies.mod.translations.TranslationKeys; import codes.cookies.mod.utils.cookies.Constants; @@ -27,41 +27,41 @@ * Adds more functionality to the recipe screen. */ public class ModifyRecipeScreen extends InventoryModifier { - private static final ConfigKey CRAFT_HELPER_SLOT = ConfigKeys.HELPER_CRAFT_HELPER_SLOT; - private static final ConfigKey CRAFT_HELPER = ConfigKeys.HELPER_CRAFT_HELPER; - public static ItemStack CRAFT_HELPER_SELECT; + private static final ConfigKey CRAFT_HELPER_SLOT = ConfigKeys.HELPER_CRAFT_HELPER_SLOT; + private static final ConfigKey CRAFT_HELPER = ConfigKeys.HELPER_CRAFT_HELPER; + public static ItemStack CRAFT_HELPER_SELECT; - static { - CRAFT_HELPER_SELECT = - new ItemBuilder(Items.DIAMOND_PICKAXE).setName(TextUtils.translatable(TranslationKeys.CRAFT_HELPER, - Constants.SUCCESS_COLOR)) - .setLore(TextUtils.translatable(TranslationKeys.CRAFT_HELPER_LINE_1, Formatting.GRAY), - TextUtils.translatable(TranslationKeys.CRAFT_HELPER_LINE_2, Formatting.GRAY), - Text.empty(), - TextUtils.translatable(TranslationKeys.LEFT_CLICK_TO_SET, Formatting.YELLOW).append("!"), - TextUtils.translatable(TranslationKeys.RIGHT_CLICK_TO_EDIT, Formatting.YELLOW).append("!")) - .set(MiscDataComponentTypes.CRAFT_HELPER_MODIFIED, Unit.INSTANCE) - .hideAdditionalTooltips() - .set(DataComponentTypes.TOOL, null) - .set(DataComponentTypes.ATTRIBUTE_MODIFIERS, null) - .build(); - } + static { + CRAFT_HELPER_SELECT = + new ItemBuilder(Items.DIAMOND_PICKAXE).setName(TextUtils.translatable(TranslationKeys.CRAFT_HELPER, + Constants.SUCCESS_COLOR)) + .setLore(TextUtils.translatable(TranslationKeys.CRAFT_HELPER_LINE_1, Formatting.GRAY), + TextUtils.translatable(TranslationKeys.CRAFT_HELPER_LINE_2, Formatting.GRAY), + Text.empty(), + TextUtils.translatable(TranslationKeys.LEFT_CLICK_TO_SET, Formatting.YELLOW).append("!"), + TextUtils.translatable(TranslationKeys.RIGHT_CLICK_TO_EDIT, Formatting.YELLOW).append("!")) + .set(MiscDataComponentTypes.CRAFT_HELPER_MODIFIED, Unit.INSTANCE) + .hideAdditionalTooltips() + .set(DataComponentTypes.TOOL, null) + .set(DataComponentTypes.ATTRIBUTE_MODIFIERS, null) + .build(); + } - private RepositoryItem current; + private RepositoryItem current; - @SuppressWarnings("MissingJavadoc") - public ModifyRecipeScreen() { - super(CRAFT_HELPER_SELECT, "cookies-regex:.*Recipe", CRAFT_HELPER, CRAFT_HELPER_SLOT); - } + @SuppressWarnings("MissingJavadoc") + public ModifyRecipeScreen() { + super(CRAFT_HELPER_SELECT, "cookies-regex:.*Recipe", CRAFT_HELPER, CRAFT_HELPER_SLOT); + } - @Override - protected void onItem(int slot, ItemStack item) { - if (slot != 25) { - return; - } + @Override + protected void onItem(int slot, ItemStack item) { + if (slot != 25) { + return; + } - this.current = ItemUtils.getData(item, CookiesDataComponentTypes.REPOSITORY_ITEM); - } + this.current = ItemUtils.getData(item, CookiesDataComponentTypes.REPOSITORY_ITEM); + } @Override protected boolean shouldInstrument(int clicked) { @@ -69,13 +69,13 @@ protected boolean shouldInstrument(int clicked) { } @Override - protected void clicked(int button) { - SoundUtils.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 0.5f); - CraftHelperManager.pushNewCraftHelperItem(this.current, 1); - } + protected void clicked(int button) { + SoundUtils.playSound(SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 0.5f); + CraftHelperManager.pushNewCraftHelperItem(this.current, 1); + } - @Override - protected ComponentType getModifiedComponentType() { - return MiscDataComponentTypes.CRAFT_HELPER_MODIFIED; - } + @Override + protected ComponentType getModifiedComponentType() { + return MiscDataComponentTypes.CRAFT_HELPER_MODIFIED; + } } diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/UtilsFeatures.java b/src/main/java/codes/cookies/mod/features/misc/utils/UtilsFeatures.java index 754034d5..140ad964 100644 --- a/src/main/java/codes/cookies/mod/features/misc/utils/UtilsFeatures.java +++ b/src/main/java/codes/cookies/mod/features/misc/utils/UtilsFeatures.java @@ -1,7 +1,6 @@ package codes.cookies.mod.features.misc.utils; import codes.cookies.mod.features.Loader; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperManager; /** * Utility class to load all utility features. @@ -9,13 +8,13 @@ @SuppressWarnings("MissingJavadoc") public class UtilsFeatures { - public static void load() { - Loader.load("CraftHelper", CraftHelperManager::init); - Loader.load("ModifiedRecipeScreen", ModifyRecipeScreen::new); - Loader.load("StoragePreview", StoragePreview::new); - Loader.load("AnvilHelper", AnvilHelper::new); - Loader.load("ForgeRecipes", ForgeRecipes::new); + public static void load() { + Loader.load("CraftHelper", codes.cookies.mod.features.crafthelper.CraftHelperManager::init); + Loader.load("ModifiedRecipeScreen", ModifyRecipeScreen::new); + Loader.load("StoragePreview", StoragePreview::new); + Loader.load("AnvilHelper", AnvilHelper::new); + Loader.load("ForgeRecipes", ForgeRecipes::new); Loader.load("StatsTracker", StatsTracker::init); - } + } } diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperLocation.java b/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperLocation.java deleted file mode 100644 index 716f560a..00000000 --- a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperLocation.java +++ /dev/null @@ -1,7 +0,0 @@ -package codes.cookies.mod.features.misc.utils.crafthelper; - -public enum CraftHelperLocation { - - LEFT, LEFT_INVENTORY, RIGHT_INVENTORY, RIGHT - -} diff --git a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperTooltipPositioner.java b/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperTooltipPositioner.java deleted file mode 100644 index dfa7a448..00000000 --- a/src/main/java/codes/cookies/mod/features/misc/utils/crafthelper/CraftHelperTooltipPositioner.java +++ /dev/null @@ -1,37 +0,0 @@ -package codes.cookies.mod.features.misc.utils.crafthelper; - -import codes.cookies.mod.utils.accessors.InventoryScreenAccessor; -import net.minecraft.client.MinecraftClient; -import net.minecraft.client.gui.tooltip.TooltipPositioner; -import org.joml.Vector2i; -import org.joml.Vector2ic; - -public class CraftHelperTooltipPositioner implements TooltipPositioner { - - public static CraftHelperTooltipPositioner INSTANCE = new CraftHelperTooltipPositioner(); - - @Override - public Vector2ic getPosition(int screenWidth, int screenHeight, int x, int y, int width, int height) { - Vector2ic position = new Vector2i(x,y); - if (!(MinecraftClient.getInstance().currentScreen instanceof InventoryScreenAccessor accessor)) { - return position; - } - final int screenX = accessor.cookies$getX(); - final int screenBackgroundWidth = accessor.cookies$getBackgroundWidth(); - - if (position.x() + width > screenX && position.x() < screenX) { - position = new Vector2i(screenX - width - 8, position.y()); - } - if (position.x() > screenX && position.x() < screenX + screenBackgroundWidth) { - position = new Vector2i(screenX + screenBackgroundWidth + 8, position.y()); - } - if (position.x() + width > screenWidth) { - position = new Vector2i(screenWidth - width - 8, position.y()); - } - if (position.x() == 0) { - position = new Vector2i(8, position.y()); - } - return position; - } - -} diff --git a/src/main/java/codes/cookies/mod/render/utils/RenderHelper.java b/src/main/java/codes/cookies/mod/render/utils/RenderHelper.java index ef60e720..8f0b9751 100644 --- a/src/main/java/codes/cookies/mod/render/utils/RenderHelper.java +++ b/src/main/java/codes/cookies/mod/render/utils/RenderHelper.java @@ -1,58 +1,63 @@ package codes.cookies.mod.render.utils; +import net.minecraft.client.font.TextRenderer; +import net.minecraft.client.gui.DrawContext; +import net.minecraft.text.Text; +import net.minecraft.util.Util; +import net.minecraft.util.math.MathHelper; + /** * Various helper methods for rendering things. */ public class RenderHelper { - /** - * Converts the color from 0-255 to 0-1. - * - * @param color The color. - * @return The value between 0 and 1. - */ - public static float wrapZeroOne(int color) { - return color / 255f; - } - - /** - * Gets the red value from a rgb color. - * - * @param color The color. - * @return The red value. - */ - public static int getRed(int color) { - return color >> 16 & 0xFF; - } + /** + * Converts the color from 0-255 to 0-1. + * + * @param color The color. + * @return The value between 0 and 1. + */ + public static float wrapZeroOne(int color) { + return color / 255f; + } - /** - * Gets the green value from a rgb color. - * - * @param color The color. - * @return The green value. - */ - public static int getGreen(int color) { - return color >> 8 & 0xFF; - } + /** + * Gets the red value from a rgb color. + * + * @param color The color. + * @return The red value. + */ + public static int getRed(int color) { + return color >> 16 & 0xFF; + } - /** - * Gets the blue value from a rgb color. - * - * @param color The color. - * @return The blue value. - */ - public static int getBlue(int color) { - return color & 0xFF; - } + /** + * Gets the green value from a rgb color. + * + * @param color The color. + * @return The green value. + */ + public static int getGreen(int color) { + return color >> 8 & 0xFF; + } - /** - * Gets the alpha value from an argb color. - * - * @param color The color. - * @return The alpha value. - */ - public static int getAlpha(int color) { - return color >> 24 & 0xFF; - } + /** + * Gets the blue value from a rgb color. + * + * @param color The color. + * @return The blue value. + */ + public static int getBlue(int color) { + return color & 0xFF; + } + /** + * Gets the alpha value from an argb color. + * + * @param color The color. + * @return The alpha value. + */ + public static int getAlpha(int color) { + return color >> 24 & 0xFF; + } } diff --git a/src/main/java/codes/cookies/mod/screen/inventory/ForgeRecipeScreen.java b/src/main/java/codes/cookies/mod/screen/inventory/ForgeRecipeScreen.java index cbfb6595..d58d045b 100644 --- a/src/main/java/codes/cookies/mod/screen/inventory/ForgeRecipeScreen.java +++ b/src/main/java/codes/cookies/mod/screen/inventory/ForgeRecipeScreen.java @@ -2,8 +2,8 @@ import codes.cookies.mod.CookiesMod; import codes.cookies.mod.config.ConfigManager; +import codes.cookies.mod.features.crafthelper.CraftHelperManager; import codes.cookies.mod.features.misc.utils.ModifyRecipeScreen; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperManager; import codes.cookies.mod.repository.Ingredient; import codes.cookies.mod.repository.RepositoryItem; import codes.cookies.mod.repository.recipes.ForgeRecipe; @@ -15,8 +15,10 @@ import codes.cookies.mod.utils.skyblock.inventories.ClientSideInventory; import codes.cookies.mod.utils.skyblock.inventories.ItemBuilder; import codes.cookies.mod.utils.skyblock.inventories.Position; + import java.util.Optional; import java.util.Set; + import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.screen.Screen; import net.minecraft.item.ItemStack; @@ -29,102 +31,102 @@ * A specific forge recipe view, the screen adapts based on the parameters passed to the constructor. */ public class ForgeRecipeScreen extends ClientSideInventory { - private static final int ROWS = 6; - private final ForgeRecipe recipe; - private final Screen previous; - - public ForgeRecipeScreen(ForgeRecipe recipe, Screen previous) { - super(getName(recipe), ROWS); - this.recipe = recipe; - this.previous = previous; - this.inventoryContents.fill(outline); - this.setItems(); - - this.inventoryContents.set(new Position(5, 4), - new ItemBuilder(Items.BARRIER) - .setName(TextUtils.translatable("mco.selectServer.close", Formatting.RED)) - .setClickRunnable(InventoryUtils.wrapWithSound(this::close)) - .hideAdditionalTooltips() - .build()); - - this.inventoryContents.set(new Position(1, 4), - new ItemBuilder(Items.LAVA_BUCKET).setName(TextUtils.translatable(TranslationKeys.SCREEN_FORGE_RECIPE_OVERVIEW_RECIPE, Formatting.GREEN)).setLore( - TextUtils.literal("<-- ", Formatting.WHITE) - .append(TextUtils.literal("Required items", Formatting.YELLOW)), - TextUtils.literal(" Result item ", Formatting.YELLOW) - .append(TextUtils.literal(" -->", Formatting.WHITE))).build()); - - if (ConfigManager.getConfig().helpersConfig.craftHelper.craftHelper.getValue()) { - final ItemStack craftHelperSelect = ModifyRecipeScreen.CRAFT_HELPER_SELECT.copy(); - craftHelperSelect.set( - CookiesDataComponentTypes.ITEM_CLICK_RUNNABLE, - InventoryUtils.wrapWithSound(this::setSelected, SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 0.5f)); - this.inventoryContents.set(new Position(3, 4), craftHelperSelect); - } - - final ItemBuilder goBack = new ItemBuilder(Items.ARROW).setName(TextUtils.translatable("dataPack.validation.back", Formatting.GREEN)) - .setClickRunnable(InventoryUtils.wrapWithSound(this::openForgeRecipes)); - if (previous instanceof ForgeRecipeOverviewScreen) { - goBack.setLore(TextUtils.translatable(TranslationKeys.SCREEN_FORGE_RECIPE_OVERVIEW_BACK_TO_FORGE_RECIPES, Formatting.GRAY)); - } else if (previous instanceof ForgeRecipeScreen forgeRecipeScreen) { - goBack.setLore(TextUtils.literal("To ", Formatting.GRAY) - .append(getName(forgeRecipeScreen.recipe).formatted(Formatting.GRAY))); - } else { - return; - } - this.inventoryContents.set(new Position(5, 3), goBack.build()); - } - - private static MutableText getName(ForgeRecipe recipe) { - return recipe.getOutput() - .getRepositoryItem() - .getName() - .copyContentOnly() - .append(" Recipe") - .formatted(Formatting.DARK_GRAY); - } - - private void setItems() { - for (int i = 0; i < this.recipe.getIngredients().length; i++) { - int row = i / 2; - final Ingredient ingredient = this.recipe.getIngredients()[i]; - final Position position = new Position(1 + row, 1 + i % 2); - final ItemStack asItem = ingredient.getAsItem(); - if (ingredient.getRepositoryItem() != null) { - final RepositoryItem repositoryItem = ingredient.getRepositoryItem(); - final Set recipes = repositoryItem.getRecipes(); - if (recipes.stream().anyMatch(ForgeRecipe.class::isInstance)) { - final Optional first = - recipes.stream().filter(ForgeRecipe.class::isInstance).map(ForgeRecipe.class::cast).findFirst(); - final ForgeRecipe forgeRecipe = first.orElseThrow(); - asItem.set(CookiesDataComponentTypes.ITEM_CLICK_RUNNABLE, - InventoryUtils.wrapWithSound(this.clickedForgeRecipe(forgeRecipe))); - } else if (!recipes.isEmpty()) { - asItem.set(CookiesDataComponentTypes.ITEM_CLICK_RUNNABLE, - InventoryUtils.wrapWithSound(this.clickedNonForge(repositoryItem.getInternalId()))); - } - } - this.inventoryContents.set(position, asItem); - } - - this.inventoryContents.set(new Position(1, 7), - this.recipe.getOutput().getRepositoryItem().constructItemStack()); - } - - private void setSelected() { - CraftHelperManager.pushNewCraftHelperItem(this.recipe.getOutput().getRepositoryItemNotNull(), 1); - } - - private void openForgeRecipes() { - CookiesMod.openScreen(previous); - } - - private Runnable clickedForgeRecipe(ForgeRecipe forgeRecipe) { - return () -> CookiesMod.openScreen(new ForgeRecipeScreen(forgeRecipe, this)); - } - - private Runnable clickedNonForge(String internalId) { - return () -> MinecraftClient.getInstance().player.networkHandler.sendCommand("viewrecipe %s".formatted( - internalId.toUpperCase())); - } + private static final int ROWS = 6; + private final ForgeRecipe recipe; + private final Screen previous; + + public ForgeRecipeScreen(ForgeRecipe recipe, Screen previous) { + super(getName(recipe), ROWS); + this.recipe = recipe; + this.previous = previous; + this.inventoryContents.fill(outline); + this.setItems(); + + this.inventoryContents.set(new Position(5, 4), + new ItemBuilder(Items.BARRIER) + .setName(TextUtils.translatable("mco.selectServer.close", Formatting.RED)) + .setClickRunnable(InventoryUtils.wrapWithSound(this::close)) + .hideAdditionalTooltips() + .build()); + + this.inventoryContents.set(new Position(1, 4), + new ItemBuilder(Items.LAVA_BUCKET).setName(TextUtils.translatable(TranslationKeys.SCREEN_FORGE_RECIPE_OVERVIEW_RECIPE, Formatting.GREEN)).setLore( + TextUtils.literal("<-- ", Formatting.WHITE) + .append(TextUtils.literal("Required items", Formatting.YELLOW)), + TextUtils.literal(" Result item ", Formatting.YELLOW) + .append(TextUtils.literal(" -->", Formatting.WHITE))).build()); + + if (ConfigManager.getConfig().helpersConfig.craftHelper.craftHelper.getValue()) { + final ItemStack craftHelperSelect = ModifyRecipeScreen.CRAFT_HELPER_SELECT.copy(); + craftHelperSelect.set( + CookiesDataComponentTypes.ITEM_CLICK_RUNNABLE, + InventoryUtils.wrapWithSound(this::setSelected, SoundEvents.ENTITY_EXPERIENCE_ORB_PICKUP, 1, 0.5f)); + this.inventoryContents.set(new Position(3, 4), craftHelperSelect); + } + + final ItemBuilder goBack = new ItemBuilder(Items.ARROW).setName(TextUtils.translatable("dataPack.validation.back", Formatting.GREEN)) + .setClickRunnable(InventoryUtils.wrapWithSound(this::openForgeRecipes)); + if (previous instanceof ForgeRecipeOverviewScreen) { + goBack.setLore(TextUtils.translatable(TranslationKeys.SCREEN_FORGE_RECIPE_OVERVIEW_BACK_TO_FORGE_RECIPES, Formatting.GRAY)); + } else if (previous instanceof ForgeRecipeScreen forgeRecipeScreen) { + goBack.setLore(TextUtils.literal("To ", Formatting.GRAY) + .append(getName(forgeRecipeScreen.recipe).formatted(Formatting.GRAY))); + } else { + return; + } + this.inventoryContents.set(new Position(5, 3), goBack.build()); + } + + private static MutableText getName(ForgeRecipe recipe) { + return recipe.getOutput() + .getRepositoryItem() + .getName() + .copyContentOnly() + .append(" Recipe") + .formatted(Formatting.DARK_GRAY); + } + + private void setItems() { + for (int i = 0; i < this.recipe.getIngredients().length; i++) { + int row = i / 2; + final Ingredient ingredient = this.recipe.getIngredients()[i]; + final Position position = new Position(1 + row, 1 + i % 2); + final ItemStack asItem = ingredient.getAsItem(); + if (ingredient.getRepositoryItem() != null) { + final RepositoryItem repositoryItem = ingredient.getRepositoryItem(); + final Set recipes = repositoryItem.getRecipes(); + if (recipes.stream().anyMatch(ForgeRecipe.class::isInstance)) { + final Optional first = + recipes.stream().filter(ForgeRecipe.class::isInstance).map(ForgeRecipe.class::cast).findFirst(); + final ForgeRecipe forgeRecipe = first.orElseThrow(); + asItem.set(CookiesDataComponentTypes.ITEM_CLICK_RUNNABLE, + InventoryUtils.wrapWithSound(this.clickedForgeRecipe(forgeRecipe))); + } else if (!recipes.isEmpty()) { + asItem.set(CookiesDataComponentTypes.ITEM_CLICK_RUNNABLE, + InventoryUtils.wrapWithSound(this.clickedNonForge(repositoryItem.getInternalId()))); + } + } + this.inventoryContents.set(position, asItem); + } + + this.inventoryContents.set(new Position(1, 7), + this.recipe.getOutput().getRepositoryItem().constructItemStack()); + } + + private void setSelected() { + CraftHelperManager.pushNewCraftHelperItem(this.recipe.getOutput().getRepositoryItemNotNull(), 1); + } + + private void openForgeRecipes() { + CookiesMod.openScreen(previous); + } + + private Runnable clickedForgeRecipe(ForgeRecipe forgeRecipe) { + return () -> CookiesMod.openScreen(new ForgeRecipeScreen(forgeRecipe, this)); + } + + private Runnable clickedNonForge(String internalId) { + return () -> MinecraftClient.getInstance().player.networkHandler.sendCommand("viewrecipe %s".formatted( + internalId.toUpperCase())); + } } diff --git a/src/main/java/codes/cookies/mod/services/item/ItemSearchService.java b/src/main/java/codes/cookies/mod/services/item/ItemSearchService.java index f537cede..32b8738b 100644 --- a/src/main/java/codes/cookies/mod/services/item/ItemSearchService.java +++ b/src/main/java/codes/cookies/mod/services/item/ItemSearchService.java @@ -7,7 +7,7 @@ import codes.cookies.mod.data.profile.items.ItemSources; import codes.cookies.mod.data.profile.items.sources.CraftableItemSource; import codes.cookies.mod.data.profile.items.sources.StorageItemSource; -import codes.cookies.mod.features.misc.utils.crafthelper.CraftHelperManager; +import codes.cookies.mod.features.crafthelper.CraftHelperManager; import codes.cookies.mod.repository.RepositoryItem; import codes.cookies.mod.services.item.search.ExactItemMatch; import codes.cookies.mod.services.item.search.IsSameMatch; diff --git a/src/main/java/codes/cookies/mod/utils/RenderUtils.java b/src/main/java/codes/cookies/mod/utils/RenderUtils.java index de5d0b0e..2d0eddd4 100644 --- a/src/main/java/codes/cookies/mod/utils/RenderUtils.java +++ b/src/main/java/codes/cookies/mod/utils/RenderUtils.java @@ -11,8 +11,10 @@ import net.minecraft.entity.Entity; import net.minecraft.text.Text; import net.minecraft.util.Identifier; +import net.minecraft.util.Util; import net.minecraft.util.math.MathHelper; import net.minecraft.util.math.Vec3d; + import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.joml.Matrix4f; @@ -21,206 +23,206 @@ * Various render related utility methods. */ public sealed interface RenderUtils permits RenderUtils.Sealed { - Identifier BACKGROUND_TEXTURE = Identifier.of("cookies-mod", "textures/gui/blank.png"); - - /** - * Renders a box in minecraft's style. - * - * @param drawContext The draw context. - * @param x The start of the box. - * @param y The start of the box. - * @param width The width of the box. - * @param height The height of the box. - */ - static void renderBox( - final DrawContext drawContext, final int x, final int y, final int width, final int height) { - drawContext.fill(x, y, x + width, y + height, 0xFF373737); - drawContext.fill(x + 1, y + 1, x + width, y + height, 0xFFFFFFFF); - drawContext.fill(x + width, y, x + width - 1, y + 1, 0xFF8B8B8B); - drawContext.fill(x, y + height, x + 1, y + height - 1, 0xFF8B8B8B); - drawContext.fill(x + 1, y + 1, x + width - 1, y + height - 1, 0xFF8B8B8B); - } - - static void renderBackgroundBox(final DrawContext drawContext, int x, int y, int width, int height, int color) { - drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x, y, 0, 0, 4, 4, 4, 4, 12, 12, color); - drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + 4, y, 4, 0, width - 8, 4, 4, 4, 12, 12, color); - drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + width - 4, y, 8, 0,4, 4, 4, 4, 12, 12, color); - - drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x, y + 4, 0, 4,4, height - 8, 4, 4, 12, 12, color); - drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + 4, y + 4, 4, 4,width - 8, height - 8, 4, 4, 12, 12, color); - drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + width - 4, y + 4,8, 4, 4, height - 8, 4, 4, 12, 12, color); - - drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x, y + height - 4, 0, 8, 4, 4, 4, 4, 12, 12, color); - drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + 4, y + height - 4, 4, 8,width - 8, 4, 4, 4, 12, 12, color); - drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + width - 4, y + height - 4, 8, 8,4, 4, 4, 4, 12, 12, color); - } - - /** - * Renders a box in minecraft's style. - * - * @param drawContext The draw context. - * @param x The start of the box. - * @param y The start of the box. - * @param width The width of the box. - * @param height The height of the box. - */ - static void renderFilledBox( - final DrawContext drawContext, final int x, final int y, final int width, final int height) { - drawContext.fill(x, y, width, height, 0xFFC6C6C6); - } - - /** - * Draws a text with a max width. - * - * @param drawContext The current draw context. - * @param text The text to render. - * @param width The width of the text. - * @param x The x coordinate. - * @param y The y coordinate. - * @param color The color of the text. - * @param shadow If the text has shadow. - */ - static void renderTextWithMaxWidth( - final @NotNull DrawContext drawContext, - final @NotNull Text text, - final int width, - final int x, - final int y, - final int color, - final boolean shadow) { - final TextRenderer textRenderer = getTextRendererOrNull(); - if (textRenderer == null) { - return; - } - final int textWidth = textRenderer.getWidth(text); - float scale = 1.0f; - if (textWidth > width) { - scale = width / (float) textWidth; - } - renderTextScaled(drawContext, text, scale, x, y, color, shadow); - } - - /** - * Gets the text renderer or null. - * - * @return The text renderer. - */ - private static @Nullable TextRenderer getTextRendererOrNull() { - if (MinecraftClient.getInstance() != null) { - return MinecraftClient.getInstance().textRenderer; - } - return null; - } - - /** - * Draws a text at a scale. - * - * @param drawContext The current draw context. - * @param text The text to render. - * @param scaleFactor The scale to render the text at. - * @param x The x coordinate. - * @param y The y coordinate. - * @param color The color of the text. - * @param shadow If the text has shadow. - */ - static void renderTextScaled( - final @NotNull DrawContext drawContext, - final @NotNull Text text, - final float scaleFactor, - final int x, - final int y, - final int color, - final boolean shadow) { - final TextRenderer textRenderer = getTextRendererOrNull(); - if (textRenderer == null) { - return; - } - drawContext.getMatrices().push(); - drawContext.getMatrices().scale(scaleFactor, scaleFactor, 1); - drawContext.drawText(textRenderer, text, (int) (x / scaleFactor), (int) (y / scaleFactor), color, shadow); - drawContext.getMatrices().pop(); - } - - - /** - * Renders a centered text at a scale. - * - * @param drawContext The current draw context. - * @param text The text to render. - * @param scaleFactor The scale to render the text at. - * @param x The center x coordinate. - * @param y The y coordinate. - * @param color The color of the text. - */ - static void renderTextCenteredScaled( - final @NotNull DrawContext drawContext, - final @NotNull Text text, - final float scaleFactor, - final int x, - final int y, - final int color) { - final TextRenderer textRenderer = getTextRendererOrNull(); - if (textRenderer == null) { - return; - } - drawContext.getMatrices().push(); - drawContext.getMatrices().scale(scaleFactor, scaleFactor, 1); + Identifier BACKGROUND_TEXTURE = Identifier.of("cookies-mod", "textures/gui/blank.png"); + + /** + * Renders a box in minecraft's style. + * + * @param drawContext The draw context. + * @param x The start of the box. + * @param y The start of the box. + * @param width The width of the box. + * @param height The height of the box. + */ + static void renderBox( + final DrawContext drawContext, final int x, final int y, final int width, final int height) { + drawContext.fill(x, y, x + width, y + height, 0xFF373737); + drawContext.fill(x + 1, y + 1, x + width, y + height, 0xFFFFFFFF); + drawContext.fill(x + width, y, x + width - 1, y + 1, 0xFF8B8B8B); + drawContext.fill(x, y + height, x + 1, y + height - 1, 0xFF8B8B8B); + drawContext.fill(x + 1, y + 1, x + width - 1, y + height - 1, 0xFF8B8B8B); + } + + static void renderBackgroundBox(final DrawContext drawContext, int x, int y, int width, int height, int color) { + drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x, y, 0, 0, 4, 4, 4, 4, 12, 12, color); + drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + 4, y, 4, 0, width - 8, 4, 4, 4, 12, 12, color); + drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + width - 4, y, 8, 0, 4, 4, 4, 4, 12, 12, color); + + drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x, y + 4, 0, 4, 4, height - 8, 4, 4, 12, 12, color); + drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + 4, y + 4, 4, 4, width - 8, height - 8, 4, 4, 12, 12, color); + drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + width - 4, y + 4, 8, 4, 4, height - 8, 4, 4, 12, 12, color); + + drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x, y + height - 4, 0, 8, 4, 4, 4, 4, 12, 12, color); + drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + 4, y + height - 4, 4, 8, width - 8, 4, 4, 4, 12, 12, color); + drawContext.drawTexture(RenderLayer::getGuiTextured, BACKGROUND_TEXTURE, x + width - 4, y + height - 4, 8, 8, 4, 4, 4, 4, 12, 12, color); + } + + /** + * Renders a box in minecraft's style. + * + * @param drawContext The draw context. + * @param x The start of the box. + * @param y The start of the box. + * @param width The width of the box. + * @param height The height of the box. + */ + static void renderFilledBox( + final DrawContext drawContext, final int x, final int y, final int width, final int height) { + drawContext.fill(x, y, width, height, 0xFFC6C6C6); + } + + /** + * Draws a text with a max width. + * + * @param drawContext The current draw context. + * @param text The text to render. + * @param width The width of the text. + * @param x The x coordinate. + * @param y The y coordinate. + * @param color The color of the text. + * @param shadow If the text has shadow. + */ + static void renderTextWithMaxWidth( + final @NotNull DrawContext drawContext, + final @NotNull Text text, + final int width, + final int x, + final int y, + final int color, + final boolean shadow) { + final TextRenderer textRenderer = getTextRendererOrNull(); + if (textRenderer == null) { + return; + } + final int textWidth = textRenderer.getWidth(text); + float scale = 1.0f; + if (textWidth > width) { + scale = width / (float) textWidth; + } + renderTextScaled(drawContext, text, scale, x, y, color, shadow); + } + + /** + * Gets the text renderer or null. + * + * @return The text renderer. + */ + private static @Nullable TextRenderer getTextRendererOrNull() { + if (MinecraftClient.getInstance() != null) { + return MinecraftClient.getInstance().textRenderer; + } + return null; + } + + /** + * Draws a text at a scale. + * + * @param drawContext The current draw context. + * @param text The text to render. + * @param scaleFactor The scale to render the text at. + * @param x The x coordinate. + * @param y The y coordinate. + * @param color The color of the text. + * @param shadow If the text has shadow. + */ + static void renderTextScaled( + final @NotNull DrawContext drawContext, + final @NotNull Text text, + final float scaleFactor, + final int x, + final int y, + final int color, + final boolean shadow) { + final TextRenderer textRenderer = getTextRendererOrNull(); + if (textRenderer == null) { + return; + } + drawContext.getMatrices().push(); + drawContext.getMatrices().scale(scaleFactor, scaleFactor, 1); + drawContext.drawText(textRenderer, text, (int) (x / scaleFactor), (int) (y / scaleFactor), color, shadow); + drawContext.getMatrices().pop(); + } + + + /** + * Renders a centered text at a scale. + * + * @param drawContext The current draw context. + * @param text The text to render. + * @param scaleFactor The scale to render the text at. + * @param x The center x coordinate. + * @param y The y coordinate. + * @param color The color of the text. + */ + static void renderTextCenteredScaled( + final @NotNull DrawContext drawContext, + final @NotNull Text text, + final float scaleFactor, + final int x, + final int y, + final int color) { + final TextRenderer textRenderer = getTextRendererOrNull(); + if (textRenderer == null) { + return; + } + drawContext.getMatrices().push(); + drawContext.getMatrices().scale(scaleFactor, scaleFactor, 1); drawContext.drawText(textRenderer, text, (int) (x / scaleFactor) - textRenderer.getWidth(text) / 2, (int) (y / scaleFactor), color, false); - drawContext.getMatrices().pop(); - } - - /** - * Renders a text into the world as a billboard. - * - * @param matrixStack The current matrix stack. - * @param position The position. - * @param text The text. - * @param vertexConsumerProvider The vertex consumer. - * @param size The size of the text. - * @param center If the text should be centered. - * @param throughWalls If the text should be visible through walls. - * @param color The color of the text. - */ - static void renderTextInWorld( - final MatrixStack matrixStack, - final Vec3d position, - final Text text, - final VertexConsumerProvider vertexConsumerProvider, - final float size, - final boolean center, - final boolean throughWalls, - final int color) { - final MinecraftClient minecraftClient = MinecraftClient.getInstance(); - final Camera camera = minecraftClient.gameRenderer.getCamera(); - if (!camera.isReady() || minecraftClient.getEntityRenderDispatcher().gameOptions == null) { - return; - } - final TextRenderer textRenderer = minecraftClient.textRenderer; - final double d = camera.getPos().x; - final double e = camera.getPos().y; - final double f = camera.getPos().z; - matrixStack.push(); - matrixStack.translate((float) (position.x - d), (float) (position.y - e) + 0.07f, (float) (position.z - f)); - matrixStack.multiplyPositionMatrix(new Matrix4f().rotation(camera.getRotation())); - matrixStack.scale(-size, -size, size); - final float g = center ? (float) (-textRenderer.getWidth(text)) / 2.0f : 0.0f; - - - final float backgroundOpacity = MinecraftClient.getInstance().options.getTextBackgroundOpacity(0.25f); - final int background = (int) (backgroundOpacity * 255.0f) << 24; - - textRenderer.draw(text, - g, - 0.0f, - color, - false, - matrixStack.peek().getPositionMatrix(), - vertexConsumerProvider, - TextRenderer.TextLayerType.SEE_THROUGH, - background, - 0xF000F0); - matrixStack.pop(); - } + drawContext.getMatrices().pop(); + } + + /** + * Renders a text into the world as a billboard. + * + * @param matrixStack The current matrix stack. + * @param position The position. + * @param text The text. + * @param vertexConsumerProvider The vertex consumer. + * @param size The size of the text. + * @param center If the text should be centered. + * @param throughWalls If the text should be visible through walls. + * @param color The color of the text. + */ + static void renderTextInWorld( + final MatrixStack matrixStack, + final Vec3d position, + final Text text, + final VertexConsumerProvider vertexConsumerProvider, + final float size, + final boolean center, + final boolean throughWalls, + final int color) { + final MinecraftClient minecraftClient = MinecraftClient.getInstance(); + final Camera camera = minecraftClient.gameRenderer.getCamera(); + if (!camera.isReady() || minecraftClient.getEntityRenderDispatcher().gameOptions == null) { + return; + } + final TextRenderer textRenderer = minecraftClient.textRenderer; + final double d = camera.getPos().x; + final double e = camera.getPos().y; + final double f = camera.getPos().z; + matrixStack.push(); + matrixStack.translate((float) (position.x - d), (float) (position.y - e) + 0.07f, (float) (position.z - f)); + matrixStack.multiplyPositionMatrix(new Matrix4f().rotation(camera.getRotation())); + matrixStack.scale(-size, -size, size); + final float g = center ? (float) (-textRenderer.getWidth(text)) / 2.0f : 0.0f; + + + final float backgroundOpacity = MinecraftClient.getInstance().options.getTextBackgroundOpacity(0.25f); + final int background = (int) (backgroundOpacity * 255.0f) << 24; + + textRenderer.draw(text, + g, + 0.0f, + color, + false, + matrixStack.peek().getPositionMatrix(), + vertexConsumerProvider, + TextRenderer.TextLayerType.SEE_THROUGH, + background, + 0xF000F0); + matrixStack.pop(); + } static void drawSlotHighlightBack(DrawContext context, int slotX, int slotY) { @@ -233,9 +235,35 @@ static void drawSlotHighlightFront(DrawContext context, int slotX, int slotY) { static Vec3d getInterpolated(Entity entity, double tickDelta) { return new Vec3d(MathHelper.lerp(tickDelta, entity.lastRenderX, entity.getX()), - MathHelper.lerp(tickDelta, entity.lastRenderY, entity.getY()), - MathHelper.lerp(tickDelta, entity.lastRenderZ, entity.getZ())); + MathHelper.lerp(tickDelta, entity.lastRenderY, entity.getY()), + MathHelper.lerp(tickDelta, entity.lastRenderZ, entity.getZ())); + } + + static void drawScrollableText(DrawContext context, TextRenderer textRenderer, Text text, int startX, int startY, int endX, int endY, int color) { + drawScrollableText(context, textRenderer, text, (startX + endX) / 2, startX, startY, endX, endY, color); } - final class Sealed implements RenderUtils {} + static void drawScrollableText( + DrawContext context, TextRenderer textRenderer, Text text, int centerX, int startX, int startY, int endX, int endY, int color + ) { + int i = textRenderer.getWidth(text); + int j = (startY + endY - 9) / 2 + 1; + int k = endX - startX; + if (i > k) { + int l = i - k; + double d = (double) Util.getMeasuringTimeMs() / 1000.0; + double e = Math.max((double) l * 0.5, 3.0); + double f = Math.sin((Math.PI / 2) * Math.cos((Math.PI * 2) * d / e)) / 2.0 + 0.5; + double g = MathHelper.lerp(f, 0.0, (double) l); + context.enableScissor(startX, startY, endX, endY); + context.drawTextWithShadow(textRenderer, text, startX - (int) g, j, color); + context.disableScissor(); + } else { + int l = MathHelper.clamp(centerX, startX + i / 2, endX - i / 2); + context.drawCenteredTextWithShadow(textRenderer, text, l, j, color); + } + } + + final class Sealed implements RenderUtils { + } } diff --git a/src/main/java/codes/cookies/mod/utils/accessors/InventoryScreenAccessor.java b/src/main/java/codes/cookies/mod/utils/accessors/InventoryScreenAccessor.java index bd941b8a..8d4f2d0e 100644 --- a/src/main/java/codes/cookies/mod/utils/accessors/InventoryScreenAccessor.java +++ b/src/main/java/codes/cookies/mod/utils/accessors/InventoryScreenAccessor.java @@ -1,6 +1,10 @@ package codes.cookies.mod.utils.accessors; import java.util.List; + +import net.minecraft.client.gui.Drawable; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; import net.minecraft.client.gui.screen.Screen; /** @@ -8,46 +12,54 @@ */ public interface InventoryScreenAccessor { - static int getBackgroundWidth(Screen screen) { - return ((InventoryScreenAccessor) screen).cookies$getBackgroundWidth(); - } + static int getBackgroundWidth(Screen screen) { + return ((InventoryScreenAccessor) screen).cookies$getBackgroundWidth(); + } + + static int getBackgroundHeight(Screen screen) { + return ((InventoryScreenAccessor) screen).cookies$getBackgroundHeight(); + } + + static int getX(Screen screen) { + return ((InventoryScreenAccessor) screen).cookies$getX(); + } - static int getBackgroundHeight(Screen screen) { - return ((InventoryScreenAccessor) screen).cookies$getBackgroundHeight(); - } + static int getY(Screen screen) { + return ((InventoryScreenAccessor) screen).cookies$getY(); + } - static int getX(Screen screen) { - return ((InventoryScreenAccessor) screen).cookies$getX(); - } + static void setDisabled(Screen screen, Disabled disabled) { + ((InventoryScreenAccessor) screen).cookies$setDisable(disabled); + } - static int getY(Screen screen) { - return ((InventoryScreenAccessor) screen).cookies$getY(); - } + static boolean isDisabled(Screen screen, Disabled disabled) { + return ((InventoryScreenAccessor) screen).cookies$getDisabled().contains(disabled); + } - static void setDisabled(Screen screen, Disabled disabled) { - ((InventoryScreenAccessor) screen).cookies$setDisable(disabled); - } + int cookies$getBackgroundWidth(); - static boolean isDisabled(Screen screen, Disabled disabled) { - return ((InventoryScreenAccessor) screen).cookies$getDisabled().contains(disabled); - } + int cookies$getBackgroundHeight(); - int cookies$getBackgroundWidth(); + int cookies$getX(); - int cookies$getBackgroundHeight(); + int cookies$getY(); - int cookies$getX(); - int cookies$getY(); + default void cookies$setDisable(Disabled disabled) { + cookies$getDisabled().add(disabled); + } + List cookies$getDisabled(); - default void cookies$setDisable(Disabled disabled) { - cookies$getDisabled().add(disabled); - } + default T cookies$addDrawableChild(T drawableElement) { + throw new UnsupportedOperationException(); + } - List cookies$getDisabled(); + static T addDrawableChild(Screen screen, T drawableElement) { + return ((InventoryScreenAccessor) screen).cookies$addDrawableChild(drawableElement); + } - enum Disabled { - CRAFT_HELPER; - } + enum Disabled { + CRAFT_HELPER; + } } diff --git a/src/main/java/codes/cookies/mod/utils/mixins/HandledScreenMixin.java b/src/main/java/codes/cookies/mod/utils/mixins/HandledScreenMixin.java index 76019b0e..1d139865 100644 --- a/src/main/java/codes/cookies/mod/utils/mixins/HandledScreenMixin.java +++ b/src/main/java/codes/cookies/mod/utils/mixins/HandledScreenMixin.java @@ -23,6 +23,13 @@ import codes.cookies.mod.utils.items.ItemTooltipComponent; import codes.cookies.mod.utils.items.ItemUtils; import codes.cookies.mod.utils.items.ScrollableTooltipHandler; + +import net.minecraft.client.gui.Drawable; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; + +import net.minecraft.client.gui.screen.Screen; + import org.jetbrains.annotations.Nullable; import org.lwjgl.glfw.GLFW; import org.spongepowered.asm.mixin.Mixin; @@ -60,7 +67,7 @@ * Allows for saving of screens/inventories. */ @Mixin(HandledScreen.class) -public abstract class HandledScreenMixin implements InventoryScreenAccessor { +public abstract class HandledScreenMixin extends Screen implements InventoryScreenAccessor { @Unique private static final Identifier ALLOW_SCREEN_SAVING = DevUtils.createIdentifier("save_handled_screens"); @@ -83,6 +90,10 @@ public abstract class HandledScreenMixin implements InventoryScreenAccessor { @Nullable public Slot focusedSlot; + protected HandledScreenMixin(Text title) { + super(title); + } + @Inject(method = "keyPressed", at = @At("HEAD"), cancellable = true) @SuppressWarnings("MissingJavadoc") public void keyPressed(int keyCode, int scanCode, int modifiers, CallbackInfoReturnable cir) { @@ -210,20 +221,24 @@ public void drawTooltip( @Inject(method = "mouseScrolled", at = @At("HEAD"), cancellable = true) public void onScroll( CallbackInfoReturnable cir, + @Local(argsOnly = true, ordinal = 0) double mouseX, + @Local(argsOnly = true, ordinal = 1) double mouseY, @Local(argsOnly = true, ordinal = 2) double horizontalAmount, @Local(argsOnly = true, ordinal = 3) double verticalAmount ) { - if (!ConfigKeys.MISC_SCROLLABLE_TOOLTIP.get()) { - return; - } if (((Object) this) instanceof HandledScreen handledScreen) { Slot focusedSlot = FocusedSlotAccessor.getFocusedSlot(handledScreen); - if (handledScreen.getScreenHandler().getCursorStack().isEmpty() && focusedSlot != null && - focusedSlot.hasStack()) { - final ItemStack stack = focusedSlot.getStack(); - ScrollableTooltipHandler.scroll(stack, horizontalAmount, verticalAmount); + if (handledScreen.getScreenHandler().getCursorStack().isEmpty() && focusedSlot != null && focusedSlot.hasStack()) { + if (ConfigKeys.MISC_SCROLLABLE_TOOLTIP.get()) { + final ItemStack stack = focusedSlot.getStack(); + ScrollableTooltipHandler.scroll(stack, horizontalAmount, verticalAmount); + cir.setReturnValue(true); + } + } + if (this.hoveredElement(mouseX, mouseY).filter(element -> element.mouseScrolled(mouseX, mouseY, horizontalAmount, verticalAmount)).isPresent()) { cir.setReturnValue(true); } + } } @@ -252,6 +267,11 @@ public void onScroll( return this.cookies$disabled; } + @Override + public T cookies$addDrawableChild(T drawableElement) { + return this.addDrawableChild(drawableElement); + } + @Inject( method = "mouseClicked", at = @At(value = "INVOKE", target = "Lnet/minecraft/util/Util;getMeasuringTimeMs()J"), diff --git a/src/main/java/codes/cookies/mod/utils/skyblock/inventories/ClientSideInventory.java b/src/main/java/codes/cookies/mod/utils/skyblock/inventories/ClientSideInventory.java index 0a40d1be..d0d68acb 100644 --- a/src/main/java/codes/cookies/mod/utils/skyblock/inventories/ClientSideInventory.java +++ b/src/main/java/codes/cookies/mod/utils/skyblock/inventories/ClientSideInventory.java @@ -15,14 +15,23 @@ import net.minecraft.client.MinecraftClient; import net.minecraft.client.gui.DrawContext; +import net.minecraft.client.gui.Drawable; +import net.minecraft.client.gui.Element; +import net.minecraft.client.gui.Selectable; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.client.gui.screen.ingame.HandledScreen; import net.minecraft.client.render.RenderLayer; +import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerInventory; import net.minecraft.item.ItemStack; import net.minecraft.item.Items; +import net.minecraft.screen.ScreenHandler; +import net.minecraft.screen.ScreenHandlerType; import net.minecraft.text.Text; import net.minecraft.util.Identifier; +import org.jetbrains.annotations.Nullable; + /** * Allows for creation of "inventories" that do exactly look like normal ones. * They actually just act like screens though. @@ -202,6 +211,11 @@ private void renderSlot(DrawContext context, Slot slot, int mouseX, int mouseY) return this.disableds; } + @Override + public T cookies$addDrawableChild(T drawableElement) { + return this.addDrawableChild(drawableElement); + } + @Override public boolean mouseClicked(double mouseX, double mouseY, int button) { int offsetX = this.x + 8; @@ -209,7 +223,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) { double localX = mouseX - offsetX; double localY = mouseY - offsetY; if (localX > this.slots[0].x && localX < this.slots[8].x + 18 && localY > this.slots[0].y && - localY < this.slots[this.slots.length - 1].y + 18) { + localY < this.slots[this.slots.length - 1].y + 18) { int slotX = (int) Math.floor(localX / 18); int slotY = (int) Math.floor(localY / 18); @@ -223,7 +237,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) { this.executeClick(slot, button); } } else if (localX > this.playerInventorySlots[9].x && localX < this.playerInventorySlots[8].x + 18 && - localY > this.playerInventorySlots[9].y && localY < this.playerInventorySlots[0].y + 18) { + localY > this.playerInventorySlots[9].y && localY < this.playerInventorySlots[0].y + 18) { int slotX = (int) Math.floor(localX / 18); int slotY = (int) Math.floor((localY - playerInventorySlots[9].y) / 18); @@ -272,5 +286,4 @@ public Slot(int x, int y) { this.y = y; } } - }