Skip to content

Commit

Permalink
Refactor SpeedPresets
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinthegreat1 committed Dec 29, 2024
1 parent ea4efb8 commit 5332083
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

@Mixin(DrawContext.class)
public abstract class DrawContextMixin {
@ModifyExpressionValue(method = "drawCooldownProgress", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/ItemCooldownManager;getCooldownProgress(Lnet/minecraft/item/ItemStack;F)F"))
@ModifyExpressionValue(method = "drawCooldownProgress", at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/ItemCooldownManager;getCooldownProgress(Lnet/minecraft/item/ItemStack;F)F"))
private float skyblocker$modifyItemCooldown(float cooldownProgress, @Local(argsOnly = true) ItemStack stack) {
return Utils.isOnSkyblock() && ItemCooldowns.isOnCooldown(stack) ? ItemCooldowns.getItemCooldownEntry(stack).getRemainingCooldownPercent() : cooldownProgress;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.hysky.skyblocker.skyblock.speedPreset;

import it.unimi.dsi.fastutil.Pair;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import it.unimi.dsi.fastutil.objects.ObjectIntPair;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.Drawable;
Expand All @@ -15,6 +14,7 @@
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> {

Expand Down Expand Up @@ -42,15 +42,13 @@ 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 = new Object2IntOpenHashMap<String>();
this.children().stream()
var childrenMap = this.children().stream()
.filter(SpeedPresetEntry.class::isInstance)
.map(SpeedPresetEntry.class::cast)
.filter(entry -> !entry.isEmpty())
.map(SpeedPresetEntry::getMapping)
.filter(Objects::nonNull)
.forEach(mapping -> childrenMap.put(mapping.first(), mapping.second()));
return !presets.compare(childrenMap);
.collect(Collectors.toMap(ObjectIntPair::key, ObjectIntPair::valueInt));
return !presets.arePresetsEqual(childrenMap);
}

public void updatePosition() {
Expand Down Expand Up @@ -149,11 +147,9 @@ public List<? extends Element> children() {
}

public void save() {
var presets = SpeedPresets.getInstance();
if (this.isEmpty()) return;
var mapping = getMapping();
if (mapping != null)
presets.setPreset(mapping.first(), mapping.second());
SpeedPresets.getInstance().setPreset(mapping.key(), mapping.valueInt());
}

protected boolean isEmpty() {
Expand All @@ -172,9 +168,10 @@ protected void updatePosition() {
}

@Nullable
protected Pair<String, Short> getMapping() {
protected ObjectIntPair<String> getMapping() {
if (isEmpty()) return null;
try {
return Pair.of(titleInput.getText(), Short.parseShort(speedInput.getText()));
return ObjectIntPair.of(titleInput.getText(), Integer.parseInt(speedInput.getText()));
} catch (NumberFormatException e) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public int getPreset(String name) {
return this.presets.getOrDefault(name, 0);
}

public void setPreset(String name, short value) {
public void setPreset(String name, int value) {
this.presets.put(name, value);
savePresets();
}
Expand All @@ -97,7 +97,7 @@ public void forEach(BiConsumer<String, Integer> consumer) {
this.presets.forEach(consumer);
}

public boolean compare(Map<String, Integer> presets) {
public boolean arePresetsEqual(Map<String, Integer> presets) {
return this.presets.equals(presets);
}

Expand Down Expand Up @@ -135,12 +135,12 @@ public void savePresets() {
// According to: https://www.reddit.com/r/HypixelSkyblock/comments/14kkz07/speed_vs_farming_fortune/
public void loadDefaults() {
this.presets.clear();
this.presets.put("default", (short) 100);
this.presets.put("crops", (short) 93);
this.presets.put("cocoa", (short) 155);
this.presets.put("mushroom", (short) 233);
this.presets.put("cane", (short) 327);
this.presets.put("squash", (short) 327);
this.presets.put("cactus", (short) 464);
this.presets.put("default", 100);
this.presets.put("crops", 93);
this.presets.put("cocoa", 155);
this.presets.put("mushroom", 233);
this.presets.put("cane", 327);
this.presets.put("squash", 327);
this.presets.put("cactus", 464);
}
}

0 comments on commit 5332083

Please sign in to comment.