aboutsummaryrefslogtreecommitdiff
path: root/src/main/kotlin/moe/nea
diff options
context:
space:
mode:
authornea <nea@nea.moe>2023-05-18 03:20:22 +0200
committernea <nea@nea.moe>2023-05-18 03:20:22 +0200
commit1dd603ab1cc42a602f1d82595dd15789ef63d2a3 (patch)
tree1658e1d32849d69102ff1e1b4c856da8428ab82b /src/main/kotlin/moe/nea
parent3ebe3e80b929b03ad0bcb0c76cec6b3285fe08bd (diff)
downloadFirmament-1dd603ab1cc42a602f1d82595dd15789ef63d2a3.tar.gz
Firmament-1dd603ab1cc42a602f1d82595dd15789ef63d2a3.tar.bz2
Firmament-1dd603ab1cc42a602f1d82595dd15789ef63d2a3.zip
Add slot locking
Diffstat (limited to 'src/main/kotlin/moe/nea')
-rw-r--r--src/main/kotlin/moe/nea/firmament/Firmament.kt5
-rw-r--r--src/main/kotlin/moe/nea/firmament/events/HandledScreenKeyPressedEvent.kt10
-rw-r--r--src/main/kotlin/moe/nea/firmament/events/IsSlotProtectedEvent.kt26
-rw-r--r--src/main/kotlin/moe/nea/firmament/events/SlotRenderEvents.kt32
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/FeatureManager.kt6
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/FirmamentFeature.kt (renamed from src/main/kotlin/moe/nea/firmament/features/NEUFeature.kt)2
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/fishing/FishingWarning.kt4
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/inventory/SlotLocking.kt68
-rw-r--r--src/main/kotlin/moe/nea/firmament/features/world/FairySouls.kt4
-rw-r--r--src/main/kotlin/moe/nea/firmament/keybindings/FirmamentKeyBindings.kt17
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/CommonSoundEffects.kt28
-rw-r--r--src/main/kotlin/moe/nea/firmament/util/MC.kt5
12 files changed, 198 insertions, 9 deletions
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<HandledScreenKeyPressedEvent>()
+ 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<IsSlotProtectedEvent>() {
+ @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<Before>()
+ }
+
+ 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<After>()
+ }
+}
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<FeatureManager.Config>(serializer(), "feature
val enabledFeatures: MutableMap<String, Boolean> = mutableMapOf()
)
- private val features = mutableMapOf<String, NEUFeature>()
+ private val features = mutableMapOf<String, FirmamentFeature>()
private var hasAutoloaded = false
@@ -26,11 +27,12 @@ object FeatureManager : DataHolder<FeatureManager.Config>(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/NEUFeature.kt b/src/main/kotlin/moe/nea/firmament/features/FirmamentFeature.kt
index f231003..c9cb8f0 100644
--- a/src/main/kotlin/moe/nea/firmament/features/NEUFeature.kt
+++ b/src/main/kotlin/moe/nea/firmament/features/FirmamentFeature.kt
@@ -2,7 +2,7 @@ package moe.nea.firmament.features
import moe.nea.firmament.util.config.ManagedConfig
-interface NEUFeature {
+interface FirmamentFeature {
val name: String
val identifier: String
val defaultEnabled: Boolean
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<Int> = mutableSetOf(),
+ )
+
+ object DConfig : ProfileSpecificDataHolder<Data>(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