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

feat: Scaffold block counter as HUD component #4114

Merged
merged 3 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src-theme/public/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,36 @@
}
]
},
{
"name": "BlockCounter",
"value": [
{
"name": "Enabled",
"value": true
},
{
"name": "Alignment",
"value": [
{
"name": "Horizontal",
"value": "Center"
},
{
"name": "HorizontalOffset",
"value": -20
},
{
"name": "Vertical",
"value": "CenterTranslated"
},
{
"name": "VerticalOffset",
"value": 0
}
]
}
]
},
{
"name": "Effects",
"value": [
Expand Down
2 changes: 2 additions & 0 deletions src-theme/src/colors.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ $targethud-base-color: black;
$targethud-text-color: white;
$targethud-text-dimmed-color: rgba(211, 211, 211, 255);

$blockcounter-base-color: black;

$hotbar-base-color: black;
$hotbar-text-color: white;
/* TODO: currently unused */
Expand Down
4 changes: 4 additions & 0 deletions src-theme/src/integration/events.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ export interface TargetChangeEvent {
target: PlayerData | null;
}

export interface BlockCountChangeEvent {
count: number | null;
}

export interface AccountManagerAdditionEvent {
username: string | null;
error: string | null;
Expand Down
3 changes: 3 additions & 0 deletions src-theme/src/routes/hud/Hud.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import type {ComponentsUpdateEvent, ScaleFactorChangeEvent} from "../../integration/events";
import Keystrokes from "./elements/keystrokes/Keystrokes.svelte";
import Effects from "./elements/Effects.svelte";
import BlockCounter from "./elements/BlockCounter.svelte";

let zoom = 100;
let components: Component[] = [];
Expand Down Expand Up @@ -48,6 +49,8 @@
<Notifications/>
{:else if c.name === "TargetHud"}
<TargetHud/>
{:else if c.name === "BlockCounter"}
<BlockCounter/>
{:else if c.name === "Hotbar"}
<HotBar/>
{:else if c.name === "Scoreboard"}
Expand Down
43 changes: 43 additions & 0 deletions src-theme/src/routes/hud/elements/BlockCounter.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<script lang="ts">
import {listen} from "../../../integration/ws";
import {fly} from "svelte/transition";
import type {BlockCountChangeEvent} from "../../../integration/events";

let count: number | null = null;

function mapToColor(value: number): string {
if (value <= 0) {
return 'rgb(255, 0, 0)';
} else if (value <= 60) {
return `rgb(255, ${Math.floor(value * 255 / 60)}, 0)`;
} else if (value <= 120) {
return `rgb(${Math.floor((120 - value) * 255 / 60)}, 255, 0)`;
} else {
return 'rgb(0, 255, 0)';
}
}

listen("blockCountChange", (data: BlockCountChangeEvent) => {
count = data.count;
});

</script>

{#if count !== null}
<div class="counter" style="color: {mapToColor(count)}" transition:fly={{ y: -5, duration: 200 }}>
{count}
</div>
{/if}

<style lang="scss">
@import "../../../colors";

.counter {
background-color: rgba($blockcounter-base-color, 0.68);
border-radius: 5px;
white-space: nowrap;
padding: 5px 8px;
font-weight: 500;
transform: translate(-100%);
}
</style>
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ val ALL_EVENT_CLASSES: Array<KClass<out Event>> = arrayOf(
ServerConnectEvent::class,
ServerPingedEvent::class,
TargetChangeEvent::class,
BlockCountChangeEvent::class,
GameModeChangeEvent::class,
ComponentsUpdate::class,
ResourceReloadEvent::class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ class GameModeChangeEvent(val gameMode: GameMode) : Event()
@WebSocketEvent
class TargetChangeEvent(val target: PlayerData?) : Event()

@Nameable("blockCountChange")
@WebSocketEvent
class BlockCountChangeEvent(val count: Int?): Event()

@Nameable("clientChatStateChange")
@WebSocketEvent
class ClientChatStateChange(val state: State) : Event() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,14 @@ object ModuleBedPlates : Module("BedPlates", Category.RENDER) {
distSq
}

val env = this

trackedBlockMap.forEachIndexed { idx, (_, trackState) ->
val bedPlates = trackState.bedPlates
with(matrixStack) {
push()
try {
val z = idx.toFloat() / trackedBlockMap.size.toFloat()

renderBedPlates(env, trackState, fontBuffers, bedPlates, z * 1000.0F)
renderBedPlates(trackState, fontBuffers, bedPlates, z * 1000.0F)
} finally {
pop()
}
Expand All @@ -94,8 +92,7 @@ object ModuleBedPlates : Module("BedPlates", Category.RENDER) {
}
}

private fun renderBedPlates(
env: GUIRenderEnvironment,
private fun GUIRenderEnvironment.renderBedPlates(
trackState: TrackedState,
fontBuffers: FontRendererBuffers,
bedPlates: Set<Block>,
Expand Down Expand Up @@ -141,15 +138,15 @@ object ModuleBedPlates : Module("BedPlates", Category.RENDER) {
0.0f
)

env.matrixStack.push()
matrixStack.push()

env.matrixStack.translate(screenPos.x, screenPos.y, z)
env.matrixStack.translate(0.0f, -height + fontRenderer.height * scale - 6.0F, 0.0f)
env.matrixStack.scale(scale, scale, 1.0F)
matrixStack.translate(screenPos.x, screenPos.y, z)
matrixStack.translate(0.0f, -height + fontRenderer.height * scale - 6.0F, 0.0f)
matrixStack.scale(scale, scale, 1.0F)

fontRenderer.commit(env, fontBuffers)
fontRenderer.commit(fontBuffers)

env.matrixStack.pop()
matrixStack.pop()

// draw items
val itemStartX = width / 2 - (bedPlates.size + 1) * ITEM_SIZE / 2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ object ModuleItemTags : Module("ItemTags", Category.RENDER) {
}
}

fontRenderer.commit(this, fontBuffers)
fontRenderer.commit(fontBuffers)
matrixStack.pop()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,14 @@ class NametagRenderer {
env: RenderEnvironment,
info: NametagInfo,
pos: Vec3,
) {
) = with(env) {
val c = Fonts.DEFAULT_FONT_SIZE.toFloat()

val scale = 1.0F / (c * 0.15F) * ModuleNametags.scale

env.matrixStack.push()
env.matrixStack.translate(pos.x, pos.y, pos.z)
env.matrixStack.scale(scale, scale, 1.0F)
matrixStack.push()
matrixStack.translate(pos.x, pos.y, pos.z)
matrixStack.scale(scale, scale, 1.0F)

val x =
ModuleNametags.fontRenderer.draw(
Expand All @@ -72,7 +72,7 @@ class NametagRenderer {
)

// Make the model view matrix center the text when rendering
env.matrixStack.translate(-x * 0.5F, -ModuleNametags.fontRenderer.height * 0.5F, 0.00F)
matrixStack.translate(-x * 0.5F, -ModuleNametags.fontRenderer.height * 0.5F, 0.00F)

ModuleNametags.fontRenderer.commit(env, fontBuffers)

Expand All @@ -89,7 +89,7 @@ class NametagRenderer {
drawItemList(pos, info.items)
}

env.matrixStack.pop()
matrixStack.pop()
}

private fun drawItemList(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ import net.ccbluex.liquidbounce.config.Choice
import net.ccbluex.liquidbounce.config.NamedChoice
import net.ccbluex.liquidbounce.config.NoneChoice
import net.ccbluex.liquidbounce.config.ToggleableConfigurable
import net.ccbluex.liquidbounce.event.events.GameTickEvent
import net.ccbluex.liquidbounce.event.EventManager
import net.ccbluex.liquidbounce.event.events.BlockCountChangeEvent
import net.ccbluex.liquidbounce.event.events.MovementInputEvent
import net.ccbluex.liquidbounce.event.events.SimulatedTickEvent
import net.ccbluex.liquidbounce.event.handler
Expand Down Expand Up @@ -233,7 +234,8 @@ object ModuleScaffold : Module("Scaffold", Category.WORLD) {
// In this case we expand the bounding box by 0.5 in all directions and check if there is a collision
// This might cause for "Spider-like" behavior, but it's the most reliable way to check
// and usually the scaffold should start placing blocks
return world.getBlockCollisions(player,
return world.getBlockCollisions(
player,
player.boundingBox.expand(0.5, 0.0, 0.5).offset(0.0, -1.05, 0.0)
).any { shape -> shape != VoxelShapes.empty() }
}
Expand Down Expand Up @@ -279,8 +281,11 @@ object ModuleScaffold : Module("Scaffold", Category.WORLD) {
NoFallBlink.waitUntilGround = false
ScaffoldMovementPlanner.reset()
SilentHotbar.resetSlot(this)
updateRenderCount()
}

private fun updateRenderCount(count: Int? = null) = EventManager.callEvent(BlockCountChangeEvent(count))

@Suppress("unused")
val rotationUpdateHandler = handler<SimulatedTickEvent> {
NoFallBlink.waitUntilGround = true
Expand Down Expand Up @@ -402,13 +407,15 @@ object ModuleScaffold : Module("Scaffold", Category.WORLD) {

@Suppress("unused")
val tickHandler = repeatable {
updateRenderCount(blockCount)

if (player.isOnGround) {
// Placement Y is the Y coordinate of the block below the player
placementY = player.blockPos.y - 1
jumps++
}

if(mc.options.jumpKey.isPressed) {
if (mc.options.jumpKey.isPressed) {
startY = player.blockPos.y
jumps = 2
}
Expand Down Expand Up @@ -449,7 +456,8 @@ object ModuleScaffold : Module("Scaffold", Category.WORLD) {

// Does the crosshair target meet the requirements?
if (!target.doesCrosshairTargetFullFillRequirements(currentCrosshairTarget) ||
!isValidCrosshairTarget(currentCrosshairTarget)) {
!isValidCrosshairTarget(currentCrosshairTarget)
) {
return@repeatable
}

Expand Down Expand Up @@ -559,7 +567,8 @@ object ModuleScaffold : Module("Scaffold", Category.WORLD) {
if (!isTowering) {
sameYMode.getTargetedBlockPos(blockPos)?.let { return it }
} else if (towerMode.activeChoice == ScaffoldTowerMotion &&
ScaffoldTowerMotion.placeOffOnNoInput && !player.moving) {
ScaffoldTowerMotion.placeOffOnNoInput && !player.moving
) {
// Find the block closest to the player
val blocks = arrayOf(
blockPos.add(0, 0, 1),
Expand Down Expand Up @@ -632,8 +641,10 @@ object ModuleScaffold : Module("Scaffold", Category.WORLD) {
val bestMainHandSlot = findBestValidHotbarSlotForTarget()

if (bestMainHandSlot != null) {
SilentHotbar.selectSlotSilently(this, bestMainHandSlot,
ScaffoldAutoBlockFeature.slotResetDelay)
SilentHotbar.selectSlotSilently(
this, bestMainHandSlot,
ScaffoldAutoBlockFeature.slotResetDelay
)

return true
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import com.mojang.blaze3d.platform.GlStateManager
import com.mojang.blaze3d.systems.RenderSystem
import net.ccbluex.liquidbounce.render.engine.Color4b
import net.ccbluex.liquidbounce.render.engine.Vec3
import net.ccbluex.liquidbounce.render.engine.font.FontRenderer
import net.ccbluex.liquidbounce.render.engine.font.FontRendererBuffers
import net.ccbluex.liquidbounce.utils.client.mc
import net.minecraft.client.gl.ShaderProgram
import net.minecraft.client.render.*
Expand Down Expand Up @@ -58,6 +60,8 @@ class GUIRenderEnvironment(matrixStack: MatrixStack) : RenderEnvironment(matrixS
override fun relativeToCamera(pos: Vec3d): Vec3d {
return pos
}

fun FontRenderer.commit(buffer: FontRendererBuffers) = commit(this@GUIRenderEnvironment, buffer)
}

class WorldRenderEnvironment(matrixStack: MatrixStack, val camera: Camera) : RenderEnvironment(matrixStack) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ object ClientSocket {
"serverConnect",
"serverPinged",
"targetChange",
"blockCountChange",
"gameModeChange",
"componentsUpdate",
"proxyAdditionResult",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ import net.ccbluex.liquidbounce.web.theme.ThemeManager
import net.ccbluex.liquidbounce.web.theme.component.types.IntegratedComponent
import net.ccbluex.liquidbounce.web.theme.component.types.TextComponent

var components: MutableList<Component> = mutableListOf()
var customComponents: MutableList<Component> = mutableListOf(
val components: MutableList<Component> = mutableListOf()
val customComponents: MutableList<Component> = mutableListOf(
TextComponent("hello! :)", enabled = false)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ enum class ComponentType(
)),
MINIMAP("Minimap", createComponent = { MinimapComponent }),
TARGET_HUD("TargetHud"),
BLOCK_COUNTER("BlockCounter"),
KEYSTROKES("Keystrokes"),
TACO("Taco");

Expand Down
Loading