-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(forge): WIP started working on forge support
- Loading branch information
Showing
12 changed files
with
150 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
core/src/main/kotlin/dev/gallon/horsestatsmod/common/HorseStats.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
) |
12 changes: 12 additions & 0 deletions
12
core/src/main/kotlin/dev/gallon/horsestatsmod/common/HorseStatsLimits.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
6 changes: 6 additions & 0 deletions
6
core/src/main/kotlin/dev/gallon/horsestatsmod/common/I18nKeys.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
5 changes: 5 additions & 0 deletions
5
core/src/main/kotlin/dev/gallon/horsestatsmod/common/ModMetadata.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
core/src/main/kotlin/dev/gallon/horsestatsmod/common/UsersCache.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
5 changes: 5 additions & 0 deletions
5
core/src/main/kotlin/dev/gallon/horsestatsmod/domain/I18nRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
7 changes: 7 additions & 0 deletions
7
core/src/main/kotlin/dev/gallon/horsestatsmod/domain/UserRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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? | ||
} |
33 changes: 33 additions & 0 deletions
33
forge/src/main/kotlin/dev/gallon/horsestatsmod/forge/AbstractHorseExt.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 37 additions & 1 deletion
38
forge/src/main/kotlin/dev/gallon/horsestatsmod/forge/HorseStatsMod.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) { | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters