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

2 Decimal places, larger search limit, and choosing the search direction #1

Merged
merged 3 commits into from
Feb 9, 2025
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
103 changes: 69 additions & 34 deletions src/main/kotlin/dev/lyzev/hp/client/HorsePowerClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,19 +67,45 @@ object HorsePowerClient : ClientModInitializer {
dispatcher.register(
ClientCommandManager.literal("search")
.then(
ClientCommandManager.argument("criteria", StringArgumentType.word()).suggests { _, builder ->
builder.suggest("health").suggest("speed").suggest("jump").suggest("average").buildFuture()
}.then(
ClientCommandManager.argument("amount", IntegerArgumentType.integer(1, 10))
.executes { context ->
val criteria = StringArgumentType.getString(context, "criteria")
val amount = IntegerArgumentType.getInteger(context, "amount")
executeSearch(context, criteria, amount)
})).executes { context: CommandContext<FabricClientCommandSource> ->
ClientCommandManager.argument("criteria", StringArgumentType.word())
.suggests { _, builder ->
builder.suggest("health").suggest("speed").suggest("jump").suggest("average").buildFuture()
}
.then(
ClientCommandManager.argument("amount", IntegerArgumentType.integer(1, 100))
.then(
ClientCommandManager.argument("direction", StringArgumentType.word())
.suggests { _, builder ->
builder.suggest("best").suggest("worst").buildFuture()
}
.executes { context ->
val criteria = StringArgumentType.getString(context, "criteria").lowercase()
val amount = IntegerArgumentType.getInteger(context, "amount")
val searchDirection = StringArgumentType.getString(context, "direction")
.equals("best", ignoreCase = true)

executeSearch(context, criteria, amount, searchDirection)
}
).executes { context ->
val criteria = StringArgumentType.getString(context, "criteria").lowercase()
val amount = IntegerArgumentType.getInteger(context, "amount")
val searchDirection = true
executeSearch(context, criteria, amount, searchDirection)
}
).executes { context ->
val criteria = StringArgumentType.getString(context, "criteria").lowercase()
val amount = 2
val dir = true
executeSearch(context, criteria, amount, dir)
}
)
.executes { context ->
val criteria = "average"
val amount = 2
executeSearch(context, criteria, amount)
})
val dir = true
executeSearch(context, criteria, amount, dir)
}
)
dispatcher.register(
ClientCommandManager.literal("stats").executes { context: CommandContext<FabricClientCommandSource> ->
val targetEntity = mc.targetedEntity
Expand Down Expand Up @@ -143,45 +169,54 @@ object HorsePowerClient : ClientModInitializer {
logger.info("SearchAllowedPayload registered")
}

private fun executeSearch(context: CommandContext<FabricClientCommandSource>, criteria: String, amount: Int): Int {
private fun executeSearch(context: CommandContext<FabricClientCommandSource>, criteria: String, amount: Int, searchDirection: Boolean): Int {
var criteria = criteria
if (!HorsePowerConfig.isSearchCommandAllowed) {
context.source.sendError(Text.translatable("horsepower.search.disabled"))
return 0
}
val horses =
mc.world!!.entities.filter { it is HorseEntity || it is DonkeyEntity || it is MuleEntity }.sortedBy {
val horse = it as AbstractHorseEntity
when (criteria) {
"health" -> horse.getAttributeBaseValue(EntityAttributes.MAX_HEALTH)
"speed" -> horse.getAttributeBaseValue(EntityAttributes.MOVEMENT_SPEED)
"jump" -> horse.getAttributeBaseValue(EntityAttributes.JUMP_STRENGTH)
else -> {
val movementSpeed = horse.getAttributeBaseValue(EntityAttributes.MOVEMENT_SPEED).coerceIn(
AbstractHorseEntity.MIN_MOVEMENT_SPEED_BONUS.toDouble(),
AbstractHorseEntity.MAX_MOVEMENT_SPEED_BONUS.toDouble()
) / AbstractHorseEntity.MAX_MOVEMENT_SPEED_BONUS.toDouble()
val jumpStrength = horse.getAttributeBaseValue(EntityAttributes.JUMP_STRENGTH).coerceIn(
AbstractHorseEntity.MIN_JUMP_STRENGTH_BONUS.toDouble(),
AbstractHorseEntity.MAX_JUMP_STRENGTH_BONUS.toDouble()
) / AbstractHorseEntity.MAX_JUMP_STRENGTH_BONUS.toDouble()
val health = horse.getAttributeBaseValue(EntityAttributes.MAX_HEALTH).coerceIn(
AbstractHorseEntity.MIN_HEALTH_BONUS.toDouble(),
AbstractHorseEntity.MAX_HEALTH_BONUS.toDouble()
) / AbstractHorseEntity.MAX_HEALTH_BONUS.toDouble()
movementSpeed + jumpStrength + health
}
val horse = it as AbstractHorseEntity
when (criteria) {
"health" -> horse.getAttributeBaseValue(EntityAttributes.MAX_HEALTH)
"speed" -> horse.getAttributeBaseValue(EntityAttributes.MOVEMENT_SPEED)
"jump" -> horse.getAttributeBaseValue(EntityAttributes.JUMP_STRENGTH)
else -> {
criteria = "average"
val movementSpeed = horse.getAttributeBaseValue(EntityAttributes.MOVEMENT_SPEED).coerceIn(
AbstractHorseEntity.MIN_MOVEMENT_SPEED_BONUS.toDouble(),
AbstractHorseEntity.MAX_MOVEMENT_SPEED_BONUS.toDouble()
) / AbstractHorseEntity.MAX_MOVEMENT_SPEED_BONUS.toDouble()
val jumpStrength = horse.getAttributeBaseValue(EntityAttributes.JUMP_STRENGTH).coerceIn(
AbstractHorseEntity.MIN_JUMP_STRENGTH_BONUS.toDouble(),
AbstractHorseEntity.MAX_JUMP_STRENGTH_BONUS.toDouble()
) / AbstractHorseEntity.MAX_JUMP_STRENGTH_BONUS.toDouble()
val health = horse.getAttributeBaseValue(EntityAttributes.MAX_HEALTH).coerceIn(
AbstractHorseEntity.MIN_HEALTH_BONUS.toDouble(),
AbstractHorseEntity.MAX_HEALTH_BONUS.toDouble()
) / AbstractHorseEntity.MAX_HEALTH_BONUS.toDouble()
movementSpeed + jumpStrength + health
}
}
}
return if (horses.isEmpty()) {
context.source.sendError(Text.translatable("horsepower.search.error"))
0
} else {
last = System.currentTimeMillis()
HorsePowerClient.horses.clear()
HorsePowerClient.horses += horses.takeLast(amount)
if (searchDirection) {
HorsePowerClient.horses += horses.takeLast(amount)
}else{
HorsePowerClient.horses += horses.take(amount)
}
context.source.sendFeedback(
Text.translatable(
"horsepower.search.success", HorsePowerClient.horses.size, criteria
"horsepower.search.success",
HorsePowerClient.horses.size,
Text.translatable("horsepower.search.criteria.$criteria"),
Text.translatable(if (searchDirection) "horsepower.search.best" else "horsepower.search.worst")
).withColor(Formatting.GREEN.colorValue!!)
)
1
Expand Down
15 changes: 7 additions & 8 deletions src/main/kotlin/dev/lyzev/hp/client/util/HorseStatsRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import net.minecraft.entity.passive.AbstractHorseEntity
import net.minecraft.text.Text
import net.minecraft.util.Formatting
import net.minecraft.util.Identifier
import kotlin.math.roundToInt

object HorseStatsRenderer {

Expand All @@ -37,9 +36,9 @@ object HorseStatsRenderer {
fun render(drawContext: DrawContext, entity: AbstractHorseEntity, x: Int, y: Int, mouseX: Int, mouseY: Int) {
if (!HorsePowerConfig.SHOW_INVENTORY.value) return

val speed = entity.getAttributeBaseValue(EntityAttributes.MOVEMENT_SPEED).toBPS().round(1)
val jump = entity.getAttributeBaseValue(EntityAttributes.JUMP_STRENGTH).toJump().round(1)
val health = entity.getAttributeBaseValue(EntityAttributes.MAX_HEALTH).round(1)
val speed = entity.getAttributeBaseValue(EntityAttributes.MOVEMENT_SPEED).toBPS().round(3)
val jump = entity.getAttributeBaseValue(EntityAttributes.JUMP_STRENGTH).toJump().round(3)
val health = entity.getAttributeBaseValue(EntityAttributes.MAX_HEALTH).round(3)

val speedPercentage = entity.getAttributeBaseValue(EntityAttributes.MOVEMENT_SPEED).toPercentage(AbstractHorseEntity.MAX_MOVEMENT_SPEED_BONUS)
val jumpPercentage = entity.getAttributeBaseValue(EntityAttributes.JUMP_STRENGTH).toPercentage(AbstractHorseEntity.MAX_JUMP_STRENGTH_BONUS)
Expand Down Expand Up @@ -79,7 +78,7 @@ object HorseStatsRenderer {
append(symbol.getUnit())
}
if (HorsePowerConfig.SHOW_PERCENTAGE.value) {
append(" (").append((percentage * 100).roundToInt()).append("%)")
append(" (").append((percentage * 100).round(2)).append("%)")
}
}
}
Expand All @@ -101,15 +100,15 @@ object HorseStatsRenderer {

private fun DrawContext.drawTooltip(minValue: Double, maxValue: Double, mouseX: Int, mouseY: Int) {
val hoverText = listOf(
Text.literal("Min: ${minValue.round(1)}").formatted(Formatting.DARK_RED),
Text.literal("Max: ${maxValue.round(1)}").formatted(Formatting.DARK_GREEN)
Text.literal("Min: ${minValue.round(2)}").formatted(Formatting.DARK_RED),
Text.literal("Max: ${maxValue.round(2)}").formatted(Formatting.DARK_GREEN)
)
drawTooltip(HorsePowerClient.mc.textRenderer, hoverText, mouseX, mouseY)
}

private fun DrawContext.drawAverage(speedPercentage: Double, jumpPercentage: Double, healthPercentage: Double, x: Int, y: Int) {
val average = (speedPercentage + jumpPercentage + healthPercentage) / 3
val averageFormatting = getFormatting(average)
drawTextWithShadow(HorsePowerClient.mc.textRenderer, Text.literal("Average: ${(average * 100).roundToInt()}%").formatted(averageFormatting), x, y, WHITE)
drawTextWithShadow(HorsePowerClient.mc.textRenderer, Text.literal("Average: ${(average * 100).round(2)}%").formatted(averageFormatting), x, y, WHITE)
}
}
8 changes: 7 additions & 1 deletion src/main/resources/assets/horsepower/lang/de_de.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"horsepower.search.error": "Keine Pferde in der Nähe gefunden.",
"horsepower.search.success": "Die %1$s besten Pferde wurden in der Nähe basierend auf %2$s gefunden und mit einem Leuchten hervorgehoben.",
"horsepower.search.success": "Die %1$s %3$s Pferde wurden in der Nähe gefunden basierend auf %2$s und mit einem Leuchten hervorgehoben.",
"horsepower.search.disabled": "Die Pferdesuche wurde vom Server deaktiviert.",
"horsepower.search.criteria.health": "Gesundheit",
"horsepower.search.criteria.speed": "Geschwindigkeit",
"horsepower.search.criteria.jump_strength": "Sprungkraft",
"horsepower.search.criteria.average": "Durchschnitt",
"horsepower.search.best": "besten",
"horsepower.search.worst": "schlechtesten",
"horsepower.stats.error": "Kein Pferd befindet sich derzeit in deinem Fadenkreuz.",
"horsepower.stats.success": "Pferdestatistiken: Geschwindigkeit: %1$s, Sprungkraft: %2$s, Gesundheit: %3$s",
"horsepower.options": "HorsePower Optionen",
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/assets/horsepower/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"horsepower.search.error": "No horses were found nearby.",
"horsepower.search.success": "The %1$s best horses were found nearby based on %2$s and highlighted with a glow.",
"horsepower.search.success": "The %1$s %3$s horses were found nearby based on %2$s and highlighted with a glow.",
"horsepower.search.disabled": "Horse search is disabled by the server.",
"horsepower.search.criteria.health": "health",
"horsepower.search.criteria.speed": "speed",
"horsepower.search.criteria.jump_strength": "jump strength",
"horsepower.search.criteria.average": "average",
"horsepower.search.best": "best",
"horsepower.search.worst": "worst",
"horsepower.stats.error": "No horse is currently in your crosshair.",
"horsepower.stats.success": "Horse stats: Speed: %1$s, Jump Strength: %2$s, Health: %3$s",
"horsepower.options": "HorsePower Options",
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/assets/horsepower/lang/fr_fr.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"horsepower.search.error": "Aucun cheval trouvé à proximité.",
"horsepower.search.success": "Les %1$s meilleurs chevaux ont été trouvés à proximité en fonction de %2$s et mis en évidence avec une lueur.",
"horsepower.search.success": "Les %1$s %3$s chevaux ont été trouvés à proximité en fonction de %2$s et mis en évidence avec une lueur.",
"horsepower.search.disabled": "La recherche de chevaux est désactivée par le serveur.",
"horsepower.search.criteria.health": "santé",
"horsepower.search.criteria.speed": "vitesse",
"horsepower.search.criteria.jump_strength": "force de saut",
"horsepower.search.criteria.average": "moyenne",
"horsepower.search.best": "meilleurs",
"horsepower.search.worst": "pire",
"horsepower.stats.error": "Aucun cheval n'est actuellement dans votre viseur.",
"horsepower.stats.success": "Statistiques du cheval : Vitesse : %1$s, Force de saut : %2$s, Santé : %3$s",
"horsepower.options": "Options HorsePower",
Expand Down
8 changes: 7 additions & 1 deletion src/main/resources/assets/horsepower/lang/ru_ru.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
{
"horsepower.search.error": "Поблизости не найдено лошадей.",
"horsepower.search.success": "Лучшие %1$s лошади были найдены поблизости на основе %2$s и выделены свечением.",
"horsepower.search.success": "%1$s %3$s лошади были найдены поблизости на основе %2$s и выделены свечением.",
"horsepower.search.disabled": "Поиск лошадей отключен сервером.",
"horsepower.search.criteria.health": "здоровье",
"horsepower.search.criteria.speed": "скорость",
"horsepower.search.criteria.jump_strength": "сила прыжка",
"horsepower.search.criteria.average": "среднее",
"horsepower.search.best": "лучшие",
"horsepower.search.worst": "худшие",
"horsepower.stats.error": "В данный момент в вашем прицеле нет лошади.",
"horsepower.stats.success": "Статистика лошади: Скорость: %1$s, Сила прыжка: %2$s, Здоровье: %3$s",
"horsepower.options": "Настройки HorsePower",
Expand Down
Loading