Skip to content
This repository has been archived by the owner on Sep 2, 2024. It is now read-only.

Commit

Permalink
remove ToggleRequesterGroup; /lagslayer is now a special case
Browse files Browse the repository at this point in the history
  • Loading branch information
homchom committed Apr 6, 2024
1 parent c505c15 commit 69291c3
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 142 deletions.
2 changes: 1 addition & 1 deletion src/main/java/io/github/homchom/recode/event/EventTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -133,4 +133,4 @@ interface Requester<T : Any, out R : Any> : Sender<T, R?>, Detector<T, R> {
*
* @see Requester.request
*/
suspend fun <R : Any> Requester<Unit, R>.request(): R = request(Unit)
suspend fun <R : Any> Requester<Unit, R>.request() = request(Unit)

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,15 @@ object AutoCommands {

register("lagslayer", "autolagslayer", DFStateDetectors.ChangeMode) { (new) ->
if (new.mode is PlotMode.Dev && !currentDFState.isInMode(PlotMode.Dev)) {
launch { CommandSenders.LagSlayer.enable.send() }
launch {
if (!CommandSenders.LagSlayer.isLagSlayerEnabled) CommandSenders.LagSlayer.send()
}
}
}

register("nightvis", "autonightvis", DFStateDetectors.ChangeMode) { (new) ->
if (new.session != SupportSession.Helping && new.mode != PlotMode.Play) {
launch { CommandSenders.NightVision.enable.send() }
launch { CommandSenders.NightVision.send() }
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ enum class CommandAliasGroup(vararg aliases: String) : Set<String> by aliases.to
PLOT_NAME("plot name", "p name"),

// legacy
RELORE("relore");
RELORE("relore")
}
84 changes: 39 additions & 45 deletions src/main/java/io/github/homchom/recode/hypercube/CommandSenders.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
package io.github.homchom.recode.hypercube

import io.github.homchom.recode.Power
import io.github.homchom.recode.event.Requester
import io.github.homchom.recode.event.listenEach
import io.github.homchom.recode.event.trial.requester
import io.github.homchom.recode.event.trial.toggleRequesterGroup
import io.github.homchom.recode.event.trial.trial
import io.github.homchom.recode.hypercube.state.DFStateDetectors
import io.github.homchom.recode.multiplayer.ReceiveMessageEvent
import io.github.homchom.recode.multiplayer.SendPacketEvent
import io.github.homchom.recode.multiplayer.Sender
import io.github.homchom.recode.ui.text.equalsPlain
import io.github.homchom.recode.ui.text.matchesPlain
import io.github.homchom.recode.ui.text.plainText
import io.github.homchom.recode.util.regex.dynamicRegex
import io.github.homchom.recode.util.regex.regex
import net.minecraft.network.protocol.game.ServerboundChatCommandPacket

// TODO: replace more requesters with senders and update documentation
object CommandSenders {
Expand All @@ -25,62 +28,53 @@ object CommandSenders {
}
))

private val timeRegex = dynamicRegex { time: Long? ->
str("$MAIN_ARROW Set your player time to ")
if (time == null) digit.oneOrMore() else str(time.toString())
period
// TODO: support time keywords through command suggestions (not enum)
val ClientTime = Sender(DFStateDetectors) { time: Long ->
Sender.sendCommand("time $time")
}

// TODO: support time keywords through command suggestions (not enum)
val ClientTime = requester("/time", DFStateDetectors, trial(
ReceiveMessageEvent.Chat,
null as Long?,
start = { time -> Sender.sendCommand("time $time") },
tests = { context, time, _: Boolean ->
timeRegex(time).matchesPlain(context.value).instantUnitOrNull()
}
))
private val lsRegex = regex {
str(LAGSLAYER_PREFIX); space
group {
str("Now monitoring plot ")
digit.oneOrMore()
str(". Type /lagslayer to stop monitoring.")

val Flight = toggleRequesterGroup("/fly", DFStateDetectors, trial(
ReceiveMessageEvent.Chat,
Unit,
start = { Sender.sendCommand("fly") },
tests = t@{ message, _, _ ->
val enabled = when (message().plainText) {
"$MAIN_ARROW Flight enabled." -> true
"$MAIN_ARROW Flight disabled." -> false
else -> return@t null
}
instant(enabled)
}
))
or

private val lsEnabledRegex = regex {
str("$LAGSLAYER_PREFIX Now monitoring plot ")
val plot by digit.oneOrMore()
str(". Type /lagslayer to stop monitoring.")
}
private val lsDisabledRegex = regex {
str("$LAGSLAYER_PREFIX Stopped monitoring plot ")
val plot by digit.oneOrMore()
period
str("Stopped monitoring plot ")
digit.oneOrMore()
period
}
}

val LagSlayer = toggleRequesterGroup("/lagslayer", DFStateDetectors, trial(
private val lsDelegate = requester("/lagslayer", DFStateDetectors, trial(
ReceiveMessageEvent.Chat,
Unit,
start = { Sender.sendCommand("lagslayer") },
tests = t@{ (message), _, _ ->
val enabled = when {
lsEnabledRegex.matchesPlain(message) -> true
lsDisabledRegex.matchesPlain(message) -> false
else -> return@t null
}
instant(enabled)
lsRegex.matchesPlain(message).instantUnitOrNull()
}
))

val NightVision = toggleRequesterGroup("/nightvis", DFStateDetectors, trial(
object LagSlayer : Requester<Unit, Unit> by lsDelegate {
var isLagSlayerEnabled = false
private set

// this does not extend delegate because isLagSlayerEnabled should not desync
private val power = Power(startEnabled = true)

init {
power.listenEach(SendPacketEvent) { packet ->
if (packet !is ServerboundChatCommandPacket) return@listenEach
if (!packet.command.equals("lagslayer", true)) return@listenEach

isLagSlayerEnabled = !isLagSlayerEnabled
}
}
}

val NightVision = requester("/nightvis", DFStateDetectors, trial(
ReceiveMessageEvent.Chat,
Unit,
start = { Sender.sendCommand("nightvis") },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ private val eventGroup = GroupListenable<Case<DFState?>>()
object DFStateDetectors : StateListenable<Case<DFState?>> by eventGroup {
private val scoreboardNodeRegex = Regex("""(?<node>.+) - .+""")

private val teleportEvent = ReceiveGamePacketEvent.filterIsInstance<ClientboundPlayerPositionPacket>()
private val teleportEvent = ReceivePacketEvent.filterIsInstance<ClientboundPlayerPositionPacket>()

val EnterSpawn = eventGroup.add(detector("spawn",
trial(teleportEvent, Unit) t@{ _, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import net.minecraft.client.Minecraft;
import net.minecraft.network.Connection;
import net.minecraft.network.PacketListener;
import net.minecraft.network.PacketSendListener;
import net.minecraft.network.protocol.Packet;
import net.minecraft.network.protocol.game.ClientGamePacketListener;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -14,13 +14,21 @@
@Mixin(Connection.class)
public abstract class MConnection {
@Inject(method = "genericsFtw", at = @At("HEAD"))
private static <T extends PacketListener> void interceptPacketHandling(
private static <T extends PacketListener> void runReceivePacketEvent(
Packet<T> packet,
PacketListener packetListener,
CallbackInfo ci
) {
if (packetListener instanceof ClientGamePacketListener) {
Minecraft.getInstance().execute(() -> MultiplayerEvents.getReceiveGamePacketEvent().run(packet));
}
Minecraft.getInstance().execute(() -> MultiplayerEvents.getReceivePacketEvent().run(packet));
}

@Inject(method = "sendPacket", at = @At("HEAD"))
private void runSendPacketEvent(
Packet<?> packet,
PacketSendListener packetSendListener,
boolean flush,
CallbackInfo ci
) {
Minecraft.getInstance().execute(() -> MultiplayerEvents.getSendPacketEvent().run(packet));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ val DisconnectFromServerEvent = wrapFabricEvent(ClientPlayConnectionEvents.DISCO
data class ServerJoinContext(val handler: ClientPacketListener, val sender: PacketSender, val client: Minecraft)
data class ServerDisconnectContext(val handler: ClientPacketListener, val client: Minecraft)

val ReceiveGamePacketEvent = createEvent<Packet<*>>()
val ReceivePacketEvent = createEvent<Packet<*>>()

val SendPacketEvent = createEvent<Packet<*>>()

sealed class ReceiveMessageEvent : CustomEvent<SimpleValidated<Component>, Boolean> by createValidatedEvent() {
data object Chat : ReceiveMessageEvent()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import net.kyori.adventure.text.Component
import net.kyori.adventure.text.event.ClickEvent
import net.kyori.adventure.text.event.HoverEvent
import net.kyori.adventure.text.format.Style
import net.kyori.adventure.text.format.TextColor
import net.kyori.adventure.text.format.TextDecoration

/**
Expand Down Expand Up @@ -55,7 +56,7 @@ value class StyleWrapper(private val builder: Style.Builder) {
fun white() = color(ColorPalette.WHITE)

fun color(color: RGB) = apply {
builder.color { color.hex }
builder.color(TextColor.color(color))
}

fun bold() = apply {
Expand Down

1 comment on commit 69291c3

@homchom
Copy link
Owner Author

@homchom homchom commented on 69291c3 Apr 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also generalizes ReceiveGamePacketEvent into ReceivePacketEvent, and adds SendPacketEvent

Please sign in to comment.