diff --git a/app/lib/models/communicator.dart b/app/lib/models/communicator.dart index dab9209d65..23d35675cb 100644 --- a/app/lib/models/communicator.dart +++ b/app/lib/models/communicator.dart @@ -169,6 +169,14 @@ class SocketNotifier extends StateNotifier { "stagingState", (data) => ref.read(communicatorProvider).handleStagingState(data), ) + ..on( + "updatePages", + (data) => ref.read(communicatorProvider).handleUpdatePages(data), + ) + ..on( + "updateExtensions", + (data) => ref.read(communicatorProvider).handleUpdateExtensions(data), + ) ..on( "createPage", (data) => ref.read(communicatorProvider).handleCreatePage(data), @@ -476,6 +484,27 @@ class Communicator { ref.read(stagingStateProvider.notifier).state = state; } + void handleUpdatePages(dynamic data) { + final rawPages = data as String; + final jsonPages = jsonDecode(rawPages) as List; + + // ignore: unnecessary_lambdas + final pages = jsonPages.map((p) => Page.fromJson(p)).toList(); + ref.read(bookProvider.notifier).book = + ref.read(bookProvider).copyWith(pages: pages); + } + + void handleUpdateExtensions(dynamic data) { + final rawExtensions = data as String; + final jsonExtensions = jsonDecode(rawExtensions) as List; + + // ignore: unnecessary_lambdas + final extensions = + jsonExtensions.map((e) => Extension.fromJson(e)).toList(); + ref.read(bookProvider.notifier).book = + ref.read(bookProvider).copyWith(extensions: extensions); + } + void handleCreatePage(dynamic data) { final json = jsonDecode(data as String) as Map; final page = Page.fromJson(json); diff --git a/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/TypewriterPaperPlugin.kt b/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/TypewriterPaperPlugin.kt index 55d25fb38d..c66228ab99 100644 --- a/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/TypewriterPaperPlugin.kt +++ b/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/TypewriterPaperPlugin.kt @@ -165,6 +165,7 @@ class TypewriterPaperPlugin : KotlinPlugin(), KoinComponent { get().load() get().loadState() + get().load() get().load() get().load() get().load() diff --git a/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/ui/CommunicationHandler.kt b/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/ui/CommunicationHandler.kt index 78621d5dc8..bb6120c492 100644 --- a/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/ui/CommunicationHandler.kt +++ b/engine/engine-paper/src/main/kotlin/com/typewritermc/engine/paper/ui/CommunicationHandler.kt @@ -4,6 +4,7 @@ import com.corundumstudio.socketio.Configuration import com.corundumstudio.socketio.HandshakeData import com.corundumstudio.socketio.SocketIOClient import com.corundumstudio.socketio.SocketIOServer +import com.google.gson.JsonArray import lirand.api.extensions.events.listen import lirand.api.extensions.server.server import com.typewritermc.engine.paper.entry.StagingManager @@ -13,6 +14,7 @@ import com.typewritermc.engine.paper.plugin import com.typewritermc.engine.paper.utils.config import com.typewritermc.engine.paper.utils.logErrorIfNull import com.typewritermc.engine.paper.utils.optionalConfig +import com.typewritermc.loader.ExtensionLoader import org.bukkit.entity.Player import org.koin.core.component.KoinComponent import org.koin.core.component.inject @@ -26,6 +28,8 @@ class CommunicationHandler : KoinComponent { private val writers: Writers by inject() private val stagingManager: StagingManager by inject() private val panelHost: PanelHost by inject() + private val extensionLoader: ExtensionLoader by inject() + private val extensionJson by extensionLoader::extensionJson private val hostName: String by config( "hostname", @@ -133,6 +137,18 @@ class CommunicationHandler : KoinComponent { } } + fun load() { + if (!enabled) return + server?.broadcastOperations?.sendEvent("stagingState", stagingManager.stagingState.name.lowercase()) + + val array = JsonArray() + stagingManager.pages.forEach { (_, page) -> + array.add(page) + } + server?.broadcastOperations?.sendEvent("updatePages", array.toString()) + server?.broadcastOperations?.sendEvent("updateExtensions", extensionJson.toString()) + } + private fun authenticate(data: HandshakeData): Boolean { if (auth == "none") return true if (auth == "session") { diff --git a/extensions/BasicExtension/src/main/kotlin/com/typewritermc/basic/entries/action/DelayedActionEntry.kt b/extensions/BasicExtension/src/main/kotlin/com/typewritermc/basic/entries/action/DelayedActionEntry.kt index e90003166a..ec23c70393 100644 --- a/extensions/BasicExtension/src/main/kotlin/com/typewritermc/basic/entries/action/DelayedActionEntry.kt +++ b/extensions/BasicExtension/src/main/kotlin/com/typewritermc/basic/entries/action/DelayedActionEntry.kt @@ -35,7 +35,6 @@ class DelayedActionEntry( @Help("The duration before the next triggers are fired.") private val duration: Var = ConstVar(Duration.ZERO), ) : CustomTriggeringActionEntry { - override fun execute(player: Player) { DISPATCHERS_ASYNC.launch { delay(duration.get(player).toMillis())