Skip to content

Commit

Permalink
Hotfix.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Apr 18, 2024
1 parent 05c40ba commit cc4d14f
Show file tree
Hide file tree
Showing 36 changed files with 374 additions and 184 deletions.
8 changes: 8 additions & 0 deletions api/src/main/java/kr/toxicity/hud/api/BetterHud.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.InputStream;
import java.util.Objects;
import java.util.function.BiConsumer;
import java.util.function.Consumer;

/**
Expand Down Expand Up @@ -94,6 +96,12 @@ public void onLoad() { // Do not use this
* @param dir target directory
*/
public abstract void loadAssets(@NotNull String prefix, @NotNull File dir);
/**
* Loads player's resource to some directory
* @param prefix resource folder
* @param consumer for each callback
*/
public abstract void loadAssets(@NotNull String prefix, @NotNull BiConsumer<String, InputStream> consumer);

/**
* Gets a width of default font's char
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package kr.toxicity.hud.api.event;

import lombok.Getter;
import org.bukkit.event.Event;
import org.bukkit.event.HandlerList;
import org.jetbrains.annotations.NotNull;

/**
* Plugin reloaded event.
*/
@Getter
public class PluginReloadStartEvent extends Event implements BetterHudEvent {
public PluginReloadStartEvent() {
super(true);
}
@NotNull
@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}
public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,13 @@
/**
* Plugin reloaded event.
*/
@RequiredArgsConstructor
@Getter
public class PluginReloadedEvent extends Event implements BetterHudEvent {
private final ReloadResult result;
public PluginReloadedEvent(@NotNull ReloadResult result) {
super(true);
this.result = result;
}
@NotNull
@Override
public HandlerList getHandlers() {
Expand Down
4 changes: 2 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
`java-library`
kotlin("jvm") version "1.9.23"
id("com.github.johnrengelman.shadow") version("8.1.1")
id("io.papermc.paperweight.userdev") version("1.5.6") apply(false)
id("io.papermc.paperweight.userdev") version("1.5.12") apply(false)
id("org.jetbrains.dokka") version "1.9.20"
}

Expand Down Expand Up @@ -32,7 +32,7 @@ allprojects {
apply(plugin = "kotlin")

group = "kr.toxicity.hud"
version = "beta-14"
version = "beta-15"

repositories {
mavenCentral()
Expand Down
65 changes: 39 additions & 26 deletions dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kr.toxicity.hud

import kr.toxicity.hud.api.BetterHud
import kr.toxicity.hud.api.bedrock.BedrockAdapter
import kr.toxicity.hud.api.event.PluginReloadStartEvent
import kr.toxicity.hud.api.event.PluginReloadedEvent
import kr.toxicity.hud.api.manager.*
import kr.toxicity.hud.api.nms.NMS
Expand Down Expand Up @@ -31,10 +32,12 @@ import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import java.io.File
import java.io.InputStream
import java.net.URI
import java.net.http.HttpClient
import java.net.http.HttpRequest
import java.net.http.HttpResponse
import java.util.function.BiConsumer
import java.util.function.Consumer
import java.util.jar.JarFile

Expand Down Expand Up @@ -213,6 +216,7 @@ class BetterHudImpl: BetterHud() {
onReload = true
val time = System.currentTimeMillis()
asyncTask {
PluginReloadStartEvent().call()
runCatching {
managers.forEach {
it.preReload()
Expand All @@ -221,25 +225,23 @@ class BetterHudImpl: BetterHud() {
val index = TaskIndex(managers.size)

fun managerReload() {
synchronized(index) {
if (index.current < managers.size) {
val manager = managers[index.current++]
info("Loading ${manager.javaClass.simpleName}...")
if (index.current < managers.size) {
val manager = managers[index.current++]
info("Loading ${manager.javaClass.simpleName}...")
synchronized(manager) {
manager.reload(resource) {
managerReload()
}
} else {
PackGenerator.generate {
managers.forEach {
it.postReload()
}
onReload = false
val result = ReloadResult(ReloadState.SUCCESS, System.currentTimeMillis() - time)
task {
PluginReloadedEvent(result).call()
}
consumer.accept(result)
}
} else {
PackGenerator.generate {
managers.forEach {
it.postReload()
}
onReload = false
val result = ReloadResult(ReloadState.SUCCESS, System.currentTimeMillis() - time)
PluginReloadedEvent(result).call()
consumer.accept(result)
}
}
}
Expand Down Expand Up @@ -269,26 +271,37 @@ class BetterHudImpl: BetterHud() {
override fun getAudiences(): BukkitAudiences = audience
override fun getHudPlayer(player: Player): HudPlayer = PlayerManager.getHudPlayer(player)


override fun loadAssets(prefix: String, dir: File) {
loadAssets(prefix, {
File(dir, it).run {
if (!exists()) mkdir()
}
}) { s, i ->
File(dir, s).outputStream().buffered().use {
i.copyTo(it)
}
}
}

override fun loadAssets(prefix: String, consumer: BiConsumer<String, InputStream>) {
loadAssets(prefix, {}) { s, i ->
consumer.accept(s, i)
}
}

private fun loadAssets(prefix: String, dir: (String) -> Unit, consumer: (String, InputStream) -> Unit) {
JarFile(file).use {
it.entries().asSequence().sortedBy { dir ->
dir.name.length
}.forEach { entry ->
if (!entry.name.startsWith(prefix)) return@forEach
if (entry.name.length <= prefix.length + 1) return@forEach
val name = entry.name.substring(prefix.length + 1)
val file = File(dir, name)
if (entry.isDirectory) {
if (!file.exists()) file.mkdir()
} else {
getResource(entry.name)?.buffered()?.use { stream ->
if (!file.exists()) {
file.createNewFile()
file.outputStream().buffered().use { fos ->
stream.copyTo(fos)
}
}
}
dir(name)
} else getResource(entry.name)?.buffered()?.use { stream ->
consumer(name, stream)
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions dist/src/main/kotlin/kr/toxicity/hud/hud/HudElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,14 @@ import kr.toxicity.hud.util.EMPTY_WIDTH_COMPONENT
import kr.toxicity.hud.util.subFolder
import java.io.File

class HudElement(hud: HudImpl, name: String, file: File, private val layout: LayoutGroup, gui: GuiLocation, pixel: ImageLocation) {
class HudElement(hud: HudImpl, name: String, file: List<String>, private val layout: LayoutGroup, gui: GuiLocation, pixel: ImageLocation) {
private val imageElement = layout.image.map {image ->
HudImageElement(hud, image, gui, pixel)
}
private val textElement = run {
val subFile = file.subFolder("text")
val subFile = ArrayList(file).apply {
add("text")
}
layout.text.mapIndexed { index, textLayout ->
HudTextElement(hud, name, subFile, textLayout, index, gui, pixel)
}
Expand Down
16 changes: 11 additions & 5 deletions dist/src/main/kotlin/kr/toxicity/hud/hud/HudImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ import kr.toxicity.hud.shader.HudShader
import kr.toxicity.hud.util.*
import net.kyori.adventure.key.Key
import org.bukkit.configuration.ConfigurationSection
import java.io.File

class HudImpl(private val internalName: String, file: File, section: ConfigurationSection) : Hud {
class HudImpl(private val internalName: String, file: List<String>, section: ConfigurationSection) : Hud {
companion object {
const val DEFAULT_BIT = 13
const val MAX_BIT = 23 - DEFAULT_BIT
Expand All @@ -37,7 +36,9 @@ class HudImpl(private val internalName: String, file: File, section: Configurati
var textIndex = 0

private val elements = run {
val subFile = file.subFolder(internalName)
val subFile = ArrayList(file).apply {
add(internalName)
}
ArrayList<HudAnimation>().apply {
section.getConfigurationSection("layouts").ifNull("layout configuration not set.").forEachSubConfiguration { s, configurationSection ->
val layout = configurationSection.getString("name").ifNull("name value not set: $s").let {
Expand Down Expand Up @@ -69,10 +70,15 @@ class HudImpl(private val internalName: String, file: File, section: Configurati
}
}
init {
PackGenerator.addTask {
PackGenerator.addTask(
ArrayList(file).apply {
add(internalName)
add("image.json")
}
) {
JsonObject().apply {
add("providers", jsonArray)
}.save(file.subFolder(internalName).subFile("image.json"))
}.toByteArray()
}
}

Expand Down
9 changes: 5 additions & 4 deletions dist/src/main/kotlin/kr/toxicity/hud/hud/HudTextElement.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import kr.toxicity.hud.text.HudTextData
import kr.toxicity.hud.util.*
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
import java.io.File
import kotlin.math.roundToInt

class HudTextElement(parent: HudImpl, name: String, file: File, private val text: TextLayout, index: Int, gui: GuiLocation, pixel: ImageLocation) {
class HudTextElement(parent: HudImpl, name: String, file: List<String>, private val text: TextLayout, index: Int, gui: GuiLocation, pixel: ImageLocation) {

private val renderer = run {
val shader = HudShader(
Expand Down Expand Up @@ -108,10 +107,12 @@ class HudTextElement(parent: HudImpl, name: String, file: File, private val text
)
}
)
PackGenerator.addTask {
PackGenerator.addTask(ArrayList(file).apply {
add("text_${index + 1}_${index2 + 1}.json")
}) {
JsonObject().apply {
add("providers", array)
}.save(File(file, "text_${index + 1}_${index2 + 1}.json"))
}.toByteArray()
}
TextManager.setKey(group, result)
result
Expand Down
17 changes: 13 additions & 4 deletions dist/src/main/kotlin/kr/toxicity/hud/manager/BackgroundManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package kr.toxicity.hud.manager

import kr.toxicity.hud.background.HudBackground
import kr.toxicity.hud.image.ImageLocation
import kr.toxicity.hud.pack.PackGenerator
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.util.*
import java.io.File
Expand All @@ -18,21 +19,29 @@ object BackgroundManager: BetterHudManager {

override fun reload(resource: GlobalResource, callback: () -> Unit) {
val folder = DATA_FOLDER.subFolder("backgrounds")
val outputParent = resource.textures.subFolder("background")
val outputParent = ArrayList(resource.textures).apply {
add("background")
}
backgroundMap.clear()
folder.forEachAsync({ _, it ->
folder.forEachAsync({
if (it.extension == "yml") {
runCatching {
val yaml = it.toYaml()
val name = it.nameWithoutExtension
val backgroundFolder = folder.subFolder(name)
val output = outputParent.subFolder(name)
val output = ArrayList(outputParent).apply {
add(name)
}
fun getImage(imageName: String) = File(backgroundFolder, "$imageName.png")
.ifNotExist("this image doesn't exist: $imageName.png in $name")
.toImage()
.removeEmptyWidth()
.ifNull("this image is empty: $imageName.png in $name").apply {
image.save(output.subFile("$imageName.png"))
PackGenerator.addTask(ArrayList(output).apply {
add("$imageName.png")
}) {
image.toByteArray()
}
}
backgroundMap[name] = HudBackground(
name,
Expand Down
11 changes: 8 additions & 3 deletions dist/src/main/kotlin/kr/toxicity/hud/manager/ConfigManager.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.toxicity.hud.manager

import kr.toxicity.hud.pack.PackType
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.util.*
import net.kyori.adventure.text.Component
Expand All @@ -26,11 +27,11 @@ object ConfigManager: BetterHudManager {
private set
var buildFolderLocation = "BetterHud\\build"
private set
var separateResourcePackNameSpace = false
private set

var mergeBossBar = true
private set
var packType = PackType.FOLDER
private set

override fun start() {

Expand All @@ -50,14 +51,18 @@ object ConfigManager: BetterHudManager {
yaml.getString("default-font-name")?.let {
defaultFontName = it
}
yaml.getString("pack-type")?.let {
runCatching {
packType = PackType.valueOf(it.uppercase())
}
}
tickSpeed = yaml.getLong("tick-speed", 1)
numberFormat = (yaml.getString("number-format")?.let {
runCatching {
DecimalFormat(it)
}.getOrNull()
} ?: DecimalFormat("#,###.#"))
disableToBedrockPlayer = yaml.getBoolean("disable-to-bedrock-player", true)
separateResourcePackNameSpace = yaml.getBoolean("separate-resource-pack-namespace", false)
yaml.getString("build-folder-location")?.let {
buildFolderLocation = it
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ object HudManagerImpl: BetterHudManager, HudManager {

override fun reload(resource: GlobalResource, callback: () -> Unit) {
hudMap.clear()
val hudFolder = resource.font.subFolder("hud")
DATA_FOLDER.subFolder("huds").forEachAllYamlAsync({ _, file, s, configurationSection ->
val hudFolder = ArrayList(resource.font).apply {
add("hud")
}
DATA_FOLDER.subFolder("huds").forEachAllYamlAsync({ file, s, configurationSection ->
runCatching {
hudMap[s] = HudImpl(s, hudFolder, configurationSection)
}.onFailure { e ->
Expand Down
Loading

0 comments on commit cc4d14f

Please sign in to comment.