Skip to content

Commit

Permalink
Add 'placeholder-option' and 'placeholder-string-format'
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Dec 2, 2024
1 parent 3746df2 commit 2c8cddf
Show file tree
Hide file tree
Showing 29 changed files with 257 additions and 102 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.jetbrains.annotations.NotNull;

/**
* HudPlayer quit event.
* player quit event.
*/
@Getter
public class HudPlayerQuitEvent extends PlayerEvent implements BetterHudEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.jetbrains.annotations.NotNull;

/**
* HudPlayer update event.
* player update event.
*/
@Getter
public class HudUpdateEvent extends PlayerEvent implements BetterHudEvent {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ public interface Popup extends HudObject {
@Nullable PopupUpdater show(@NotNull UpdateEvent reason, @NotNull HudPlayer player);

/**
* Hides popup to some hudPlayer.
* @param hudPlayer target HudPlayer
* Hides popup to some player.
* @param player target player
* @return whether to success
*/
default boolean hide(@NotNull HudPlayer hudPlayer) {
var group = hudPlayer.getPopupGroupIteratorMap().remove(getGroupName());
default boolean hide(@NotNull HudPlayer player) {
var group = player.getPopupGroupIteratorMap().remove(getGroupName());
if (group != null) {
group.clear();
return true;
Expand All @@ -46,7 +46,7 @@ default boolean hide(@NotNull HudPlayer hudPlayer) {

/**
* Gets a last index of popup.
* It equals getMaxStackSize() - 1
* It equals getMaxStack() - 1
* @return last index
*/
default int getLastIndex() {
Expand Down
4 changes: 2 additions & 2 deletions bootstrap/bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ dependencies {
compileOnly("com.sk89q.worldedit:worldedit-bukkit:7.4.0-SNAPSHOT")
compileOnly("com.sk89q.worldguard:worldguard-bukkit:7.1.0-SNAPSHOT")
compileOnly("com.github.MilkBowl:VaultAPI:1.7.1")
compileOnly("com.github.SkriptLang:Skript:2.9.4")
compileOnly("com.github.SkriptLang:Skript:2.9.5")
compileOnly("net.skinsrestorer:skinsrestorer-api:15.4.4")
compileOnly("com.alessiodp.parties:parties-bukkit:3.2.16")
compileOnly("com.nexomc:nexo:0.1.0-dev.62")
compileOnly("com.nexomc:nexo:0.1.0-dev.73")
compileOnly("io.th0rgal:oraxen:1.185.0")
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,12 +202,12 @@ class BukkitBootstrapImpl : BukkitBootstrap, JavaPlugin() {
if (pluginManager.isPluginEnabled("GPS")) PlayerManagerImpl.addLocationProvider(GPSLocationProvider())
pluginManager.registerEvents(object : Listener {
@EventHandler(priority = EventPriority.HIGHEST)
fun join(e: PlayerJoinEvent) {
register(e.player)
fun PlayerJoinEvent.join() {
register(player)
}
@EventHandler
fun quit(e: PlayerQuitEvent) {
val player = e.player
fun PlayerQuitEvent.quit() {
val player = player
PlayerManagerImpl.removeHudPlayer(player.uniqueId)?.let {
it.cancel()
HudPlayerQuitEvent(it).call()
Expand Down Expand Up @@ -255,8 +255,8 @@ class BukkitBootstrapImpl : BukkitBootstrap, JavaPlugin() {
warn("Download: https://www.spigotmc.org/resources/115559")
Bukkit.getPluginManager().registerEvents(object : Listener {
@EventHandler
fun join(e: PlayerJoinEvent) {
val player = e.player
fun PlayerJoinEvent.join() {
val player = player
if (player.isOp && ConfigManagerImpl.versionCheck) {
val audience = audiences.player(player)
audience.info("New BetterHud version found: $body")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,11 @@ class NexoCompatibility : Compatibility {
(BOOTSTRAP as BukkitBootstrapImpl).skipInitialReload = true
registerListener(object : Listener {
@EventHandler
fun generate(event: NexoPrePackGenerateEvent) {
fun NexoPrePackGenerateEvent.generate() {
when (val state = PLUGIN.reload()) {
is Success -> {
state.resourcePack.forEach {
event.addUnknownFile(it.key, it.value)
addUnknownFile(it.key, it.value)
}
info("Successfully merged with Nexo: (${state.time} ms)")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package kr.toxicity.hud.bootstrap.bukkit.compatibility.oraxen

import io.th0rgal.oraxen.api.events.OraxenPackGeneratedEvent
import io.th0rgal.oraxen.utils.VirtualFile
import kr.toxicity.hud.api.BetterHudAPI
import kr.toxicity.hud.api.listener.HudListener
import kr.toxicity.hud.api.placeholder.HudPlaceholder
import kr.toxicity.hud.api.plugin.ReloadState.*
Expand Down Expand Up @@ -35,10 +34,10 @@ class OraxenCompatibility : Compatibility {
override fun start() {
registerListener(object : Listener {
@EventHandler
fun generate(event: OraxenPackGeneratedEvent) {
fun OraxenPackGeneratedEvent.generate() {
when (val state = PLUGIN.reload()) {
is Success -> {
val output = event.output
val output = output
state.resourcePack.forEach {
output.add(
VirtualFile(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,16 @@ class BukkitItemModule : BukkitModule {
override fun start() {
registerListener(object : Listener {
@EventHandler
fun drop(e: PlayerDropItemEvent) {
UpdateItemEvent(e.player, e.itemDrop.itemStack, e).call()
fun PlayerDropItemEvent.drop() {
UpdateItemEvent(player, itemDrop.itemStack, this).call()
}
@EventHandler
fun pickup(e: EntityPickupItemEvent) {
val entity = e.entity
fun EntityPickupItemEvent.pickup() {
val entity = entity
if (entity is Player) UpdateItemEvent(
entity,
e.item.itemStack,
e
item.itemStack,
this
).call()
}
})
Expand Down
22 changes: 22 additions & 0 deletions changelog/1.10.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,28 @@ test_head:
y: 3.0
static-scale: true
```
- Add 'placeholder-option' and 'placeholder-string-format'
```yaml
test_text:
texts:
1:
name: unifont
pattern: |
[name]
[health_percentage]%
placeholder-option:
evaluate: t * 100
join: " "
placeholder-string-format:
number: "#,###"
background: test
align: center
line-align: center
scale: 0.5
line: 5
split-width: 300
y: 32
```
- Add these option in config.yml
```yaml
disable-legacy-offset: false #If this is true, a correct pixel offset is provided.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import kr.toxicity.hud.manager.ConfigManagerImpl
import kr.toxicity.hud.pack.PackGenerator
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.location.GuiLocation
import kr.toxicity.hud.placeholder.PlaceholderSource
import kr.toxicity.hud.shader.HudShader
import kr.toxicity.hud.shader.RenderScale
import kr.toxicity.hud.shader.ShaderProperty
Expand All @@ -30,7 +31,7 @@ class CircleCompass(
override val path: String,
private val internalName: String,
section: YamlObject
) : CompassImpl {
) : CompassImpl, PlaceholderSource by PlaceholderSource.Impl(section) {
companion object {
private val defaultColorEquation = TEquation("255").run {
ColorEquation(
Expand Down Expand Up @@ -68,7 +69,7 @@ class CircleCompass(
)
private var array: JsonArray? = JsonArray()
private val images = CompassImage(assets, section["file"]?.asObject().ifNull("file value not set."))
private val conditions = section.toConditions() build UpdateEvent.EMPTY
private val conditions = section.toConditions(this) build UpdateEvent.EMPTY
private val isDefault = ConfigManagerImpl.defaultCompass.contains(internalName) || section.getAsBoolean("default", false)


Expand Down
7 changes: 4 additions & 3 deletions dist/src/main/kotlin/kr/toxicity/hud/element/ImageElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import kr.toxicity.hud.manager.ListenerManagerImpl
import kr.toxicity.hud.manager.PlaceholderManagerImpl
import kr.toxicity.hud.placeholder.Conditions
import kr.toxicity.hud.placeholder.ConditionSource
import kr.toxicity.hud.placeholder.PlaceholderSource
import kr.toxicity.hud.util.ifNull

class ImageElement(
Expand All @@ -18,7 +19,7 @@ class ImageElement(
val image: List<NamedLoadedImage>,
val type: ImageType,
setting: YamlObject
) : HudElement, ConditionSource by ConditionSource.Impl(setting) {
) : HudElement, ConditionSource by ConditionSource.Impl(setting), PlaceholderSource by PlaceholderSource.Impl(setting) {
val listener = setting["listener"]?.asObject()?.let {
ListenerManagerImpl.getListener(it)
}
Expand Down Expand Up @@ -48,11 +49,11 @@ class ImageElement(
}

val follow = setting["follow"]?.asString()?.let {
PlaceholderManagerImpl.find(it).apply {
PlaceholderManagerImpl.find(it, this).apply {
if (!java.lang.String::class.java.isAssignableFrom(clazz)) throw RuntimeException("This placeholder is not a string in image $name: $it")
}
}
val childrenMapper = setting["children-mapper"]?.asObject()?.map {
it.key to Conditions.parse(it.value.asObject())
it.key to Conditions.parse(it.value.asObject(), this)
}
}
7 changes: 4 additions & 3 deletions dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import kr.toxicity.hud.manager.ConfigManagerImpl
import kr.toxicity.hud.manager.LayoutManager
import kr.toxicity.hud.pack.PackGenerator
import kr.toxicity.hud.location.GuiLocation
import kr.toxicity.hud.placeholder.PlaceholderSource
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.util.*

Expand All @@ -22,7 +23,7 @@ class HudImpl(
private val internalName: String,
resource: GlobalResource,
section: YamlObject
) : Hud, HudConfiguration {
) : Hud, HudConfiguration, PlaceholderSource by PlaceholderSource.Impl(section) {

private var imageChar = 0xCE000

Expand All @@ -37,7 +38,7 @@ class HudImpl(
var textIndex = 0

fun getOrCreateSpace(int: Int) = spaces.computeIfAbsent(int) {
(++imageChar).parseChar()
newChar
}

private val elements = section["layouts"]?.asObject().ifNull("layout configuration not set.").mapSubConfiguration { s, yamlObject ->
Expand Down Expand Up @@ -86,7 +87,7 @@ class HudImpl(
return HudObjectType.HUD
}

private val conditions = section.toConditions() build UpdateEvent.EMPTY
private val conditions = section.toConditions(this) build UpdateEvent.EMPTY

override fun getComponents(player: HudPlayer): List<WidthComponent> {
if (!conditions(player)) return emptyList()
Expand Down
5 changes: 3 additions & 2 deletions dist/src/main/kotlin/kr/toxicity/hud/layout/HudLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import kr.toxicity.hud.api.yaml.YamlObject
import kr.toxicity.hud.element.HudElement
import kr.toxicity.hud.location.PixelLocation
import kr.toxicity.hud.placeholder.ConditionSource
import kr.toxicity.hud.placeholder.PlaceholderSource
import kr.toxicity.hud.shader.RenderScale
import kr.toxicity.hud.shader.ShaderProperty

interface HudLayout<T : HudElement> : ConditionSource {
interface HudLayout<T : HudElement> : ConditionSource, PlaceholderSource {
val source: T
val outline: Boolean
val layer: Int
Expand All @@ -26,7 +27,7 @@ interface HudLayout<T : HudElement> : ConditionSource {
group: LayoutGroup,
originalLoc: PixelLocation,
yaml: YamlObject
) : HudLayout<T>, ConditionSource by ConditionSource.Impl(source, yaml) + group {
) : HudLayout<T>, ConditionSource by source + ConditionSource.Impl(yaml) + group, PlaceholderSource by PlaceholderSource.Impl(yaml) {
override val outline: Boolean = yaml.getAsBoolean("outline", false)
override val layer: Int = yaml.getAsInt("layer", 0)
override val property: Int = ShaderProperty.properties(yaml["properties"]?.asArray())
Expand Down
4 changes: 2 additions & 2 deletions dist/src/main/kotlin/kr/toxicity/hud/layout/ImageLayout.kt
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ interface ImageLayout : HudLayout<ImageElement> {
override val scale: Double = yamlObject.getAsDouble("scale", 1.0)
override val space: Int = yamlObject.getAsInt("space", 1)
override val stack: PlaceholderBuilder<*>? = yamlObject["stack"]?.asString()?.let {
PlaceholderManagerImpl.find(it).ifNull("this placeholder doesn't exist: $it").apply {
PlaceholderManagerImpl.find(it, this).ifNull("this placeholder doesn't exist: $it").apply {
if (clazz != java.lang.Number::class.java) throw RuntimeException("this placeholder is not integer: $it")
}
}
override val maxStack: PlaceholderBuilder<*>? = yamlObject["max-stack"]?.asString()?.let {
PlaceholderManagerImpl.find(it).ifNull("this placeholder doesn't exist: $it").apply {
PlaceholderManagerImpl.find(it, this).ifNull("this placeholder doesn't exist: $it").apply {
if (clazz != java.lang.Number::class.java) throw RuntimeException("this placeholder is not integer: $it")
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import kr.toxicity.hud.api.listener.HudListener
import kr.toxicity.hud.api.manager.ListenerManager
import kr.toxicity.hud.api.update.UpdateEvent
import kr.toxicity.hud.api.yaml.YamlObject
import kr.toxicity.hud.placeholder.PlaceholderSource
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.util.ifNull
import net.kyori.adventure.audience.Audience
Expand All @@ -14,8 +15,9 @@ object ListenerManagerImpl : BetterHudManager, ListenerManager {

private val listenerMap = mutableMapOf<String, (YamlObject) -> (UpdateEvent) -> HudListener>(
"placeholder" to placeholder@ { c ->
val v = PlaceholderManagerImpl.find(c["value"]?.asString().ifNull("value value not set."))
val m = PlaceholderManagerImpl.find(c["max"]?.asString().ifNull("max value not set."))
val source = PlaceholderSource.Impl(c)
val v = PlaceholderManagerImpl.find(c["value"]?.asString().ifNull("value value not set."), source)
val m = PlaceholderManagerImpl.find(c["max"]?.asString().ifNull("max value not set."), source)
return@placeholder { event ->
val value = v build event
val max = m build event
Expand Down
Loading

0 comments on commit 2c8cddf

Please sign in to comment.