Skip to content

Commit

Permalink
[6.0.10][publish] update ui & update particle api
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed May 2, 2023
1 parent 897b2bc commit cc0a965
Show file tree
Hide file tree
Showing 4 changed files with 123 additions and 51 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package taboolib.module.nms

import org.bukkit.Location
import org.bukkit.Particle
import org.bukkit.util.Vector
import taboolib.common.util.unsafeLazy

/**
* TabooLib
* taboolib.module.nms.NMSParticle
*
* @author 坏黑
* @since 2023/5/2 21:57
*/
abstract class NMSParticle {

abstract fun createParticlePacket(particle: Particle, location: Location, offset: Vector = Vector(), speed: Double = 0.0, count: Int = 1, data: Any? = null): Any

companion object {

val instance by unsafeLazy { nmsProxy<NMSParticle>() }

/** 创建粒子数据包 */
fun Particle.createPacket(location: Location, offset: Vector = Vector(), speed: Double = 0.0, count: Int = 1, data: Any? = null): Any {
return instance.createParticlePacket(this, location, offset, speed, count, data)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package taboolib.module.nms

import net.minecraft.server.v1_16_R1.PacketPlayOutWorldParticles
import org.bukkit.Location
import org.bukkit.Particle
import org.bukkit.craftbukkit.v1_16_R1.CraftParticle
import org.bukkit.util.Vector

/**
* TabooLib
* taboolib.module.nms.NMSParticleImpl
*
* @author 坏黑
* @since 2023/5/2 21:58
*/
class NMSParticleImpl : NMSParticle() {

override fun createParticlePacket(particle: Particle, location: Location, offset: Vector, speed: Double, count: Int, data: Any?): Any {
if (data != null && !particle.dataType.isInstance(data)) {
error("data should be ${particle.dataType} got ${data.javaClass}")
}
return PacketPlayOutWorldParticles(
CraftParticle.toNMS(particle, data),
true,
location.x,
location.y,
location.z,
offset.x.toFloat(),
offset.y.toFloat(),
offset.z.toFloat(),
speed.toFloat(),
count
)
}
}
12 changes: 12 additions & 0 deletions module/module-ui/src/main/kotlin/taboolib/module/ui/type/Linked.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ open class Linked<T>(title: String) : Basic(title) {
/** 异步元素生成回调 **/
internal var asyncGenerateCallback: ((player: Player, element: T, index: Int, slot: Int) -> ItemStack) = { _, _, _, _ -> ItemStack(Material.AIR) }

/** 页面切换回调 */
internal var pageChangeCallback: ((player: Player) -> Unit) = { _ -> }

/** 页面玩家 **/
private lateinit var player: Player

Expand Down Expand Up @@ -120,6 +123,7 @@ open class Linked<T>(title: String) : Basic(title) {
} else {
player.openInventory(build())
}
pageChangeCallback(player)
}
}
}
Expand All @@ -140,10 +144,18 @@ open class Linked<T>(title: String) : Basic(title) {
} else {
player.openInventory(build())
}
pageChangeCallback(player)
}
}
}

/**
* 切换页面回调
*/
open fun onPageChange(callback: (player: Player) -> Unit) {
pageChangeCallback = callback
}

/**
* 是否可以返回上一页
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,61 +369,58 @@ class BukkitPlayer(val player: Player) : ProxyPlayer {
}

override fun sendParticle(particle: ProxyParticle, location: Location, offset: Vector, count: Int, speed: Double, data: ProxyParticle.Data?) {
val bukkitParticle = try {
Particle.valueOf(particle.name)
} catch (ignored: IllegalArgumentException) {
error("Unsupported particle ${particle.name}")
}
player.spawnParticle(
bukkitParticle, location.toBukkitLocation(), count, offset.x, offset.y, offset.z, speed, when (data) {
is ProxyParticle.DustTransitionData -> {
Particle.DustTransition(
Color.fromRGB(data.color.red, data.color.green, data.color.blue),
Color.fromRGB(data.toColor.red, data.toColor.blue, data.toColor.green),
data.size
)
}
is ProxyParticle.DustData -> {
Particle.DustOptions(Color.fromRGB(data.color.red, data.color.green, data.color.blue), data.size)
}
is ProxyParticle.ItemData -> {
val item = ItemStack(Material.valueOf(data.material))
val itemMeta = item.itemMeta!!
itemMeta.setDisplayName(data.name)
itemMeta.lore = data.lore
try {
itemMeta.setCustomModelData(data.customModelData)
} catch (ignored: NoSuchMethodError) {
}
item.itemMeta = itemMeta
if (data.data != 0) {
item.durability = data.data.toShort()
}
item
// 获取粒子
val bukkitParticle = runCatching { Particle.valueOf(particle.name) }.getOrNull() ?: error("Unsupported particle ${particle.name}")
// 获取粒子数据
val bukkitData: Any? = when (data) {
is ProxyParticle.DustTransitionData -> {
Particle.DustTransition(
Color.fromRGB(data.color.red, data.color.green, data.color.blue),
Color.fromRGB(data.toColor.red, data.toColor.blue, data.toColor.green),
data.size
)
}
is ProxyParticle.DustData -> {
Particle.DustOptions(Color.fromRGB(data.color.red, data.color.green, data.color.blue), data.size)
}
is ProxyParticle.ItemData -> {
val item = ItemStack(Material.valueOf(data.material))
val itemMeta = item.itemMeta!!
itemMeta.setDisplayName(data.name)
itemMeta.lore = data.lore
try {
itemMeta.setCustomModelData(data.customModelData)
} catch (ignored: NoSuchMethodError) {
}
is ProxyParticle.BlockData -> {
if (bukkitParticle.dataType == MaterialData::class.java) {
MaterialData(Material.valueOf(data.material), data.data.toByte())
} else {
Material.valueOf(data.material).createBlockData()
}
item.itemMeta = itemMeta
if (data.data != 0) {
item.durability = data.data.toShort()
}
is ProxyParticle.VibrationData -> {
Vibration(
data.origin.toBukkitLocation(), when (val destination = data.destination) {
is ProxyParticle.VibrationData.LocationDestination -> {
Vibration.Destination.BlockDestination(destination.location.toBukkitLocation())
}
is ProxyParticle.VibrationData.EntityDestination -> {
Vibration.Destination.EntityDestination(Bukkit.getEntity(destination.entity)!!)
}
else -> error("out of case")
}, data.arrivalTime
)
item
}
is ProxyParticle.BlockData -> {
if (bukkitParticle.dataType == MaterialData::class.java) {
MaterialData(Material.valueOf(data.material), data.data.toByte())
} else {
Material.valueOf(data.material).createBlockData()
}
else -> null
}
)
is ProxyParticle.VibrationData -> {
Vibration(
data.origin.toBukkitLocation(), when (val destination = data.destination) {
is ProxyParticle.VibrationData.LocationDestination -> {
Vibration.Destination.BlockDestination(destination.location.toBukkitLocation())
}
is ProxyParticle.VibrationData.EntityDestination -> {
Vibration.Destination.EntityDestination(Bukkit.getEntity(destination.entity)!!)
}
else -> error("out of case")
}, data.arrivalTime
)
}
else -> null
}
player.spawnParticle(bukkitParticle, location.toBukkitLocation(), count, offset.x, offset.y, offset.z, speed, bukkitData)
}

override fun performCommand(command: String): Boolean {
Expand Down

0 comments on commit cc0a965

Please sign in to comment.