Skip to content

Commit

Permalink
[6.2.0] 为 ItemBuilder 增加 unique 方法
Browse files Browse the repository at this point in the history
  • Loading branch information
Bkm016 committed Oct 17, 2024
1 parent 0ed9943 commit 1c8cac5
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,7 @@ val isBukkitServerRunning: Boolean
* 获取 MinecraftServer 实例
*/
val minecraftServerObject: Any by unsafeLazy {
Bukkit.getServer().getProperty(
when (MinecraftVersion.major) {
// 1.8, 1.9, 1.10, 1.11, 1.12, 1.13 类型为:MinecraftServer
in MinecraftVersion.V1_8..MinecraftVersion.V1_13 -> "console"
// 1.14, 1.15, 1.16, 1.17, 1.18, 1.19, 1.20 类型为:DedicatedServer
in MinecraftVersion.V1_14..MinecraftVersion.V1_20 -> "console"
// 其他版本
else -> "console"
}
)!!
Bukkit.getServer().getProperty("console")!!
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ package taboolib.platform.util
import org.bukkit.ChatColor
import org.bukkit.Color
import org.bukkit.Material
import org.bukkit.attribute.Attribute
import org.bukkit.attribute.AttributeModifier
import org.bukkit.block.banner.Pattern
import org.bukkit.enchantments.Enchantment
import org.bukkit.entity.EntityType
Expand All @@ -15,6 +17,7 @@ import org.bukkit.potion.PotionData
import org.bukkit.potion.PotionEffect
import org.tabooproject.reflex.Reflex.Companion.getProperty
import org.tabooproject.reflex.Reflex.Companion.invokeMethod
import taboolib.common.util.random
import taboolib.library.xseries.XMaterial
import taboolib.module.chat.colored
import java.util.*
Expand Down Expand Up @@ -138,6 +141,11 @@ open class ItemBuilder {
*/
var customModelData = -1

/**
* 唯一化
*/
var unique = false

/**
* 原始数据
* 尝试修复自定义 nbt 失效的问题
Expand Down Expand Up @@ -171,6 +179,15 @@ open class ItemBuilder {
flags.addAll(ItemFlag.values())
}

/**
* 唯一化
* 写入一个唯一的属性以避免被合并,通常用于页面元件
* 在低版本不可用
*/
fun unique() {
unique = true
}

/**
* 上色
*/
Expand Down Expand Up @@ -246,29 +263,38 @@ open class ItemBuilder {
} catch (ex: NoSuchMethodError) {
try {
itemMeta.invokeMethod<Any>("spigot")!!.invokeMethod<Any>("setUnbreakable", isUnbreakable)
} catch (ignored: NoSuchMethodException) {
} catch (_: NoSuchMethodException) {
}
}
//
try {
if (spawnType != null && itemMeta is SpawnEggMeta) {
itemMeta.spawnedType = spawnType
}
} catch (ignored: NoClassDefFoundError) {
} catch (_: NoClassDefFoundError) {
}
// 旗帜
try {
if (patterns.isNotEmpty() && itemMeta is BannerMeta) {
patterns.forEach { itemMeta.addPattern(it) }
}
} catch (ignored: NoClassDefFoundError) {
} catch (_: NoClassDefFoundError) {
}
// CustomModelData
try {
if (customModelData != -1) {
itemMeta.invokeMethod<Void>("setCustomModelData", customModelData)
}
} catch (ignored: NoSuchMethodException) {
} catch (_: NoSuchMethodException) {
}
// 唯一化
try {
if (unique) {
val modifier = AttributeModifier(UUID.randomUUID(), "unique", random(0.0, 1.0), AttributeModifier.Operation.ADD_NUMBER)
itemMeta.addAttributeModifier(Attribute.GENERIC_ATTACK_DAMAGE, modifier)
itemMeta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES)
}
} catch (_: NoSuchMethodError) {
}

// 返回
Expand Down

0 comments on commit 1c8cac5

Please sign in to comment.