diff --git a/src/main/java/com/minelittlepony/unicopia/entity/player/ExperienceGroup.java b/src/main/java/com/minelittlepony/unicopia/entity/player/ExperienceGroup.java index c18d70f7..463276cf 100644 --- a/src/main/java/com/minelittlepony/unicopia/entity/player/ExperienceGroup.java +++ b/src/main/java/com/minelittlepony/unicopia/entity/player/ExperienceGroup.java @@ -1,43 +1,51 @@ package com.minelittlepony.unicopia.entity.player; +import java.lang.ref.WeakReference; + +import it.unimi.dsi.fastutil.ints.Int2ObjectArrayMap; import net.minecraft.text.Text; import net.minecraft.util.math.MathHelper; public interface ExperienceGroup { - LinearSelector EXPERIENCES = new LinearSelector<>(new String[] { - "MAGICAL_KINDERGARTENER", - "FRIENDSHIP_STUDENT", - "SENIOR_FRIENDSHIP_STUDENT", - "JUNIOR_MAGE", - "MAGE", - "ARCHMAGE", - "ARCHMAGUS", - "SENIOR_ARCHMAGUS", - "ASCENDED_SENIOR_ARCHMAGUS", - "DEMI_GOD", - "ARCH_DEMI_GOD", - "ALICORN_PRINCESS", - "POLYCORN_PRINCESS", - "FAUSTIAN_LEGEND" - }, 2); - LinearSelector CORRUPTIONS = new LinearSelector<>(new String[] { - "PURE", - "IMPURE", - "TAINTED", - "TWISTED", - "CORRUPT", - "MONSTROUS" - }, 1F/8F); + String[] EXPERIENCES = { + "magical_kindergartner", + "friendship_student", + "senior_friendship_student", + "junior_mage", + "mage", + "archmage", + "archmagus", + "senior_archmagus", + "ascended_senior_archmagus", + "demi_god", + "arch_demi_god", + "alicorn_princess", + "polycorn_princess", + "faustian_legend" + }; + String[] CORRUPTIONS = { + "pure", + "impure", + "tainted", + "twisted", + "corrupt", + "monstrous" + }; + Int2ObjectArrayMap> CACHE = new Int2ObjectArrayMap<>(); static Text forLevel(float level, float corruption) { - return Text.translatable( - "experience.unicopia." + CORRUPTIONS.get(corruption).toLowerCase() + "." + EXPERIENCES.get(level).toLowerCase() - ); + int c = get(CORRUPTIONS, corruption); + int x = get(EXPERIENCES, level); + int i = x * c; + + Text value; + if (!CACHE.containsKey(i) || (value = CACHE.get(i).get()) == null) { + CACHE.put(i, new WeakReference<>(value = Text.translatable("experience.unicopia." + CORRUPTIONS[c] + "." + EXPERIENCES[x]))); + } + return value; } - public record LinearSelector (T[] values, float ratio) { - public T get(float level) { - return values()[MathHelper.clamp(MathHelper.floor(level * values().length), 0, values().length - 1)]; - } + private static int get(String[] values, float level) { + return MathHelper.clamp(MathHelper.floor(level * values.length), 0, values.length - 1); } }