Skip to content

Commit

Permalink
(forge): WIP started working on forge support
Browse files Browse the repository at this point in the history
  • Loading branch information
lilgallon committed Dec 3, 2022
1 parent 2824f37 commit d15e141
Show file tree
Hide file tree
Showing 12 changed files with 150 additions and 27 deletions.
2 changes: 2 additions & 0 deletions core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ repositories {
}

dependencies {
implementation("com.google.guava:guava:31.1-jre")
implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.4")
}

tasks {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package dev.gallon.horsestatsmod.common

data class HorseStats(
val health: Double,
val jump: Double,
val speed: Double,
val inventory: Int?,
val owner: String?
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package dev.gallon.horsestatsmod.common

object HorseStatsLimits {
const val MIN_HEALTH = 15
const val MAX_HEALTH = 30
const val MIN_JUMP_HEIGHT = 1.25
const val MAX_JUMP_HEIGHT = 5
const val MIN_SPEED = 4.8
const val MAX_SPEED = 14.5
const val MIN_SLOTS = 3
const val MAX_SLOTS = 15
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package dev.gallon.horsestatsmod.common

object I18nKeys {
const val LOADING = "${ModMetadata.ID}.loading"
const val UNKNOWN_PLAYER = "${ModMetadata.ID}.unknownPlayer"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.gallon.horsestatsmod.common

object ModMetadata {
const val ID = "horsestatsmod"
}
33 changes: 33 additions & 0 deletions core/src/main/kotlin/dev/gallon/horsestatsmod/common/UsersCache.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.gallon.horsestatsmod.common

import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import dev.gallon.horsestatsmod.domain.I18nRepository
import dev.gallon.horsestatsmod.domain.UserRepository
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.async
import java.util.UUID
import java.util.concurrent.TimeUnit

class UsersCache(
val userRepository: UserRepository,
val i18nRepository: I18nRepository
) {
private val coroutineScope = CoroutineScope(Dispatchers.IO)

private val cache = CacheBuilder
.newBuilder()
.expireAfterWrite(6, TimeUnit.HOURS)
.build(object : CacheLoader<UUID, Deferred<String>>() {
override fun load(uuid: UUID): Deferred<String> = coroutineScope
.async {
userRepository
.fetchUsernameFromUUID(uuid)
?: i18nRepository.get(I18nKeys.UNKNOWN_PLAYER)
}
})

fun fetchUsernameFromUuid(uuid: UUID) = cache.get(uuid)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package dev.gallon.horsestatsmod.domain

interface I18nRepository {
fun get(key: String): String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package dev.gallon.horsestatsmod.domain

import java.util.UUID

interface UserRepository {
suspend fun fetchUsernameFromUUID(uuid: UUID): String?
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package dev.gallon.horsestatsmod.forge

import dev.gallon.horsestatsmod.common.HorseStats
import net.minecraft.world.entity.ai.attributes.Attributes
import net.minecraft.world.entity.animal.horse.AbstractHorse
import net.minecraft.world.entity.animal.horse.Llama
import net.minecraftforge.common.UsernameCache
import kotlin.math.pow

fun AbstractHorse.getStats() = HorseStats(
health = getAttribute(Attributes.MAX_HEALTH)?.value ?: 0.0,
jump = getAttribute(Attributes.JUMP_STRENGTH)
?.value
?.let { rawJump ->
// Convert to blocks
- 0.1817584952 * rawJump.pow(3) +
3.689713992 * rawJump.pow(2) +
2.128599134 * rawJump - 0.343930367
}
?: 0.0,
speed = getAttribute(Attributes.MOVEMENT_SPEED)
?.value
?.let { rawSpeed ->
// convert to m/s
rawSpeed * 43
}
?: 0.0,
inventory = when(this) {
is Llama -> inventoryColumns * 3
else -> null
},
owner = ownerUUID?.run(UsernameCache::getLastKnownUsername)
)
Original file line number Diff line number Diff line change
Expand Up @@ -215,31 +215,6 @@
// Minecraft.getInstance().gui.setOverlayMessage(message, false);
// }
//
// private void getHorseStats(AbstractHorse horse) {
// health = horse.getAttribute(Attributes.MAX_HEALTH).getValue();
// jumpHeight = horse.getAttribute(Attributes.JUMP_STRENGTH).getValue();
// speed = horse.getAttribute(Attributes.MOVEMENT_SPEED).getValue();
//
// final UUID ownerUUID = horse.getOwnerUUID();
// if (ownerUUID != null) {
// owner = usernameCache.getUnchecked(ownerUUID).orElse(null);
// } else {
// owner = null;
// }
//
// if (horse instanceof Llama)
// slots = ((Llama) horse).getInventoryColumns() * 3;
// else
// slots = -1;
//
// jumpHeight = (
// - 0.1817584952 * Math.pow(jumpHeight, 3) +
// 3.689713992 * Math.pow(jumpHeight, 2) +
// 2.128599134 * jumpHeight - 0.343930367
// ); // convert to blocks
// speed = speed * 43; // convert to m/s
// }
//
// private void displayStatsInHoveringText(HorseInventoryScreen guiContainer, int mouseX, int mouseY) {
//
// // todo double -> int
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,43 @@
package dev.gallon.horsestatsmod.forge

import dev.gallon.horsestatsmod.common.ModMetadata
import net.minecraft.client.Minecraft
import net.minecraft.network.chat.Component
import net.minecraft.world.entity.animal.horse.AbstractHorse
import net.minecraftforge.client.event.ContainerScreenEvent
import net.minecraftforge.common.MinecraftForge
import net.minecraftforge.event.TickEvent
import net.minecraftforge.event.entity.player.PlayerInteractEvent
import net.minecraftforge.fml.common.Mod

@Mod("horsestatsmod")
@Mod(ModMetadata.ID)
class HorseStatsMod {

init {
MinecraftForge.EVENT_BUS.addListener(this::onEntityInteractEvent)
MinecraftForge.EVENT_BUS.addListener(this::onDrawForegroundEvent)
MinecraftForge.EVENT_BUS.addListener(this::onRenderTickEvent)
}

private fun onRenderTickEvent(event: TickEvent.RenderTickEvent) {

}

private fun onEntityInteractEvent(event: PlayerInteractEvent.EntityInteractSpecific) {
val target = event.target
if (target is AbstractHorse) {
val stats = target.getStats()

Minecraft.getInstance()
.gui
.setOverlayMessage(
Component.translatable("horsestatsmod.health"),
false
)
}
}

private fun onDrawForegroundEvent(event: ContainerScreenEvent.Render.Foreground) {

}
}
2 changes: 1 addition & 1 deletion forge/src/main/resources/META-INF/mods.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ version="2.0.0" #mandatory
displayName="Horse Stats Mod" #mandatory
updateJSONURL="https://raw.githubusercontent.com/lilgallon/HorseStatsMod/versions/update.json" #optional
displayURL="https://github.com/lilgallon/HorseStatsMod" #optional
logoFile="horsestatsmod.png" #optional (jar root)
logoFile="icon.png" #optional (jar root)
# credits="" #optional
authors="nero (@lilgallon on Github)" #optional
description='''
Expand Down

0 comments on commit d15e141

Please sign in to comment.