Skip to content

Commit

Permalink
Merge pull request #896 from viciscat/fastutil-refactor
Browse files Browse the repository at this point in the history
Fast util refactor
  • Loading branch information
AzureAaron authored Aug 12, 2024
2 parents 9b95005 + a09b40e commit 11fc3c2
Show file tree
Hide file tree
Showing 18 changed files with 102 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.utils.Utils;
import de.hysky.skyblocker.utils.scheduler.Scheduler;
import it.unimi.dsi.fastutil.booleans.BooleanPredicate;
import net.fabricmc.fabric.api.client.event.lifecycle.v1.ClientEntityEvents;
import net.fabricmc.fabric.api.client.message.v1.ClientReceiveMessageEvents;
import net.fabricmc.fabric.api.client.networking.v1.ClientPlayConnectionEvents;
Expand All @@ -28,7 +29,6 @@

import java.util.Arrays;
import java.util.Objects;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand All @@ -51,9 +51,9 @@ protected enum DojoChallenges {
TENACITY("Tenacity", enabled -> SkyblockerConfigManager.get().crimsonIsle.dojo.enableTenacityHelper);

private final String name;
private final Predicate<Boolean> enabled;
private final BooleanPredicate enabled;

DojoChallenges(String name, Predicate<Boolean> enabled) {
DojoChallenges(String name, BooleanPredicate enabled) {
this.name = name;
this.enabled = enabled;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.hysky.skyblocker.skyblock.crimson.dojo;

import de.hysky.skyblocker.utils.render.RenderHelper;
import it.unimi.dsi.fastutil.objects.Object2LongMap;
import it.unimi.dsi.fastutil.objects.Object2LongOpenHashMap;
import net.fabricmc.fabric.api.client.rendering.v1.WorldRenderContext;
import net.minecraft.entity.Entity;
Expand All @@ -12,7 +13,6 @@

import java.awt.*;
import java.text.DecimalFormat;
import java.util.Map;

public class ForceTestHelper {

Expand Down Expand Up @@ -62,8 +62,8 @@ protected static void onEntityDespawn(Entity entity) {
protected static void render(WorldRenderContext context) {
//render times
long currentTime = System.currentTimeMillis();
for (Map.Entry<ZombieEntity, Long> zombie : zombies.object2LongEntrySet()) {
float secondsTime = Math.max((zombie.getValue() - currentTime) / 1000f, 0);
for (Object2LongMap.Entry<ZombieEntity> zombie : zombies.object2LongEntrySet()) {
float secondsTime = Math.max((zombie.getLongValue() - currentTime) / 1000f, 0);

MutableText text = Text.literal(FORMATTER.format(secondsTime));
if (secondsTime > 1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ private static void render(WorldRenderContext context) {
Vec3d highlightSpot = Vec3d.ZERO;

//if to old remove particle
activeParticles.entrySet().removeIf(e -> System.currentTimeMillis() - e.getValue() > MAX_PARTICLE_LIFE_TIME);
activeParticles.object2LongEntrySet().removeIf(e -> System.currentTimeMillis() - e.getLongValue() > MAX_PARTICLE_LIFE_TIME);

//add up all particle within range of active block
for (Vec3d particlePos : activeParticles.keySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,11 @@ private static void timeUpdate() {
if (skyblockEvent == null) continue;
}
String eventName = entry.getKey();
// Cannot be changed to fast util due to casting issues
List<Integer> reminderTimes = SkyblockerConfigManager.get().eventNotifications.eventsReminderTimes.getOrDefault(eventName, DEFAULT_REMINDERS);
if (reminderTimes.isEmpty()) continue;

for (Integer reminderTime : reminderTimes) {
for (int reminderTime : reminderTimes) {
if (criterionMet() && currentTime + reminderTime < skyblockEvent.start() && newTime + reminderTime >= skyblockEvent.start()) {
MinecraftClient instance = MinecraftClient.getInstance();
if (eventName.equals(JACOBS)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,22 @@
import de.hysky.skyblocker.config.SkyblockerConfigManager;
import de.hysky.skyblocker.config.configs.HelperConfig;
import de.hysky.skyblocker.utils.container.SimpleContainerSolver;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import net.fabricmc.fabric.api.client.screen.v1.ScreenEvents;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen;
import net.minecraft.item.ItemStack;
import org.intellij.lang.annotations.Language;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

public abstract sealed class ExperimentSolver extends SimpleContainerSolver permits ChronomatronSolver, SuperpairsSolver, UltrasequencerSolver {
public enum State {
REMEMBER, WAIT, SHOW, END
}

private State state = State.REMEMBER;
private final Map<Integer, ItemStack> slots = new HashMap<>();
private final Int2ObjectMap<ItemStack> slots = new Int2ObjectOpenHashMap<>();

protected ExperimentSolver(@NotNull @Language("RegExp") String containerName) {
super(containerName);
Expand All @@ -33,7 +32,7 @@ public void setState(State state) {
this.state = state;
}

public Map<Integer, ItemStack> getSlots() {
public Int2ObjectMap<ItemStack> getSlots() {
return slots;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import de.hysky.skyblocker.config.configs.HelperConfig;
import de.hysky.skyblocker.utils.render.gui.ColorHighlight;
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.IntOpenHashSet;
import it.unimi.dsi.fastutil.ints.IntSet;
import net.minecraft.client.gui.screen.Screen;
import net.minecraft.client.gui.screen.ingame.GenericContainerScreen;
import net.minecraft.item.ItemStack;
Expand All @@ -13,7 +15,7 @@
public final class SuperpairsSolver extends ExperimentSolver {
private int superpairsPrevClickedSlot;
private ItemStack superpairsCurrentSlot;
private final Set<Integer> superpairsDuplicatedSlots = new HashSet<>();
private final IntSet superpairsDuplicatedSlots = new IntOpenHashSet();

public SuperpairsSolver() {
super("^Superpairs \\(\\w+\\)$");
Expand Down Expand Up @@ -50,7 +52,7 @@ protected void tick(Screen screen) {
if (getState() == State.SHOW && getSlots().get(superpairsPrevClickedSlot) == null) {
ItemStack itemStack = genericContainerScreen.getScreenHandler().getInventory().getStack(superpairsPrevClickedSlot);
if (!(itemStack.isOf(Items.CYAN_STAINED_GLASS) || itemStack.isOf(Items.BLACK_STAINED_GLASS_PANE) || itemStack.isOf(Items.AIR))) {
getSlots().entrySet().stream().filter((entry -> ItemStack.areEqual(entry.getValue(), itemStack))).findAny().ifPresent(entry -> superpairsDuplicatedSlots.add(entry.getKey()));
getSlots().int2ObjectEntrySet().stream().filter((entry -> ItemStack.areEqual(entry.getValue(), itemStack))).findAny().ifPresent(entry -> superpairsDuplicatedSlots.add(entry.getIntKey()));
getSlots().put(superpairsPrevClickedSlot, itemStack);
superpairsCurrentSlot = itemStack;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package de.hysky.skyblocker.skyblock.fancybars;

import it.unimi.dsi.fastutil.booleans.BooleanConsumer;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
Expand Down Expand Up @@ -188,7 +189,7 @@ public void setOnChange(Consumer<T> onChange) {
public static class BooleanOption extends ClickableWidget {

private boolean current = false;
private Consumer<Boolean> onChange = null;
private BooleanConsumer onChange = null;

public BooleanOption(int x, int y, int width, Text message) {
super(x, y, width, 11, message);
Expand Down Expand Up @@ -220,7 +221,7 @@ public void setCurrent(boolean current) {
this.current = current;
}

public void setOnChange(Consumer<Boolean> onChange) {
public void setOnChange(BooleanConsumer onChange) {
this.onChange = onChange;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import it.unimi.dsi.fastutil.Pair;
import it.unimi.dsi.fastutil.objects.ObjectBooleanMutablePair;
import it.unimi.dsi.fastutil.objects.ObjectBooleanPair;
import it.unimi.dsi.fastutil.objects.ObjectObjectMutablePair;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.client.gui.ScreenPos;
Expand Down Expand Up @@ -39,7 +40,7 @@ public StatusBarsConfigScreen() {

private BarLocation currentInsertLocation = new BarLocation(null, 0, 0);

private final Pair<BarLocation, Boolean> resizeHover = new ObjectBooleanMutablePair<>(BarLocation.NULL, false);
private final ObjectBooleanPair<BarLocation> resizeHover = new ObjectBooleanMutablePair<>(BarLocation.NULL, false);

private final Pair<BarLocation, BarLocation> resizedBars = ObjectObjectMutablePair.of(BarLocation.NULL, BarLocation.NULL);
private boolean resizing = false;
Expand Down Expand Up @@ -346,7 +347,7 @@ public boolean mouseClicked(double mouseX, double mouseY, int button) {
if (!editBarWidget.isMouseOver(mouseX, mouseY) && button == 0 && !first.equals(BarLocation.NULL)) {
BarPositioner.BarAnchor barAnchor = first.barAnchor();
assert barAnchor != null;
if (resizeHover.right()) {
if (resizeHover.rightBoolean()) {
resizedBars.left(first);

if (FancyStatusBars.barPositioner.hasNeighbor(barAnchor, first.y(), first.x(), true)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ private static void drawScreenItems(DrawContext context, TextRenderer textRender
context.getMatrices().pop();
}

private static int drawItemEntryWithHover(DrawContext context, TextRenderer textRenderer, Map.Entry<String, Integer> itemEntry, int index, int mouseX, int mouseY) {
private static int drawItemEntryWithHover(DrawContext context, TextRenderer textRenderer, Object2IntMap.Entry<String> itemEntry, int index, int mouseX, int mouseY) {
String itemName = itemEntry.getKey();
int amount = itemEntry.getValue();
int amount = itemEntry.getIntValue();
ItemStack stack = getCachedItem(itemName);
drawItemEntryWithHover(context, textRenderer, stack, itemName, amount, index, mouseX, mouseY);
return index + 1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package de.hysky.skyblocker.skyblock.itemlist;

import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;

import java.util.Map;

public class ItemFixerUpper {
Expand Down Expand Up @@ -173,7 +177,7 @@ public class ItemFixerUpper {
"minecraft:fern"
};

private final static Map<Integer, String> SPAWN_EGG_VARIANTS = Map.ofEntries(
private final static Int2ObjectMap<String> SPAWN_EGG_VARIANTS = Int2ObjectMaps.unmodifiable(new Int2ObjectOpenHashMap<>(Map.ofEntries(
//This entry 0 is technically not right but Hypixel decided to make it polar bear so well we use that
Map.entry(0, "minecraft:polar_bear_spawn_egg"),
Map.entry(50, "minecraft:creeper_spawn_egg"),
Expand Down Expand Up @@ -203,7 +207,7 @@ public class ItemFixerUpper {
Map.entry(100, "minecraft:horse_spawn_egg"),
Map.entry(101, "minecraft:rabbit_spawn_egg"),
Map.entry(120, "minecraft:villager_spawn_egg")
);
)));

private final static String[] SANDSTONE_VARIANTS = {
":",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import de.hysky.skyblocker.utils.Http;
import de.hysky.skyblocker.utils.ProfileUtils;
import de.hysky.skyblocker.utils.scheduler.Scheduler;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenHashMap;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager;
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback;
Expand Down Expand Up @@ -218,15 +220,15 @@ public static void initClass() {
JsonObject jsonObject = JsonParser.parseString(Http.sendGetRequest(HYPIXEL_COLLECTIONS)).getAsJsonObject();
if (jsonObject.get("success").getAsBoolean()) {
Map<String, String[]> collectionsMap = new HashMap<>();
Map<String, List<Integer>> tierRequirementsMap = new HashMap<>();
Map<String, IntList> tierRequirementsMap = new HashMap<>();
JsonObject collections = jsonObject.getAsJsonObject("collections");
collections.entrySet().forEach(entry -> {
String category = entry.getKey();
JsonObject itemsObject = entry.getValue().getAsJsonObject().getAsJsonObject("items");
String[] items = itemsObject.keySet().toArray(new String[0]);
collectionsMap.put(category, items);
itemsObject.entrySet().forEach(itemEntry -> {
List<Integer> tierReqs = new ArrayList<>();
IntList tierReqs = new IntArrayList();
itemEntry.getValue().getAsJsonObject().getAsJsonArray("tiers").forEach(req ->
tierReqs.add(req.getAsJsonObject().get("amountRequired").getAsInt()));
tierRequirementsMap.put(itemEntry.getKey(), tierReqs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import de.hysky.skyblocker.skyblock.itemlist.ItemRepository;
import de.hysky.skyblocker.skyblock.profileviewer.ProfileViewerPage;
import de.hysky.skyblocker.skyblock.tabhud.util.Ico;
import it.unimi.dsi.fastutil.ints.IntList;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
Expand Down Expand Up @@ -38,7 +39,7 @@ public class GenericCategory implements ProfileViewerPage {
private static final int COLUMNS = 7;

private final Map<String, String[]> collectionsMap;
private final Map<String, List<Integer>> tierRequirementsMap;
private final Map<String, IntList> tierRequirementsMap;
private final Map<String, String> ICON_TRANSLATION = Map.ofEntries(
Map.entry("MUSHROOM_COLLECTION", "RED_MUSHROOM"));
private final String[] ROMAN_NUMERALS = {"-", "I", "II", "III", "IV", "V", "VI", "VII", "VIII", "IX", "X", "XI", "XII", "XIII", "XIV", "XV", "XVI", "XVII", "XVIII", "XIX", "XX"};
Expand All @@ -48,13 +49,13 @@ public GenericCategory(JsonObject hProfile, JsonObject pProfile, String collecti
//noinspection unchecked
collectionsMap = (Map<String, String[]>) fetchedData.get("COLLECTIONS");
//noinspection unchecked
tierRequirementsMap = (Map<String, List<Integer>>) fetchedData.get("TIER_REQS");
tierRequirementsMap = (Map<String, IntList>) fetchedData.get("TIER_REQS");
this.category = collection;
setupItemStacks(hProfile, pProfile);
}

private int calculateTier(int achieved, List<Integer> requirements) {
return (int) requirements.stream().filter(req -> achieved >= req).count();
private int calculateTier(int achieved, IntList requirements) {
return (int) requirements.intStream().filter(req -> achieved >= req).count();
}

private void setupItemStacks(JsonObject hProfile, JsonObject pProfile) {
Expand Down Expand Up @@ -84,7 +85,7 @@ private void setupItemStacks(JsonObject hProfile, JsonObject pProfile) {
}

int collectionTier = calculateTier(totalCollection, tierRequirementsMap.get(collection));
List<Integer> tierRequirements = tierRequirementsMap.get(collection);
IntList tierRequirements = tierRequirementsMap.get(collection);

List<Text> lore = new ArrayList<>();
lore.add(Text.literal("Collection Item").setStyle(style).formatted(Formatting.DARK_GRAY));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@
import com.google.gson.JsonObject;
import de.hysky.skyblocker.SkyblockerMod;
import de.hysky.skyblocker.skyblock.tabhud.util.Ico;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.client.MinecraftClient;
import net.minecraft.client.font.TextRenderer;
import net.minecraft.client.gui.DrawContext;
import net.minecraft.util.Identifier;

import java.awt.*;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.Map;

public class DungeonMiscStatsWidgets {
Expand All @@ -21,7 +22,7 @@ public class DungeonMiscStatsWidgets {
private static final DecimalFormat DF = new DecimalFormat("#.##");
private static final String[] DUNGEONS = {"catacombs", "master_catacombs"};

private final Map<String, Integer> dungeonRuns = new HashMap<>();
private final Object2IntMap<String> dungeonRuns = new Object2IntOpenHashMap<>();
private int secrets = 0;
private int totalRuns = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
import it.unimi.dsi.fastutil.ints.Int2ObjectMap;
import it.unimi.dsi.fastutil.ints.Int2ObjectMaps;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.objects.Object2IntMap;
import it.unimi.dsi.fastutil.objects.Object2IntMaps;
import it.unimi.dsi.fastutil.objects.Object2IntOpenHashMap;
import net.minecraft.component.DataComponentTypes;
import net.minecraft.component.type.LoreComponent;
import net.minecraft.component.type.ProfileComponent;
Expand Down Expand Up @@ -51,9 +54,9 @@ public class Pet {



private static final Map<String, Integer> TIER_MAP = Map.of(
private static final Object2IntMap<String> TIER_MAP = Object2IntMaps.unmodifiable(new Object2IntOpenHashMap<>(Map.of(
"COMMON", 0, "UNCOMMON", 1, "RARE", 2, "EPIC", 3, "LEGENDARY", 4, "MYTHIC", 5
);
)));

private static final Int2ObjectMap<Formatting> RARITY_COLOR_MAP = Int2ObjectMaps.unmodifiable(new Int2ObjectOpenHashMap<>(Map.of(
0, Formatting.WHITE, // COMMON
Expand Down
Loading

0 comments on commit 11fc3c2

Please sign in to comment.