From 4272aa3bde348441983a299d2ef537c9f06a9fbf Mon Sep 17 00:00:00 2001 From: unldenis Date: Thu, 1 Feb 2024 23:11:09 +0100 Subject: [PATCH] working on reactive text lines --- .../org/holoeasy/builder/HologramBuilder.java | 14 +++-------- .../org/holoeasy/builder/HologramConfig.java | 1 - src/main/java/org/holoeasy/builder/Service.kt | 3 +-- .../builder/interfaces/PlayerFun.java | 12 --------- .../java/org/holoeasy/hologram/Hologram.kt | 4 +++ .../org/holoeasy/line/ClickableTextLine.kt | 3 +-- src/main/java/org/holoeasy/line/ITextLine.kt | 3 +-- src/main/java/org/holoeasy/line/TextLine.kt | 25 +++++++++++++------ src/test/java/ExampleKotlin.kt | 10 +++----- 9 files changed, 30 insertions(+), 45 deletions(-) delete mode 100644 src/main/java/org/holoeasy/builder/interfaces/PlayerFun.java diff --git a/src/main/java/org/holoeasy/builder/HologramBuilder.java b/src/main/java/org/holoeasy/builder/HologramBuilder.java index 29facae..a4e0ca6 100644 --- a/src/main/java/org/holoeasy/builder/HologramBuilder.java +++ b/src/main/java/org/holoeasy/builder/HologramBuilder.java @@ -2,7 +2,6 @@ import org.holoeasy.builder.interfaces.HologramConfigGroup; import org.holoeasy.builder.interfaces.HologramSetupGroup; -import org.holoeasy.builder.interfaces.PlayerFun; import org.holoeasy.config.HologramKey; import org.bukkit.Location; import org.bukkit.inventory.ItemStack; @@ -28,13 +27,6 @@ public static Hologram hologram( Hologram holo = new Hologram(key, holoConfig.location, holoConfig.loader); holo.load(holoConfig.lines.toArray(new ILine[0])); - - IHologramPool pool = key.getPool(); - if(pool != null) { - pool.takeCareOf(key, holo); - } - - return holo; } @@ -43,7 +35,7 @@ public static void config(@NotNull HologramConfigGroup configGroup) { } - public static void textline(@NotNull String text, @NotNull PlayerFun... args) { + public static void textline(@NotNull String text, @NotNull Object... args) { getInstance().textline( text, false, @@ -53,7 +45,7 @@ public static void textline(@NotNull String text, @NotNull PlayerFun... args) { ); } - public static ITextLine clickable(@NotNull String text, @NotNull PlayerFun... args) { + public static ITextLine clickable(@NotNull String text, @NotNull Object... args) { return getInstance().textline( text, true, @@ -65,7 +57,7 @@ public static ITextLine clickable(@NotNull String text, @NotNull PlayerFun... ar } public static ITextLine clickable(@NotNull String text, float minHitDistance, float maxHitDistance, - @NotNull PlayerFun... args) { + @NotNull Object... args) { return getInstance().textline( text, false, diff --git a/src/main/java/org/holoeasy/builder/HologramConfig.java b/src/main/java/org/holoeasy/builder/HologramConfig.java index 8862935..36089a3 100644 --- a/src/main/java/org/holoeasy/builder/HologramConfig.java +++ b/src/main/java/org/holoeasy/builder/HologramConfig.java @@ -2,7 +2,6 @@ import org.holoeasy.config.HologramKey; import org.bukkit.Location; -import org.holoeasy.config.HologramKey; import org.holoeasy.hologram.IHologramLoader; import org.holoeasy.hologram.TextBlockStandardLoader; import org.holoeasy.line.ILine; diff --git a/src/main/java/org/holoeasy/builder/Service.kt b/src/main/java/org/holoeasy/builder/Service.kt index e6a67d5..3f4e398 100644 --- a/src/main/java/org/holoeasy/builder/Service.kt +++ b/src/main/java/org/holoeasy/builder/Service.kt @@ -3,7 +3,6 @@ package org.holoeasy.builder import org.bukkit.inventory.ItemStack import org.holoeasy.builder.interfaces.HologramConfigGroup -import org.holoeasy.builder.interfaces.PlayerFun import org.holoeasy.hologram.TextBlockStandardLoader import org.holoeasy.line.* import org.holoeasy.reactive.MutableState @@ -29,7 +28,7 @@ object Service { @JvmOverloads fun textline( text: String, clickable: Boolean = false, minHitDistance: Float? = null, - maxHitDistance: Float? = null, args: Array? = null + maxHitDistance: Float? = null, args: Array<*>? = null ) : ITextLine { val holo = getStaticHolo() diff --git a/src/main/java/org/holoeasy/builder/interfaces/PlayerFun.java b/src/main/java/org/holoeasy/builder/interfaces/PlayerFun.java deleted file mode 100644 index 277ea10..0000000 --- a/src/main/java/org/holoeasy/builder/interfaces/PlayerFun.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.holoeasy.builder.interfaces; - -import org.bukkit.entity.Player; -import org.jetbrains.annotations.NotNull; - -@FunctionalInterface -public interface PlayerFun { - - @NotNull - Object invoke(Player player); - -} diff --git a/src/main/java/org/holoeasy/hologram/Hologram.kt b/src/main/java/org/holoeasy/hologram/Hologram.kt index f6580a1..cf19c1e 100644 --- a/src/main/java/org/holoeasy/hologram/Hologram.kt +++ b/src/main/java/org/holoeasy/hologram/Hologram.kt @@ -9,6 +9,10 @@ import java.util.concurrent.ConcurrentHashMap import java.util.concurrent.CopyOnWriteArrayList class Hologram(val key: HologramKey, location: Location, val loader: IHologramLoader) { + init { + key.pool?.takeCareOf(key, this) + } + var location: Location = location private set diff --git a/src/main/java/org/holoeasy/line/ClickableTextLine.kt b/src/main/java/org/holoeasy/line/ClickableTextLine.kt index a10b020..1969eb8 100644 --- a/src/main/java/org/holoeasy/line/ClickableTextLine.kt +++ b/src/main/java/org/holoeasy/line/ClickableTextLine.kt @@ -1,6 +1,5 @@ package org.holoeasy.line -import org.holoeasy.builder.interfaces.PlayerFun import org.bukkit.Bukkit import org.bukkit.Location import org.bukkit.entity.Player @@ -39,7 +38,7 @@ class ClickableTextLine(private val line: TextLine, minHitDistance: Float, maxHi override val textLine: TextLine get() = line - override val args: Array? + override val args: Array<*>? get() = textLine.args diff --git a/src/main/java/org/holoeasy/line/ITextLine.kt b/src/main/java/org/holoeasy/line/ITextLine.kt index 0c85fbb..6427de0 100644 --- a/src/main/java/org/holoeasy/line/ITextLine.kt +++ b/src/main/java/org/holoeasy/line/ITextLine.kt @@ -1,6 +1,5 @@ package org.holoeasy.line -import org.holoeasy.builder.interfaces.PlayerFun import org.bukkit.entity.Player interface ITextLine : ILine { @@ -9,7 +8,7 @@ interface ITextLine : ILine { val textLine: TextLine - val args: Array? + val args: Array<*>? fun parse(player: Player): String diff --git a/src/main/java/org/holoeasy/line/TextLine.kt b/src/main/java/org/holoeasy/line/TextLine.kt index adad372..ae93d37 100644 --- a/src/main/java/org/holoeasy/line/TextLine.kt +++ b/src/main/java/org/holoeasy/line/TextLine.kt @@ -1,27 +1,27 @@ package org.holoeasy.line -import org.holoeasy.builder.interfaces.PlayerFun import org.bukkit.Location import org.bukkit.entity.EntityType import org.bukkit.entity.Player import org.bukkit.plugin.Plugin import org.holoeasy.ext.send import org.holoeasy.packet.IPacket +import org.holoeasy.reactive.MutableState import org.holoeasy.util.AABB -class TextLine(plugin: Plugin, obj: String, override val args: Array? = null, - override val clickable: Boolean = false +class TextLine( + plugin: Plugin, + obj: String, + override val args: Array<*>? = null, + override val clickable: Boolean = false ) : ITextLine { private val line: Line = Line(plugin, EntityType.ARMOR_STAND) - - override var obj: String = "" - var clickEvent : ClickEvent? = null + private var firstRender = true init { - if (args == null) { this.obj = obj } else { @@ -42,7 +42,16 @@ class TextLine(plugin: Plugin, obj: String, override val args: Array? } val res = arrayOfNulls(args.size) for (i in args.indices) { - res[i] = args[i](player) + val tmp = args[i] + if(tmp is MutableState<*>) { + res[i] = tmp.get() + if(firstRender) { + firstRender = false + tmp.addObserver(pvt) + } + } else { + res[i] = tmp + } } return String.format(obj, args = res) diff --git a/src/test/java/ExampleKotlin.kt b/src/test/java/ExampleKotlin.kt index c1f83da..0aa119b 100644 --- a/src/test/java/ExampleKotlin.kt +++ b/src/test/java/ExampleKotlin.kt @@ -1,6 +1,4 @@ import org.bukkit.Location -import org.bukkit.Material -import org.bukkit.inventory.ItemStack import org.bukkit.plugin.Plugin import org.holoeasy.HoloEasy.startInteractivePool import org.holoeasy.builder.HologramBuilder.* @@ -14,13 +12,11 @@ data class ExampleKotlin(val plugin: Plugin) { fun addHologram(location: Location) { - val itemState = mutableStateOf(ItemStack(Material.APPLE)) + val clickCount = mutableStateOf(0) hologram(HologramKey(pool, "unique-id-holo"), location) { - clickable("Click me").onClick { - itemState.set(ItemStack(Material.ENCHANTED_GOLDEN_APPLE)) - } - item(itemState) + textline("Count: {}", clickCount) + clickable("Click me").onClick { clickCount.set(clickCount.get() + 1)} } }