Skip to content

Commit

Permalink
working on reactive lines
Browse files Browse the repository at this point in the history
  • Loading branch information
unldenis committed Feb 1, 2024
1 parent 894b6e8 commit 65ba1cd
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/main/java/org/holoeasy/line/BlockLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.Plugin
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, override var obj: 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)

override val plugin: Plugin
get() = line.plugin

Expand All @@ -26,6 +30,12 @@ class BlockLine(plugin: Plugin, override var obj: ItemStack) : ILine<ItemStack>
override val location: Location?
get() = line.location

override var obj : ItemStack
get() = _mutableStateOf.get()
set(value) = _mutableStateOf.set(value)

override lateinit var pvt: ILine.PrivateConfig

override fun setLocation(value: Location) {
line.location = value
}
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/holoeasy/line/ClickableTextLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class ClickableTextLine(private val line: TextLine, minHitDistance: Float, maxHi
line.obj = value
}

override var pvt: ILine.PrivateConfig = line.pvt

override fun setLocation(value: Location) {
line.setLocation(value)
this.updateHitBox()
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/org/holoeasy/line/ILine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,22 @@ package org.holoeasy.line
import org.bukkit.Location
import org.bukkit.entity.Player
import org.bukkit.plugin.Plugin
import org.holoeasy.hologram.Hologram
import org.holoeasy.reactive.Observer
import org.jetbrains.annotations.ApiStatus

interface ILine<T> {

data class PrivateConfig(private val line: ILine<*>) : Observer {

var hologram : Hologram? = null
override fun observerUpdate() {
hologram?.let {
line.update(it.seeingPlayers)
}
}
}

val plugin: Plugin

val type: Type
Expand All @@ -17,6 +29,8 @@ interface ILine<T> {

var obj: T

var pvt : PrivateConfig

fun setLocation(value: Location)

fun hide(player: Player)
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/org/holoeasy/line/TextLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TextLine(plugin: Plugin, obj: String, override val args: Array<PlayerFun>?

private val line: Line = Line(plugin, EntityType.ARMOR_STAND)


override var obj: String = ""

var clickEvent : ClickEvent? = null
Expand Down Expand Up @@ -60,6 +61,9 @@ class TextLine(plugin: Plugin, obj: String, override val args: Array<PlayerFun>?
override val location: Location?
get() = line.location


override lateinit var pvt: ILine.PrivateConfig

override fun setLocation(value: Location) {
line.location = value
if (clickable) {
Expand Down
29 changes: 29 additions & 0 deletions src/main/java/org/holoeasy/reactive/MutableState.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.holoeasy.reactive

data class MutableState<T>(private var value : T) {

private val observers= mutableListOf<Observer>()

fun get() : T {
return value
}

fun set(newValue : T) {
value = newValue
this.notifyObservers()
}

fun addObserver(observer: Observer) {
observers.add(observer)
}

fun removeObserver(observer: Observer) {
observers.remove(observer)
}

private fun notifyObservers() {
for (observer in observers) {
observer.observerUpdate()
}
}
}
5 changes: 5 additions & 0 deletions src/main/java/org/holoeasy/reactive/Observer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.holoeasy.reactive

interface Observer {
fun observerUpdate()
}

0 comments on commit 65ba1cd

Please sign in to comment.