Skip to content

Commit

Permalink
🔒 Change PowerNBT to NBTApi for better compatibility
Browse files Browse the repository at this point in the history
✨ Add val logger for Item
💥 BreakChange: features.NBT.api has changed
  • Loading branch information
way-zer committed Mar 3, 2020
1 parent e13e754 commit 41fbc0e
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 52 deletions.
21 changes: 6 additions & 15 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -17,30 +17,21 @@ allprojects{
maven{ url "http://maven.aliyun.com/nexus/content/groups/public/"}
jcenter()
mavenCentral()
maven {
url = "https://hub.spigotmc.org/nexus/content/groups/public/"
}
maven {
url "https://dl.bintray.com/way-zer/maven"
}
maven {url "https://hub.spigotmc.org/nexus/content/groups/public/"}
maven {url "https://dl.bintray.com/way-zer/maven"}
maven {url "https://repo.codemc.org/repository/maven-public"}
}
}
shadowJar {
// minimize()
configurations = [project.configurations.shadow]
relocate("me.dpohvar.powernbt.api","cf.wayzer.util.nbt")
relocate("me.dpohvar.powernbt.nbt","cf.wayzer.util.nbt.nbt")
relocate("me.dpohvar.powernbt.utils","cf.wayzer.util.nbt.utils")
relocate("me.dpohvar.powernbt.exception","cf.wayzer.util.nbt.exception")
relocate("de.tr7zw.changeme.nbtapi","cf.wayzer.util.nbt")
// exclude("me/**")
// exclude("example/**")
// exclude("(?!plugin).yml")
include("*.jar")
//for NBT
include("me/dpohvar/powernbt/api/**")
include("me/dpohvar/powernbt/utils/**")
include("me/dpohvar/powernbt/nbt/**")
include("me/dpohvar/powernbt/exception/**")
include("de/tr7zw/changeme/nbtapi/**")

include("cf/wayzer/**")
include("*.kts")
Expand All @@ -52,8 +43,8 @@ shadowJar {
//start

dependencies{
shadow 'de.tr7zw:item-nbt-api:2.2.0'
shadow 'cf.wayzer:LibraryManager:1.3.2'
shadow(files("$rootDir/lib/PowerNBT.jar"))
compile "org.jetbrains.kotlin:kotlin-script-util:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-scripting-jvm-host:$kotlin_version"
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
Expand Down
3 changes: 0 additions & 3 deletions lib/PowerNBT.jar

This file was deleted.

4 changes: 0 additions & 4 deletions lib/README.md

This file was deleted.

4 changes: 4 additions & 0 deletions src/main/kotlin/cf/wayzer/SuperItem/Item.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import org.bukkit.inventory.ItemStack
import org.bukkit.inventory.PlayerInventory
import org.bukkit.plugin.java.JavaPlugin
import java.util.*
import java.util.logging.Logger

/**
* 继承后编译放到items目录下
Expand Down Expand Up @@ -41,6 +42,9 @@ abstract class Item : Listener {
open val packageName: String
get() = javaClass.`package`?.name?:"ROOT"

val logger: Logger
get() = Logger.getLogger("SI-$packageName-$name")


/**
* 安全的获取指定类型的feature
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/cf/wayzer/SuperItem/ItemManager.kt
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ object ItemManager {
fun getItem(item: ItemStack?): Item? {
if (item == null || item.type == Material.AIR)
return null
return NBT.api.read(item)?.let { getItem(it.getString("SICN")) }
return NBT.API.read(item)?.let { getItem(it.getString("SICN")) }
}

/**
Expand Down
22 changes: 11 additions & 11 deletions src/main/kotlin/cf/wayzer/SuperItem/features/Durability.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package cf.wayzer.SuperItem.features

import cf.wayzer.SuperItem.Feature
import me.dpohvar.powernbt.api.NBTCompound
import cf.wayzer.SuperItem.features.NBT.API.set
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerItemDamageEvent
Expand All @@ -22,10 +22,10 @@ class Durability(override val defaultData: Int) : Feature<Int>(), Feature.HasLis
* @param now 值(默认 满)
*/
fun setDurability(item: ItemStack, now: Int = data) {
val nbt = NBT.api.read(item) ?: let { NBTCompound() }
val nbt = NBT.API.readOrCreate(item)
nbt["SICD"] = now
nbt["SIMD"] = data
NBT.api.write(item, nbt)
NBT.API.write(item, nbt)
}

/**
Expand All @@ -34,8 +34,8 @@ class Durability(override val defaultData: Int) : Feature<Int>(), Feature.HasLis
* @return 获得到的耐久(错误的物品为-1)
*/
fun getDurability(item: ItemStack): Int {
val nbt = NBT.api.read(item)
return if (nbt?.containsKey("SICN") == true) nbt.getInt("SICN")
val nbt = NBT.API.read(item)
return if (nbt?.hasKey("SICN") == true) nbt.getInteger("SICN")
else -1
}

Expand All @@ -45,16 +45,16 @@ class Durability(override val defaultData: Int) : Feature<Int>(), Feature.HasLis
fun onDurability(e: PlayerItemDamageEvent) {
if (e.item.durability < 1)
return
val nbt = NBT.api.read(e.item)
if (nbt?.contains("SIMD") == true) {
val max = nbt.getInt("SIMD")
var cur = nbt.getInt("SICD")
val nbt = NBT.API.read(e.item)
if (nbt?.hasKey("SIMD") == true) {
val max = nbt.getInteger("SIMD")
var cur = nbt.getInteger("SICD")
cur -= e.damage
if (cur < 0) {
e.damage = 99999
} else {
nbt["SICD"] = cur
NBT.api.write(e.item, nbt)
NBT.API.write(e.item, nbt)
cur = ((max.toDouble()) / cur * e.item.type.maxDurability).toInt()
e.damage = e.item.durability - cur
}
Expand All @@ -64,4 +64,4 @@ class Durability(override val defaultData: Int) : Feature<Int>(), Feature.HasLis

private val mListener = MListener()
}
}
}
5 changes: 3 additions & 2 deletions src/main/kotlin/cf/wayzer/SuperItem/features/ItemInfo.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import cf.wayzer.SuperItem.Feature
import cf.wayzer.SuperItem.Main
import cf.wayzer.SuperItem.Main.Companion.main
import cf.wayzer.SuperItem.events.ItemStackHandleEvent
import cf.wayzer.SuperItem.features.NBT.API.set
import org.bukkit.Material
import org.bukkit.entity.Player
import org.bukkit.inventory.ItemStack
Expand Down Expand Up @@ -71,9 +72,9 @@ class ItemInfo(
itemStack.itemMeta = im
loadOther(im, itemStack)
itemStack.itemMeta = im
val nbt = NBT.api.read(itemStack)
val nbt = NBT.API.readOrCreate(itemStack)
nbt[NBT_TAG_NAME] = item.name
NBT.api.write(itemStack, nbt)
NBT.API.write(itemStack, nbt)

this.itemStackTemplate = itemStack
}
Expand Down
49 changes: 33 additions & 16 deletions src/main/kotlin/cf/wayzer/SuperItem/features/NBT.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package cf.wayzer.SuperItem.features

import cf.wayzer.SuperItem.Feature
import cf.wayzer.SuperItem.Main
import me.dpohvar.powernbt.api.NBTCompound
import me.dpohvar.powernbt.api.NBTManager
import de.tr7zw.changeme.nbtapi.NBTCompound
import de.tr7zw.changeme.nbtapi.NBTItem
import de.tr7zw.changeme.nbtapi.utils.nmsmappings.ReflectionMethod
import org.bukkit.inventory.ItemStack
import java.util.logging.Level

/**
Expand Down Expand Up @@ -52,25 +54,40 @@ class NBT(override vararg val defaultData: AttributeModifier) : Feature<Array<ou
)

override fun onPostLoad(main: Main) {
val nbt = api.read(item.get<ItemInfo>().itemStackTemplate).compound("tag").list("AttributeModifiers")
data.forEach {
if (it.amount < it.type.max) {
val node = NBTCompound()
node["AttributeName"] = it.type.attributeName
node["Name"] = "SuperItem NBT ${it.type.name}"
it.slot?.let { node["Slot"] = it.name.toLowerCase() }
node["Operation"] = it.operation.ordinal
node["Amount"] = it.amount
node["UUIDLeast"] = 894654
node["UUIDMost"] = 2872
nbt.add(node)
val nbt = NBTItem(item.get<ItemInfo>().itemStackTemplate).getCompoundList("AttributeModifiers")
data.forEach { attr ->
if (attr.amount < attr.type.max) {
val node = nbt.addCompound()
node.setString("AttributeName", attr.type.attributeName)
node.setString("Name", "SuperItem NBT ${attr.type.name}")
attr.slot?.let { node.setString("Slot", it.name.toLowerCase()) }
node.setInteger("Operation", attr.operation.ordinal)
node.setDouble("Amount", attr.amount)
node.setInteger("UUIDLeast", 894654)
node.setInteger("UUIDMost", 2872)
} else {
main.logger.log(Level.WARNING, "错误的NBT属性: $it")
main.logger.log(Level.WARNING, "错误的NBT属性: $attr")
}
}
}

object API {
fun read(item: ItemStack): NBTCompound? = NBTItem(item).let { if (it.hasNBTData()) it else null }
fun readOrCreate(item: ItemStack): NBTCompound = NBTItem(item)
fun write(item: ItemStack,nbt:NBTCompound){
val stack = ReflectionMethod.ITEMSTACK_NMSCOPY.run(null, item)
ReflectionMethod.ITEMSTACK_SET_TAG.run(stack, nbt.compound)
}
operator fun NBTCompound.set(key:String,v:Int) {
setInteger(key,v)
}
operator fun NBTCompound.set(key:String,v:String) {
setString(key,v)
}
}

companion object {
val api = NBTManager.getInstance()
@Deprecated("Use API", ReplaceWith("NBT.API"))
val api = API
}
}

0 comments on commit 41fbc0e

Please sign in to comment.