Skip to content

Commit

Permalink
3.1.0 Reactive Holograms
Browse files Browse the repository at this point in the history
  • Loading branch information
unldenis committed Feb 1, 2024
1 parent 4272aa3 commit 89542d3
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 50 deletions.
55 changes: 47 additions & 8 deletions README.MD
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,21 @@ HoloEasy is a simple, modern and high-performant Java and Kotlin Minecraft Holog
<dependency>
<groupId>com.github.unldenis</groupId>
<artifactId>holoeasy</artifactId>
<version>3.0.1</version>
<version>3.1.0</version>
</dependency>
```

#### Gradle

```kotlin
implementation("com.github.unldenis:holoeasy:3.0.1")
implementation("com.github.unldenis:holoeasy:3.1.0")
```

Make sure you include the <a href="https://jitpack.io/">repository</a> as well.

### Start programming (Java)
## Start programming

### Java

```java
import static org.holoeasy.builder.HologramBuilder.*;
Expand All @@ -40,8 +42,7 @@ IHologramPool pool = HoloEasy.startInteractivePool(plugin, 60, 0.5f, 5f);
public void addHologram(Location location) {
hologram(new HologramKey(pool, "unique-id-holo"), location, () -> {
textline("Hello");
textline("{} Stats", Player::getName);
textline("Score {} - {}", $ -> 0, $ -> 1);
textline("Score {} - {}", 0, 1);
clickable("Click me").onClick(p -> {
p.sendTitle(ChatColor.AQUA + "Hi", ChatColor.BOLD + "by HoloEasy",
20, 20, 20);
Expand All @@ -51,7 +52,7 @@ public void addHologram(Location location) {
}
```

### Start programming (Kotlin)
### Kotlin
```kotlin
import org.holoeasy.builder.HologramBuilder.*

Expand All @@ -61,8 +62,7 @@ val pool = startInteractivePool(plugin, 60.0, 0.5f, 5f)
fun addHologram(location: Location) {
hologram(HologramKey(pool, "unique-id-holo"), location) {
textline("Hello")
textline("{} Stats", Player::getName)
textline("Score {} - {}", { 0 }, { 1 })
textline("Score {} - {}", 0, 1)
clickable("Click me").onClick {
it.sendTitle(ChatColor.AQUA + "Hi", ChatColor.BOLD + "by HoloEasy",
20, 20, 20)
Expand All @@ -72,6 +72,45 @@ fun addHologram(location: Location) {
}
```

## Reactive Holograms
From 3.1.0 version, the parameters of the text lines **_can also be reactive_**. This means that you can update the line by simply calling the 'set' method to these.

<p align="center">
<img src="state.gif" alt="holoeasy state video"/>
</p>

> [!WARNING]
> Mutable states have no player information at this time. If you need to create a hologram for a specific player, it is recommended that you do **_not_** add it to a Pool.
### Java
```java
var clickCount = mutableStateOf(0); // can be any type

var holo = hologram(new HologramKey(plugin, "unique-id-holo"), location, () -> {
textline("{}!", "Static");
textline("Count: {}", clickCount);
clickable("Click me", 0.5f, 5f).onClick($ -> clickCount.set(clickCount.get() + 1));
});

// It hasn't been added to a pool, so it's up to us to make it visible and hide it from players. It's better to use a pool because it's automatic and performs asynchronous operations.
// HologramKey decides whether to add it to a pool or not.
holo.show(player);
```

### Kotlin
```kotlin
val clickCount = mutableStateOf(0) // can be any type

val holo = hologram(HologramKey(plugin, "unique-id-holo"), location) {
textline("{}!", "Static")
textline("Dynamic: {}", clickCount)
clickable("Click me", 0.5f, 5f).onClick { clickCount.set(clickCount.get() + 1)}
}

// It hasn't been added to a pool, so it's up to us to make it visible and hide it from players. It's better to use a pool because it's automatic and performs asynchronous operations.
// HologramKey decides whether to add it to a pool or not.
holo.show(player)
```


## Ex (Hologram-Lib)
Are you using a version older than 3.0.0? You can find the documentation <a href="https://unldenis.github.io/hologramlib/">here</a>.
11 changes: 10 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

<groupId>org.holoeasy</groupId>
<artifactId>holoeasy</artifactId>
<version>3.0.1</version>
<version>3.1.0</version>

<properties>
<maven.compiler.source>1.8</maven.compiler.source>
Expand Down Expand Up @@ -131,6 +131,15 @@
</configuration>
</plugin>

<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<outputDirectory>C:\Users\mehil\OneDrive\Desktop\Spigot 1.16.5\plugins</outputDirectory>
</configuration>
</plugin>

<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-jar-plugin</artifactId>-->
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/holoeasy/builder/HologramBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public static ITextLine clickable(@NotNull String text, float minHitDistance, fl
@NotNull Object... args) {
return getInstance().textline(
text,
false,
true,
minHitDistance,
maxHitDistance,
args.length == 0 ? null : args
Expand All @@ -79,7 +79,7 @@ public static void customline(@NotNull ILine<?> customLine) {
getInstance().customLine(customLine);
}

public static <T> MutableState<T> mutableStateOf(T initialValue) {
public static <T> MutableState<T> mutableStateOf(@NotNull T initialValue) {
return new MutableState<>(initialValue);
}
}
16 changes: 10 additions & 6 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.hologram.TextBlockStandardLoader
import org.holoeasy.line.*
import org.holoeasy.reactive.MutableState
import kotlin.math.min

object Service {

Expand Down Expand Up @@ -33,16 +34,19 @@ object Service {
val holo = getStaticHolo()

if (minHitDistance == null || maxHitDistance == null) {
if(holo.key.pool == null && clickable) {
throw IllegalStateException("This hologram is not in a pool,so use the method #clickable(text, minHitDistance, maxHitDistance)")
}

val textLine = TextLine(holo.key.plugin, text, clickable = clickable, args = args)
holo.lines.add(textLine)
return textLine
} else {
val textLine = TextLine(holo.key.plugin, text, clickable = false, args = args)
val clickableTextLine = ClickableTextLine(textLine, minHitDistance, maxHitDistance)
holo.lines.add(clickableTextLine)
return clickableTextLine
}

}
val textLine = TextLine(holo.key.plugin, text, clickable = false, args = args)
val clickableTextLine = ClickableTextLine(textLine, minHitDistance, maxHitDistance)
holo.lines.add(clickableTextLine)
return clickableTextLine
}

fun itemline(block: ItemStack) {
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/holoeasy/line/BlockLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class BlockLine(plugin: Plugin, obj: MutableState<ItemStack>) : ILine<ItemStack>
get() = _mutableStateOf.get()
set(value) = _mutableStateOf.set(value)

override lateinit var pvt: ILine.PrivateConfig
override var pvt = ILine.PrivateConfig(this)

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

override var pvt: ILine.PrivateConfig = line.pvt
override var pvt = ILine.PrivateConfig(this)

override fun setLocation(value: Location) {
line.setLocation(value)
Expand Down Expand Up @@ -119,9 +119,9 @@ class ClickableTextLine(private val line: TextLine, minHitDistance: Float, maxHi
val dist = size * (chars / 2.0)

hitbox = AABB(
AABB.Vec3D(-dist, -0.039, -dist),
AABB.Vec3D(dist, +0.039, dist)
AABB.Vec3D(-dist, -0.040, -dist),
AABB.Vec3D(dist, +0.040, dist)
)
hitbox!!.translate(fromLocation(location!!.clone().add(0.0, 2.42, 0.0)))
hitbox!!.translate(fromLocation(location!!.clone().add(0.0, 2.35, 0.0)))
}
}
8 changes: 4 additions & 4 deletions src/main/java/org/holoeasy/line/TextLine.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class TextLine(
get() = line.location


override lateinit var pvt: ILine.PrivateConfig
override var pvt = ILine.PrivateConfig(this)

override fun setLocation(value: Location) {
line.location = value
Expand All @@ -81,10 +81,10 @@ class TextLine(
val dist = size * (chars / 2.0)

hitbox = AABB(
AABB.Vec3D(-dist, -0.039, -dist),
AABB.Vec3D(dist, +0.039, dist)
AABB.Vec3D(-dist, -0.040, -dist),
AABB.Vec3D(dist, +0.040, dist)
).also {
it.translate(AABB.Vec3D.fromLocation(value.clone().add(0.0, 2.42, 0.0)))
it.translate(AABB.Vec3D.fromLocation(value.clone().add(0.0, 2.35, 0.0)))
}
}
}
Expand Down
24 changes: 0 additions & 24 deletions src/test/java/ExampleKotlin.kt

This file was deleted.

Binary file added state.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 89542d3

Please sign in to comment.