|
| 1 | +package moe.nea.firmament.features.items.recipes |
| 2 | + |
| 3 | +import java.util.Optional |
| 4 | +import net.minecraft.client.gui.navigation.ScreenRectangle |
| 5 | +import net.minecraft.client.gui.screens.Screen |
| 6 | +import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen |
| 7 | +import net.minecraft.world.item.ItemStack |
| 8 | +import net.minecraft.world.item.Items |
| 9 | +import moe.nea.firmament.annotations.Subscribe |
| 10 | +import moe.nea.firmament.api.v1.FirmamentAPI |
| 11 | +import moe.nea.firmament.events.HandledScreenForegroundEvent |
| 12 | +import moe.nea.firmament.events.ReloadRegistrationEvent |
| 13 | +import moe.nea.firmament.repo.RepoManager |
| 14 | +import moe.nea.firmament.repo.SBItemStack |
| 15 | +import moe.nea.firmament.util.MC |
| 16 | +import moe.nea.firmament.util.accessors.castAccessor |
| 17 | +import moe.nea.firmament.util.skyblockId |
| 18 | + |
| 19 | +object ItemList { |
| 20 | + // TODO: add a global toggle for this and RecipeRegistry |
| 21 | + |
| 22 | + fun collectExclusions(screen: Screen): Set<ScreenRectangle> { |
| 23 | + val exclusions = mutableSetOf<ScreenRectangle>() |
| 24 | + if (screen is AbstractContainerScreen<*>) { |
| 25 | + val screenHandler = screen.castAccessor() |
| 26 | + exclusions.add( |
| 27 | + ScreenRectangle( |
| 28 | + screenHandler.x_Firmament, |
| 29 | + screenHandler.y_Firmament, |
| 30 | + screenHandler.backgroundWidth_Firmament, |
| 31 | + screenHandler.backgroundHeight_Firmament |
| 32 | + ) |
| 33 | + ) |
| 34 | + } |
| 35 | + FirmamentAPI.getInstance().extensions |
| 36 | + .forEach { extension -> |
| 37 | + for (rectangle in extension.getExclusionZones(screen)) { |
| 38 | + if (exclusions.any { it.encompasses(rectangle) }) |
| 39 | + continue |
| 40 | + exclusions.add(rectangle) |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + return exclusions |
| 45 | + } |
| 46 | + |
| 47 | + var reachableItems = listOf<SBItemStack>() |
| 48 | + var pageOffset = 0 |
| 49 | + fun recalculateVisibleItems() { |
| 50 | + reachableItems = RepoManager.neuRepo.items |
| 51 | + .items.values.map { SBItemStack(it.skyblockId) } |
| 52 | + } |
| 53 | + |
| 54 | + @Subscribe |
| 55 | + fun onReload(event: ReloadRegistrationEvent) { |
| 56 | + event.repo.registerReloadListener { recalculateVisibleItems() } |
| 57 | + } |
| 58 | + |
| 59 | + fun coordinates(outer: ScreenRectangle, exclusions: Collection<ScreenRectangle>): Sequence<ScreenRectangle> { |
| 60 | + val entryWidth = 18 |
| 61 | + val columns = outer.width / entryWidth |
| 62 | + val rows = outer.height / entryWidth |
| 63 | + val lowX = outer.right() - columns * entryWidth |
| 64 | + val lowY = outer.top() |
| 65 | + return generateSequence(0) { it + 1 } |
| 66 | + .map { |
| 67 | + val xIndex = it % columns |
| 68 | + val yIndex = it / columns |
| 69 | + ScreenRectangle( |
| 70 | + lowX + xIndex * entryWidth, lowY + yIndex * entryWidth, |
| 71 | + entryWidth, entryWidth |
| 72 | + ) |
| 73 | + } |
| 74 | + .take(rows * columns) |
| 75 | + .filter { candidate -> exclusions.none { it.intersects(candidate) } } |
| 76 | + } |
| 77 | + |
| 78 | + var lastRenderPositions: List<Pair<ScreenRectangle, SBItemStack>> = listOf() |
| 79 | + var lastHoveredItemStack: Pair<ScreenRectangle, SBItemStack>? = null |
| 80 | + |
| 81 | + fun findStackUnder(mouseX: Int, mouseY: Int): Pair<ScreenRectangle, SBItemStack>? { |
| 82 | + val lhis = lastHoveredItemStack |
| 83 | + if (lhis != null && lhis.first.containsPoint(mouseX, mouseY)) |
| 84 | + return lhis |
| 85 | + return lastRenderPositions.firstOrNull { it.first.containsPoint(mouseX, mouseY) } |
| 86 | + } |
| 87 | + |
| 88 | + @Subscribe |
| 89 | + fun onRender(event: HandledScreenForegroundEvent) { |
| 90 | + lastHoveredItemStack = null |
| 91 | + lastRenderPositions = listOf() |
| 92 | + val exclusions = collectExclusions(event.screen) |
| 93 | + val potentiallyVisible = reachableItems.subList(pageOffset, reachableItems.size) |
| 94 | + val screenWidth = event.screen.width |
| 95 | + val rightThird = ScreenRectangle( |
| 96 | + screenWidth - screenWidth / 3, 0, |
| 97 | + screenWidth / 3, event.screen.height |
| 98 | + ) |
| 99 | + val coords = coordinates(rightThird, exclusions) |
| 100 | + |
| 101 | + lastRenderPositions = coords.zip(potentiallyVisible.asSequence()).toList() |
| 102 | + lastRenderPositions.forEach { (pos, stack) -> |
| 103 | + val realStack = stack.asLazyImmutableItemStack() |
| 104 | + val toRender = realStack ?: ItemStack(Items.PAINTING) |
| 105 | + event.context.renderItem(toRender, pos.left() + 1, pos.top() + 1) |
| 106 | + if (pos.containsPoint(event.mouseX, event.mouseY)) { |
| 107 | + lastHoveredItemStack = pos to stack |
| 108 | + event.context.setTooltipForNextFrame( |
| 109 | + MC.font, |
| 110 | + if (realStack != null) |
| 111 | + ItemSlotWidget.getTooltip(realStack) |
| 112 | + else |
| 113 | + stack.estimateLore(), |
| 114 | + Optional.empty(), |
| 115 | + event.mouseX, event.mouseY |
| 116 | + ) |
| 117 | + } |
| 118 | + } |
| 119 | + } |
| 120 | +} |
0 commit comments