Skip to content

Commit

Permalink
v0.5 inheritance improvement, expression evaluator
Browse files Browse the repository at this point in the history
Implemented recursive descent parser to evaluate arithmetic expressions including suffixes "k", "m", "b". Updated the base feature classes and added a specific class for text overlay features that extends the base renderable feature.
  • Loading branch information
shmoe6 committed May 19, 2024
1 parent 65fd513 commit 2e87ead
Show file tree
Hide file tree
Showing 14 changed files with 367 additions and 35 deletions.
4 changes: 4 additions & 0 deletions src/main/kotlin/com/github/shmoe6/melody/Melody.kt
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
@file:SuppressWarnings("unused")
package com.github.shmoe6.melody

import com.github.shmoe6.melody.command.EvaluateCommand
import com.github.shmoe6.melody.command.MelodyCommand
import com.github.shmoe6.melody.command.SimulateCommand
import com.github.shmoe6.melody.command.TestCommand
import com.github.shmoe6.melody.features.combat.DisplayArrowCount
import com.github.shmoe6.melody.features.dungeons.DeathNotifier
import com.github.shmoe6.melody.features.farming.GardenVisitorDisplay
import com.github.shmoe6.melody.features.general.SilenceSkyBlockNotifications
import com.github.shmoe6.melody.features.inventory.HideEffectsHud
Expand Down Expand Up @@ -42,6 +44,7 @@ class Melody {

// register feature-specific event handlers
MinecraftForge.EVENT_BUS.register(Clock)
MinecraftForge.EVENT_BUS.register(DeathNotifier)
MinecraftForge.EVENT_BUS.register(DisplayArrowCount)
MinecraftForge.EVENT_BUS.register(GardenVisitorDisplay)
MinecraftForge.EVENT_BUS.register(HideEffectsHud)
Expand All @@ -51,6 +54,7 @@ class Melody {
MinecraftForge.EVENT_BUS.register(WormCooldownTimer)

// register commands
ClientCommandHandler.instance.registerCommand(EvaluateCommand)
ClientCommandHandler.instance.registerCommand(MelodyCommand)
ClientCommandHandler.instance.registerCommand(SimulateCommand)
ClientCommandHandler.instance.registerCommand(TestCommand)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.github.shmoe6.melody.command

import com.github.shmoe6.melody.handlers.math.ArithmeticExpressionHandler
import net.minecraft.command.CommandBase
import net.minecraft.command.ICommandSender
import net.minecraft.util.ChatComponentText
import java.util.*

object EvaluateCommand : CommandBase() {
override fun getCommandName(): String {
return "evaluate"
}

override fun getCommandUsage(sender: ICommandSender?): String {
return ""
}

override fun processCommand(sender: ICommandSender?, args: Array<out String>?) {

var expr = ""
args?.forEach { expr += it }

sender?.addChatMessage(ChatComponentText("§5[Melody] §f$expr evaluates to: ${ArithmeticExpressionHandler.handle(expr)}"))
}

override fun canCommandSenderUseCommand(sender: ICommandSender?): Boolean {
return true
}

override fun getCommandAliases(): MutableList<String> {
return Arrays.asList("eval", "expr", "calcexpr")
}
}
26 changes: 26 additions & 0 deletions src/main/kotlin/com/github/shmoe6/melody/core/MelodyConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,24 @@ object MelodyConfig: Vigilant(File("./config/Melody.toml")) {
)
var gardenVisitorDisplayEnabled = false

@Property(
type = PropertyType.NUMBER,
name = "Visitor Display xPos",
description = "x coordinate to render the feature at",
category = "General",
hidden = true
)
var visitorDisplayXPos = 50

@Property(
type = PropertyType.NUMBER,
name = "Visitor Display yPos",
description = "y coordinate to render the feature at",
category = "General",
hidden = true
)
var visitorDisplayYPos = 50

@Property(
type = PropertyType.SWITCH,
name = "Display Arrow Count",
Expand Down Expand Up @@ -128,6 +146,14 @@ object MelodyConfig: Vigilant(File("./config/Melody.toml")) {
)
var clockYPos = 50

@Property(
type = PropertyType.SWITCH,
name = "Dungeon Death Notifier",
description = "Plays a sound when someone dies in a dungeon.",
category = "General"
)
var dungeonDeathNotifierEnabled = false

init {
initialize()
preload()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.github.shmoe6.melody.features

import gg.essential.elementa.components.UIText


interface MelodyFeatureOverlayText : MelodyFeatureRenderable {

var xPos: Int
var yPos: Int
var selectedInEditGui: Boolean
override val mainUiComponent: UIText
}

Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
package com.github.shmoe6.melody.features

import gg.essential.elementa.UIComponent
import gg.essential.elementa.components.UIText

interface MelodyFeatureRenderable : MelodyFeature {

var xPos: Int
var yPos: Int
var mainUiComponent: UIText
val mainUiComponent: UIComponent
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ package com.github.shmoe6.melody.features.combat

import com.github.shmoe6.melody.Melody
import com.github.shmoe6.melody.core.MelodyConfig
import com.github.shmoe6.melody.features.MelodyFeatureRenderable
import com.github.shmoe6.melody.features.overlay.Clock
import com.github.shmoe6.melody.features.MelodyFeatureOverlayText
import com.github.shmoe6.melody.handlers.OverlayHandler
import gg.essential.elementa.components.UIText
import gg.essential.elementa.dsl.constrain
Expand All @@ -12,19 +11,19 @@ import net.minecraft.client.Minecraft
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent

object DisplayArrowCount : MelodyFeatureRenderable {
object DisplayArrowCount : MelodyFeatureOverlayText {

override var xPos: Int = MelodyConfig.arrowCountXPos
override var yPos: Int = MelodyConfig.arrowCountYPos
override var selectedInEditGui = false
override var mainUiComponent: UIText = UIText("Current Arrow: null (0)").constrain {
x = xPos.pixels
y = yPos.pixels
}

var selectedInEditGui = false

init {
this.mainUiComponent.onMouseClick { event ->
mainUiComponent.onMouseClick { event ->
if (OverlayHandler.editMode && isPointInside(event.absoluteX, event.absoluteY)) {
selectedInEditGui = true
}
Expand All @@ -40,8 +39,6 @@ object DisplayArrowCount : MelodyFeatureRenderable {
selectedInEditGui = false
MelodyConfig.arrowCountXPos = xPos
MelodyConfig.arrowCountYPos = yPos
// MelodyConfig.markDirty()
// MelodyConfig.writeData()
}

Melody.overlayHandler.overlay.addToScreen(this)
Expand All @@ -57,12 +54,12 @@ object DisplayArrowCount : MelodyFeatureRenderable {
}

val numArrows = player?.inventory?.mainInventory?.get(8)?.getTooltip(player, false)?.get(5)
(this.mainUiComponent as UIText).setText("$numArrows")
mainUiComponent.setText("$numArrows")
}

private fun clearText() {
if ((this.mainUiComponent as UIText).getText() != "") {
(this.mainUiComponent as UIText).setText("")
if (mainUiComponent.getText() != "") {
mainUiComponent.setText("")
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.github.shmoe6.melody.features.dungeons

import com.github.shmoe6.melody.Melody
import com.github.shmoe6.melody.core.MelodyConfig
import com.github.shmoe6.melody.features.MelodyFeature
import net.minecraft.client.Minecraft
import net.minecraftforge.client.event.ClientChatReceivedEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent

object DeathNotifier : MelodyFeature {

@SubscribeEvent
fun onDeath(event: ClientChatReceivedEvent) {
if (!isFeatureEnabled() || Melody.currentWorld != "The Catacombs") return

// TODO: play sound, add functionality
}

override fun isFeatureEnabled(): Boolean {
return MelodyConfig.dungeonDeathNotifierEnabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,21 @@ package com.github.shmoe6.melody.features.farming
import com.github.shmoe6.melody.Melody
import com.github.shmoe6.melody.core.MelodyConfig
import com.github.shmoe6.melody.event.PacketReceivedEvent
import com.github.shmoe6.melody.features.MelodyFeatureOverlayText
import com.github.shmoe6.melody.handlers.OverlayHandler
import gg.essential.elementa.components.UIText
import gg.essential.elementa.dsl.constrain
import gg.essential.elementa.dsl.pixels
import net.minecraft.client.Minecraft

import net.minecraft.network.play.server.S2DPacketOpenWindow
import net.minecraft.network.play.server.S30PacketWindowItems
import net.minecraft.util.StringUtils
import net.minecraftforge.client.event.ClientChatReceivedEvent
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent


object GardenVisitorDisplay {
object GardenVisitorDisplay : MelodyFeatureOverlayText {

// represents whether we want the contents of the gui or not (dont want to handle every time)
private var awaitS30PacketWindowItems = false
Expand All @@ -32,6 +36,36 @@ object GardenVisitorDisplay {
"Shaggy", "Shifty", "Sirius", "Spaceman", "Stella", "Tammy", "Tarwen", "Terry", "Tia the Fairy", "Tom",
"Trevor", "Vex", "Weaponsmith", "Wizard", "Xalx", "Zog")

override var xPos: Int = MelodyConfig.clockXPos
override var yPos: Int = MelodyConfig.clockYPos
override var selectedInEditGui = false
override var mainUiComponent: UIText = UIText("Current Visitor: null").constrain {
x = xPos.pixels
y = yPos.pixels
}

init {
mainUiComponent.onMouseClick { event ->
if (OverlayHandler.editMode && isPointInside(event.absoluteX, event.absoluteY)) {
selectedInEditGui = true
}
}.onMouseDrag { mouseX, mouseY, _ ->
if (OverlayHandler.editMode && selectedInEditGui) {
xPos = (mouseX + this.getLeft()).toInt()
yPos = (mouseY + this.getTop()).toInt()
setX(xPos.pixels)
setY(yPos.pixels)
OverlayHandler.editMade = true
}
}.onMouseRelease {
selectedInEditGui = false
MelodyConfig.clockXPos = xPos
MelodyConfig.clockYPos = yPos
}

Melody.overlayHandler.overlay.addToScreen(this)
}

@SubscribeEvent
fun onPacketReceived(event: PacketReceivedEvent) {
if (!MelodyConfig.gardenVisitorDisplayEnabled) return
Expand All @@ -50,26 +84,34 @@ object GardenVisitorDisplay {
// index 29 is where the accept offer button is in the garden
val acceptOfferItem = event.packet.itemStacks[29]
// line 3 (index 2) of the tooltip has the required items for the garden request
val tooltip = acceptOfferItem.getTooltip(Minecraft.getMinecraft().thePlayer, false)[2]
var tooltip = ""

activeGardenOffer = StringUtils.stripControlCodes(tooltip)
if (acceptOfferItem != null) {
tooltip = acceptOfferItem.getTooltip(Minecraft.getMinecraft().thePlayer, false)[2]
}

mainUiComponent.setText("Current request: ${StringUtils.stripControlCodes(tooltip)}")
}
}

@SubscribeEvent
fun onRenderTick(event: TickEvent.RenderTickEvent) {
if (!MelodyConfig.gardenVisitorDisplayEnabled || activeGardenOffer == null) return

Minecraft.getMinecraft().fontRendererObj.drawStringWithShadow(activeGardenOffer, 30.0F, 30.0F, 0xdcbeb8)
}
// @SubscribeEvent
// fun onRenderTick(event: TickEvent.RenderTickEvent) {
// if (!MelodyConfig.gardenVisitorDisplayEnabled || activeGardenOffer == null) return
//
// Minecraft.getMinecraft().fontRendererObj.drawStringWithShadow(activeGardenOffer, 30.0F, 30.0F, 0xdcbeb8)
// }

@SubscribeEvent
fun onClientChatReceived(event: ClientChatReceivedEvent) {
if (!MelodyConfig.gardenVisitorDisplayEnabled || activeGardenOffer == null) return

// stop displaying offer information after accepted
if (StringUtils.stripControlCodes(event.message.formattedText).contains("OFFER ACCEPTED")) {
activeGardenOffer = null
mainUiComponent.setText("Current Request: null")
}
}

override fun isFeatureEnabled(): Boolean{
return MelodyConfig.gardenVisitorDisplayEnabled
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,29 @@ package com.github.shmoe6.melody.features.overlay

import com.github.shmoe6.melody.Melody
import com.github.shmoe6.melody.core.MelodyConfig
import com.github.shmoe6.melody.features.MelodyFeatureRenderable
import com.github.shmoe6.melody.features.MelodyFeatureOverlayText
import com.github.shmoe6.melody.handlers.OverlayHandler
import gg.essential.elementa.components.UIText
import gg.essential.elementa.dsl.constrain
import gg.essential.elementa.dsl.pixels
import net.minecraft.client.Minecraft

import net.minecraftforge.fml.common.eventhandler.SubscribeEvent
import net.minecraftforge.fml.common.gameevent.TickEvent
import org.lwjgl.input.Mouse
import java.time.ZoneId
import java.time.ZonedDateTime

object Clock : MelodyFeatureRenderable {
object Clock : MelodyFeatureOverlayText {

private var displayedTime: String = ZonedDateTime.now(ZoneId.systemDefault()).toString().substring(11, 16)

override var xPos: Int = MelodyConfig.clockXPos
override var yPos: Int = MelodyConfig.clockYPos
override var selectedInEditGui = false
override var mainUiComponent: UIText = UIText(displayedTime).constrain {
x = xPos.pixels
y = yPos.pixels
}

var selectedInEditGui = false

init {
this.mainUiComponent.onMouseClick { event ->
if (OverlayHandler.editMode && isPointInside(event.absoluteX, event.absoluteY)) {
Expand Down Expand Up @@ -62,7 +59,7 @@ object Clock : MelodyFeatureRenderable {
this.displayedTime = currentTime
}

(this.mainUiComponent as UIText).setText(this.displayedTime)
mainUiComponent.setText(this.displayedTime)
}

override fun isFeatureEnabled(): Boolean {
Expand Down
Loading

0 comments on commit 2e87ead

Please sign in to comment.