Skip to content

Commit

Permalink
feat: esc handler 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
cccgh5 committed Nov 11, 2023
1 parent ff97970 commit 247ddec
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ open class VirtualContainer(
containersClass.java,
baseComponentService.getTargetClass().java
)

return VirtualMessageImpl(
constructor.newInstance(
container.getContainerId(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,31 @@ import kr.hqservice.framework.nms.extension.callAccess
import kr.hqservice.framework.nms.virtual.handler.HandlerUnregisterType
import kr.hqservice.framework.nms.virtual.handler.VirtualHandler
import kr.hqservice.framework.nms.wrapper.NmsReflectionWrapper
import org.bukkit.entity.Player
import org.bukkit.event.Listener
import org.bukkit.event.inventory.InventoryClickEvent
import org.bukkit.plugin.Plugin

class VirtualAnvilHandler(
private val reflectionWrapper: NmsReflectionWrapper,
private val textScope: suspend (String) -> Unit,
private val confirmScope: suspend (String) -> Boolean,
private val otherSlotClickScope: suspend () -> Unit,
private val dummyListener: Listener,
private val dummyListener: VirtualAnvilListener,
private val closeScope: suspend (String) -> Unit
) : VirtualHandler {
class VirtualAnvilListener(
private val player: Player,
private val plugin: Plugin
) : Listener {
fun close() {
plugin.server.scheduler.runTask(plugin, Runnable {
InventoryClickEvent.getHandlerList().unregister(this)
player.updateInventory()
})
}
}

private var currentText = ""
private var unregistered = false

Expand All @@ -33,7 +48,8 @@ class VirtualAnvilHandler(

override fun unregisterCondition(message: Any): Boolean {
if (unregistered || message::class.simpleName == "PacketPlayInCloseWindow") {
InventoryClickEvent.getHandlerList().unregister(dummyListener)
runBlocking { closeScope.invoke(currentText) }
dummyListener.close()
return true
}
return false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ abstract class AbstractVirtualScope(
val containerFactory = VirtualAnvilContainerScope(it, title)
containerFactory.anvilFactoryScope()
reflectionWrapper.sendPacket(it, *containerFactory.getMessages())
val dummyListener = object : Listener {}
val dummyListener = VirtualAnvilHandler.VirtualAnvilListener(it, lazyPlugin)

lazyPlugin.server.pluginManager.registerEvent(InventoryClickEvent::class.java, dummyListener, EventPriority.LOWEST, { _, event ->
event as InventoryClickEvent
Expand All @@ -59,11 +59,14 @@ abstract class AbstractVirtualScope(
}, { text -> if (containerFactory.confirm(text)) {
lazyPlugin.server.scheduler.runTask(lazyPlugin, Runnable {
it.closeInventory()
it.updateInventory()
})
true
} else false }, {
reflectionWrapper.sendPacket(it, *containerFactory.getBaseItem().toTypedArray())
}, dummyListener))
}, dummyListener, { text ->
containerFactory.close(text)
}))
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ class VirtualAnvilContainerScope(
private var resultItem: VirtualItem? = null
private var virtualResultSlotHandler: ((String) -> ItemStack)? = null
private var virtualConfirmHandler: ((String) -> Boolean)? = null
private var closeHandler: ((String) -> Unit)? = null
private var anvilPacket: VirtualContainer = VirtualAnvilContainer(receiver, ComponentSerializer.toString(title))

fun setBaseItem(itemStack: ItemStack?) {
Expand Down Expand Up @@ -79,6 +80,10 @@ class VirtualAnvilContainerScope(
virtualConfirmHandler = confirmHandleScope
}

fun setCloseHandler(closeScope: (String) -> Unit) {
closeHandler = closeScope
}

internal fun getBaseItem(): List<VirtualItem> {
return baseItem ?: defaultBaseItems(receiver)
}
Expand All @@ -92,6 +97,10 @@ class VirtualAnvilContainerScope(
return virtualConfirmHandler?.invoke(text) != false
}

internal fun close(text: String) {
closeHandler?.invoke(text)
}

fun getMessages(): Array<Virtual> {
val messages = mutableListOf<Virtual>()
anvilPacket.apply(messages::add)
Expand Down

0 comments on commit 247ddec

Please sign in to comment.