Skip to content

Commit

Permalink
Code refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Jan 3, 2025
1 parent c144a62 commit 9964ad7
Show file tree
Hide file tree
Showing 29 changed files with 54 additions and 124 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ This project implements a server-side HUD.
### Platform
- Bukkit(including Folia) 1.19-1.21.4
- Velocity 3.3-3.4
- Fabric server 1.21.4
- Fabric server 1.21.2-1.21.4

### Library
- [kotlin stdlib](https://github.com/JetBrains/kotlin): Implements better functional programming.
- [adventure](https://github.com/KyoriPowered/adventure): Implements multi-platform component.
- [bstats](https://bstats.org/getting-started/include-metrics): Implements metrics.
- [exp4j](https://github.com/fasseg/exp4j): Implements equation.
- [snakeyaml](https://github.com/snakeyaml): Implements yaml parser.
- [snakeyaml](https://github.com/snakeyaml/snakeyaml): Implements yaml parser.
- [gson](https://github.com/google/gson): Implements json parser/writer.
- [better command](https://github.com/toxicity188/BetterCommand): Implements multi-platform supporting command.
- [expiring map](https://github.com/jhalterman/expiringmap): Implements cache map.


### Dependency
Expand All @@ -48,7 +49,7 @@ Requires Java 17, 21 Eclipse Adoptium.
- Build Bukkit plugin: ./gradlew pluginJar
- Build Velocity plugin: ./gradlew velocityJar
- Build Fabric server side mod: ./gradlew fabricJar
- Build source code jar: ./gradlew sourceJar
- Build source code jar: ./gradlew sourcesJar
- Build dokka-based docs jar: ./gradlew javadocJar

### API
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ import kr.toxicity.hud.api.manager.ConfigManager
import kr.toxicity.hud.api.placeholder.HudPlaceholder
import kr.toxicity.hud.api.player.HudPlayer
import kr.toxicity.hud.api.scheduler.HudScheduler
import kr.toxicity.hud.api.update.UpdateEvent
import kr.toxicity.hud.bedrock.FloodgateAdapter
import kr.toxicity.hud.bedrock.GeyserAdapter
import kr.toxicity.hud.bootstrap.bukkit.manager.CompatibilityManager
import kr.toxicity.hud.bootstrap.bukkit.manager.ModuleManager
import kr.toxicity.hud.bootstrap.bukkit.player.HudPlayerBukkit
import kr.toxicity.hud.bootstrap.bukkit.player.head.SkinsRestorerSkinProvider
import kr.toxicity.hud.bootstrap.bukkit.player.location.GPSLocationProvider
import kr.toxicity.hud.bootstrap.bukkit.util.*
import kr.toxicity.hud.bootstrap.bukkit.util.MinecraftVersion
import kr.toxicity.hud.bootstrap.bukkit.util.bukkitPlayer
import kr.toxicity.hud.bootstrap.bukkit.util.call
import kr.toxicity.hud.bootstrap.bukkit.util.registerListener
import kr.toxicity.hud.manager.*
import kr.toxicity.hud.pack.PackType
import kr.toxicity.hud.pack.PackUploader
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kr.toxicity.hud.util.ifNull
import net.Indyuce.mmocore.MMOCore
import net.Indyuce.mmocore.api.player.PlayerData
import net.Indyuce.mmocore.api.player.stats.PlayerStats
import net.Indyuce.mmocore.skill.RegisteredSkill
import org.bukkit.entity.Player
import java.util.function.Function

Expand All @@ -24,6 +25,12 @@ class MMOCoreCompatibility : Compatibility {
return MMOCore.plugin.playerDataManager.getOrNull(uniqueId)
}

private fun PlayerData.modifier(skill: RegisteredSkill, key: String) = getSkillLevel(skill).let { level ->
profess.getSkill(skill.handler.id)?.getParameter(key, level) ?: skill.getModifier(key, level)
}

private fun skill(name: String) = MMOCore.plugin.skillManager.getSkill(name) ?: throw RuntimeException("Unable to find that skill: $name")

override val triggers: Map<String, (YamlObject) -> HudTrigger<*>>
get() = mapOf()

Expand Down Expand Up @@ -67,7 +74,7 @@ class MMOCoreCompatibility : Compatibility {
HudListener { p ->
val mmo = p.bukkitPlayer.toMMOCore() ?: return@HudListener 0.0
mmo.getBoundSkill(slot)?.let {
(mmo.cooldownMap.getCooldown(it) / it.skill.getModifier("cooldown", mmo.getSkillLevel(it.skill))).coerceAtLeast(0.0)
mmo.cooldownMap.getCooldown(it) / mmo.modifier(it.skill, "cooldown").coerceAtLeast(0.0)
} ?: 0.0
}
}
Expand All @@ -78,7 +85,7 @@ class MMOCoreCompatibility : Compatibility {
return@search { _: UpdateEvent ->
HudListener { p ->
val mmo = p.bukkitPlayer.toMMOCore() ?: return@HudListener 0.0
(mmo.cooldownMap.getCooldown("skill_" + skill.handler.id) / skill.getModifier("cooldown", mmo.getSkillLevel(skill))).coerceAtLeast(0.0)
mmo.cooldownMap.getCooldown("skill_" + skill.handler.id) / mmo.modifier(skill, "cooldown").coerceAtLeast(0.0)
}
}
}
Expand Down Expand Up @@ -211,8 +218,8 @@ class MMOCoreCompatibility : Compatibility {
val i = args[0].toInt()
Function { p ->
val mmo = p.bukkitPlayer.toMMOCore() ?: return@Function 0.0
mmo.getBoundSkill(i)?.skill?.let {
it.getModifier("cooldown", mmo.getSkillLevel(it))
mmo.getBoundSkill(i)?.let {
it.getParameter("cooldown", mmo.getSkillLevel(it.skill))
} ?: -1
}
}
Expand All @@ -223,16 +230,16 @@ class MMOCoreCompatibility : Compatibility {
val i = args[0].toInt()
Function { p ->
val mmo = p.bukkitPlayer.toMMOCore() ?: return@Function 0.0
mmo.getBoundSkill(i)?.skill?.let {
it.getModifier("cooldown", mmo.getSkillLevel(it))
mmo.getBoundSkill(i)?.let {
it.getParameter("cooldown", mmo.getSkillLevel(it.skill))
} ?: -1
}
}
.build(),
"current_cooldown_skill" to HudPlaceholder.builder<Number>()
.requiredArgsLength(1)
.function { args, _ ->
val skill = MMOCore.plugin.skillManager.getSkill(args[0]) ?: throw RuntimeException("Unable to find that skill: ${args[0]}")
val skill = skill(args[0])
Function { p ->
val mmo = p.bukkitPlayer.toMMOCore() ?: return@Function 0.0
mmo.cooldownMap.getCooldown("skill_" + skill.handler.id)
Expand All @@ -242,27 +249,27 @@ class MMOCoreCompatibility : Compatibility {
"required_mana_skill" to HudPlaceholder.builder<Number>()
.requiredArgsLength(1)
.function { args, _ ->
val skill = MMOCore.plugin.skillManager.getSkill(args[0]) ?: throw RuntimeException("Unable to find that skill: ${args[0]}")
val skill = skill(args[0])
Function { p ->
val mmo = p.bukkitPlayer.toMMOCore() ?: return@Function 0.0
skill.getModifier("mana", mmo.getSkillLevel(skill))
mmo.modifier(skill, "mana")
}
}
.build(),
"required_stamina_skill" to HudPlaceholder.builder<Number>()
.requiredArgsLength(1)
.function { args, _ ->
val skill = MMOCore.plugin.skillManager.getSkill(args[0]) ?: throw RuntimeException("Unable to find that skill: ${args[0]}")
val skill = skill(args[0])
Function { p ->
val mmo = p.bukkitPlayer.toMMOCore() ?: return@Function 0.0
skill.getModifier("stamina", mmo.getSkillLevel(skill))
mmo.modifier(skill, "stamina")
}
}
.build(),
"skill_bound_index" to HudPlaceholder.builder<Number>()
.requiredArgsLength(1)
.function { args, _ ->
val skill = MMOCore.plugin.skillManager.getSkill(args[0]) ?: throw RuntimeException("Unable to find that skill: ${args[0]}")
val skill = skill(args[0])
Function { p ->
val mmo = p.bukkitPlayer.toMMOCore() ?: return@Function 0.0
(0..8).firstOrNull {
Expand All @@ -274,7 +281,7 @@ class MMOCoreCompatibility : Compatibility {
"skill_level" to HudPlaceholder.builder<Number>()
.requiredArgsLength(1)
.function { args, _ ->
val skill = MMOCore.plugin.skillManager.getSkill(args[0]) ?: throw RuntimeException("Unable to find that skill: ${args[0]}")
val skill = skill(args[0])
Function { p ->
(p.bukkitPlayer.toMMOCore() ?: return@Function 0.0).getSkillLevel(skill)
}
Expand All @@ -283,7 +290,7 @@ class MMOCoreCompatibility : Compatibility {
"casting_slot" to HudPlaceholder.builder<Number>()
.requiredArgsLength(1)
.function { args, _ ->
val skill = MMOCore.plugin.skillManager.getSkill(args[0]) ?: throw RuntimeException("Unable to find that skill: ${args[0]}")
val skill = skill(args[0])
Function { p ->
val bar = p.bukkitPlayer.inventory.heldItemSlot
var i = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import kr.toxicity.hud.api.bukkit.event.CustomPopupEvent
import kr.toxicity.hud.api.bukkit.trigger.HudBukkitEventTrigger
import kr.toxicity.hud.api.listener.HudListener
import kr.toxicity.hud.api.placeholder.HudPlaceholder
import kr.toxicity.hud.api.player.HudPlayer
import kr.toxicity.hud.api.update.UpdateEvent
import kr.toxicity.hud.api.yaml.YamlObject
import kr.toxicity.hud.bootstrap.bukkit.module.BukkitModule
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,6 @@ data class MinecraftVersion(
val version1_19_2 = MinecraftVersion(1, 19, 2)
val version1_19_1 = MinecraftVersion(1, 19, 1)
val version1_19 = MinecraftVersion(1, 19, 0)
val version1_18_2 = MinecraftVersion(1, 18, 2)
//val version1_18_1 = MinecraftVersion(1, 18, 1)
//val version1_18 = MinecraftVersion(1, 18, 0)
//val version1_17_1 = MinecraftVersion(1, 17, 1)
//val version1_17 = MinecraftVersion(1, 17, 0)

private val comparator = Comparator.comparing { v: MinecraftVersion ->
v.first
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,25 +42,6 @@ val Player.emptySpace
}
return r
}
fun Player.storage(target : ItemStack?): Int {
if (target == null || target.type == Material.AIR) return emptySpace
val inv = inventory
val max = target.maxStackSize
return Array(36) { i ->
inv.getItem(i)?.run {
if (type == Material.AIR) max
else if (isSimilar(target)) (max - amount).coerceAtLeast(0)
else 0
} ?: max
}.sum()
}
fun Player.totalAmount(item: ItemStack): Int {
var i = 0
for (content in inventory.contents) {
if (content != null && item.isSimilar(content)) i += content.amount
}
return i
}
fun Player.storage(material: Material): Int {
if (material == Material.AIR) return emptySpace
val inv = inventory
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class FabricEntityModule : FabricModule {
"health" to {
{ event ->
event.unwrap ref@ { target: EntityEvent<*> ->
val entity = target.entity() as? LivingEntity ?: return@ref HudListener.EMPTY
val entity = target.entity()
entity.getAttribute(Attributes.MAX_HEALTH)?.value?.let { maxHealth ->
HudListener {
entity.health / maxHealth
Expand All @@ -76,7 +76,7 @@ class FabricEntityModule : FabricModule {
"health" to HudPlaceholder.of { _, u ->
u.unwrap { e: EntityEvent<*> ->
Function {
(e.entity() as? LivingEntity)?.health ?: 0.0
e.entity().health
}
}
},
Expand Down Expand Up @@ -106,14 +106,14 @@ class FabricEntityModule : FabricModule {
"max_health" to HudPlaceholder.of { _, u ->
u.unwrap { e: EntityEvent<*> ->
Function {
(e.entity() as? LivingEntity)?.maxHealth ?: 0.0
e.entity().maxHealth
}
}
},
"health_percentage" to HudPlaceholder.of { _, u ->
u.unwrap { e: EntityEvent<*> ->
Function get@ {
val entity = e.entity() as? LivingEntity ?: return@get 0.0
val entity = e.entity()
entity.health / entity.maxHealth
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import java.util.function.BiConsumer

private val PERMISSION_GETTER: (ServerPlayer, String) -> Boolean = if (FabricLoader.getInstance().isModLoaded("luckperms")) LuckPermsProvider.get().let {
{ player: ServerPlayer, perm: String ->
it.userManager.getUser(player.uuid)?.cachedData?.permissionData?.checkPermission(perm)?.asBoolean() ?: false
it.userManager.getUser(player.uuid)?.cachedData?.permissionData?.checkPermission(perm)?.asBoolean() == true
}
} else {
{ player: ServerPlayer, _: String ->
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/velocity/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ velocityPluginJson {
name = "BetterHud"
authors = listOf("toxicity")
description = "Make a hud in minecraft!"
url = "https://www.spigotmc.org/resources/115559"
url = "https://hangar.papermc.io/toxicity188/BetterHud"
}

Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ class VelocityVolatileCodeHandler : VolatileCodeHandler {
.name(packet.name?.component ?: Component.empty())
.color(BossBar.Color.entries[packet.color])
.progress(packet.percent)
.overlay(BossBar.Overlay.entries[packet.overlay])
.overlay(Overlay.entries[packet.overlay])
.flags(set)
}
fun createUpdateNamePacket(): BossBarPacket {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ allprojects {
apply(plugin = "org.jetbrains.dokka")

group = "kr.toxicity.hud"
version = "1.11.2" + (buildNumber?.let { ".$it" } ?: "")
version = "1.11.3" + (buildNumber?.let { ".$it" } ?: "")

repositories {
mavenCentral()
Expand Down
4 changes: 4 additions & 0 deletions changelog/1.11.3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# BetterHud 1.11.3

### Fix
- Fix mmocore skill reference.
2 changes: 1 addition & 1 deletion dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ class BetterHudImpl(val bootstrap: BetterHudBootstrap) : BetterHud {
it.getInputStream(ZipEntry("META-INF/MANIFEST.MF"))?.buffered()?.use { stream ->
Manifest(stream).mainAttributes.getValue(Attributes.Name("Dev-Build"))?.toBoolean()
}
} ?: false
} == true

override fun addReloadStartTask(runnable: Runnable) {
reloadStartTask += {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class DependencyInjector(version: String, dataFolder: File, private val logger:
val ucp: Any = fetchField(URLClassLoader::class.java, classLoader, "ucp")
unopenedURLs = fetchField(ucp.javaClass, ucp, "unopenedUrls") as MutableCollection<URL>
pathURLs = fetchField(ucp.javaClass, ucp, "path") as MutableCollection<URL>
} catch (e: Throwable) {
} catch (_: Throwable) {
throw RuntimeException("Unsupported jdk.")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ class ImageElement(
}
is YamlElement -> listOf(child.asString())
null -> emptyList()
else -> throw RuntimeException("Unsupported children section: $id")
}

val children by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import kr.toxicity.hud.element.TextElement
import kr.toxicity.hud.image.LocatedImage
import kr.toxicity.hud.layout.HudLayout
import kr.toxicity.hud.location.PixelLocation
import kr.toxicity.hud.manager.TextManagerImpl.TextSupplier
import kr.toxicity.hud.pack.PackGenerator
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.text.BackgroundKey
Expand All @@ -26,7 +25,6 @@ import java.io.File
import java.io.InputStreamReader
import java.util.*
import java.util.concurrent.ConcurrentHashMap
import kotlin.collections.HashSet
import kotlin.math.roundToInt

object TextManagerImpl : BetterHudManager, TextManager {
Expand Down
2 changes: 1 addition & 1 deletion dist/src/main/kotlin/kr/toxicity/hud/popup/PopupImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class PopupImpl(
TriggerManagerImpl.getTrigger(yamlObject).registerEvent { t, u ->
PlayerManagerImpl.getHudPlayer(t)?.let {
hide(it)
} ?: false
} == true
task(u, t)
}
}
Expand Down
27 changes: 0 additions & 27 deletions dist/src/main/kotlin/kr/toxicity/hud/util/Encodes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,5 @@ fun String.encodeFile(namespace: EncodeManager.EncodeNamespace): String {
if (split.size != 2) throw RuntimeException("Invaild file name: $this")
return "${split[0].encodeKey(namespace)}.${split[1]}"
}
fun String.decodeFile(): String {
val split = split('.')
if (split.size != 2) throw RuntimeException("Invaild file name: $this")
return "${split[0].decodeKey()}.${split[1]}"
}

fun String.decodeKey(): String {
return if (ConfigManagerImpl.resourcePackObfuscation) {
val sb = StringBuilder()
if (length % 2 != 0) throw RuntimeException("$this isn't encode key.")
var t = 0
fun Int.decode(): Int {
var m = this - 3
if (m < 0) m += 0xF + 1
return m.inv() and 0xF
}

val reversed = reversed()
for (i in 0..<length / 2) {
sb.append(
((Character.digit(reversed[t++], 16).decode() shl 4) or
Character.digit(reversed[t++], 16).decode()).toChar()
)
}
return sb.toString()
} else this
}

fun String.encodeKey(namespace: EncodeManager.EncodeNamespace) = if (ConfigManagerImpl.resourcePackObfuscation) EncodeManager.generateKey(namespace, this) else this
Loading

0 comments on commit 9964ad7

Please sign in to comment.