|
| 1 | +package at.hannibal2.skyhanni.features.event.hoppity |
| 2 | + |
| 3 | +import at.hannibal2.skyhanni.api.event.HandleEvent |
| 4 | +import at.hannibal2.skyhanni.events.NeuRepositoryReloadEvent |
| 5 | +import at.hannibal2.skyhanni.events.RepositoryReloadEvent |
| 6 | +import at.hannibal2.skyhanni.events.item.ItemHoverEvent |
| 7 | +import at.hannibal2.skyhanni.features.inventory.chocolatefactory.ChocolateFactoryAPI |
| 8 | +import at.hannibal2.skyhanni.features.misc.ContributorManager |
| 9 | +import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule |
| 10 | +import at.hannibal2.skyhanni.utils.CircularList |
| 11 | +import at.hannibal2.skyhanni.utils.ItemUtils.getLore |
| 12 | +import at.hannibal2.skyhanni.utils.ItemUtils.name |
| 13 | +import at.hannibal2.skyhanni.utils.LorenzUtils |
| 14 | +import at.hannibal2.skyhanni.utils.StringUtils.allLettersFirstUppercase |
| 15 | +import at.hannibal2.skyhanni.utils.StringUtils.removeColor |
| 16 | +import net.minecraftforge.fml.common.eventhandler.EventPriority |
| 17 | +import net.minecraftforge.fml.common.eventhandler.SubscribeEvent |
| 18 | + |
| 19 | +@SkyHanniModule |
| 20 | +object ReplaceHoppityWithContributor { |
| 21 | + |
| 22 | + private val config get() = ChocolateFactoryAPI.config |
| 23 | + |
| 24 | + private val replaceMap = mutableMapOf<String, String>() |
| 25 | + |
| 26 | + @HandleEvent(priority = 5) |
| 27 | + fun onNeuRepoReload(event: NeuRepositoryReloadEvent) { |
| 28 | + update() |
| 29 | + } |
| 30 | + |
| 31 | + @SubscribeEvent(priority = EventPriority.LOW) |
| 32 | + fun onRepoReload(event: RepositoryReloadEvent) { |
| 33 | + update() |
| 34 | + } |
| 35 | + |
| 36 | + fun update() { |
| 37 | + replaceMap.clear() |
| 38 | + |
| 39 | + val contributors = ContributorManager.contributorNames |
| 40 | + val rabbits = HoppityCollectionData.rabbitRarities |
| 41 | + |
| 42 | + if (contributors.isEmpty()) return |
| 43 | + if (rabbits.isEmpty()) return |
| 44 | + |
| 45 | + val newNames = CircularList(contributors.toList()) |
| 46 | + for (internalName in rabbits.map { it.key }.shuffled()) { |
| 47 | + val realName = internalName.allLettersFirstUppercase() |
| 48 | + val newName = newNames.next() |
| 49 | + replaceMap[realName] = newName |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + @HandleEvent(priority = HandleEvent.LOWEST) |
| 54 | + fun onTooltip(event: ItemHoverEvent) { |
| 55 | + if (!isEnabled()) return |
| 56 | + if (!HoppityCollectionStats.inInventory) return |
| 57 | + |
| 58 | + val itemStack = event.itemStack |
| 59 | + val lore = itemStack.getLore() |
| 60 | + val last = lore.lastOrNull() ?: return |
| 61 | + if (!last.endsWith(" RABBIT")) return |
| 62 | + |
| 63 | + val realName = itemStack.name |
| 64 | + val cleanName = realName.removeColor() |
| 65 | + val fakeName = replaceMap[cleanName] ?: return |
| 66 | + |
| 67 | + val newName = event.toolTip[0].replace(cleanName, fakeName) |
| 68 | + event.toolTip[0] = newName |
| 69 | + |
| 70 | + event.toolTip.add(" ") |
| 71 | + event.toolTip.add("§8§oSome might say this rabbit is also known as $realName") |
| 72 | + |
| 73 | + // TODO find a way to handle non containing entries in a kotlin nullable way instead of checking for -1 |
| 74 | + val index = event.toolTip.indexOfFirst { it.contains(" a duplicate") } |
| 75 | + if (index == -1) return |
| 76 | + val oldLine = event.toolTip[index] |
| 77 | + val newLine = oldLine.replace(cleanName, fakeName) |
| 78 | + event.toolTip[index] = newLine |
| 79 | + } |
| 80 | + |
| 81 | + fun isEnabled() = LorenzUtils.inSkyBlock && config.contributorRabbitName |
| 82 | +} |
0 commit comments