diff options
Diffstat (limited to 'src/main/kotlin/features/inventory/SlotLocking.kt')
| -rw-r--r-- | src/main/kotlin/features/inventory/SlotLocking.kt | 260 |
1 files changed, 158 insertions, 102 deletions
diff --git a/src/main/kotlin/features/inventory/SlotLocking.kt b/src/main/kotlin/features/inventory/SlotLocking.kt index d3348a2..fca40c8 100644 --- a/src/main/kotlin/features/inventory/SlotLocking.kt +++ b/src/main/kotlin/features/inventory/SlotLocking.kt @@ -4,7 +4,6 @@ package moe.nea.firmament.features.inventory import java.util.UUID import org.lwjgl.glfw.GLFW -import util.render.CustomRenderLayers import kotlinx.serialization.KSerializer import kotlinx.serialization.Serializable import kotlinx.serialization.UseSerializers @@ -17,55 +16,76 @@ import kotlinx.serialization.json.JsonObject import kotlinx.serialization.json.JsonPrimitive import kotlinx.serialization.json.int import kotlinx.serialization.serializer -import net.minecraft.client.gui.screen.ingame.HandledScreen -import net.minecraft.client.render.RenderLayer -import net.minecraft.client.render.RenderLayers -import net.minecraft.client.render.TexturedRenderLayers -import net.minecraft.entity.player.PlayerInventory -import net.minecraft.screen.GenericContainerScreenHandler -import net.minecraft.screen.slot.Slot -import net.minecraft.screen.slot.SlotActionType -import net.minecraft.util.Identifier -import net.minecraft.util.StringIdentifiable +import net.minecraft.client.renderer.RenderPipelines +import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen +import net.minecraft.client.gui.screens.inventory.InventoryScreen +import net.minecraft.world.entity.player.Inventory +import net.minecraft.world.item.ItemStack +import net.minecraft.world.inventory.ChestMenu +import net.minecraft.world.inventory.InventoryMenu +import net.minecraft.world.inventory.Slot +import net.minecraft.world.inventory.ClickType +import net.minecraft.resources.ResourceLocation +import net.minecraft.util.StringRepresentable import moe.nea.firmament.annotations.Subscribe -import moe.nea.firmament.events.FeaturesInitializedEvent +import moe.nea.firmament.events.ClientInitEvent import moe.nea.firmament.events.HandledScreenForegroundEvent import moe.nea.firmament.events.HandledScreenKeyPressedEvent import moe.nea.firmament.events.HandledScreenKeyReleasedEvent import moe.nea.firmament.events.IsSlotProtectedEvent import moe.nea.firmament.events.ScreenChangeEvent import moe.nea.firmament.events.SlotRenderEvents -import moe.nea.firmament.features.FirmamentFeature -import moe.nea.firmament.gui.config.ManagedConfig +import moe.nea.firmament.keybindings.InputModifiers import moe.nea.firmament.keybindings.SavedKeyBinding import moe.nea.firmament.mixins.accessor.AccessorHandledScreen import moe.nea.firmament.util.CommonSoundEffects import moe.nea.firmament.util.MC import moe.nea.firmament.util.SBData import moe.nea.firmament.util.SkyBlockIsland +import moe.nea.firmament.util.accessors.castAccessor +import moe.nea.firmament.util.data.Config +import moe.nea.firmament.util.data.ManagedConfig import moe.nea.firmament.util.data.ProfileSpecificDataHolder +import moe.nea.firmament.util.extraAttributes import moe.nea.firmament.util.json.DashlessUUIDSerializer +import moe.nea.firmament.util.lime import moe.nea.firmament.util.mc.ScreenUtil.getSlotByIndex import moe.nea.firmament.util.mc.SlotUtils.swapWithHotBar import moe.nea.firmament.util.mc.displayNameAccordingToNbt import moe.nea.firmament.util.mc.loreAccordingToNbt +import moe.nea.firmament.util.red import moe.nea.firmament.util.render.drawLine +import moe.nea.firmament.util.skyBlockId import moe.nea.firmament.util.skyblock.DungeonUtil +import moe.nea.firmament.util.skyblock.SkyBlockItems import moe.nea.firmament.util.skyblockUUID +import moe.nea.firmament.util.tr import moe.nea.firmament.util.unformattedString -object SlotLocking : FirmamentFeature { - override val identifier: String +object SlotLocking { + val identifier: String get() = "slot-locking" @Serializable - data class Data( + data class DimensionData( val lockedSlots: MutableSet<Int> = mutableSetOf(), - val lockedSlotsRift: MutableSet<Int> = mutableSetOf(), + val boundSlots: BoundSlots = BoundSlots(), + ) + + @Serializable + data class Data( val lockedUUIDs: MutableSet<UUID> = mutableSetOf(), - val boundSlots: BoundSlots = BoundSlots() + val rift: DimensionData = DimensionData(), + val overworld: DimensionData = DimensionData(), ) + + val currentWorldData + get() = if (SBData.skyblockLocation == SkyBlockIsland.RIFT) + DConfig.data.rift + else + DConfig.data.overworld + @Serializable data class BoundSlot( val hotbar: Int, @@ -123,61 +143,58 @@ object SlotLocking : FirmamentFeature { } + @Config object TConfig : ManagedConfig(identifier, Category.INVENTORY) { val lockSlot by keyBinding("lock") { GLFW.GLFW_KEY_L } val lockUUID by keyBindingWithOutDefaultModifiers("lock-uuid") { - SavedKeyBinding(GLFW.GLFW_KEY_L, shift = true) + SavedKeyBinding.keyWithMods(GLFW.GLFW_KEY_L, InputModifiers.of(shift = true)) } val slotBind by keyBinding("bind") { GLFW.GLFW_KEY_L } val slotBindRequireShift by toggle("require-quick-move") { true } val slotRenderLines by choice("bind-render") { SlotRenderLinesMode.ONLY_BOXES } + val slotBindOnlyInInv by toggle("bind-only-in-inv") { false } val allowMultiBinding by toggle("multi-bind") { true } // TODO: filter based on this option + val protectAllHuntingBoxes by toggle("hunting-box") { false } val allowDroppingInDungeons by toggle("drop-in-dungeons") { true } } - enum class SlotRenderLinesMode : StringIdentifiable { + enum class SlotRenderLinesMode : StringRepresentable { EVERYTHING, ONLY_BOXES, NOTHING; - override fun asString(): String { + override fun getSerializedName(): String { return name } } - override val config: TConfig - get() = TConfig - + @Config object DConfig : ProfileSpecificDataHolder<Data>(serializer(), "locked-slots", ::Data) val lockedUUIDs get() = DConfig.data?.lockedUUIDs val lockedSlots - get() = when (SBData.skyblockLocation) { - SkyBlockIsland.RIFT -> DConfig.data?.lockedSlotsRift - null -> null - else -> DConfig.data?.lockedSlots - } + get() = currentWorldData?.lockedSlots - fun isSalvageScreen(screen: HandledScreen<*>?): Boolean { + fun isSalvageScreen(screen: AbstractContainerScreen<*>?): Boolean { if (screen == null) return false return screen.title.unformattedString.contains("Salvage Item") } - fun isTradeScreen(screen: HandledScreen<*>?): Boolean { + fun isTradeScreen(screen: AbstractContainerScreen<*>?): Boolean { if (screen == null) return false - val handler = screen.screenHandler as? GenericContainerScreenHandler ?: return false - if (handler.inventory.size() < 9) return false - val middlePane = handler.inventory.getStack(handler.inventory.size() - 5) + val handler = screen.menu as? ChestMenu ?: return false + if (handler.container.containerSize < 9) return false + val middlePane = handler.container.getItem(handler.container.containerSize - 5) if (middlePane == null) return false return middlePane.displayNameAccordingToNbt?.unformattedString == "⇦ Your stuff" } - fun isNpcShop(screen: HandledScreen<*>?): Boolean { + fun isNpcShop(screen: AbstractContainerScreen<*>?): Boolean { if (screen == null) return false - val handler = screen.screenHandler as? GenericContainerScreenHandler ?: return false - if (handler.inventory.size() < 9) return false - val sellItem = handler.inventory.getStack(handler.inventory.size() - 5) + val handler = screen.menu as? ChestMenu ?: return false + if (handler.container.containerSize < 9) return false + val sellItem = handler.container.getItem(handler.container.containerSize - 5) if (sellItem == null) return false if (sellItem.displayNameAccordingToNbt.unformattedString == "Sell Item") return true val lore = sellItem.loreAccordingToNbt @@ -187,16 +204,18 @@ object SlotLocking : FirmamentFeature { @Subscribe fun onSalvageProtect(event: IsSlotProtectedEvent) { if (event.slot == null) return - if (!event.slot.hasStack()) return - if (event.slot.stack.displayNameAccordingToNbt.unformattedString != "Salvage Items") return - val inv = event.slot.inventory + if (!event.slot.hasItem()) return + if (event.slot.item.displayNameAccordingToNbt.unformattedString != "Salvage Items") return + val inv = event.slot.container var anyBlocked = false - for (i in 0 until event.slot.index) { - val stack = inv.getStack(i) - if (IsSlotProtectedEvent.shouldBlockInteraction(null, - SlotActionType.THROW, - IsSlotProtectedEvent.MoveOrigin.SALVAGE, - stack) + for (i in 0 until event.slot.containerSlot) { + val stack = inv.getItem(i) + if (IsSlotProtectedEvent.shouldBlockInteraction( + null, + ClickType.THROW, + IsSlotProtectedEvent.MoveOrigin.SALVAGE, + stack + ) ) anyBlocked = true } @@ -207,33 +226,41 @@ object SlotLocking : FirmamentFeature { @Subscribe fun onProtectUuidItems(event: IsSlotProtectedEvent) { - val doesNotDeleteItem = event.actionType == SlotActionType.SWAP - || event.actionType == SlotActionType.PICKUP - || event.actionType == SlotActionType.QUICK_MOVE - || event.actionType == SlotActionType.QUICK_CRAFT - || event.actionType == SlotActionType.CLONE - || event.actionType == SlotActionType.PICKUP_ALL + val doesNotDeleteItem = event.actionType == ClickType.SWAP + || event.actionType == ClickType.PICKUP + || event.actionType == ClickType.QUICK_MOVE + || event.actionType == ClickType.QUICK_CRAFT + || event.actionType == ClickType.CLONE + || event.actionType == ClickType.PICKUP_ALL val isSellOrTradeScreen = isNpcShop(MC.handledScreen) || isTradeScreen(MC.handledScreen) || isSalvageScreen(MC.handledScreen) - if ((!isSellOrTradeScreen || event.slot?.inventory !is PlayerInventory) + if ((!isSellOrTradeScreen || event.slot?.container !is Inventory) && doesNotDeleteItem ) return val stack = event.itemStack ?: return + if (TConfig.protectAllHuntingBoxes && (stack.isHuntingBox())) { + event.protect() + return + } val uuid = stack.skyblockUUID ?: return if (uuid in (lockedUUIDs ?: return)) { event.protect() } } + fun ItemStack.isHuntingBox(): Boolean { + return skyBlockId == SkyBlockItems.HUNTING_TOOLKIT || extraAttributes.get("tool_kit") != null + } + @Subscribe fun onProtectSlot(it: IsSlotProtectedEvent) { - if (it.slot != null && it.slot.inventory is PlayerInventory && it.slot.index in (lockedSlots ?: setOf())) { + if (it.slot != null && it.slot.container is Inventory && it.slot.containerSlot in (lockedSlots ?: setOf())) { it.protect() } } @Subscribe - fun onEvent(event: FeaturesInitializedEvent) { + fun onEvent(event: ClientInitEvent) { IsSlotProtectedEvent.subscribe(receivesCancelled = true, "SlotLocking:unlockInDungeons") { if (it.isProtected && it.origin == IsSlotProtectedEvent.MoveOrigin.DROP_FROM_HOTBAR @@ -247,14 +274,16 @@ object SlotLocking : FirmamentFeature { @Subscribe fun onQuickMoveBoundSlot(it: IsSlotProtectedEvent) { - val boundSlots = DConfig.data?.boundSlots ?: BoundSlots() + val boundSlots = currentWorldData?.boundSlots ?: BoundSlots() val isValidAction = - it.actionType == SlotActionType.QUICK_MOVE || (it.actionType == SlotActionType.PICKUP && !TConfig.slotBindRequireShift) + it.actionType == ClickType.QUICK_MOVE || (it.actionType == ClickType.PICKUP && !TConfig.slotBindRequireShift) if (!isValidAction) return - val handler = MC.handledScreen?.screenHandler ?: return + val handler = MC.handledScreen?.menu ?: return + if (TConfig.slotBindOnlyInInv && handler !is InventoryMenu) + return val slot = it.slot - if (slot != null && it.slot.inventory is PlayerInventory) { - val matchingSlots = boundSlots.findMatchingSlots(slot.index) + if (slot != null && it.slot.container is Inventory) { + val matchingSlots = boundSlots.findMatchingSlots(slot.containerSlot) if (matchingSlots.isEmpty()) return it.protectSilent() val boundSlot = matchingSlots.singleOrNull() ?: return @@ -267,10 +296,25 @@ object SlotLocking : FirmamentFeature { fun onLockUUID(it: HandledScreenKeyPressedEvent) { if (!it.matches(TConfig.lockUUID)) return val inventory = MC.handledScreen ?: return - inventory as AccessorHandledScreen + inventory.castAccessor() val slot = inventory.focusedSlot_Firmament ?: return - val stack = slot.stack ?: return + val stack = slot.item ?: return + if (stack.isHuntingBox()) { + MC.sendChat( + tr( + "firmament.slot-locking.hunting-box-unbindable-hint", + "The hunting box cannot be UUID bound reliably. It changes its own UUID frequently when switching tools. " + ).red().append( + tr( + "firmament.slot-locking.hunting-box-unbindable-hint.solution", + "Use the Firmament config option for locking all hunting boxes instead." + ).lime() + ) + ) + CommonSoundEffects.playFailure() + return + } val uuid = stack.skyblockUUID ?: return val lockedUUIDs = lockedUUIDs ?: return if (uuid in lockedUUIDs) { @@ -287,7 +331,7 @@ object SlotLocking : FirmamentFeature { @Subscribe fun onLockSlotKeyRelease(it: HandledScreenKeyReleasedEvent) { val inventory = MC.handledScreen ?: return - inventory as AccessorHandledScreen + inventory.castAccessor() val slot = inventory.focusedSlot_Firmament val storedSlot = storedLockingSlot ?: return @@ -295,11 +339,11 @@ object SlotLocking : FirmamentFeature { storedLockingSlot = null val hotBarSlot = if (slot.isHotbar()) slot else storedSlot val invSlot = if (slot.isHotbar()) storedSlot else slot - val boundSlots = DConfig.data?.boundSlots ?: return - lockedSlots?.remove(hotBarSlot.index) - lockedSlots?.remove(invSlot.index) - boundSlots.removeDuplicateForInventory(invSlot.index) - boundSlots.insert(hotBarSlot.index, invSlot.index) + val boundSlots = currentWorldData?.boundSlots ?: return + lockedSlots?.remove(hotBarSlot.containerSlot) + lockedSlots?.remove(invSlot.containerSlot) + boundSlots.removeDuplicateForInventory(invSlot.containerSlot) + boundSlots.insert(hotBarSlot.containerSlot, invSlot.containerSlot) DConfig.markDirty() CommonSoundEffects.playSuccess() return @@ -311,17 +355,17 @@ object SlotLocking : FirmamentFeature { } if (it.matches(TConfig.slotBind)) { storedLockingSlot = null - val boundSlots = DConfig.data?.boundSlots ?: return + val boundSlots = currentWorldData?.boundSlots ?: return if (slot != null) - boundSlots.removeAllInvolving(slot.index) + boundSlots.removeAllInvolving(slot.containerSlot) } } @Subscribe fun onRenderAllBoundSlots(event: HandledScreenForegroundEvent) { - val boundSlots = DConfig.data?.boundSlots ?: return + val boundSlots = currentWorldData?.boundSlots ?: return fun findByIndex(index: Int) = event.screen.getSlotByIndex(index, true) - val accScreen = event.screen as AccessorHandledScreen + val accScreen = event.screen.castAccessor() val sx = accScreen.x_Firmament val sy = accScreen.y_Firmament val highlitSlots = mutableSetOf<Slot>() @@ -350,26 +394,36 @@ object SlotLocking : FirmamentFeature { hotX + sx, hotY + sy, color(anyHovered) ) - event.context.drawBorder(hotbarSlot.x + sx, - hotbarSlot.y + sy, - 16, 16, color(hotbarSlot in highlitSlots).color) - event.context.drawBorder(inventorySlot.x + sx, - inventorySlot.y + sy, - 16, 16, color(inventorySlot in highlitSlots).color) + event.context.submitOutline( + hotbarSlot.x + sx, + hotbarSlot.y + sy, + 16, 16, color(hotbarSlot in highlitSlots).color + ) + event.context.submitOutline( // TODO: 1.21.10 + inventorySlot.x + sx, + inventorySlot.y + sy, + 16, 16, color(inventorySlot in highlitSlots).color + ) } } @Subscribe fun onRenderCurrentDraggingSlot(event: HandledScreenForegroundEvent) { val draggingSlot = storedLockingSlot ?: return - val accScreen = event.screen as AccessorHandledScreen + val accScreen = event.screen.castAccessor() val hoveredSlot = accScreen.focusedSlot_Firmament - ?.takeIf { it.inventory is PlayerInventory } + ?.takeIf { it.container is Inventory } ?.takeIf { it == draggingSlot || it.isHotbar() != draggingSlot.isHotbar() } val sx = accScreen.x_Firmament val sy = accScreen.y_Firmament val (borderX, borderY) = draggingSlot.lineCenter() - event.context.drawBorder(draggingSlot.x + sx, draggingSlot.y + sy, 16, 16, 0xFF00FF00u.toInt()) + event.context.submitOutline( + draggingSlot.x + sx, + draggingSlot.y + sy, + 16, + 16, + 0xFF00FF00u.toInt() + ) // TODO: 1.21.10 if (hoveredSlot == null) { event.context.drawLine( borderX + sx, borderY + sy, @@ -383,9 +437,11 @@ object SlotLocking : FirmamentFeature { hovX + sx, hovY + sy, me.shedaniel.math.Color.ofOpaque(0x00FF00) ) - event.context.drawBorder(hoveredSlot.x + sx, - hoveredSlot.y + sy, - 16, 16, 0xFF00FF00u.toInt()) + event.context.submitOutline( + hoveredSlot.x + sx, + hoveredSlot.y + sy, + 16, 16, 0xFF00FF00u.toInt() + ) } } @@ -393,13 +449,13 @@ object SlotLocking : FirmamentFeature { return if (isHotbar()) { x + 9 to y } else { - x + 9 to y + 17 + x + 9 to y + 16 } } fun Slot.isHotbar(): Boolean { - return index < 9 + return containerSlot < 9 } @Subscribe @@ -411,14 +467,14 @@ object SlotLocking : FirmamentFeature { fun toggleSlotLock(slot: Slot) { val lockedSlots = lockedSlots ?: return - val boundSlots = DConfig.data?.boundSlots ?: BoundSlots() - if (slot.inventory is PlayerInventory) { - if (boundSlots.removeAllInvolving(slot.index)) { + val boundSlots = currentWorldData?.boundSlots ?: BoundSlots() + if (slot.container is Inventory) { + if (boundSlots.removeAllInvolving(slot.containerSlot)) { // intentionally do nothing - } else if (slot.index in lockedSlots) { - lockedSlots.remove(slot.index) + } else if (slot.containerSlot in lockedSlots) { + lockedSlots.remove(slot.containerSlot) } else { - lockedSlots.add(slot.index) + lockedSlots.add(slot.containerSlot) } DConfig.markDirty() CommonSoundEffects.playSuccess() @@ -428,10 +484,10 @@ object SlotLocking : FirmamentFeature { @Subscribe fun onLockSlot(it: HandledScreenKeyPressedEvent) { val inventory = MC.handledScreen ?: return - inventory as AccessorHandledScreen + inventory.castAccessor() val slot = inventory.focusedSlot_Firmament ?: return - if (slot.inventory !is PlayerInventory) return + if (slot.container !is Inventory) return if (it.matches(TConfig.slotBind)) { storedLockingSlot = storedLockingSlot ?: slot return @@ -444,17 +500,17 @@ object SlotLocking : FirmamentFeature { @Subscribe fun onRenderSlotOverlay(it: SlotRenderEvents.After) { - val isSlotLocked = it.slot.inventory is PlayerInventory && it.slot.index in (lockedSlots ?: setOf()) - val isUUIDLocked = (it.slot.stack?.skyblockUUID) in (lockedUUIDs ?: setOf()) + val isSlotLocked = it.slot.container is Inventory && it.slot.containerSlot in (lockedSlots ?: setOf()) + val isUUIDLocked = (it.slot.item?.skyblockUUID) in (lockedUUIDs ?: setOf()) if (isSlotLocked || isUUIDLocked) { - it.context.drawGuiTexture( - RenderLayer::getGuiTexturedOverlay, + it.context.blitSprite( + RenderPipelines.GUI_TEXTURED, when { isSlotLocked -> - (Identifier.of("firmament:slot_locked")) + (ResourceLocation.parse("firmament:slot_locked")) isUUIDLocked -> - (Identifier.of("firmament:uuid_locked")) + (ResourceLocation.parse("firmament:uuid_locked")) else -> error("unreachable") |
