diff --git a/src/main/java/org/holoeasy/builder/HologramBuilder.java b/src/main/java/org/holoeasy/builder/HologramBuilder.java index 68b5caf..29facae 100644 --- a/src/main/java/org/holoeasy/builder/HologramBuilder.java +++ b/src/main/java/org/holoeasy/builder/HologramBuilder.java @@ -10,6 +10,7 @@ import org.holoeasy.line.ILine; import org.holoeasy.line.ITextLine; import org.holoeasy.pool.IHologramPool; +import org.holoeasy.reactive.MutableState; import org.jetbrains.annotations.NotNull; public class HologramBuilder { @@ -78,8 +79,15 @@ public static void item(@NotNull ItemStack block) { getInstance().itemline(block); } + public static void item(@NotNull MutableState block) { + getInstance().itemline(block); + } + public static void customline(@NotNull ILine customLine) { getInstance().customLine(customLine); } + public static MutableState mutableStateOf(T initialValue) { + return new MutableState<>(initialValue); + } } \ No newline at end of file diff --git a/src/main/java/org/holoeasy/builder/Service.kt b/src/main/java/org/holoeasy/builder/Service.kt index 8316222..e6a67d5 100644 --- a/src/main/java/org/holoeasy/builder/Service.kt +++ b/src/main/java/org/holoeasy/builder/Service.kt @@ -6,6 +6,7 @@ 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 object Service { @@ -51,6 +52,12 @@ object Service { holo.lines.add(blockline) } + fun itemline(block: MutableState) { + val holo = getStaticHolo() + val blockline = BlockLine(holo.key.plugin, block) + holo.lines.add(blockline) + } + fun customLine(customLine: ILine<*>) { val holo = getStaticHolo() holo.lines.add(customLine) diff --git a/src/main/java/org/holoeasy/hologram/Hologram.kt b/src/main/java/org/holoeasy/hologram/Hologram.kt index d924232..f6580a1 100644 --- a/src/main/java/org/holoeasy/hologram/Hologram.kt +++ b/src/main/java/org/holoeasy/hologram/Hologram.kt @@ -35,6 +35,7 @@ class Hologram(val key: HologramKey, location: Location, val loader: IHologramLo fun load(vararg lines: ILine<*>) { hLines.clear() + lines.forEach { it.pvt.hologram = this } loader.load(this, lines) } diff --git a/src/main/java/org/holoeasy/line/BlockLine.kt b/src/main/java/org/holoeasy/line/BlockLine.kt index f64ac52..d98bc9d 100644 --- a/src/main/java/org/holoeasy/line/BlockLine.kt +++ b/src/main/java/org/holoeasy/line/BlockLine.kt @@ -10,13 +10,16 @@ import org.holoeasy.ext.send import org.holoeasy.packet.IPacket import org.holoeasy.reactive.MutableState -class BlockLine(plugin: Plugin, obj: ItemStack) : ILine { +class BlockLine(plugin: Plugin, obj: MutableState) : ILine { private val line: Line = Line(plugin, EntityType.DROPPED_ITEM) private val resetVelocity = IPacket.get(IPacket.Type.VELOCITY).velocity(line.entityID, 0, 0,0) - private val _mutableStateOf = MutableState(obj) + private val _mutableStateOf = obj.also { it.addObserver(pvt) } + + constructor(plugin: Plugin, obj: ItemStack) : this(plugin, MutableState(obj)){ + } override val plugin: Plugin get() = line.plugin diff --git a/src/main/java/org/holoeasy/line/ILine.kt b/src/main/java/org/holoeasy/line/ILine.kt index 2ad7d08..d03d303 100644 --- a/src/main/java/org/holoeasy/line/ILine.kt +++ b/src/main/java/org/holoeasy/line/ILine.kt @@ -11,9 +11,9 @@ interface ILine { data class PrivateConfig(private val line: ILine<*>) : Observer { - var hologram : Hologram? = null + lateinit var hologram: Hologram override fun observerUpdate() { - hologram?.let { + hologram.let { line.update(it.seeingPlayers) } } diff --git a/src/test/java/ExampleKotlin.kt b/src/test/java/ExampleKotlin.kt new file mode 100644 index 0000000..c1f83da --- /dev/null +++ b/src/test/java/ExampleKotlin.kt @@ -0,0 +1,28 @@ +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.* +import org.holoeasy.config.HologramKey + + +data class ExampleKotlin(val plugin: Plugin) { + + // you can use a Pool or a org.bukkit.Plugin for HologramKey + val pool = startInteractivePool(plugin, 60.0, 0.5f, 5f) + + + fun addHologram(location: Location) { + val itemState = mutableStateOf(ItemStack(Material.APPLE)) + + hologram(HologramKey(pool, "unique-id-holo"), location) { + clickable("Click me").onClick { + itemState.set(ItemStack(Material.ENCHANTED_GOLDEN_APPLE)) + } + item(itemState) + } + + } + +} \ No newline at end of file