Skip to content

Commit

Permalink
implementing reactive item lines
Browse files Browse the repository at this point in the history
  • Loading branch information
unldenis committed Feb 1, 2024
1 parent 65ba1cd commit 209257b
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 4 deletions.
8 changes: 8 additions & 0 deletions src/main/java/org/holoeasy/builder/HologramBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -78,8 +79,15 @@ public static void item(@NotNull ItemStack block) {
getInstance().itemline(block);
}

public static void item(@NotNull MutableState<ItemStack> block) {
getInstance().itemline(block);
}

public static void customline(@NotNull ILine<?> customLine) {
getInstance().customLine(customLine);
}

public static <T> MutableState<T> mutableStateOf(T initialValue) {
return new MutableState<>(initialValue);
}
}
7 changes: 7 additions & 0 deletions src/main/java/org/holoeasy/builder/Service.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -51,6 +52,12 @@ object Service {
holo.lines.add(blockline)
}

fun itemline(block: MutableState<ItemStack>) {
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)
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/holoeasy/hologram/Hologram.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/holoeasy/line/BlockLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<ItemStack> {
class BlockLine(plugin: Plugin, obj: MutableState<ItemStack>) : ILine<ItemStack> {


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
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/holoeasy/line/ILine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ interface ILine<T> {

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)
}
}
Expand Down
28 changes: 28 additions & 0 deletions src/test/java/ExampleKotlin.kt
Original file line number Diff line number Diff line change
@@ -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)
}

}

}

0 comments on commit 209257b

Please sign in to comment.