Skip to content

Commit

Permalink
working on reactive text lines
Browse files Browse the repository at this point in the history
  • Loading branch information
unldenis committed Feb 1, 2024
1 parent 209257b commit 4272aa3
Show file tree
Hide file tree
Showing 9 changed files with 30 additions and 45 deletions.
14 changes: 3 additions & 11 deletions src/main/java/org/holoeasy/builder/HologramBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/holoeasy/builder/HologramConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/holoeasy/builder/Service.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -29,7 +28,7 @@ object Service {
@JvmOverloads
fun textline(
text: String, clickable: Boolean = false, minHitDistance: Float? = null,
maxHitDistance: Float? = null, args: Array<PlayerFun>? = null
maxHitDistance: Float? = null, args: Array<*>? = null
) : ITextLine {
val holo = getStaticHolo()

Expand Down
12 changes: 0 additions & 12 deletions src/main/java/org/holoeasy/builder/interfaces/PlayerFun.java

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/java/org/holoeasy/hologram/Hologram.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/holoeasy/line/ClickableTextLine.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -39,7 +38,7 @@ class ClickableTextLine(private val line: TextLine, minHitDistance: Float, maxHi
override val textLine: TextLine
get() = line

override val args: Array<PlayerFun>?
override val args: Array<*>?
get() = textLine.args


Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/holoeasy/line/ITextLine.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.holoeasy.line

import org.holoeasy.builder.interfaces.PlayerFun
import org.bukkit.entity.Player

interface ITextLine : ILine<String> {
Expand All @@ -9,7 +8,7 @@ interface ITextLine : ILine<String> {

val textLine: TextLine

val args: Array<PlayerFun>?
val args: Array<*>?

fun parse(player: Player): String

Expand Down
25 changes: 17 additions & 8 deletions src/main/java/org/holoeasy/line/TextLine.kt
Original file line number Diff line number Diff line change
@@ -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<PlayerFun>? = 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 {
Expand All @@ -42,7 +42,16 @@ class TextLine(plugin: Plugin, obj: String, override val args: Array<PlayerFun>?
}
val res = arrayOfNulls<Any>(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)
Expand Down
10 changes: 3 additions & 7 deletions src/test/java/ExampleKotlin.kt
Original file line number Diff line number Diff line change
@@ -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.*
Expand All @@ -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)}
}

}
Expand Down

0 comments on commit 4272aa3

Please sign in to comment.