Skip to content

Commit

Permalink
Adds Skript support.
Browse files Browse the repository at this point in the history
  • Loading branch information
toxicity188 committed Mar 19, 2024
1 parent 179d8d6 commit 9e7a3a4
Show file tree
Hide file tree
Showing 9 changed files with 209 additions and 18 deletions.
29 changes: 29 additions & 0 deletions api/src/main/java/kr/toxicity/hud/api/event/CustomPopupEvent.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package kr.toxicity.hud.api.event;

import lombok.Getter;
import org.bukkit.entity.Player;
import org.bukkit.event.HandlerList;
import org.bukkit.event.player.PlayerEvent;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.Map;

@Getter
public class CustomPopupEvent extends PlayerEvent implements BetterHudEvent {
private final String name;
private final Map<String, String> variables = new HashMap<>();
public CustomPopupEvent(@NotNull Player who, @NotNull String name) {
super(who);
this.name = name;
}

@NotNull
@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
}
public static HandlerList getHandlerList() {
return HANDLER_LIST;
}
}
2 changes: 2 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/BetterHudImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import kr.toxicity.hud.manager.PlaceholderManagerImpl
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.scheduler.FoliaScheduler
import kr.toxicity.hud.scheduler.StandardScheduler
import kr.toxicity.hud.skript.SkriptManager
import kr.toxicity.hud.util.PLUGIN
import kr.toxicity.hud.util.info
import kr.toxicity.hud.util.task
Expand All @@ -40,6 +41,7 @@ class BetterHudImpl: BetterHud() {
ConfigManager,
CommandManager,
CompatibilityManager,
SkriptManager,
ModuleManager,
DatabaseManagerImpl,

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package kr.toxicity.hud.manager

import kr.toxicity.hud.api.event.CustomPopupEvent
import kr.toxicity.hud.api.manager.PlaceholderManager
import kr.toxicity.hud.api.placeholder.HudPlaceholder
import kr.toxicity.hud.api.placeholder.PlaceholderContainer
Expand Down Expand Up @@ -166,6 +167,17 @@ object PlaceholderManagerImpl: PlaceholderManager, BetterHudManager {
p.variableMap[args[0]] ?: "<none>"
}
}
},
"custom_variable" to object : HudPlaceholder<String> {
override fun getRequiredArgsLength(): Int = 1
override fun invoke(args: MutableList<String>, reason: UpdateEvent): Function<HudPlayer, String> {
val key = args[0]
return reason.unwrap { e: CustomPopupEvent ->
Function { _ ->
e.variables[key] ?: "<none>"
}
}
}
}
), {
val matcher = stringPattern.matcher(it)
Expand Down Expand Up @@ -305,9 +317,9 @@ object PlaceholderManagerImpl: PlaceholderManager, BetterHudManager {
}
}

fun parse(r: UpdateEvent, target: String): (HudPlayer) -> String {
fun parse(target: String): (UpdateEvent) -> (HudPlayer) -> String {
var skip = false
val builder = ArrayList<(HudPlayer) -> String>()
val builder = ArrayList<(UpdateEvent) -> (HudPlayer) -> String>()
val sb = StringBuilder()
target.forEach { char ->
if (!skip) {
Expand All @@ -316,17 +328,23 @@ object PlaceholderManagerImpl: PlaceholderManager, BetterHudManager {
'[' -> {
val build = sb.toString()
builder.add {
build
{
build
}
}
sb.setLength(0)
}
']' -> {
val result = sb.toString()
sb.setLength(0)
val find = find(result)
runCatching {
val find = find(result).build(r)
builder.add {
find.stringValue(it)
builder.add { r ->
find.build(r).let { b ->
{
b.stringValue(it)
}
}
}
}
}
Expand All @@ -342,15 +360,23 @@ object PlaceholderManagerImpl: PlaceholderManager, BetterHudManager {
if (sb.isNotEmpty()) {
val build = sb.toString()
builder.add {
build
{
build
}
}
}
return { p ->
val result = StringBuilder()
builder.forEach {
result.append(it(p))
return { r ->
builder.map {
it(r)
}.let { bb ->
{ p ->
val result = StringBuilder()
bb.forEach {
result.append(it(p))
}
result.toString()
}
}
result.toString()
}
}

Expand Down
13 changes: 10 additions & 3 deletions dist/src/main/kotlin/kr/toxicity/hud/manager/TriggerManagerImpl.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,35 @@
package kr.toxicity.hud.manager

import kr.toxicity.hud.api.event.CustomPopupEvent
import kr.toxicity.hud.api.manager.TriggerManager
import kr.toxicity.hud.api.trgger.HudBukkitEventTrigger
import kr.toxicity.hud.api.trgger.HudTrigger
import kr.toxicity.hud.api.update.BukkitEventUpdateEvent
import kr.toxicity.hud.api.update.UpdateEvent
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.util.PLUGIN
import kr.toxicity.hud.util.createBukkitTrigger
import kr.toxicity.hud.util.ifNull
import org.bukkit.Bukkit
import org.bukkit.configuration.ConfigurationSection
import org.bukkit.entity.Player
import org.bukkit.event.Event
import org.bukkit.event.EventPriority
import org.bukkit.event.HandlerList
import org.bukkit.event.Listener
import org.bukkit.event.entity.EntityDamageByEntityEvent
import java.util.UUID
import java.util.function.Function

object TriggerManagerImpl: BetterHudManager, TriggerManager {
private val listener = object : Listener {}

private val map = mutableMapOf<String, (ConfigurationSection) -> HudTrigger<*>>()
private val map = mutableMapOf<String, (ConfigurationSection) -> HudTrigger<*>>(
"custom" to {
val n = it.getString("name").ifNull("name value not set.")
createBukkitTrigger(CustomPopupEvent::class.java, { e ->
if (e.name == n) e.player.uniqueId else null
})
}
)

override fun start() {

Expand Down
7 changes: 4 additions & 3 deletions dist/src/main/kotlin/kr/toxicity/hud/renderer/TextRenderer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kr.toxicity.hud.layout.LayoutAlign
import kr.toxicity.hud.layout.TextLayout
import kr.toxicity.hud.manager.PlaceholderManagerImpl
import kr.toxicity.hud.placeholder.ConditionBuilder
import kr.toxicity.hud.placeholder.PlaceholderBuilder
import kr.toxicity.hud.util.*
import net.kyori.adventure.key.Key
import net.kyori.adventure.text.Component
Expand Down Expand Up @@ -133,7 +134,7 @@ class TextRenderer(
})

fun build() = ComponentStyle(
pattern,
PlaceholderManagerImpl.parse(pattern),
Style.style(color)
.decorations(decoration)
.font(key),
Expand All @@ -146,12 +147,12 @@ class TextRenderer(
)
}
private class ComponentStyle(
val pattern: String,
val pattern: (UpdateEvent) -> (HudPlayer) -> String,
val style: Style,
val multiply: Int
) {
fun map(updateEvent: UpdateEvent) = MappedComponentStyle(
PlaceholderManagerImpl.parse(updateEvent, pattern),
pattern(updateEvent),
style,
multiply
)
Expand Down
23 changes: 23 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/skript/SkriptManager.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package kr.toxicity.hud.skript

import ch.njol.skript.Skript
import kr.toxicity.hud.manager.BetterHudManager
import kr.toxicity.hud.resource.GlobalResource
import kr.toxicity.hud.skript.effect.EffCallPopupEvent
import kr.toxicity.hud.skript.effect.EffShowPopup
import org.bukkit.Bukkit

object SkriptManager: BetterHudManager {
override fun start() {
if (Bukkit.getPluginManager().isPluginEnabled("Skript")) {
Skript.registerEffect(EffShowPopup::class.java, "[show] popup %string% to %players% [with [variable] [of] %-objects%] [keyed by %-object%]")
Skript.registerEffect(EffCallPopupEvent::class.java, "call popup event for %players% named %string% [with [variable] [of] %-objects%] [keyed by %-object%]")
}
}

override fun reload(resource: GlobalResource) {
}

override fun end() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package kr.toxicity.hud.skript.effect

import ch.njol.skript.lang.Effect
import ch.njol.skript.lang.Expression
import ch.njol.skript.lang.SkriptParser
import ch.njol.skript.util.LiteralUtils
import ch.njol.util.Kleenean
import kr.toxicity.hud.api.event.CustomPopupEvent
import kr.toxicity.hud.util.call
import org.bukkit.entity.Player
import org.bukkit.event.Event

class EffCallPopupEvent: Effect() {
override fun toString(p0: Event?, p1: Boolean): String {
return "call popup event for ${player.toString(p0, p1)} named ${name.toString(p0, p1)} with variable of ${objects?.toString(p0, p1)} keyed by ${key?.toString(p0, p1)}"
}

private lateinit var player: Expression<Player>
private lateinit var name: Expression<String>
private var objects: Expression<*>? = null
private var key: Expression<*>? = null

@Suppress("UNCHECKED_CAST")
override fun init(p0: Array<out Expression<*>?>, p1: Int, p2: Kleenean, p3: SkriptParser.ParseResult): Boolean {
player = p0[0] as Expression<Player>
name = p0[1] as Expression<String>
if (p0[2] != null) {
objects = p0[2] as Expression<String>
if (p0[3] != null) key = p0[3]
if (LiteralUtils.hasUnparsedLiteral(objects)) objects = LiteralUtils.defendExpression<Any>(objects)
}
return true
}

override fun execute(p0: Event) {
val obj = objects?.getAll(p0) ?: emptyArray()
name.getSingle(p0)?.let { n ->
player.getAll(p0).forEach { p ->
CustomPopupEvent(p, n).apply {
obj.forEachIndexed { i, s ->
variables["skript_${i + 1}"] = s.toString()
}
}.call()
}
}

}
}
54 changes: 54 additions & 0 deletions dist/src/main/kotlin/kr/toxicity/hud/skript/effect/EffShowPopup.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package kr.toxicity.hud.skript.effect

import ch.njol.skript.lang.Effect
import ch.njol.skript.lang.Expression
import ch.njol.skript.lang.ExpressionList
import ch.njol.skript.lang.SkriptParser
import ch.njol.skript.util.LiteralUtils
import ch.njol.util.Kleenean
import kr.toxicity.hud.api.event.CustomPopupEvent
import kr.toxicity.hud.api.update.BukkitEventUpdateEvent
import kr.toxicity.hud.manager.PopupManagerImpl
import kr.toxicity.hud.util.call
import org.bukkit.entity.Player
import org.bukkit.event.Event
import java.util.UUID

class EffShowPopup: Effect() {
override fun toString(p0: Event?, p1: Boolean): String {
return "show popup ${popup.toString(p0, p1)} to ${player.toString(p0, p1)} with variable of ${objects?.toString(p0, p1)} keyed by ${key?.toString(p0, p1)}"
}

private lateinit var popup: Expression<String>
private lateinit var player: Expression<Player>
private var objects: Expression<*>? = null
private var key: Expression<*>? = null

@Suppress("UNCHECKED_CAST")
override fun init(p0: Array<out Expression<*>?>, p1: Int, p2: Kleenean, p3: SkriptParser.ParseResult): Boolean {
popup = p0[0] as Expression<String>
player = p0[1] as Expression<Player>
if (p0[2] != null) {
objects = p0[2] as Expression<String>
if (p0[3] != null) key = p0[3]
if (LiteralUtils.hasUnparsedLiteral(objects)) objects = LiteralUtils.defendExpression<Any>(objects)
}
return true
}

override fun execute(p0: Event) {
val popup = PopupManagerImpl.getPopup(popup.getSingle(p0) ?: return) ?: return
val obj = objects?.getAll(p0) ?: emptyArray()
player.getAll(p0).forEach { p ->
val event = CustomPopupEvent(p, "").apply {
obj.forEachIndexed { i, s ->
variables["skript_${i + 1}"] = s.toString()
}
}
runCatching {
popup.show(BukkitEventUpdateEvent(event, key?.getSingle(p0) ?: UUID.randomUUID()), p)
}
}

}
}
1 change: 1 addition & 0 deletions dist/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ softdepend:
- Vault
- floodgate
- Geyser-Spigot
- Skript
commands:
betterhud:
aliases:
Expand Down

0 comments on commit 9e7a3a4

Please sign in to comment.