Skip to content

Commit

Permalink
0.1.2 - Added hud modules, moved eventListener method to extensions
Browse files Browse the repository at this point in the history
  • Loading branch information
ya-ilya committed May 9, 2023
1 parent ad1152b commit 76db1bd
Show file tree
Hide file tree
Showing 19 changed files with 221 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ object ModuleManager : ModuleContainer {
(modules + PluginManager.modules).filter { it.bind.value == key }.forEach { it.toggle() }
}

fun getModulesByCategory(category: Category): List<AbstractModule> {
return (modules + PluginManager.modules).filter { it.category == category }
fun getModulesByCategory(category: Category, vararg exclude: AbstractModule): List<AbstractModule> {
return (modules + PluginManager.modules)
.filter { it.category == category }
.filter { it !in exclude }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.progreso.api.module

/**
* Hud module abstract class
*
* @param name Hud module name
* @param description Hud module description
* @param category Hud module category
*/
abstract class AbstractHudModule(
name: String,
description: String,
category: Category
) : AbstractModule(name, description, category) {
var dragging = false
var dragX = 0
var dragY = 0

var x by setting("X", 0, 0..Int.MAX_VALUE) { false }
var y by setting("Y", 0, 0..Int.MAX_VALUE) { false }

open var width: Int = 0
open var height: Int = 0

abstract fun render()

fun isHover(x: Int, y: Int) =
x > this.x && x < this.x + width && y > this.y && y < this.y + height
}
9 changes: 7 additions & 2 deletions progreso-client/src/main/kotlin/org/progreso/client/Client.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.progreso.client.accessors.ChatAccessor
import org.progreso.client.accessors.EventAccessor
import org.progreso.client.accessors.LoggerAccessor
import org.progreso.client.gui.ClickGUI
import org.progreso.client.gui.HudEditor
import org.progreso.client.manager.Managers
import org.reflections.Reflections

Expand All @@ -28,7 +29,10 @@ class Client {
val EVENT_BUS = EventBus()

@JvmField
val CLICK_GUI: ClickGUI = ClickGUI()
val CLICK_GUI = ClickGUI()

@JvmField
val HUD_EDITOR = HudEditor()

@JvmStatic
fun initialize() {
Expand All @@ -55,8 +59,9 @@ class Client {
LOGGER.info("Initializing client managers...")
Managers.initialize()

LOGGER.info("Initializing client ClickGUI...")
LOGGER.info("Initializing client guis...")
CLICK_GUI.initialize()
HUD_EDITOR.initialize()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.progreso.client.events

import net.minecraft.client.Minecraft
import org.progreso.api.event.Event
import org.progreso.api.event.EventListener
import org.progreso.api.event.EventPriority
import org.progreso.client.Client

val mc: Minecraft = Minecraft.getMinecraft()

@Suppress("RedundantSamConstructor")
inline fun <reified T : Event> Any.eventListener(crossinline block: (T) -> Unit) {
Client.EVENT_BUS.registerListener(
instanceClass = javaClass,
eventClass = T::class.java,
priority = EventPriority.NORMAL,
listener = EventListener<T> {
block(it)
}
)
}

inline fun <reified T : Event> Any.safeEventListener(crossinline block: (T) -> Unit) {
eventListener<T> {
if (mc.player != null && mc.world != null) {
block(it)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ open class ClickGUI : GuiScreen() {
val MODULE by lazy { ModuleManager.getModuleByClass(ClickGUI::class) }
}

private var components = mutableListOf<AbstractComponent>()
protected var components = mutableListOf<AbstractComponent>()

fun initialize() {
open fun initialize() {
var x = 10

for (category in Category.values()) {
for (category in Category.values().filter { it != Category.Hud }) {
components.add(Window(x, 10, COMPONENT_WIDTH).apply {
x += COMPONENT_WIDTH + 10

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.progreso.client.gui

import org.progreso.api.managers.ModuleManager
import org.progreso.api.module.AbstractHudModule
import org.progreso.client.gui.component.components.CategoryComponent
import org.progreso.client.module.Category

class HudEditor : ClickGUI() {
private companion object {
val HUD_MODULES by lazy {
ModuleManager.getModulesByCategory(Category.Hud)
.filterIsInstance<AbstractHudModule>()
}
}

override fun initialize() {
components.add(Window(10, 10, COMPONENT_WIDTH).apply {
x += COMPONENT_WIDTH + 10

this.components.add(
CategoryComponent(
Category.Hud,
COMPONENT_HEIGHT,
this
)
)
})
}

override fun drawScreen(mouseX: Int, mouseY: Int, partialTicks: Float) {
super.drawScreen(mouseX, mouseY, partialTicks)

HUD_MODULES.filter { it.dragging }.forEach {
it.x = mouseX - it.dragX
it.y = mouseY - it.dragY
}
}

override fun mouseClicked(mouseX: Int, mouseY: Int, mouseButton: Int) {
super.mouseClicked(mouseX, mouseY, mouseButton)

HUD_MODULES.filter { it.isHover(mouseX, mouseY) }.forEach {
it.dragging = true
it.dragX = mouseX - it.x
it.dragY = mouseY - it.y
}
}

override fun mouseReleased(mouseX: Int, mouseY: Int, state: Int) {
super.mouseReleased(mouseX, mouseY, state)

HUD_MODULES.forEach { it.dragging = false }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ enum class Category : Category {
Movement,
Render,
Misc,
Client
Client,
Hud
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.progreso.client.module

import net.minecraft.client.Minecraft
import net.minecraftforge.client.event.RenderGameOverlayEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import org.progreso.api.module.AbstractHudModule
import org.progreso.client.Client
import org.progreso.client.events.client.ModuleEvent
import org.progreso.client.gui.ClickGUI
import org.progreso.client.gui.HudEditor
import org.progreso.client.util.Render2DUtil.drawRect

abstract class HudModule(
name: String,
description: String,
category: Category
) : AbstractHudModule(name, description, category) {
constructor(name: String, category: Category) : this(name, "", category)

protected companion object {
val mc: Minecraft = Minecraft.getMinecraft()
}

@SubscribeEvent
fun onRender(event: RenderGameOverlayEvent.Text) {
if (mc.currentScreen is HudEditor) {
drawRect(x, y, width, height, ClickGUI.DEFAULT_RECT_COLOR)
}

render()
}

override fun onEnable() {
if (Client.EVENT_BUS.post(ModuleEvent(this))) {
return
}
}

override fun onDisable() {
if (Client.EVENT_BUS.post(ModuleEvent(this))) {
return
}
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package org.progreso.client.module

import net.minecraft.client.Minecraft
import org.progreso.api.event.Event
import org.progreso.api.event.EventListener
import org.progreso.api.event.EventPriority
import org.progreso.api.module.AbstractModule
import org.progreso.client.Client
import org.progreso.client.events.client.ModuleEvent
Expand Down Expand Up @@ -45,24 +42,4 @@ abstract class Module(
protected fun onDisable(block: () -> Unit) {
disableBlock = block
}

@Suppress("RedundantSamConstructor")
protected inline fun <reified T : Event> eventListener(crossinline block: (T) -> Unit) {
Client.EVENT_BUS.registerListener(
instanceClass = javaClass,
eventClass = T::class.java,
priority = EventPriority.NORMAL,
listener = EventListener<T> {
block(it)
}
)
}

protected inline fun <reified T : Event> safeEventListener(crossinline block: (T) -> Unit) {
eventListener<T> {
if (mc.player != null && mc.world != null) {
block(it)
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package org.progreso.client.module.modules.client

import org.progreso.client.Client
import org.progreso.client.module.Category
import org.progreso.client.module.Module

class HudClickGUI : Module("HudClickGUI", Category.Client) {
init {
onEnable {
mc.displayGuiScreen(Client.HUD_EDITOR)
toggle()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import org.progreso.api.Api
import org.progreso.client.events.client.ModuleEvent
import org.progreso.client.events.entity.EntityDeathEvent
import org.progreso.client.events.player.TotemPopEvent
import org.progreso.client.events.safeEventListener
import org.progreso.client.manager.managers.minecraft.CombatManager
import org.progreso.client.module.Category
import org.progreso.client.module.Module
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.minecraft.entity.EntityLivingBase
import net.minecraft.network.play.client.CPacketPlayer
import net.minecraft.network.play.client.CPacketUseEntity
import org.progreso.api.managers.ModuleManager
import org.progreso.client.events.eventListener
import org.progreso.client.events.network.PacketEvent
import org.progreso.client.mixin.mixins.accessors.IEntity
import org.progreso.client.module.Category
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.minecraft.entity.EntityLiving
import net.minecraft.entity.EntityLivingBase
import net.minecraft.item.ItemSword
import org.progreso.client.events.misc.TickEvent
import org.progreso.client.events.safeEventListener
import org.progreso.client.module.Category
import org.progreso.client.module.Module
import org.progreso.client.util.EntityUtil.EntityType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import net.minecraft.client.gui.inventory.GuiContainer
import net.minecraft.init.Items
import net.minecraft.item.Item
import org.progreso.client.events.misc.TickEvent
import org.progreso.client.events.safeEventListener
import org.progreso.client.module.Category
import org.progreso.client.module.Module
import org.progreso.client.util.InventoryUtil
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package org.progreso.client.module.modules.hud

import org.progreso.client.manager.managers.render.TextRenderManager
import org.progreso.client.manager.managers.render.TextRenderManager.getStringWidth
import org.progreso.client.module.Category
import org.progreso.client.module.HudModule
import org.progreso.client.util.Render2DUtil.drawString
import java.awt.Color

class Watermark : HudModule("Watermark", Category.Hud) {
private val color by setting("Color", Color.RED)

init {
x = 10
y = 10
width = getStringWidth("Progreso Client") + 2
height = TextRenderManager.height + 6
}

override fun render() {
drawString("Progreso Client", x + 1, y + 2, color)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import net.minecraft.init.Enchantments
import net.minecraft.item.ItemStack
import net.minecraft.util.math.BlockPos
import org.progreso.client.events.block.DamageBlockEvent
import org.progreso.client.events.safeEventListener
import org.progreso.client.module.Category
import org.progreso.client.module.Module
import org.progreso.client.util.InventoryUtil
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.progreso.client.module.modules.movement

import org.progreso.client.events.misc.TickEvent
import org.progreso.client.events.safeEventListener
import org.progreso.client.module.Category
import org.progreso.client.module.Module

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.progreso.client.module.modules.render

import org.progreso.client.events.misc.TickEvent
import org.progreso.client.events.safeEventListener
import org.progreso.client.module.Category
import org.progreso.client.module.Module

Expand All @@ -12,12 +13,12 @@ class FullBright : Module("FullBright", Category.Render) {
oldGamma = mc.gameSettings.gammaSetting
}

safeEventListener<TickEvent> { _ ->
mc.gameSettings.gammaSetting = 1000.0f
}

onDisable {
mc.gameSettings.gammaSetting = oldGamma
}

safeEventListener<TickEvent> { _ ->
mc.gameSettings.gammaSetting = 1000.0f
}
}
}
2 changes: 1 addition & 1 deletion progreso-client/src/main/resources/mcmod.info
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"modid": "progreso",
"name": "Progreso Client",
"description": "",
"version": "0.1.1",
"version": "0.1.2",
"mcversion": "1.12.2",
"logoFile": "",
"url": "https://github.com/ya-ilya/progreso",
Expand Down

0 comments on commit 76db1bd

Please sign in to comment.