-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
Add Speed Presets for configurable speed settings #1111
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
2ac58b9
Add Speed Presets for configurable speed settings
9682f4a
Reverted the proposed changes to the protected item texture
919f9b8
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 50774d4
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 4dd9fb6
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 1aa8859
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 d36930b
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 617084c
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 947c21b
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 5c4d6ee
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 6a4b474
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 ed4cfdc
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 ab8f7ce
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 1c78583
Update src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPr…
Manchick0 43d08ea
Refactor modification checks for SpeedPreset entries.
010b328
Clean up pointless changes
ea4efb8
Refactor SpeedPresets to use Object2IntMap.
5332083
Refactor SpeedPresets
kevinthegreat1 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 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 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 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 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 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
180 changes: 180 additions & 0 deletions
180
src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPresetListWidget.java
This file contains 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,180 @@ | ||
package de.hysky.skyblocker.skyblock.speedPreset; | ||
|
||
import it.unimi.dsi.fastutil.objects.ObjectIntPair; | ||
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.widget.*; | ||
import net.minecraft.text.Text; | ||
import net.minecraft.util.Formatting; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.regex.Pattern; | ||
import java.util.stream.Collectors; | ||
|
||
public class SpeedPresetListWidget extends ElementListWidget<SpeedPresetListWidget.AbstractEntry> { | ||
|
||
private static final Pattern NUMBER = Pattern.compile("^-?\\d+(\\.\\d+)?$"); | ||
// Alphanumeric sequence that doesn't start with a number. | ||
private static final Pattern TITLE = Pattern.compile("^[a-zA-Z][a-zA-Z0-9_]*$"); | ||
|
||
public SpeedPresetListWidget(int width, int height, int y) { | ||
super(MinecraftClient.getInstance(), width, height, y, 25); | ||
var presets = SpeedPresets.getInstance(); | ||
addEntry(new TitleEntry()); | ||
if (presets.getPresetCount() > 0) | ||
presets.forEach((title, speed) -> | ||
this.addEntry(new SpeedPresetEntry(title, String.valueOf(speed)))); | ||
else | ||
this.addEntry(new SpeedPresetEntry("", "")); | ||
} | ||
|
||
@Override | ||
public int getRowWidth() { | ||
return super.getRowWidth() + 104; | ||
} | ||
|
||
public boolean hasBeenChanged() { | ||
var presets = SpeedPresets.getInstance(); | ||
// If there are fewer children than presets, some were removed, and all further checks are pointless | ||
if (children().size() < presets.getPresetCount()) return true; | ||
var childrenMap = this.children().stream() | ||
.filter(SpeedPresetEntry.class::isInstance) | ||
.map(SpeedPresetEntry.class::cast) | ||
.map(SpeedPresetEntry::getMapping) | ||
.filter(Objects::nonNull) | ||
.collect(Collectors.toMap(ObjectIntPair::key, ObjectIntPair::valueInt)); | ||
return !presets.arePresetsEqual(childrenMap); | ||
} | ||
|
||
public void updatePosition() { | ||
children().forEach(AbstractEntry::updatePosition); | ||
} | ||
|
||
public void newEntry() { | ||
var entry = new SpeedPresetEntry("", ""); | ||
this.addEntry(entry); | ||
this.centerScrollOn(entry); | ||
this.setSelected(entry); | ||
this.setFocused(entry); | ||
} | ||
|
||
public void save() { | ||
var presets = SpeedPresets.getInstance(); | ||
presets.clear(); | ||
children().stream().filter(SpeedPresetEntry.class::isInstance).map(SpeedPresetEntry.class::cast).forEach(SpeedPresetEntry::save); | ||
presets.savePresets(); // Write down the changes. | ||
} | ||
|
||
public abstract static class AbstractEntry extends ElementListWidget.Entry<AbstractEntry> { | ||
|
||
protected void updatePosition() {} | ||
|
||
@Override | ||
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { | ||
this.children().forEach(child -> { | ||
if (child instanceof Widget widget) | ||
widget.setY(y); | ||
if (child instanceof Drawable drawable) | ||
drawable.render(context, mouseX, mouseY, tickDelta); | ||
}); | ||
} | ||
} | ||
|
||
public class TitleEntry extends AbstractEntry { | ||
|
||
@Override | ||
public void render(DrawContext context, int index, int y, int x, int entryWidth, int entryHeight, int mouseX, int mouseY, boolean hovered, float tickDelta) { | ||
// The line height is 25, the height of a single character is always 9. | ||
// 25 - 9 = 16, 16 / 2 = 8, therefore the Y-offset should be 8. | ||
context.drawCenteredTextWithShadow(client.textRenderer, Text.translatable("skyblocker.config.general.speedPresets.config.title"), width / 2 - 50, y + 8, 0xFFFFFF); | ||
context.drawCenteredTextWithShadow(client.textRenderer, Text.translatable("skyblocker.config.general.speedPresets.config.speed"), width / 2 + 50, y + 8, 0xFFFFFF); | ||
} | ||
|
||
@Override | ||
public List<? extends Selectable> selectableChildren() { | ||
return List.of(); | ||
} | ||
|
||
@Override | ||
public List<? extends Element> children() { | ||
return List.of(); | ||
} | ||
} | ||
|
||
public class SpeedPresetEntry extends AbstractEntry { | ||
|
||
protected final TextFieldWidget titleInput; | ||
protected final TextFieldWidget speedInput; | ||
protected final ButtonWidget removeButton; | ||
|
||
public SpeedPresetEntry(String title, String speed) { | ||
var client = SpeedPresetListWidget.this.client; | ||
|
||
// All Xs and Ys are then set using the initPosition() method. | ||
this.titleInput = new TextFieldWidget(client.textRenderer, 0, 0, 120, 20, Text.empty()); | ||
this.titleInput.setTextPredicate(str -> str.isEmpty() || TITLE.matcher(str).matches()); | ||
this.titleInput.setText(title); | ||
this.titleInput.setMaxLength(16); | ||
this.titleInput.setPlaceholder(Text.literal("newPreset").formatted(Formatting.DARK_GRAY)); | ||
this.speedInput = new TextFieldWidget(client.textRenderer, 0, 0, 50, 20, Text.empty()); | ||
|
||
this.speedInput.setTextPredicate(str -> str.isEmpty() || NUMBER.matcher(str).matches()); | ||
this.speedInput.setText(speed); | ||
this.speedInput.setMaxLength(3); | ||
this.speedInput.setPlaceholder(Text.literal("0").formatted(Formatting.DARK_GRAY)); | ||
|
||
this.removeButton = ButtonWidget.builder(Text.literal("-"), | ||
(btn) -> SpeedPresetListWidget.this.removeEntry(this)) | ||
.dimensions(0, 0, 20, 20) | ||
.build(); | ||
|
||
this.updatePosition(); | ||
} | ||
|
||
@Override | ||
public List<? extends Selectable> selectableChildren() { | ||
return List.of(titleInput, speedInput, removeButton); | ||
} | ||
|
||
@Override | ||
public List<? extends Element> children() { | ||
return List.of(titleInput, speedInput, removeButton); | ||
} | ||
|
||
public void save() { | ||
var mapping = getMapping(); | ||
if (mapping != null) | ||
SpeedPresets.getInstance().setPreset(mapping.key(), mapping.valueInt()); | ||
} | ||
|
||
protected boolean isEmpty() { | ||
return titleInput.getText().isEmpty() && speedInput.getText().isEmpty(); | ||
} | ||
|
||
@Override | ||
protected void updatePosition() { | ||
var grid = new GridWidget(); | ||
grid.setSpacing(2); | ||
grid.add(titleInput, 0, 0, 1, 3); | ||
grid.add(speedInput, 0, 3, 1, 2); | ||
grid.add(removeButton, 0, 5, 1, 1); | ||
grid.refreshPositions(); | ||
SimplePositioningWidget.setPos(grid, 0, 0, width, itemHeight, 0.5f, 0.5f); | ||
} | ||
|
||
@Nullable | ||
protected ObjectIntPair<String> getMapping() { | ||
if (isEmpty()) return null; | ||
try { | ||
return ObjectIntPair.of(titleInput.getText(), Integer.parseInt(speedInput.getText())); | ||
} catch (NumberFormatException e) { | ||
return null; | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
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.
This logic has some problems, where an entry changed and then changed back to the original would still be marked as changed. In general, you need a validation method that checks if all entries are the same. I'd suggest looking at
ShortcutsConfigListWidget#hasChanges
.