Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,11 @@ open class RebarBlock private constructor(val block: Block) : WailaSupplier, Key
open fun getBlockTextureProperties(): MutableMap<String, Pair<String, Int>> {
val properties = mutableMapOf<String, Pair<String, Int>>()
if (this is DirectionalRebarBlock) {
properties["facing"] = facing.name.lowercase() to IMMEDIATE_FACES.size
try {
properties["facing"] = facing.name.lowercase() to IMMEDIATE_FACES.size
} catch (e: Exception) {
e.printStackTrace()
}
}
return properties
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,27 @@ class RebarBlockSchema(
val default = "$prefix.waila"
}

private val createConstructor: MethodHandle = blockClass.findConstructorMatching(
Block::class.java,
BlockCreateContext::class.java
) ?: throw NoSuchMethodException(
"Block '$key' ($blockClass) is missing a create constructor (${javaClass.simpleName}, Block, BlockCreateContext)"
)

private val loadConstructor: MethodHandle = blockClass.findConstructorMatching(
Block::class.java,
PersistentDataContainer::class.java
) ?: throw NoSuchMethodException(
"Block '$key' ($blockClass) is missing a load constructor (${javaClass.simpleName}, Block, PersistentDataContainer)"
)
private val createConstructor: MethodHandle = try {
blockClass.findConstructorMatching(
Block::class.java,
BlockCreateContext::class.java
) ?: throw NoSuchMethodException(
"Block '$key' ($blockClass) is missing a create constructor (${javaClass.simpleName}, Block, BlockCreateContext)"
)
} catch (e: Exception) {
throw RuntimeException("Error while finding the create constructor for $key", e)
}

private val loadConstructor: MethodHandle = try{
blockClass.findConstructorMatching(
Block::class.java,
PersistentDataContainer::class.java
) ?: throw NoSuchMethodException(
"Block '$key' ($blockClass) is missing a load constructor (${javaClass.simpleName}, Block, PersistentDataContainer)"
)
} catch (e: Exception) {
throw RuntimeException("Error while finding the load constructor for $key", e)
}

@JvmSynthetic
internal fun create(block: Block, context: BlockCreateContext): RebarBlock {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.github.pylonmc.rebar.datatypes

import io.github.pylonmc.rebar.processor.RebarProcessor
import io.github.pylonmc.rebar.util.rebarKey
import io.github.pylonmc.rebar.util.setNullable
import org.bukkit.Bukkit
import org.bukkit.persistence.PersistentDataAdapterContext
import org.bukkit.persistence.PersistentDataContainer
import org.bukkit.persistence.PersistentDataType

object ProcessorPersistentDataType : PersistentDataType<PersistentDataContainer, RebarProcessor> {
val durationTicksKey = rebarKey("duration_ticks")
val remainingTicksKey = rebarKey("remaining_ticks")

override fun getPrimitiveType(): Class<PersistentDataContainer> = PersistentDataContainer::class.java

override fun getComplexType(): Class<RebarProcessor> = RebarProcessor::class.java

override fun fromPrimitive(
primitive: PersistentDataContainer,
context: PersistentDataAdapterContext
): RebarProcessor {
val durationTicks = primitive.get(durationTicksKey, RebarSerializers.INTEGER)
val remainingTicks = primitive.get(remainingTicksKey, RebarSerializers.INTEGER)
val processor = RebarProcessor()
if (durationTicks != null && remainingTicks != null) {
processor.process = RebarProcessor.RebarProcess(durationTicks, remainingTicks)
}
return processor
}

override fun toPrimitive(complex: RebarProcessor, context: PersistentDataAdapterContext): PersistentDataContainer {
val pdc = context.newPersistentDataContainer()
pdc.setNullable(durationTicksKey, RebarSerializers.INTEGER, complex.durationTicks)
pdc.setNullable(remainingTicksKey, RebarSerializers.INTEGER, complex.remainingTicks)
return pdc
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ object RebarSerializers {
@JvmField
val COMPONENT = ComponentPersistentDataType

@JvmField
val PROCESSOR = ProcessorPersistentDataType

@JvmSynthetic
internal val CARGO_BLOCK_DATA = CargoBlockPersistentDataType

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package io.github.pylonmc.rebar.processor

import org.jetbrains.annotations.ApiStatus

/**
* Unfinished, do not use
*/
@ApiStatus.Experimental
class RebarProcessor {
Comment thread
Seggan marked this conversation as resolved.

internal class RebarProcess(
var durationTicks: Int,
var remainingTicks: Int
)

@JvmSynthetic
internal var process: RebarProcess? = null

private var onStart: Runnable? = null
private var onCancel: Runnable? = null
private var onFinish: Runnable? = null

val isRunning
get() = process != null

val durationTicks
get() = process?.durationTicks

val durationSeconds
get() = durationTicks?.let { it / 20.0 }

val elapsedTicks
get() = process?.let { it.durationTicks - it.remainingTicks }

val elapsedSeconds
get() = elapsedTicks?.let { it / 20.0 }

/**
* The proportion from 0-1 of the process that has been completed so far
*/
val elapsedProportion
get() = remainingProportion?.let { 1.0 - it }

val remainingTicks
get() = process?.remainingTicks

val remainingSeconds
get() = remainingTicks?.let { it / 20.0 }

/**
* The proportion from 0-1 of the process that is left to go
*/
val remainingProportion
get() = process?.let { it.remainingTicks.toDouble() / it.durationTicks }

fun onStart(runnable: Runnable) { onStart = runnable }
fun onCancel(runnable: Runnable) { onCancel = runnable }
fun onFinish(runnable: Runnable) { onFinish = runnable }

fun start(durationTicks: Int) {
process = RebarProcess(durationTicks, durationTicks)
onStart?.run()
}

fun cancel() {
process = null
onCancel?.run()
}

fun tick(ticks: Int) {
if (process == null) {
return
}

process!!.remainingTicks -= ticks

if (process!!.remainingTicks <= 0) {
process = null
onFinish?.run()
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import io.github.pylonmc.rebar.util.overriddenDataTypes
import io.papermc.paper.datacomponent.DataComponentType
import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.RecipeChoice
import org.jetbrains.annotations.Contract
import java.util.function.Predicate


Expand All @@ -42,13 +43,15 @@ class ItemChoice internal constructor(
fun matches(slot: LogisticSlot) = matches(slot.getItemStack())
}

@Contract("null -> false")
fun matchesIgnoringAmount(stack: ItemStack?)
= stack != null && internalChoices.any { it.matches(stack) }

fun matchesIgnoringAmount(slot: LogisticSlot) = slot.getItemStack()?.let { stack ->
internalChoices.any { choice -> choice.matches(stack) }
} ?: false

@Contract("null -> false")
fun matches(stack: ItemStack?)
= stack != null && stack.amount >= amount && matchesIgnoringAmount(stack)

Expand Down
28 changes: 16 additions & 12 deletions rebar/src/main/kotlin/io/github/pylonmc/rebar/util/ProgressBar.kt
Original file line number Diff line number Diff line change
Expand Up @@ -68,11 +68,11 @@ class ProgressBar : ComponentLike {
* (where | represents a filled bar and - an empty bar)
*/
@JvmStatic
fun recipeProgress(progress: Double) = ProgressBar()
fun recipeProgress(elapsedProportion: Double) = ProgressBar()
.barColor(TextColor.fromHexString("#cccccc")!!)
.proportion(progress)
.proportion(elapsedProportion)
.suffix(Component.text(" ")
.append(UnitFormat.PERCENT.format(progress * 100).decimalPlaces(0))
.append(UnitFormat.PERCENT.format(elapsedProportion * 100).decimalPlaces(0))
)

/**
Expand All @@ -81,11 +81,11 @@ class ProgressBar : ComponentLike {
* (where | represents a filled bar and - an empty bar)
*/
@JvmStatic
fun timeRemaining(totalTimeSeconds: Double, remainingTimeSeconds: Double) = ProgressBar()
fun timeRemaining(durationSeconds: Double, remainingSeconds: Double) = ProgressBar()
.barColor(TextColor.fromHexString("#ccafc8")!!)
.proportion(remainingTimeSeconds / totalTimeSeconds)
.proportion(remainingSeconds / durationSeconds)
.suffix(Component.text(" ")
.append(UnitFormat.SECONDS.format(remainingTimeSeconds))
.append(UnitFormat.SECONDS.format(remainingSeconds).decimalPlaces(0))
)

/**
Expand All @@ -94,12 +94,16 @@ class ProgressBar : ComponentLike {
* (where | represents a filled bar and - an empty bar)
*/
@JvmStatic
fun fuelRemaining(total: Double, remaining: Double) = ProgressBar()
.barColor(TextColor.fromHexString("#e4b09f")!!)
.proportion(remaining / total)
.suffix(Component.text(" ")
.append(UnitFormat.SECONDS.format(remaining))
)
fun fuelRemaining(total: Double, remaining: Double): ProgressBar {
check(total != 0.0) { "Total fuel cannot be zero" }
check(remaining <= total + 1.0e-6) { "Remaining fuel must be less than or equal to total (remaining: $remaining, total: $total)" }
return ProgressBar()
.barColor(TextColor.fromHexString("#e4b09f")!!)
.proportion(remaining / total)
.suffix(Component.text(" ")
.append(UnitFormat.SECONDS.format(remaining).decimalPlaces(0))
)
}

/**
* Example: '||||||||||||||||---- 80/100mB'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package io.github.pylonmc.rebar.util.gui.unit

import net.kyori.adventure.text.Component
import net.kyori.adventure.text.ComponentLike
import net.kyori.adventure.text.format.NamedTextColor
import net.kyori.adventure.text.format.Style
import net.kyori.adventure.text.format.TextColor
import java.math.BigDecimal
Expand Down Expand Up @@ -77,6 +78,7 @@ class UnitFormat @JvmOverloads constructor(
private var forceDecimalPlaces = false
private var abbreviate = true
private var unitStyle = defaultStyle
private var valueStyle = Style.empty()
private var prefix: MetricPrefix? = defaultPrefix
private val badPrefixes = EnumSet.noneOf(MetricPrefix::class.java)

Expand Down Expand Up @@ -109,6 +111,11 @@ class UnitFormat @JvmOverloads constructor(
*/
fun unitStyle(style: Style) = apply { this.unitStyle = style }

/**
* Overrides the style of the value.
*/
fun valueStyle(style: Style) = apply { this.valueStyle = style }

/**
* Overrides the default prefix (and adjusts the value shown accordingly).
*/
Expand Down Expand Up @@ -153,7 +160,7 @@ class UnitFormat @JvmOverloads constructor(

usedValue = usedValue.movePointLeft(usedPrefix.scale - defaultPrefix.scale)

val number = Component.text(usedValue.toPlainString())
val number = Component.text(usedValue.toPlainString()).style(valueStyle)
var unit = Component.empty().style(unitStyle)
unit = if (abbreviate && abbreviation != null) {
unit
Expand All @@ -165,7 +172,9 @@ class UnitFormat @JvmOverloads constructor(
.append(if (usedValue == BigDecimal.ONE) singular else plural)
}

return number.append(Component.text(" ")).append(unit)
return number
.append(Component.text(" "))
.append(unit)
}
}

Expand Down
Loading