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

Commit

Permalink
fix lagslayer hud!!
Browse files Browse the repository at this point in the history
  • Loading branch information
homchom committed Mar 24, 2024
1 parent 6f9a7e2 commit fe42fd6
Show file tree
Hide file tree
Showing 13 changed files with 133 additions and 121 deletions.
6 changes: 2 additions & 4 deletions src/main/java/io/github/homchom/recode/Recode.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@ import com.google.gson.GsonBuilder
import io.github.homchom.recode.event.listenEach
import io.github.homchom.recode.feature.automation.AutoCommands
import io.github.homchom.recode.feature.meta.FDebugMode
import io.github.homchom.recode.feature.visual.FBuiltInResourcePacks
import io.github.homchom.recode.feature.visual.FCodeSearch
import io.github.homchom.recode.feature.visual.FMessageStacking
import io.github.homchom.recode.feature.visual.FSignRenderDistance
import io.github.homchom.recode.feature.visual.*
import io.github.homchom.recode.game.QuitGameEvent
import io.github.homchom.recode.hypercube.JoinDFDetector
import io.github.homchom.recode.mod.commands.CommandHandler
Expand Down Expand Up @@ -76,6 +73,7 @@ object Recode : ClientModInitializer, ModContainer {
// Visual
FBuiltInResourcePacks
FCodeSearch
FLagSlayerHUD
FMessageStacking
FSignRenderDistance

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import kotlinx.coroutines.*
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.SendChannel
import kotlinx.coroutines.flow.*
import java.util.concurrent.CancellationException
import java.util.concurrent.ConcurrentLinkedDeque
import java.util.concurrent.atomic.AtomicBoolean
import kotlin.time.Duration
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
package io.github.homchom.recode.feature.visual

import io.github.homchom.recode.Power
import io.github.homchom.recode.config.Config
import io.github.homchom.recode.config.Setting
import io.github.homchom.recode.event.listenEach
import io.github.homchom.recode.feature.AddsFeature
import io.github.homchom.recode.feature.registerFeature
import io.github.homchom.recode.game.TickCountdown
import io.github.homchom.recode.mc
import io.github.homchom.recode.multiplayer.ReceiveMessageEvent
import io.github.homchom.recode.render.AfterRenderHudEvent
import io.github.homchom.recode.ui.text.StyledString
import io.github.homchom.recode.ui.text.matchesPlain
import io.github.homchom.recode.ui.text.substring
import io.github.homchom.recode.ui.text.toVanillaComponent
import io.github.homchom.recode.util.regex.regex
import net.minecraft.client.gui.GuiGraphics

@OptIn(AddsFeature::class)
object FLagSlayerHUD : Setting<Boolean> {
override val configString = "cpuOnScreen"

private var regex: Regex? = null

private var line1: StyledString? = null
private var line2: StyledString? = null
private var line3: StyledString? = null
private val renderCountdown = TickCountdown(30)

init {
registerFeature("LagSlayer HUD") {
onEnable {
regex = regex {
str("CPU Usage: [")
str('') * 20
str("] (")
digit; digit.optional()
period; digit.oneOrMore()
str("%)")
}

interceptActionBarPackets()
render()
}
onDisable {
regex = null
}
}
}

private fun Power.interceptActionBarPackets() = listenEach(ReceiveMessageEvent.ActionBar) l@{ message ->
if (Config[FLagSlayerHUD] == false) return@l
if (!regex!!.matchesPlain(message.value)) return@l

message.invalidate()

val text = StyledString(message.value)
line1 = text.substring(0, 11)
line2 = text.substring(11, 33)
line3 = text.substring(33)
renderCountdown.wind()
}

private fun Power.render() = listenEach(AfterRenderHudEvent) l@{ (guiGraphics) ->
if (!renderCountdown.isActive) return@l
drawString(guiGraphics, line1!!, 3)
drawString(guiGraphics, line2!!, 2)
drawString(guiGraphics, line3!!, 1)
}

// TODO: rework post-workflow
private fun drawString(guiGraphics: GuiGraphics, string: StyledString, index: Int) {
val font = mc.font
val window = mc.window
guiGraphics.drawString(
font,
string.toVanillaComponent(),
4,
window.guiScaledHeight - (font.lineHeight * index + 4),
0xffffff,
true
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ 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.ReceiveChatMessageEvent
import io.github.homchom.recode.multiplayer.ReceiveMessageEvent
import io.github.homchom.recode.multiplayer.Sender
import io.github.homchom.recode.ui.text.equalsPlain
import io.github.homchom.recode.ui.text.matchesPlain
Expand All @@ -15,7 +15,7 @@ import io.github.homchom.recode.util.regex.regex
// TODO: replace more requesters with senders and update documentation
object CommandSenders {
val ChatLocal = requester("/chat local", DFStateDetectors, trial(
ReceiveChatMessageEvent,
ReceiveMessageEvent.Chat,
Unit,
start = { Sender.sendCommand("chat local") },
tests = { (text), _, _ ->
Expand All @@ -33,7 +33,7 @@ object CommandSenders {

// TODO: support time keywords through command suggestions (not enum)
val ClientTime = requester("/time", DFStateDetectors, trial(
ReceiveChatMessageEvent,
ReceiveMessageEvent.Chat,
null as Long?,
start = { time -> Sender.sendCommand("time $time") },
tests = { context, time, _: Boolean ->
Expand All @@ -42,7 +42,7 @@ object CommandSenders {
))

val Flight = toggleRequesterGroup("/fly", DFStateDetectors, trial(
ReceiveChatMessageEvent,
ReceiveMessageEvent.Chat,
Unit,
start = { Sender.sendCommand("fly") },
tests = t@{ message, _, _ ->
Expand All @@ -67,7 +67,7 @@ object CommandSenders {
}

val LagSlayer = toggleRequesterGroup("/lagslayer", DFStateDetectors, trial(
ReceiveChatMessageEvent,
ReceiveMessageEvent.Chat,
Unit,
start = { Sender.sendCommand("lagslayer") },
tests = t@{ (message), _, _ ->
Expand All @@ -81,7 +81,7 @@ object CommandSenders {
))

val NightVision = toggleRequesterGroup("/nightvis", DFStateDetectors, trial(
ReceiveChatMessageEvent,
ReceiveMessageEvent.Chat,
Unit,
start = { Sender.sendCommand("nightvis") },
tests = t@{ (message), _, _ ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import io.github.homchom.recode.hypercube.state.isOnDF
import io.github.homchom.recode.mc
import io.github.homchom.recode.multiplayer.DisconnectFromServerEvent
import io.github.homchom.recode.multiplayer.JoinServerEvent
import io.github.homchom.recode.multiplayer.ReceiveChatMessageEvent
import io.github.homchom.recode.multiplayer.ReceiveMessageEvent
import io.github.homchom.recode.multiplayer.username
import io.github.homchom.recode.ui.text.matchEntirePlain
import io.github.homchom.recode.util.Case
Expand All @@ -28,7 +28,7 @@ val JoinDFDetector = detector("DF join",
if (isOnDF) return@t null // if already on DF, this is a node switch and should not be tested
if (!mc.currentServer.ipMatchesDF) return@t null

val messages = ReceiveChatMessageEvent.add()
val messages = ReceiveMessageEvent.Chat.add()
val tipMessage = ActiveBoosterInfo.detect(null).map(::Case).addOptional()

val disconnect = DisconnectFromServerEvent.add()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import io.github.homchom.recode.event.SimpleValidated
import io.github.homchom.recode.event.trial.detector
import io.github.homchom.recode.event.trial.requester
import io.github.homchom.recode.event.trial.trial
import io.github.homchom.recode.multiplayer.ReceiveChatMessageEvent
import io.github.homchom.recode.multiplayer.ReceiveMessageEvent
import io.github.homchom.recode.util.Matcher
import io.github.homchom.recode.util.matcherOf
import io.github.homchom.recode.util.splitByHumps
Expand All @@ -20,7 +20,7 @@ import net.kyori.adventure.text.Component
sealed interface MessageParser<T : Any, out R : ParsedMessage> : Matcher<Component, R>, Detector<T, R>

val ParsedMessageDetector = detector("parsed message",
trial(ReceiveChatMessageEvent, Unit) t@{ message, _ ->
trial(ReceiveMessageEvent.Chat, Unit) t@{ message, _ ->
val parsed = ParsedMessage.match(message.value) ?: return@t null
if (hidden) message.invalidate()
instant(ParsedMessageContext(parsed, message))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.github.homchom.recode.event.trial.detector
import io.github.homchom.recode.event.trial.trial
import io.github.homchom.recode.hypercube.BOOSTER_ARROW
import io.github.homchom.recode.hypercube.TOKEN_NOTCH_CHAR
import io.github.homchom.recode.multiplayer.ReceiveChatMessageEvent
import io.github.homchom.recode.multiplayer.ReceiveMessageEvent
import io.github.homchom.recode.multiplayer.username
import io.github.homchom.recode.ui.text.matchEntirePlain
import io.github.homchom.recode.ui.text.matchesPlain
Expand Down Expand Up @@ -41,7 +41,7 @@ data class ActiveBoosterInfo(val player: String, val canTip: Boolean) {
ShopMessages.BoosterActive,
Unit,
tests = { (player), _ ->
val subsequent = ReceiveChatMessageEvent.add()
val subsequent = ReceiveMessageEvent.Chat.add()

suspending s@{
val (first) = subsequent.receive()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ object DFStateDetectors : StateListenable<Case<DFState?>> by eventGroup {
))

val ChangeMode = eventGroup.add(detector("mode change",
trial(ReceiveChatMessageEvent, Unit) t@{ (message), _ ->
trial(ReceiveMessageEvent.Chat, Unit) t@{ (message), _ ->
enforceOnDF()
val (mode, plotName, plotOwner) = PlotMode.ID.match(message) ?: return@t null
suspending s@{
Expand Down Expand Up @@ -124,14 +124,14 @@ object DFStateDetectors : StateListenable<Case<DFState?>> by eventGroup {
))

val StartSession = eventGroup.add(detector("session start",
trial(ReceiveChatMessageEvent, Unit) t@{ (message), _ ->
trial(ReceiveMessageEvent.Chat, Unit) t@{ (message), _ ->
enforceOnDF()

if (currentDFState!!.session != null) return@t null
val session = SupportSession.match(message) ?: return@t null

val subsequent = ReceiveChatMessageEvent.add()
val enforceChannel = ReceiveChatMessageEvent.add()
val subsequent = ReceiveMessageEvent.Chat.add()
val enforceChannel = ReceiveMessageEvent.Chat.add()
suspending s@{
enforce(enforceChannel) { (text) -> SupportSession.match(text) == null }

Expand All @@ -152,7 +152,7 @@ object DFStateDetectors : StateListenable<Case<DFState?>> by eventGroup {
))

val EndSession = eventGroup.add(detector("session end",
trial(ReceiveChatMessageEvent, Unit) t@{ (message), _ ->
trial(ReceiveMessageEvent.Chat, Unit) t@{ (message), _ ->
enforceOnDF()

if (currentDFState!!.session == null) return@t null
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.github.homchom.recode.mixin.multiplayer;

import io.github.homchom.recode.multiplayer.ReceiveMessageEvent;
import io.github.homchom.recode.multiplayer.Sender;
import io.github.homchom.recode.sys.hypercube.templates.TemplateStorageHandler;
import io.github.homchom.recode.sys.hypercube.templates.TemplateUtil;
import net.minecraft.client.multiplayer.ClientPacketListener;
import net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket;
import net.minecraft.network.protocol.game.ClientboundSetActionBarTextPacket;
import net.minecraft.network.protocol.game.ClientboundSystemChatPacket;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.injection.At;
import org.spongepowered.asm.mixin.injection.Inject;
Expand All @@ -13,6 +16,26 @@

@Mixin(ClientPacketListener.class)
public abstract class MClientPacketListener {
@Inject(method = "handleSystemChat", cancellable = true, at = @At(value = "INVOKE",
target = "Lnet/minecraft/network/protocol/PacketUtils;ensureRunningOnSameThread(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketListener;Lnet/minecraft/util/thread/BlockableEventLoop;)V",
shift = At.Shift.AFTER
))
private void runChatEvent(ClientboundSystemChatPacket packet, CallbackInfo ci) {
if (!ReceiveMessageEvent.Chat.INSTANCE.cacheAndRun(packet.content())) {
ci.cancel();
}
}

@Inject(method = "setActionBarText", cancellable = true, at = @At(value = "INVOKE",
target = "Lnet/minecraft/network/protocol/PacketUtils;ensureRunningOnSameThread(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketListener;Lnet/minecraft/util/thread/BlockableEventLoop;)V",
shift = At.Shift.AFTER
))
private void runActionBarEvent(ClientboundSetActionBarTextPacket packet, CallbackInfo ci) {
if (!ReceiveMessageEvent.ActionBar.INSTANCE.cacheAndRun(packet.getText())) {
ci.cancel();
}
}

@Inject(method = "handleContainerSetSlot", at = @At(value = "INVOKE",
target = "Lnet/minecraft/network/protocol/PacketUtils;ensureRunningOnSameThread(Lnet/minecraft/network/protocol/Packet;Lnet/minecraft/network/PacketListener;Lnet/minecraft/util/thread/BlockableEventLoop;)V",
shift = At.Shift.AFTER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import io.github.homchom.recode.hypercube.state.DF;
import io.github.homchom.recode.hypercube.state.PlotMode;
import io.github.homchom.recode.mod.config.LegacyConfig;
import io.github.homchom.recode.multiplayer.ReceiveChatMessageEvent;
import io.github.homchom.recode.multiplayer.ReceiveMessageEvent;
import io.github.homchom.recode.sys.player.chat.ChatType;
import io.github.homchom.recode.sys.player.chat.ChatUtil;
import io.github.homchom.recode.sys.util.TextUtil;
Expand All @@ -20,7 +20,7 @@

public class LegacyReceiveChatMessageEvent {
public LegacyReceiveChatMessageEvent() {
ReceiveChatMessageEvent.INSTANCE.register(this::run);
ReceiveMessageEvent.Chat.INSTANCE.register(this::run);
}

public static boolean pjoin = false;
Expand Down

This file was deleted.

Loading

0 comments on commit fe42fd6

Please sign in to comment.