-
-
Notifications
You must be signed in to change notification settings - Fork 181
Added time input calculator #2189
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
NamelessJu
wants to merge
5
commits into
SkyblockerMod:main
Choose a base branch
from
NamelessJu:master
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
142 changes: 142 additions & 0 deletions
142
src/main/java/de/hysky/skyblocker/skyblock/calculators/SignTimeCalculator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,142 @@ | ||
| package de.hysky.skyblocker.skyblock.calculators; | ||
|
|
||
| import net.minecraft.ChatFormatting; | ||
| import net.minecraft.client.Minecraft; | ||
| import net.minecraft.client.gui.GuiGraphics; | ||
| import net.minecraft.network.chat.CommonComponents; | ||
| import net.minecraft.network.chat.Component; | ||
| import net.minecraft.network.chat.MutableComponent; | ||
| import org.jetbrains.annotations.VisibleForTesting; | ||
|
|
||
| import java.util.Locale; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
|
|
||
| public class SignTimeCalculator | ||
| { | ||
| public enum TimeType { | ||
| HOURS, MINUTES | ||
| } | ||
|
|
||
| private static final Minecraft CLIENT = Minecraft.getInstance(); | ||
| private static final Pattern TIME_CODE_PATTERN = Pattern.compile("(\\d+\\.\\d+|\\d+)(\\D+)?"); | ||
| private static final int SECONDS_PER_DAY = 86400; | ||
| private static final short SECONDS_PER_HOUR = 3600; | ||
| private static final byte SECONDS_PER_MINUTE = 60; | ||
|
|
||
| private static String lastInput; | ||
| private static long seconds; | ||
| private static Component error = null; | ||
|
|
||
| public static void renderTime(GuiGraphics context, String message, TimeType type, int renderX, int renderY) { | ||
| calculate(message, type); | ||
| render(context, renderX, renderY); | ||
| } | ||
|
|
||
| @VisibleForTesting | ||
| public static void calculate(String message, TimeType type) { | ||
| //only update if new input | ||
| if (message.equals(lastInput)) return; | ||
| lastInput = message; | ||
|
|
||
| if (message.isBlank()) { | ||
| seconds = -1; | ||
| error = Component.translatable("skyblocker.config.uiAndVisuals.timeInputCalculator.emptyInputError").withStyle(ChatFormatting.RED); | ||
| return; | ||
| } | ||
|
|
||
| long newSeconds = 0; | ||
| for (String segment : message.trim().split(" +")) { | ||
| Matcher matcher = TIME_CODE_PATTERN.matcher(segment); | ||
| if (matcher.matches()) { | ||
| String numberString = matcher.group(1); | ||
| if (numberString.startsWith("-")) { | ||
| seconds = -1; | ||
| error = Component.translatable("skyblocker.config.uiAndVisuals.timeInputCalculator.invalidNumberError", numberString).withStyle(ChatFormatting.RED); | ||
| return; | ||
| } | ||
|
Comment on lines
+53
to
+57
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice negative time (your regex doesn't allow this btw). |
||
| double number; | ||
| try { | ||
| number = Double.parseDouble(numberString); | ||
| } catch (NumberFormatException e) { | ||
| seconds = -1; | ||
| error = Component.translatable("skyblocker.config.uiAndVisuals.timeInputCalculator.invalidNumberError", numberString).withStyle(ChatFormatting.RED); | ||
| return; | ||
| } | ||
|
|
||
| String timeUnitString = matcher.group(2); | ||
| if (timeUnitString == null || timeUnitString.isEmpty()) { | ||
| newSeconds += switch (type) { | ||
| case HOURS -> Math.round(number * SECONDS_PER_HOUR); | ||
| case MINUTES -> Math.round(number * SECONDS_PER_MINUTE); | ||
| }; | ||
| } | ||
| //mimics how Hypixel parses numbers, except also allowing plurals | ||
| else if ("days".startsWith(timeUnitString.toLowerCase(Locale.ENGLISH))) { | ||
| newSeconds += Math.round(number * SECONDS_PER_DAY); | ||
| } else if ("hours".startsWith(timeUnitString.toLowerCase(Locale.ENGLISH))) { | ||
| newSeconds += Math.round(number * SECONDS_PER_HOUR); | ||
| } else if ("minutes".startsWith(timeUnitString.toLowerCase(Locale.ENGLISH))) { | ||
| newSeconds += Math.round(number * SECONDS_PER_MINUTE); | ||
| } else if ("seconds".startsWith(timeUnitString.toLowerCase(Locale.ENGLISH))) { | ||
| //note/fun fact: for some reason Hypixel only allows up to "secon", not "second" | ||
| newSeconds += Math.round(number); | ||
| } else { | ||
| seconds = -1; | ||
| error = Component.translatable("skyblocker.config.uiAndVisuals.timeInputCalculator.invalidTimeUnit", timeUnitString).withStyle(ChatFormatting.RED); | ||
| return; | ||
| } | ||
| } else { | ||
| seconds = -1; | ||
| error = Component.translatable("skyblocker.config.uiAndVisuals.timeInputCalculator.invalidInputSpecific", segment).withStyle(ChatFormatting.RED); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| seconds = newSeconds; | ||
| error = null; | ||
| } | ||
|
|
||
| public static String getNewValue() { | ||
| if (seconds == -1) { | ||
| //if calculator is not activated or just invalid input return what the user typed in | ||
| return lastInput; | ||
| } | ||
|
|
||
| //we can just return the total seconds, Hypixel converts it for us :) | ||
| return seconds + "s"; | ||
| } | ||
|
|
||
| private static void render(GuiGraphics context, int renderX, int renderY) { | ||
| Component text; | ||
| if (seconds == -1) { | ||
| text = error != null ? error : Component.translatable("skyblocker.config.uiAndVisuals.timeInputCalculator.invalidInput").withStyle(ChatFormatting.RED); | ||
| } else { | ||
| long remainingSeconds = seconds; | ||
| MutableComponent timeText = Component.empty(); | ||
| remainingSeconds = appendTimeComponent(timeText, remainingSeconds, SECONDS_PER_DAY, "day"); | ||
| remainingSeconds = appendTimeComponent(timeText, remainingSeconds, SECONDS_PER_HOUR, "hour"); | ||
| remainingSeconds = appendTimeComponent(timeText, remainingSeconds, SECONDS_PER_MINUTE, "minute"); | ||
| appendTimeComponent(timeText, remainingSeconds, 1, "second"); | ||
| text = timeText.withStyle(ChatFormatting.GREEN); | ||
| } | ||
|
|
||
| context.drawCenteredString(CLIENT.font, text, renderX, renderY, 0xFFFFFFFF); | ||
| } | ||
|
|
||
| private static long appendTimeComponent(MutableComponent component, long seconds, int secondsPerUnit, String translationKeyUnit) | ||
| { | ||
| long units = seconds / secondsPerUnit; //long division -> cuts off decimal places automatically | ||
| if (units > 0) { | ||
| seconds -= units * secondsPerUnit; | ||
| if (!component.equals(CommonComponents.EMPTY)) { | ||
| component.append(" "); | ||
| } | ||
| component.append(Component.translatable( | ||
| "skyblocker.config.uiAndVisuals.timeInputCalculator.unit." + translationKeyUnit + (units == 1 ? "" : "s"), | ||
| units | ||
| )); | ||
| } | ||
| return seconds; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you need to check time type as well instead of just last input?