Skip to content

Commit

Permalink
add search command functionality with server configuration support
Browse files Browse the repository at this point in the history
  • Loading branch information
Lyzev committed Jan 3, 2025
1 parent 2e2295f commit 9f0639d
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 1 deletion.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Done to increase the memory available to gradle.
org.gradle.jvmargs=-Xmx1G
# Mod Properties
mod_version=0.0.0
mod_version=1.0.0
maven_group=dev.lyzev
archives_base_name=horsepower
28 changes: 28 additions & 0 deletions src/main/kotlin/dev/lyzev/hp/HorsePower.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.context.CommandContext
import dev.lyzev.hp.modmenu.HorsePowerConfig
import dev.lyzev.hp.modmenu.HorsePowerConfigManager
import dev.lyzev.hp.payload.SearchAllowedPayload
import dev.lyzev.hp.util.HorseStatsRenderer.render
import dev.lyzev.hp.util.round
import dev.lyzev.hp.util.toBPS
Expand All @@ -30,14 +31,18 @@ import net.fabricmc.api.ClientModInitializer
import net.fabricmc.fabric.api.client.command.v2.ClientCommandManager
import net.fabricmc.fabric.api.client.command.v2.ClientCommandRegistrationCallback
import net.fabricmc.fabric.api.client.command.v2.FabricClientCommandSource
import net.fabricmc.fabric.api.client.networking.v1.C2SPlayChannelEvents
import net.fabricmc.fabric.api.client.networking.v1.ClientConfigurationNetworking
import net.fabricmc.fabric.api.client.rendering.v1.HudRenderCallback
import net.fabricmc.fabric.api.networking.v1.PayloadTypeRegistry
import net.minecraft.client.MinecraftClient
import net.minecraft.entity.Entity
import net.minecraft.entity.attribute.EntityAttributes
import net.minecraft.entity.passive.AbstractHorseEntity
import net.minecraft.entity.passive.DonkeyEntity
import net.minecraft.entity.passive.HorseEntity
import net.minecraft.entity.passive.MuleEntity
import net.minecraft.network.packet.CustomPayload
import net.minecraft.text.Text
import net.minecraft.util.Formatting

Expand Down Expand Up @@ -111,9 +116,32 @@ object HorsePower : ClientModInitializer {
}
}
})

C2SPlayChannelEvents.REGISTER.register(C2SPlayChannelEvents.Register { handler, sender, client, channels ->
HorsePowerConfig.isSearchCommandAllowed = true
println("Search command is allowed!!!!!!!!")
})

PayloadTypeRegistry.configurationS2C().register(SearchAllowedPayload.ID, SearchAllowedPayload.CODEC)

ClientConfigurationNetworking.registerGlobalReceiver(CustomPayload.Id(HorsePowerConfig.SEARCH_ALLOWED_PACKET_ID)) { payload: SearchAllowedPayload, context ->
context.client().execute {
HorsePowerConfig.isSearchCommandAllowed = payload.allowed
if (!payload.allowed) {
mc.inGameHud.chatHud.addMessage(
Text.translatable("horsepower.search.disabled").formatted(Formatting.RED)
)
}
println("Search command is ${if (payload.allowed) "allowed" else "disabled"}!!!!!!!!!")
}
}
}

private fun executeSearch(context: CommandContext<FabricClientCommandSource>, criteria: String, amount: Int): Int {
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
Expand Down
4 changes: 4 additions & 0 deletions src/main/kotlin/dev/lyzev/hp/modmenu/HorsePowerConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import com.terraformersmc.modmenu.config.FileOnlyConfig
import com.terraformersmc.modmenu.config.option.BooleanConfigOption
import com.terraformersmc.modmenu.config.option.OptionConvertable
import net.minecraft.client.option.SimpleOption
import net.minecraft.util.Identifier
import java.lang.reflect.Modifier

object HorsePowerConfig {
Expand All @@ -31,6 +32,9 @@ object HorsePowerConfig {
val SHOW_AVERAGE = BooleanConfigOption("show_average", true)
val SHOW_HUD = BooleanConfigOption("show_hud", true)

val SEARCH_ALLOWED_PACKET_ID = Identifier.of("horsepower", "search")
var isSearchCommandAllowed = true

fun asOptions(): Array<SimpleOption<*>> {
val options = ArrayList<SimpleOption<*>>()
for (field in HorsePowerConfig::class.java.declaredFields) {
Expand Down
38 changes: 38 additions & 0 deletions src/main/kotlin/dev/lyzev/hp/payload/SearchAllowedPayload.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) 2025. Lyzev
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package dev.lyzev.hp.payload

import dev.lyzev.hp.modmenu.HorsePowerConfig
import net.minecraft.network.PacketByteBuf
import net.minecraft.network.RegistryByteBuf
import net.minecraft.network.codec.PacketCodec
import net.minecraft.network.codec.PacketCodecs
import net.minecraft.network.packet.CustomPayload

@JvmRecord
data class SearchAllowedPayload(val allowed: Boolean) : CustomPayload {

override fun getId() = ID

companion object {
val ID = CustomPayload.Id<SearchAllowedPayload>(HorsePowerConfig.SEARCH_ALLOWED_PACKET_ID)
val CODEC: PacketCodec<PacketByteBuf, SearchAllowedPayload> =
PacketCodec.tuple(
PacketCodecs.BOOLEAN, SearchAllowedPayload::allowed
) { allowed: Boolean -> SearchAllowedPayload(allowed) }
}
}
1 change: 1 addition & 0 deletions src/main/resources/assets/horsepower/lang/de_de.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"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.disabled": "Die Pferdesuche wurde vom Server deaktiviert.",
"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
1 change: 1 addition & 0 deletions src/main/resources/assets/horsepower/lang/en_us.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"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.disabled": "Horse search is disabled by the server.",
"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
1 change: 1 addition & 0 deletions src/main/resources/assets/horsepower/lang/fr_fr.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"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.disabled": "La recherche de chevaux est désactivée par le serveur.",
"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
1 change: 1 addition & 0 deletions src/main/resources/assets/horsepower/lang/ru_ru.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"horsepower.search.error": "Поблизости не найдено лошадей.",
"horsepower.search.success": "Лучшие %1$s лошади были найдены поблизости на основе %2$s и выделены свечением.",
"horsepower.search.disabled": "Поиск лошадей отключен сервером.",
"horsepower.stats.error": "В данный момент в вашем прицеле нет лошади.",
"horsepower.stats.success": "Статистика лошади: Скорость: %1$s, Сила прыжка: %2$s, Здоровье: %3$s",
"horsepower.options": "Настройки HorsePower",
Expand Down

0 comments on commit 9f0639d

Please sign in to comment.