Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature + Backend: Prevent command early execution #3377

Open
wants to merge 9 commits into
base: beta
Choose a base branch
from
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,12 @@ class CommandsConfig {
@ConfigOption(name = "Transfer Cooldown Message", desc = "Sends a message in chat when the transfer cooldown ends.")
@ConfigEditorBoolean
var transferCooldownMessage: Boolean = false

@Expose
@ConfigOption(
name = "Prevent Early Commands",
desc = ""
)
@Accordion
var preventEarlyExecutionConfig: PreventEarlyExecutionConfig = PreventEarlyExecutionConfig()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package at.hannibal2.skyhanni.config.features.commands

import com.google.gson.annotations.Expose
import io.github.notenoughupdates.moulconfig.annotations.ConfigEditorBoolean
import io.github.notenoughupdates.moulconfig.annotations.ConfigOption

class PreventEarlyExecutionConfig {

@Expose
@ConfigOption(
name = "Prevent Early Command Execution",
desc = "Prevent commands from executing before the Server cooldown has ended."
)
@ConfigEditorBoolean
var preventEarlyExecution: Boolean = true

}
10 changes: 10 additions & 0 deletions src/main/java/at/hannibal2/skyhanni/data/ChatManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.config.commands.CommandCategory
import at.hannibal2.skyhanni.config.commands.CommandRegistrationEvent
import at.hannibal2.skyhanni.events.MessageSendToServerEvent
import at.hannibal2.skyhanni.events.chat.CommandSentEvent
import at.hannibal2.skyhanni.events.chat.SkyHanniChatEvent
import at.hannibal2.skyhanni.events.minecraft.packet.PacketSentEvent
import at.hannibal2.skyhanni.features.chat.ChatFilterGui
Expand Down Expand Up @@ -119,6 +120,15 @@ object ChatManager {
event.cancel()
messageHistory[IdentityCharacteristics(component)] = result.copy(actionKind = ActionKind.OUTGOING_BLOCKED)
}
if (trimmedMessage.startsWith("/") && CommandSentEvent(
trimmedMessage,
trimmedMessage.split(" ")[0].removePrefix("/"),
trimmedMessage.split(" "),
).post()
) {
event.cancel()
messageHistory[IdentityCharacteristics(component)] = result.copy(actionKind = ActionKind.OUTGOING_BLOCKED)
}
}

@SubscribeEvent(receiveCanceled = true)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package at.hannibal2.skyhanni.events.chat

import at.hannibal2.skyhanni.api.event.CancellableSkyHanniEvent

class CommandSentEvent(
val fullCommand: String,
val command: String,
val args: List<String>,
) : CancellableSkyHanniEvent()
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package at.hannibal2.skyhanni.features.commands

import at.hannibal2.skyhanni.SkyHanniMod
import at.hannibal2.skyhanni.api.event.HandleEvent
import at.hannibal2.skyhanni.events.chat.CommandSentEvent
import at.hannibal2.skyhanni.events.chat.SkyHanniChatEvent
import at.hannibal2.skyhanni.events.minecraft.WorldChangeEvent
import at.hannibal2.skyhanni.skyhannimodule.SkyHanniModule
import at.hannibal2.skyhanni.utils.ChatUtils
import at.hannibal2.skyhanni.utils.DelayedRun
import at.hannibal2.skyhanni.utils.RegexUtils.matchMatcher
import at.hannibal2.skyhanni.utils.RegexUtils.matches
import at.hannibal2.skyhanni.utils.SimpleTimeMark
import at.hannibal2.skyhanni.utils.repopatterns.RepoPattern
import kotlin.math.ceil
import kotlin.time.Duration
import kotlin.time.Duration.Companion.seconds
import kotlin.time.DurationUnit

@SkyHanniModule
object PreventEarlyCommands {
private val config get() = SkyHanniMod.feature.misc.commands.preventEarlyExecutionConfig

private var commandExecuted: SimpleTimeMark = SimpleTimeMark.farPast()
private var worldChanged: SimpleTimeMark = SimpleTimeMark.farPast()
private var command: String = ""

/**
* REGEX-TEST: §cYou may only use this command after 4s on the server!
*/
private val cooldownPattern by RepoPattern.pattern(
"commands.cooldown",
"§cYou may only use this command after (?<cooldown>\\d+)s on the server!"
)

@HandleEvent
fun onCommand(event: CommandSentEvent) {
if (!config.preventEarlyExecution) return
command = event.command

if (command == "locraw") return

ChatUtils.debug("Setting command to $command")
commandExecuted = SimpleTimeMark.now()
}

@HandleEvent
fun onWorldChange(event: WorldChangeEvent) {
worldChanged = SimpleTimeMark.now()
}

@HandleEvent
fun onRecieveChatMessage(event: SkyHanniChatEvent) {
if (!config.preventEarlyExecution) return
if (cooldownPattern.matches(event.message)) {
val cooldown = cooldownPattern.matchMatcher(event.message) {
group("cooldown")
}
val runIn: Duration = (cooldown?.toInt()?.seconds ?: 5.seconds) - worldChanged.absoluteDifference(SimpleTimeMark.now())
DelayedRun.runDelayed(runIn) {
ChatUtils.sendMessageToServer("/$command")
}
event.blockedReason = "prevent_early_command"
ChatUtils.chat("Cannot execute /$command yet. Running in ${ceil(runIn.toDouble(DurationUnit.SECONDS))} seconds.")
}
}
}
Loading