From 5332083d378f206e2ca55adb7cb1f729994a6717 Mon Sep 17 00:00:00 2001 From: Kevinthegreat <92656833+kevinthegreat1@users.noreply.github.com> Date: Sun, 29 Dec 2024 15:48:16 -0800 Subject: [PATCH] Refactor SpeedPresets --- .../skyblocker/mixins/DrawContextMixin.java | 2 +- .../speedPreset/SpeedPresetListWidget.java | 21 ++++++++----------- .../skyblock/speedPreset/SpeedPresets.java | 18 ++++++++-------- 3 files changed, 19 insertions(+), 22 deletions(-) diff --git a/src/main/java/de/hysky/skyblocker/mixins/DrawContextMixin.java b/src/main/java/de/hysky/skyblocker/mixins/DrawContextMixin.java index 8e78bf5ab2..56a5183a91 100644 --- a/src/main/java/de/hysky/skyblocker/mixins/DrawContextMixin.java +++ b/src/main/java/de/hysky/skyblocker/mixins/DrawContextMixin.java @@ -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; } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPresetListWidget.java b/src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPresetListWidget.java index fe7e2aacff..bfe22a6c97 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPresetListWidget.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPresetListWidget.java @@ -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; @@ -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 { @@ -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(); - 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() { @@ -149,11 +147,9 @@ public List 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() { @@ -172,9 +168,10 @@ protected void updatePosition() { } @Nullable - protected Pair getMapping() { + protected ObjectIntPair 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; } diff --git a/src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPresets.java b/src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPresets.java index 63d32cd5ad..b3af7c413c 100644 --- a/src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPresets.java +++ b/src/main/java/de/hysky/skyblocker/skyblock/speedPreset/SpeedPresets.java @@ -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(); } @@ -97,7 +97,7 @@ public void forEach(BiConsumer consumer) { this.presets.forEach(consumer); } - public boolean compare(Map presets) { + public boolean arePresetsEqual(Map presets) { return this.presets.equals(presets); } @@ -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); } }