Skip to content

Commit

Permalink
Create BlockCommandCinematicEntry
Browse files Browse the repository at this point in the history
  • Loading branch information
gabber235 committed Sep 29, 2024
1 parent f5a2ec4 commit c458742
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,10 @@ class CinematicSequence(
state = ENDING
val originalFrame = frame

if (settings.blockChatMessages) player.stopBlockingMessages()
if (settings.blockChatMessages) {
player.stopBlockingMessages()
player.chatHistory.resendMessages(player)
}
if (settings.blockActionBarMessages) player.stopBlockingActionBar()

actions.forEach {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ import com.github.retrooper.packetevents.protocol.packettype.PacketType
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerChatMessage
import com.github.retrooper.packetevents.wrapper.play.server.WrapperPlayServerSystemChatMessage
import com.github.shynixn.mccoroutine.bukkit.registerSuspendingEvents
import lirand.api.extensions.server.server
import com.typewritermc.engine.paper.plugin
import com.typewritermc.engine.paper.snippets.snippet
import com.typewritermc.engine.paper.utils.plainText
import io.papermc.paper.event.player.AsyncChatEvent
import lirand.api.extensions.server.server
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.format.TextColor
Expand All @@ -27,7 +26,11 @@ import java.util.*
import java.util.concurrent.ConcurrentLinkedQueue
import kotlin.math.min

private val darkenLimit by snippet("chat.darken-limit", 12, "The amount of messages displayed in the chat history during a dialogue")
private val darkenLimit by snippet(
"chat.darken-limit",
12,
"The amount of messages displayed in the chat history during a dialogue"
)
private val spacing by snippet("chat.spacing", 3, "The amount of padding between the dialogue and the chat history")

class ChatHistoryHandler :
Expand Down Expand Up @@ -60,11 +63,13 @@ class ChatHistoryHandler :
val message = packet.message as? ChatMessage_v1_19_3 ?: return packet.message.chatContent
message.unsignedChatContent.orElseGet { message.chatContent }
}

PacketType.Play.Server.SYSTEM_CHAT_MESSAGE -> {
val packet = WrapperPlayServerSystemChatMessage(event)
if (packet.isOverlay) return null
packet.message
}

else -> null
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.typewritermc.basic.entries.cinematic

import com.github.retrooper.packetevents.protocol.packettype.PacketType
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientChatCommand
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientChatCommandUnsigned
import com.typewritermc.core.books.pages.Colors
import com.typewritermc.core.extension.annotations.Entry
import com.typewritermc.core.extension.annotations.Help
import com.typewritermc.core.extension.annotations.Regex
import com.typewritermc.core.extension.annotations.Segments
import com.typewritermc.engine.paper.entry.Criteria
import com.typewritermc.engine.paper.entry.cinematic.SimpleCinematicAction
import com.typewritermc.engine.paper.entry.entries.CinematicAction
import com.typewritermc.engine.paper.entry.entries.CinematicEntry
import com.typewritermc.engine.paper.entry.entries.Segment
import com.typewritermc.engine.paper.interaction.InterceptionBundle
import com.typewritermc.engine.paper.interaction.interceptPackets
import com.typewritermc.engine.paper.utils.ThreadType
import org.bukkit.entity.Player

@Entry("block_command_cinematic", "Block commands during the cinematic", Colors.RED, "mdi:console")
/**
* The `Block Command Cinematic` entry is used to block commands during the cinematic.
*
* The `/typewriter` command is always allowed.
*
* ## How could this be used?
*
* This could be used to block commands during a cinematic.
*/
class BlockCommandCinematicEntry(
override val id: String = "",
override val name: String = "",
override val criteria: List<Criteria> = emptyList(),
@Segments(Colors.RED, "mdi:console")
val segments: List<BlockCommandSegment> = emptyList(),
) : CinematicEntry {
override fun create(player: Player): CinematicAction = BlockCommandCinematicAction(player, this)
}

data class BlockCommandSegment(
override val startFrame: Int = 0,
override val endFrame: Int = 0,
@Regex
@Help("No need to include the slash. For example, use `say` instead of `/say`")
val allowedCommands: List<String> = emptyList(),
) : Segment

class BlockCommandCinematicAction(
private val player: Player,
entry: BlockCommandCinematicEntry,
) : SimpleCinematicAction<BlockCommandSegment>() {
override val segments: List<BlockCommandSegment> = entry.segments

private var bundle: InterceptionBundle? = null
private val ranCommands = mutableListOf<String>()

override suspend fun startSegment(segment: BlockCommandSegment) {
super.startSegment(segment)
bundle = player.interceptPackets {
PacketType.Play.Client.CHAT_COMMAND { event ->
val packet = WrapperPlayClientChatCommand(event)
if (isAllowedCommand(segment, packet.command)) {
return@CHAT_COMMAND
}
ranCommands.add(packet.command)
event.isCancelled = true
}

PacketType.Play.Client.CHAT_COMMAND_UNSIGNED { event ->
val packet = WrapperPlayClientChatCommandUnsigned(event)
if (isAllowedCommand(segment, packet.command)) {
return@CHAT_COMMAND_UNSIGNED
}
ranCommands.add(packet.command)
event.isCancelled = true
}
}
}

override suspend fun stopSegment(segment: BlockCommandSegment) {
super.stopSegment(segment)
bundle?.cancel()
bundle = null
if (ranCommands.isNotEmpty()) {
ThreadType.SYNC.switchContext {
ranCommands.forEach {
player.performCommand(it)
}
}
}
ranCommands.clear()
}

private fun isAllowedCommand(segment: BlockCommandSegment, command: String): Boolean {
if (command.startsWith("tw")) return true
if (command.startsWith("typewriter")) return true
return segment.allowedCommands.any { allowedCommand ->
Regex(allowedCommand).matches(command)
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package com.typewritermc.basic.entries.cinematic

import com.github.retrooper.packetevents.protocol.packettype.PacketType.Play
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientClickWindow
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientEntityAction
import com.github.retrooper.packetevents.wrapper.play.client.WrapperPlayClientHeldItemChange
import com.typewritermc.core.books.pages.Colors
import com.typewritermc.core.extension.annotations.Entry
import com.typewritermc.core.extension.annotations.Segments
Expand Down Expand Up @@ -108,7 +106,8 @@ class SkipCinematicAction(
}
}

class CinematicSkippableEvent(player: Player, val canSkip: Boolean, val confirmationKey: SkipConfirmationKey) : PlayerEvent(player, !Bukkit.isPrimaryThread()) {
class CinematicSkippableEvent(player: Player, val canSkip: Boolean, val confirmationKey: SkipConfirmationKey) :
PlayerEvent(player, !Bukkit.isPrimaryThread()) {
override fun getHandlers(): HandlerList = HANDLER_LIST

companion object {
Expand Down

0 comments on commit c458742

Please sign in to comment.