From 1dd603ab1cc42a602f1d82595dd15789ef63d2a3 Mon Sep 17 00:00:00 2001 From: nea Date: Thu, 18 May 2023 03:20:22 +0200 Subject: Add slot locking --- src/main/kotlin/moe/nea/firmament/Firmament.kt | 5 +- .../events/HandledScreenKeyPressedEvent.kt | 10 ++++ .../nea/firmament/events/IsSlotProtectedEvent.kt | 26 +++++++++ .../moe/nea/firmament/events/SlotRenderEvents.kt | 32 ++++++++++ .../moe/nea/firmament/features/FeatureManager.kt | 6 +- .../moe/nea/firmament/features/FirmamentFeature.kt | 18 ++++++ .../moe/nea/firmament/features/NEUFeature.kt | 18 ------ .../firmament/features/fishing/FishingWarning.kt | 4 +- .../firmament/features/inventory/SlotLocking.kt | 68 ++++++++++++++++++++++ .../moe/nea/firmament/features/world/FairySouls.kt | 4 +- .../firmament/keybindings/FirmamentKeyBindings.kt | 17 ++++++ .../moe/nea/firmament/util/CommonSoundEffects.kt | 28 +++++++++ src/main/kotlin/moe/nea/firmament/util/MC.kt | 5 ++ 13 files changed, 215 insertions(+), 26 deletions(-) create mode 100644 src/main/kotlin/moe/nea/firmament/events/HandledScreenKeyPressedEvent.kt create mode 100644 src/main/kotlin/moe/nea/firmament/events/IsSlotProtectedEvent.kt create mode 100644 src/main/kotlin/moe/nea/firmament/events/SlotRenderEvents.kt create mode 100644 src/main/kotlin/moe/nea/firmament/features/FirmamentFeature.kt delete mode 100644 src/main/kotlin/moe/nea/firmament/features/NEUFeature.kt create mode 100644 src/main/kotlin/moe/nea/firmament/features/inventory/SlotLocking.kt create mode 100644 src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt create mode 100644 src/main/kotlin/moe/nea/firmament/util/CommonSoundEffects.kt (limited to 'src/main/kotlin') diff --git a/src/main/kotlin/moe/nea/firmament/Firmament.kt b/src/main/kotlin/moe/nea/firmament/Firmament.kt index ec6cd3e..a375b2d 100644 --- a/src/main/kotlin/moe/nea/firmament/Firmament.kt +++ b/src/main/kotlin/moe/nea/firmament/Firmament.kt @@ -5,6 +5,7 @@ import io.ktor.client.* import io.ktor.client.plugins.* import io.ktor.client.plugins.contentnegotiation.* import io.ktor.serialization.kotlinx.json.* +import java.awt.Taskbar.Feature import java.nio.file.Files import java.nio.file.Path import net.fabricmc.api.ClientModInitializer @@ -70,7 +71,9 @@ object Firmament : ModInitializer, ClientModInitializer { } override fun onInitialize() { + } + override fun onInitializeClient() { dbusConnection.requestBusName("moe.nea.firmament") dbusConnection.exportObject(FirmamentDbusObject) IDataHolder.registerEvents() @@ -85,8 +88,6 @@ object Firmament : ModInitializer, ClientModInitializer { globalJob.cancel() } }) - } - override fun onInitializeClient() { } } diff --git a/src/main/kotlin/moe/nea/firmament/events/HandledScreenKeyPressedEvent.kt b/src/main/kotlin/moe/nea/firmament/events/HandledScreenKeyPressedEvent.kt new file mode 100644 index 0000000..1afbfe5 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/HandledScreenKeyPressedEvent.kt @@ -0,0 +1,10 @@ +package moe.nea.firmament.events + +import net.minecraft.client.option.KeyBinding + +data class HandledScreenKeyPressedEvent(val keyCode: Int, val scanCode: Int, val modifiers: Int) : FirmamentEvent.Cancellable() { + companion object : FirmamentEventBus() + fun matches(keyBinding: KeyBinding): Boolean { + return keyBinding.matchesKey(keyCode, scanCode) + } +} diff --git a/src/main/kotlin/moe/nea/firmament/events/IsSlotProtectedEvent.kt b/src/main/kotlin/moe/nea/firmament/events/IsSlotProtectedEvent.kt new file mode 100644 index 0000000..7ca6521 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/IsSlotProtectedEvent.kt @@ -0,0 +1,26 @@ +package moe.nea.firmament.events + +import net.minecraft.screen.slot.Slot +import net.minecraft.text.Text +import moe.nea.firmament.util.CommonSoundEffects +import moe.nea.firmament.util.MC + +data class IsSlotProtectedEvent( + val slot: Slot, var isProtected: Boolean = false +) : FirmamentEvent() { + companion object : FirmamentEventBus() { + @JvmStatic + fun shouldBlockInteraction(slot: Slot): Boolean { + return publish(IsSlotProtectedEvent(slot)).isProtected.also { + if (it) { + MC.player?.sendMessage(Text.translatable("firmament.protectitem").append(slot.stack.name)) + CommonSoundEffects.playFailure() + } + } + } + } + + fun protect() { + isProtected = true + } +} diff --git a/src/main/kotlin/moe/nea/firmament/events/SlotRenderEvents.kt b/src/main/kotlin/moe/nea/firmament/events/SlotRenderEvents.kt new file mode 100644 index 0000000..afcddf2 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/events/SlotRenderEvents.kt @@ -0,0 +1,32 @@ +package moe.nea.firmament.events + +import net.minecraft.client.util.math.MatrixStack +import net.minecraft.screen.slot.Slot + +interface SlotRenderEvents { + val matrices: MatrixStack + val slot: Slot + val mouseX: Int + val mouseY: Int + val delta: Float + + data class Before( + override val matrices: MatrixStack, override val slot: Slot, + override val mouseX: Int, + override val mouseY: Int, + override val delta: Float + ) : FirmamentEvent(), + SlotRenderEvents { + companion object : FirmamentEventBus() + } + + data class After( + override val matrices: MatrixStack, override val slot: Slot, + override val mouseX: Int, + override val mouseY: Int, + override val delta: Float + ) : FirmamentEvent(), + SlotRenderEvents { + companion object : FirmamentEventBus() + } +} diff --git a/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt b/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt index 68205f4..580d745 100644 --- a/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt +++ b/src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt @@ -4,6 +4,7 @@ import kotlinx.serialization.Serializable import kotlinx.serialization.serializer import moe.nea.firmament.Firmament import moe.nea.firmament.features.fishing.FishingWarning +import moe.nea.firmament.features.inventory.SlotLocking import moe.nea.firmament.features.world.FairySouls import moe.nea.firmament.util.data.DataHolder @@ -13,7 +14,7 @@ object FeatureManager : DataHolder(serializer(), "feature val enabledFeatures: MutableMap = mutableMapOf() ) - private val features = mutableMapOf() + private val features = mutableMapOf() private var hasAutoloaded = false @@ -26,11 +27,12 @@ object FeatureManager : DataHolder(serializer(), "feature if (hasAutoloaded) return loadFeature(FairySouls) loadFeature(FishingWarning) + loadFeature(SlotLocking) hasAutoloaded = true } } - fun loadFeature(feature: NEUFeature) { + fun loadFeature(feature: FirmamentFeature) { synchronized(features) { if (feature.identifier in features) { Firmament.logger.error("Double registering feature ${feature.identifier}. Ignoring second instance $feature") diff --git a/src/main/kotlin/moe/nea/firmament/features/FirmamentFeature.kt b/src/main/kotlin/moe/nea/firmament/features/FirmamentFeature.kt new file mode 100644 index 0000000..c9cb8f0 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/features/FirmamentFeature.kt @@ -0,0 +1,18 @@ +package moe.nea.firmament.features + +import moe.nea.firmament.util.config.ManagedConfig + +interface FirmamentFeature { + val name: String + val identifier: String + val defaultEnabled: Boolean + get() = true + var isEnabled: Boolean + get() = FeatureManager.isEnabled(identifier) ?: defaultEnabled + set(value) { + FeatureManager.setEnabled(identifier, value) + } + val config: ManagedConfig? get() = null + fun onLoad() + +} diff --git a/src/main/kotlin/moe/nea/firmament/features/NEUFeature.kt b/src/main/kotlin/moe/nea/firmament/features/NEUFeature.kt deleted file mode 100644 index f231003..0000000 --- a/src/main/kotlin/moe/nea/firmament/features/NEUFeature.kt +++ /dev/null @@ -1,18 +0,0 @@ -package moe.nea.firmament.features - -import moe.nea.firmament.util.config.ManagedConfig - -interface NEUFeature { - val name: String - val identifier: String - val defaultEnabled: Boolean - get() = true - var isEnabled: Boolean - get() = FeatureManager.isEnabled(identifier) ?: defaultEnabled - set(value) { - FeatureManager.setEnabled(identifier, value) - } - val config: ManagedConfig? get() = null - fun onLoad() - -} diff --git a/src/main/kotlin/moe/nea/firmament/features/fishing/FishingWarning.kt b/src/main/kotlin/moe/nea/firmament/features/fishing/FishingWarning.kt index cdeb24c..ab8ebf4 100644 --- a/src/main/kotlin/moe/nea/firmament/features/fishing/FishingWarning.kt +++ b/src/main/kotlin/moe/nea/firmament/features/fishing/FishingWarning.kt @@ -13,13 +13,13 @@ import net.minecraft.util.math.Vec3d import moe.nea.firmament.events.ParticleSpawnEvent import moe.nea.firmament.events.WorldReadyEvent import moe.nea.firmament.events.WorldRenderLastEvent -import moe.nea.firmament.features.NEUFeature +import moe.nea.firmament.features.FirmamentFeature import moe.nea.firmament.util.MC import moe.nea.firmament.util.TimeMark import moe.nea.firmament.util.config.ManagedConfig import moe.nea.firmament.util.render.RenderBlockContext.Companion.renderBlocks -object FishingWarning : NEUFeature { +object FishingWarning : FirmamentFeature { override val name: String get() = "Fishing Warning" override val identifier: String diff --git a/src/main/kotlin/moe/nea/firmament/features/inventory/SlotLocking.kt b/src/main/kotlin/moe/nea/firmament/features/inventory/SlotLocking.kt new file mode 100644 index 0000000..9f75541 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/features/inventory/SlotLocking.kt @@ -0,0 +1,68 @@ +package moe.nea.firmament.features.inventory + +import kotlinx.serialization.Serializable +import kotlinx.serialization.serializer +import net.minecraft.client.gui.DrawableHelper +import net.minecraft.entity.player.PlayerInventory +import moe.nea.firmament.events.HandledScreenKeyPressedEvent +import moe.nea.firmament.events.IsSlotProtectedEvent +import moe.nea.firmament.events.SlotRenderEvents +import moe.nea.firmament.features.FirmamentFeature +import moe.nea.firmament.keybindings.FirmamentKeyBindings +import moe.nea.firmament.mixins.accessor.AccessorHandledScreen +import moe.nea.firmament.util.CommonSoundEffects +import moe.nea.firmament.util.MC +import moe.nea.firmament.util.data.ProfileSpecificDataHolder + +object SlotLocking : FirmamentFeature { + override val name: String + get() = "Slot Locking" + override val identifier: String + get() = "slot-locking" + + @Serializable + data class Data( + val lockedSlots: MutableSet = mutableSetOf(), + ) + + object DConfig : ProfileSpecificDataHolder(serializer(), "locked-slots", ::Data) + + val keyBinding by FirmamentKeyBindings::SLOT_LOCKING + val lockedSlots get() = DConfig.data?.lockedSlots + override fun onLoad() { + HandledScreenKeyPressedEvent.subscribe { + if (!it.matches(keyBinding)) return@subscribe + val inventory = MC.handledScreen ?: return@subscribe + inventory as AccessorHandledScreen + + val slot = inventory.focusedSlot_NEU ?: return@subscribe + val lockedSlots = lockedSlots ?: return@subscribe + if (slot.inventory is PlayerInventory) { + if (slot.index in lockedSlots) { + lockedSlots.remove(slot.index) + } else { + lockedSlots.add(slot.index) + } + DConfig.markDirty() + CommonSoundEffects.playSuccess() + } + } + IsSlotProtectedEvent.subscribe { + if (it.slot.inventory is PlayerInventory && it.slot.index in (lockedSlots ?: setOf())) { + it.protect() + } + } + SlotRenderEvents.Before.subscribe { + if (it.slot.inventory is PlayerInventory && it.slot.index in (lockedSlots ?: setOf())) { + DrawableHelper.fill( + it.matrices, + it.slot.x, + it.slot.y, + it.slot.x + 16, + it.slot.y + 16, + 0xFFFF0000.toInt() + ) + } + } + } +} diff --git a/src/main/kotlin/moe/nea/firmament/features/world/FairySouls.kt b/src/main/kotlin/moe/nea/firmament/features/world/FairySouls.kt index f752963..ffde0b9 100644 --- a/src/main/kotlin/moe/nea/firmament/features/world/FairySouls.kt +++ b/src/main/kotlin/moe/nea/firmament/features/world/FairySouls.kt @@ -6,7 +6,7 @@ import kotlinx.serialization.serializer import moe.nea.firmament.events.ServerChatLineReceivedEvent import moe.nea.firmament.events.SkyblockServerUpdateEvent import moe.nea.firmament.events.WorldRenderLastEvent -import moe.nea.firmament.features.NEUFeature +import moe.nea.firmament.features.FirmamentFeature import moe.nea.firmament.repo.RepoManager import moe.nea.firmament.util.MC import moe.nea.firmament.util.SBData @@ -17,7 +17,7 @@ import moe.nea.firmament.util.render.RenderBlockContext.Companion.renderBlocks import moe.nea.firmament.util.unformattedString -object FairySouls : NEUFeature { +object FairySouls : FirmamentFeature { @Serializable diff --git a/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt b/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt new file mode 100644 index 0000000..0f945d5 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt @@ -0,0 +1,17 @@ +package moe.nea.firmament.keybindings + +import net.fabricmc.fabric.api.client.keybinding.v1.KeyBindingHelper +import org.lwjgl.glfw.GLFW +import net.minecraft.client.option.KeyBinding +import net.minecraft.client.util.InputUtil + +object FirmamentKeyBindings { + val SLOT_LOCKING = KeyBindingHelper.registerKeyBinding( + KeyBinding( + "firmament.key.slotlocking", + InputUtil.Type.KEYSYM, + GLFW.GLFW_KEY_L, + "firmament.key.category" + ) + ) +} diff --git a/src/main/kotlin/moe/nea/firmament/util/CommonSoundEffects.kt b/src/main/kotlin/moe/nea/firmament/util/CommonSoundEffects.kt new file mode 100644 index 0000000..819ea27 --- /dev/null +++ b/src/main/kotlin/moe/nea/firmament/util/CommonSoundEffects.kt @@ -0,0 +1,28 @@ +package moe.nea.firmament.util + +import net.minecraft.client.sound.AbstractSoundInstance +import net.minecraft.client.sound.PositionedSoundInstance +import net.minecraft.client.sound.SoundInstance +import net.minecraft.sound.SoundCategory +import net.minecraft.sound.SoundEvent +import net.minecraft.util.Identifier + +// TODO: Replace these with custom sound events that just re use the vanilla ogg s +object CommonSoundEffects { + fun playSound(identifier: Identifier) { + MC.soundManager.play(PositionedSoundInstance.master(SoundEvent.of(identifier), 1F)) + } + + fun playFailure() { + playSound(Identifier("minecraft", "block.anvil.place")) + } + + + fun playSuccess() { + playDing() + } + + fun playDing() { + playSound(Identifier("minecraft", "entity.arrow.hit_player")) + } +} diff --git a/src/main/kotlin/moe/nea/firmament/util/MC.kt b/src/main/kotlin/moe/nea/firmament/util/MC.kt index ab35f0e..8fc15b5 100644 --- a/src/main/kotlin/moe/nea/firmament/util/MC.kt +++ b/src/main/kotlin/moe/nea/firmament/util/MC.kt @@ -2,11 +2,16 @@ package moe.nea.firmament.util import io.github.moulberry.repo.data.Coordinate import net.minecraft.client.MinecraftClient +import net.minecraft.client.gui.screen.ingame.HandledScreen import net.minecraft.util.math.BlockPos +import moe.nea.firmament.mixins.accessor.AccessorHandledScreen object MC { + inline val soundManager get() = MinecraftClient.getInstance().soundManager inline val player get() = MinecraftClient.getInstance().player inline val world get() = MinecraftClient.getInstance().world + inline val screen get() = MinecraftClient.getInstance().currentScreen + inline val handledScreen: HandledScreen<*>? get() = MinecraftClient.getInstance().currentScreen as? HandledScreen<*> } val Coordinate.blockPos: BlockPos -- cgit