Skip to content

Commit

Permalink
Updates non-shader core rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Jul 11, 2024
1 parent 109eaa1 commit 6e4cd72
Show file tree
Hide file tree
Showing 20 changed files with 287 additions and 147 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
package kr.toxicity.healthbar.api.healthbar;

import kr.toxicity.healthbar.api.nms.VirtualTextDisplay;
import kr.toxicity.healthbar.api.renderer.HealthBarRenderer;
import org.jetbrains.annotations.NotNull;

public interface HealthBarUpdater {
@NotNull
HealthBarUpdaterGroup parent();
@NotNull
VirtualTextDisplay display();
@NotNull
HealthBarRenderer renderer();

void updateTick();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ public interface ConfigManager {
boolean disableToInvulnerableMob();
@NotNull
CoreShadersOption shaders();
boolean useCoreShaders();
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package kr.toxicity.healthbar.api.renderer;

import kr.toxicity.healthbar.api.component.WidthComponent;
import org.bukkit.Location;
import kr.toxicity.healthbar.api.nms.VirtualTextDisplay;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Unmodifiable;

import java.util.List;

public interface HealthBarRenderer extends Renderer {
@NotNull
RenderResult render();

@Unmodifiable
List<VirtualTextDisplay> displays();
void updateTick();

record RenderResult(@NotNull WidthComponent component, @NotNull Location location) {
}
boolean work();
void stop();
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
package kr.toxicity.healthbar.api.renderer;

import kr.toxicity.healthbar.api.component.PixelComponent;
import org.jetbrains.annotations.NotNull;

public interface ImageRenderer extends Renderer {
@NotNull
PixelComponent render(int groupCount);
public interface ImageRenderer extends PixelRenderer {
boolean isBackground();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kr.toxicity.healthbar.api.renderer;

import kr.toxicity.healthbar.api.component.PixelComponent;
import org.jetbrains.annotations.NotNull;

public interface PixelRenderer extends Renderer {
@NotNull
PixelComponent render(int groupCount);
int layer();
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
package kr.toxicity.healthbar.api.renderer;

import kr.toxicity.healthbar.api.component.PixelComponent;
import org.jetbrains.annotations.NotNull;

public interface TextRenderer extends Renderer {
@NotNull
PixelComponent render(int groupCount);
public interface TextRenderer extends PixelRenderer {
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ allprojects {
apply(plugin = "kotlin")
apply(plugin = "org.jetbrains.dokka")
group = "kr.toxicity.healthbar"
version = "3.0-alpha-4"
version = "3.0-alpha-5"
repositories {
mavenCentral()
maven("https://repo.papermc.io/repository/maven-public/")
Expand Down
122 changes: 82 additions & 40 deletions dist/src/main/kotlin/kr/toxicity/healthbar/healthbar/HealthBarImpl.kt
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
package kr.toxicity.healthbar.healthbar

import kr.toxicity.healthbar.api.component.WidthComponent
import kr.toxicity.healthbar.api.condition.HealthBarCondition
import kr.toxicity.healthbar.api.healthbar.GroupIndex
import kr.toxicity.healthbar.api.healthbar.HealthBar
import kr.toxicity.healthbar.api.healthbar.HealthBarData
import kr.toxicity.healthbar.api.trigger.HealthBarTriggerType
import kr.toxicity.healthbar.api.layout.LayoutGroup
import kr.toxicity.healthbar.api.nms.VirtualTextDisplay
import kr.toxicity.healthbar.api.renderer.HealthBarRenderer
import kr.toxicity.healthbar.api.renderer.HealthBarRenderer.RenderResult
import kr.toxicity.healthbar.manager.ConfigManagerImpl
import kr.toxicity.healthbar.manager.LayoutManagerImpl
import kr.toxicity.healthbar.util.*
import org.bukkit.Location
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.util.Vector
import java.util.Collections
Expand Down Expand Up @@ -57,7 +59,7 @@ class HealthBarImpl(
override fun duration(): Int = duration

override fun createRenderer(pair: HealthBarData): HealthBarRenderer {
return Renderer(pair)
return if (ConfigManagerImpl.useCoreShaders()) SingleRenderer(pair) else MultiRenderer(pair)
}

override fun equals(other: Any?): Boolean {
Expand All @@ -73,43 +75,73 @@ class HealthBarImpl(
return s.hashCode()
}

private class RenderedLayout(group: LayoutGroup, pair: HealthBarData) {
val group = group.group()
val images = group.images().map {
it.createImageRenderer(pair)
}.toMutableList()
val texts = group.texts().map {
it.createRenderer(pair)
}.toMutableList()
}

private inner class Renderer(
private val pair: HealthBarData
): HealthBarRenderer {
private var d = 0

private val indexes = groups.mapNotNull {
private abstract inner class AbstractRenderer(protected val pair: HealthBarData): HealthBarRenderer {
protected val indexes = groups.mapNotNull {
it.group()
}.associateWith {
GroupIndex()
}

private val render = groups.map {
RenderedLayout(it, pair)
protected val render = groups.map {
RenderedLayout(it, pair)
}.toMutableList()

protected var d = 0

override fun hasNext(): Boolean {
val entity = pair.entity.entity()
val player = pair.player.player()
return entity.isValid && entity.world.uid == player.world.uid && player.location.distance(entity.location) < ConfigManagerImpl.lookDistance() && (duration < 0 || ++d <= duration)
}

override fun canRender(): Boolean {
return conditions.apply(pair)
}
override fun updateTick() {
d = 0
}
}
private inner class MultiRenderer(
pair: HealthBarData
): AbstractRenderer(pair) {

override fun render(): RenderResult {
var comp = EMPTY_WIDTH_COMPONENT
private val displays = render.map {
it.createPool(pair, indexes)
}.toMutableList()

override fun work(): Boolean {
if (!hasNext()) {
displays.forEach {
it.remove()
}
return false
} else {
indexes.values.forEach {
it.clear()
}
displays.removeIf {
!it.update()
}
return displays.isNotEmpty()
}
}

override fun displays(): List<VirtualTextDisplay> = displays.map {
it.displays()
}.sum()

override fun stop() {
displays.forEach {
it.remove()
}
}
}
private inner class SingleRenderer(
pair: HealthBarData
): AbstractRenderer(pair) {

private val display = pair.createEntity(render())

private fun removeUnused() {
indexes.values.forEach {
it.clear()
}
Expand All @@ -122,18 +154,41 @@ class HealthBarImpl(
}
it.images.isEmpty() && it.texts.isEmpty()
}
}

override fun stop() {
display.remove()
}

override fun displays(): List<VirtualTextDisplay> = listOf(display)

override fun work(): Boolean {
if (!hasNext() || render.isEmpty()) {
display.remove()
return false
} else {
removeUnused()
val render = render()
display.teleport(pair.toEntityLocation())
display.text(render.component.build())
return true
}
}

private fun render(): WidthComponent {
var comp = EMPTY_WIDTH_COMPONENT
var max = 0
render.forEach {
val index = it.group?.let { s ->
indexes[s]
}
val imageRender = it.images.filter { r ->
r.canRender()
}
val textRender = it.texts.filter { r ->
r.canRender()
}
if (imageRender.isEmpty() && textRender.isEmpty()) return@forEach
val index = it.group?.let { s ->
indexes[s]
}
val next = index?.next() ?: 0
imageRender.forEach { image ->
val render = image.render(next)
Expand All @@ -147,20 +202,7 @@ class HealthBarImpl(
comp += render.pixel.toSpaceComponent() + render.component + (-length).toSpaceComponent() + NEW_LAYER
}
}
val entity = pair.entity
return RenderResult(
comp + (max).toSpaceComponent(),
entity.entity().location.apply {
y += (PLUGIN.modelEngine().height(pair.entity.entity()) ?: pair.entity.entity().eyeHeight) + ConfigManagerImpl.defaultHeight()
entity.mob()?.let {
y += it.configuration().height()
}
}
)
}

override fun updateTick() {
d = 0
return comp + (max).toSpaceComponent()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class HealthBarUpdaterGroupImpl(
player,
entity
)
HealthBarUpdaterImpl(this, data, healthBar.createRenderer(data))
HealthBarUpdaterImpl(this, healthBar.createRenderer(data))
}.updateTick()
}
}
Original file line number Diff line number Diff line change
@@ -1,50 +1,24 @@
package kr.toxicity.healthbar.healthbar

import kr.toxicity.healthbar.api.healthbar.HealthBarData
import kr.toxicity.healthbar.api.healthbar.HealthBarUpdater
import kr.toxicity.healthbar.api.healthbar.HealthBarUpdaterGroup
import kr.toxicity.healthbar.api.nms.VirtualTextDisplay
import kr.toxicity.healthbar.api.renderer.HealthBarRenderer
import kr.toxicity.healthbar.util.PLUGIN
import org.bukkit.util.Vector

class HealthBarUpdaterImpl(
private val parent: HealthBarUpdaterGroupImpl,
data: HealthBarData,
private val renderer: HealthBarRenderer
): HealthBarUpdater {
private val display = renderer().render().run {
val player = data.player.player()
PLUGIN.nms().createTextDisplay(player, location, component.component.build()).apply {
val scale = data.healthBar.scale()
transformation(
Vector(0.0, -(1 - scale.y) * 8192 / 40 ,0.0),
scale
)
}
}

override fun parent(): HealthBarUpdaterGroup = parent
override fun display(): VirtualTextDisplay = display
override fun renderer(): HealthBarRenderer = renderer

override fun updateTick() {
renderer.updateTick()
}

override fun remove() {
display.remove()
renderer.stop()
}

override fun update(): Boolean {
if (!renderer().hasNext()) {
display.remove()
return false
} else {
val render = renderer().render()
display.teleport(render.location)
display.text(render.component.component.build())
return true
}
}
override fun update(): Boolean = renderer.work()
}
Loading

0 comments on commit 6e4cd72

Please sign in to comment.