Skip to content

Commit

Permalink
map sounds + cram gui progress + relational placeholders impl
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt-MX committed Aug 6, 2024
1 parent d2798e1 commit 00511af
Show file tree
Hide file tree
Showing 19 changed files with 353 additions and 133 deletions.
4 changes: 2 additions & 2 deletions api/.gradle/caches/paperweight/taskCache/reobfJar.log
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Command: C:\Program Files\Java\jdk-17\bin\java.exe -Xmx1G -classpath C:\Users\Mangr\.gradle\caches\modules-2\files-2.1\net.fabricmc\tiny-remapper\0.10.1\c293b2384ae12af74f407fa3aaa553bba4ac6763\tiny-remapper-0.10.1-fat.jar net.fabricmc.tinyremapper.Main D:\PC\Projects\KtBukkitGui\api\build\libs\ktgui-2.4.3-alpha-dev-all.jar D:\PC\Projects\KtBukkitGui\api\build\libs\api-2.4.3-alpha.jar C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\extractDevBundle.dir\data\mojang+yarn-spigot-reobf.tiny mojang+yarn spigot C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\applyMojangMappedPaperclipPatch.jar --threads=1
Finished after 2700.36 ms.
Command: C:\Users\Mangr\.gradle\jdks\adoptium-21-x64-hotspot-windows\bin\java.exe -Xmx1G -classpath C:\Users\Mangr\.gradle\caches\modules-2\files-2.1\net.fabricmc\tiny-remapper\0.10.1\c293b2384ae12af74f407fa3aaa553bba4ac6763\tiny-remapper-0.10.1-fat.jar net.fabricmc.tinyremapper.Main D:\PC\Projects\KtBukkitGui\api\build\libs\ktgui-2.4.4-alpha-dev-all.jar D:\PC\Projects\KtBukkitGui\api\build\libs\api-2.4.4-alpha.jar C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\extractDevBundle.dir\data\mojang+yarn-spigot-reobf.tiny mojang+yarn spigot C:\Users\Mangr\.gradle\caches\paperweight-userdev\ff775525efc29c3503a07d1006e63e5695a742b7505cf63e157d49d32419c69f\module\io.papermc.paper\dev-bundle\1.20.4-R0.1-SNAPSHOT\paperweight\setupCache\applyMojangMappedPaperclipPatch.jar --threads=1
Finished after 2715.83 ms.
5 changes: 1 addition & 4 deletions api/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,10 @@ tasks {
}
}

kotlin {
jvmToolchain(17)
}

java {
withJavadocJar()
withSourcesJar()
toolchain.languageVersion = JavaLanguageVersion.of(21)
}

publishing {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import org.bukkit.Material
import org.bukkit.inventory.ItemStack

class GuiPattern(
private var pattern: String
var pattern: String
) {
private val items = hashMapOf<Char, IGuiButton<*>>()
var blankSpaceChar = '-'
Expand Down Expand Up @@ -53,12 +53,6 @@ class GuiPattern(
return map
}

fun setPattern(pattern: String) {
this.pattern = pattern
}

fun getPattern() = pattern

private fun trimPattern() {
pattern = pattern.replace("\n", "")
.replace("\r\n", "")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
package com.mattmx.ktgui.components.screen.pagination

import com.mattmx.ktgui.components.GuiPattern
import com.mattmx.ktgui.components.button.GuiButton
import com.mattmx.ktgui.components.screen.GuiScreen
import com.mattmx.ktgui.dsl.button
import com.mattmx.ktgui.utils.not
import com.mattmx.ktgui.components.screen.pagination.GuiCramMultiPageScreen.NextSlotStrategy
import net.kyori.adventure.text.Component
import org.bukkit.Material

class GuiCramMultiPageScreen(
open class GuiCramMultiPageScreen(
title: Component,
rows: Int = 6
) : GuiMultiPageScreen(title, rows) {
val extraReservedSlots = arrayListOf<Int>()
var tryGetNextSlot = NextSlotStrategy.next()
var nextSlot: Int = -1

infix fun reserve(slots: IntRange) = extraReservedSlots.addAll(slots)
infix fun reserve(slots: List<Int>) = extraReservedSlots.addAll(slots)
Expand All @@ -26,20 +27,20 @@ class GuiCramMultiPageScreen(
lastPage = GuiScreen(Component.empty(), rows).apply { pages.add(this) }
}

val nextSlot = nextSlotToFill(lastPage)
if (nextSlot == null) {
val foundSlot = findNextEmptySpot(lastPage)
println("placing at $foundSlot")
if (foundSlot == null) {
nextSlot = -1
GuiScreen(Component.empty(), rows).apply { pages.add(this) }
return cramAdd(child)
}

child childOf lastPage slot nextSlot
child childOf lastPage slot foundSlot
}

fun nextSlotToFill(sub: GuiScreen): Int? {
var nextSlot = (sub.slotsUsed().maxOrNull() ?: -1) + 1

while (nextSlot in reservedSlots() && nextSlot < sub.totalSlots()) {
nextSlot++
open fun findNextEmptySpot(sub: GuiScreen): Int? {
while ((nextSlot in reservedSlots() || sub.items[nextSlot] != null) && (nextSlot < sub.totalSlots() || nextSlot < 0)) {
nextSlot = tryGetNextSlot.getNextSlotToTry(nextSlot)
}

if (nextSlot >= sub.totalSlots()) return null
Expand All @@ -51,6 +52,38 @@ class GuiCramMultiPageScreen(

fun reservedSlots() = (this.slotsUsed() + extraReservedSlots).toSet()

fun interface NextSlotStrategy {
fun getNextSlotToTry(previous: Int): Int

companion object {
@JvmStatic
fun next(gap: Int = 1) = NextSlotStrategy { it + gap }

@JvmStatic
fun pattern(char: Char, pattern: GuiPattern): NextSlotStrategy {
if (!pattern.pattern.contains(char)) {
// Prevent stack overflow exception
error("The pattern provided does not contain the char '$char'")
}

return NextSlotStrategy {
if (pattern.pattern.length < it) return@NextSlotStrategy Int.MAX_VALUE
val nextIndex = it + 1

val next = nextIndex + pattern.pattern
.substring(nextIndex + 1, pattern.pattern.length)
.indexOf(char)

println("char at $next")

if (next == -1) {
// No more slots
Int.MAX_VALUE
} else next
}
}
}
}
}

fun cramMultiPageScreen(title: Component, rows: Int = 6, block: GuiCramMultiPageScreen.() -> Unit) =
Expand Down
60 changes: 60 additions & 0 deletions api/src/main/kotlin/com/mattmx/ktgui/dsl/papi.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.mattmx.ktgui.dsl

import com.mattmx.ktgui.commands.declarative.ChainCommandBuilder
import com.mattmx.ktgui.commands.declarative.arg.Argument
import com.mattmx.ktgui.papi.Placeholder
import com.mattmx.ktgui.papi.PlaceholderExpansionWrapper
import com.mattmx.ktgui.papi.PlaceholderParseContext
import com.mattmx.ktgui.papi.RelationalPlaceholderParseContext
import com.mattmx.ktgui.scheduling.syncDelayed
import org.bukkit.plugin.java.JavaPlugin
import java.util.*

inline fun JavaPlugin.placeholderExpansion(builder: PlaceholderExpansionWrapper.() -> Unit) =
PlaceholderExpansionWrapper(this)
.apply(builder)
.apply {
syncDelayed(2) { register() }
}

fun PlaceholderExpansionWrapper.placeholder(string: String, supplier: PlaceholderParseContext.() -> Any?) =
Placeholder(this, ChainCommandBuilder(string)).apply {
parseDefault = Optional.of(supplier)
registerPlaceholder(this)
}

fun PlaceholderExpansionWrapper.placeholder(chain: ChainCommandBuilder, supplier: PlaceholderParseContext.() -> Any?) =
Placeholder(this, chain).apply {
parseDefault = Optional.of(supplier)
registerPlaceholder(this)
}

fun PlaceholderExpansionWrapper.placeholder(argument: Argument<*>, supplier: PlaceholderParseContext.() -> Any?) =
Placeholder(
this,
Placeholder.emptyCommandBuilder().argument(argument)
).apply {
parseDefault = Optional.of(supplier)
registerPlaceholder(this)
}

fun PlaceholderExpansionWrapper.relational(string: String, supplier: RelationalPlaceholderParseContext.() -> Any?) =
Placeholder(this, ChainCommandBuilder(string)).apply {
parseRelational = Optional.of(supplier)
registerPlaceholder(this)
}

fun PlaceholderExpansionWrapper.relational(chain: ChainCommandBuilder, supplier: RelationalPlaceholderParseContext.() -> Any?) =
Placeholder(this, chain).apply {
parseRelational = Optional.of(supplier)
registerPlaceholder(this)
}

fun PlaceholderExpansionWrapper.relational(argument: Argument<*>, supplier: RelationalPlaceholderParseContext.() -> Any?) =
Placeholder(
this,
Placeholder.emptyCommandBuilder().argument(argument)
).apply {
parseRelational = Optional.of(supplier)
registerPlaceholder(this)
}
37 changes: 30 additions & 7 deletions api/src/main/kotlin/com/mattmx/ktgui/papi/Placeholder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,27 @@ import java.util.*

class Placeholder(
val owner: PlaceholderExpansionWrapper,
val match: ChainCommandBuilder,
val supplier: (PlaceholderParseContext) -> Any?
val match: ChainCommandBuilder
) {
var parseDefault = Optional.empty<(PlaceholderParseContext) -> Any?>()
var parseRelational = Optional.empty<(RelationalPlaceholderParseContext) -> Any?>()
var description = Optional.empty<String>()
private set
var priority = 0

fun parse(context: PlaceholderParseContext) = supplier.invoke(context)
fun parseRelationally(context: RelationalPlaceholderParseContext) =
parseRelational.orElse(null)?.invoke(context)

fun parse(context: PlaceholderParseContext) =
parseDefault.orElse(null)?.invoke(context)

infix fun normally(block: PlaceholderParseContext.() -> Any?) = apply {
this.parseDefault = Optional.of(block)
}

infix fun relationally(block: RelationalPlaceholderParseContext.() -> Any?) = apply {
this.parseRelational = Optional.of(block)
}

infix fun description(desc: String?) = apply {
this.description = Optional.ofNullable(desc)
Expand All @@ -30,7 +43,9 @@ class Placeholder(
class Builder {
var match = Optional.empty<ChainCommandBuilder>()
private set
var supplier = Optional.empty<(PlaceholderParseContext) -> Any?>()
var defaultParse = Optional.empty<(PlaceholderParseContext) -> Any?>()
private set
var relationalParse = Optional.empty<(RelationalPlaceholderParseContext) -> Any?>()
private set
var description = Optional.empty<String>()
private set
Expand All @@ -42,7 +57,11 @@ class Placeholder(
}

infix fun supplier(supplier: (PlaceholderParseContext) -> Any?) = apply {
this.supplier = Optional.of(supplier)
this.defaultParse = Optional.of(supplier)
}

infix fun relationally(supplier: (RelationalPlaceholderParseContext) -> Any?) = apply {
this.relationalParse = Optional.of(supplier)
}

infix fun description(description: String?) = apply {
Expand All @@ -54,9 +73,13 @@ class Placeholder(
}

fun build(owner: PlaceholderExpansionWrapper) =
Placeholder(owner, match.get(), supplier.get())
Placeholder(owner, match.get())
.description(description.orElse(null))
.apply { priority = this@Builder.priority }
.apply {
parseDefault = this@Builder.defaultParse
parseRelational = this@Builder.relationalParse
priority = this@Builder.priority
}
}

companion object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ package com.mattmx.ktgui.papi
import com.mattmx.ktgui.commands.declarative.ChainCommandBuilder
import com.mattmx.ktgui.commands.declarative.DeclarativeCommandBuilder
import com.mattmx.ktgui.commands.declarative.arg.Argument
import com.mattmx.ktgui.commands.declarative.arg.ArgumentContext
import com.mattmx.ktgui.commands.declarative.arg.ArgumentProcessor
import com.mattmx.ktgui.commands.declarative.div
import com.mattmx.ktgui.commands.declarative.invocation.StorageCommandContext
import com.mattmx.ktgui.utils.JavaCompatibility
import me.clip.placeholderapi.expansion.PlaceholderExpansion
import me.clip.placeholderapi.expansion.Relational
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin
import java.util.*

class PlaceholderExpansionWrapper(
private val owner: JavaPlugin
) : PlaceholderExpansion() {
val owner: JavaPlugin
) : PlaceholderExpansion(), Relational {
private val placeholders = arrayListOf<Placeholder>()
private val parser = PlaceholderParser(this)
var id = owner.name
private set
var _persists = false
Expand All @@ -34,6 +33,7 @@ class PlaceholderExpansionWrapper(
override fun getAuthor() = _author
override fun getVersion() = _version
override fun getPlaceholders() = placeholders.map { it.toString() }.toMutableList()
fun getPlaceholdersList() = placeholders
override fun persist() = _persists
override fun canRegister() = if (requiresPredicate.isPresent) requiresPredicate.get()() else true

Expand Down Expand Up @@ -80,56 +80,26 @@ class PlaceholderExpansionWrapper(

override fun onPlaceholderRequest(player: Player?, params: String): String? {
val paramsSplit = splitArgs(params)
val (placeholder, args) = parser.parse(player, params) ?: return null

val args = hashMapOf<String, ArgumentContext<*>>()
val baseContext = if (player != null)
StorageCommandContext(player, paramsSplit.firstOrNull() ?: "", paramsSplit)
else null

for (placeholder in placeholders.sortedByDescending { it.priority }) {
val identifier = placeholder.match.name
if (identifier != Placeholder.EMPTY_PLACEHOLDER && paramsSplit.getOrNull(0) != identifier)
continue

val argumentParser = ArgumentProcessor(
emptyCommand,
baseContext,
paramsSplit.subList(1, paramsSplit.size)
)

var invalid = false

for (expArg in placeholder.match.arguments) {
if (invalid) continue

val consumeResult = expArg.consume(argumentParser)

if (expArg.isRequired() && consumeResult.isEmpty()) {
invalid = true
if (isDebug) {
owner.logger.warning(
"Placeholder(${
identifier
}) Failed parsing for arg $expArg in placeholder $name - $consumeResult"
)
}
continue
} else {
args[expArg.name()] = expArg.createContext(
emptyCommand,
baseContext,
consumeResult.args ?: emptyList()
)
}
}
if (invalid) continue

val context = PlaceholderParseContext(player, paramsSplit, args)
val result = placeholder.parse(context)

if (result != null) {
return result.toString()
}
val context = PlaceholderParseContext(player, paramsSplit, args)
val result = placeholder.parse(context)

if (result != null) {
return result.toString()
}
return null
}

override fun onPlaceholderRequest(one: Player, two: Player, params: String): String? {
val paramsSplit = splitArgs(params)
val (placeholder, args) = parser.parse(one, params) ?: return null

val context = RelationalPlaceholderParseContext(one, two, paramsSplit, args)
val result = placeholder.parseRelationally(context)

if (result != null) {
return result.toString()
}
return null
}
Expand Down
Loading

0 comments on commit 00511af

Please sign in to comment.