Skip to content
This repository has been archived by the owner on Feb 16, 2022. It is now read-only.

Commit

Permalink
api(nms): NBTTag を追加
Browse files Browse the repository at this point in the history
  • Loading branch information
sya-ri committed Mar 7, 2021
1 parent 32a2b4a commit 57fa463
Show file tree
Hide file tree
Showing 7 changed files with 148 additions and 23 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.github.syari.spigot.api.nms

/**
* `org.bukkit.craftbukkit.%s.inventory.CraftItemStack` を扱う。
* @since 1.8.0
*/
class CraftItemStackWrapper(override val instance: Any) : NMSWrapper() {
companion object : NMSWrapper.Companion {
/**
* クラス。
* @since 1.8.0
*/
override val clazz = getNMSClass("org.bukkit.craftbukkit.%s.inventory.CraftItemStack")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.syari.spigot.api.nms

/**
* `net.minecraft.server.%s.NBTTagCompound` を扱う。
* @since 1.8.0
*/
class NBTTagCompoundWrapper(override val instance: Any = clazz.getConstructor().newInstance()) : NMSWrapper() {
companion object : NMSWrapper.Companion {
/**
* クラス。
* @since 1.8.0
*/
override val clazz = getNMSClass("net.minecraft.server.%s.NBTTagCompound")
}

init {
require(clazz.isInstance(instance)) { "instance must be NBTTagCompound" }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.github.syari.spigot.api.nms

import org.bukkit.inventory.ItemStack

/**
* `net.minecraft.server.%s.ItemStack` を扱う。
* @since 1.8.0
*/
class NMSItemStackWrapper(val itemStack: ItemStack) : NMSWrapper() {
companion object : NMSWrapper.Companion {
/**
* クラス。
* @since 1.8.0
*/
override val clazz = getNMSClass("net.minecraft.server.%s.ItemStack")
}

override val instance: Any = CraftItemStackWrapper.clazz.getDeclaredMethod("asNMSCopy", ItemStack::class.java).invoke(null, itemStack)

/**
* [org.bukkit.inventory.ItemStack] から [NBTTagCompoundWrapper] のインスタンスを取得する。
* @since 1.8.0
*/
fun getTag(): NBTTagCompoundWrapper? {
return clazz.getDeclaredMethod("getTag").invoke(instance)?.let(::NBTTagCompoundWrapper)
}

/**
* [org.bukkit.inventory.ItemStack] の NBTTagCompound を変更する。
* @since 1.8.0
*/
fun setTag(nbtTagCompound: NBTTagCompoundWrapper) {
clazz.getDeclaredMethod("setTag", NBTTagCompoundWrapper.clazz).invoke(instance, nbtTagCompound.instance)
}

/**
* [org.bukkit.inventory.ItemStack] から NBTTagCompound を取得する。存在しない場合は空のインスタンスを作成する
* @since 1.8.0
*/
fun getOrCreateTag(): NBTTagCompoundWrapper {
return getTag() ?: NBTTagCompoundWrapper().apply(::setTag)
}
}
35 changes: 35 additions & 0 deletions api/src/main/kotlin/com/github/syari/spigot/api/nms/NMSWrapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.github.syari.spigot.api.nms

/**
* NMS を扱う為の基底クラス。
* @since 1.8.0
*/
abstract class NMSWrapper {
/**
* インスタンス。
* @since 1.8.0
*/
abstract val instance: Any

override fun toString() = instance.toString()

override fun hashCode() = instance.hashCode()

override fun equals(other: Any?) = when {
this === other -> true
other is NMSWrapper -> instance == other.instance
else -> false
}

/**
* [NMSWrapper] の Companion に継承するインターフェース。
* @since 1.8.0
*/
interface Companion {
/**
* クラス。
* @since 1.8.0
*/
val clazz: Class<*>
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.github.syari.spigot.api.util.item

import com.github.syari.spigot.api.nms.NMSItemStackWrapper
import com.github.syari.spigot.api.util.string.toColor
import com.google.common.collect.ImmutableMultimap
import com.google.common.collect.Multimap
Expand Down Expand Up @@ -298,3 +299,10 @@ inline fun <reified T : ItemMeta> ItemStack.editItemMeta(action: T.() -> Unit) {
(this as? T)?.action()
}
}

/**
* NBT タグを取得する。
* @since 1.8.0
*/
val ItemStack.nbtTag: String
get() = NMSItemStackWrapper(this).getOrCreateTag().toString()
28 changes: 27 additions & 1 deletion sample/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ object EventListener : EventRegister {
```

## NMS
NMSを使うための関数を追加します
NMSを使うための関数・クラスを追加します

```kotlin

Expand All @@ -92,6 +92,27 @@ val NMS_VERSION = Bukkit.getServer()::class.java.`package`.name.substring(23)
* NMS のクラスを取得する。`%s` が [NMS_VERSION] に置き換わる。
*/
fun getNMSClass(className: String): Class<*> = Class.forName(className.format(NMS_VERSION))


/**
* NMS を扱う為の基底クラス。
*/
abstract class NMSWrapper

/**
* `org.bukkit.craftbukkit.%s.inventory.CraftItemStack` を扱う。
*/
class CraftItemStackWrapper

/**
* `net.minecraft.server.%s.NBTTagCompound` を扱う。
*/
class NBTTagCompoundWrapper

/**
* `net.minecraft.server.%s.ItemStack` を扱う。
*/
class NMSItemStackWrapper
```

## [Scheduler](scheduler)
Expand Down Expand Up @@ -292,6 +313,11 @@ inline fun ItemStack.editItemMeta(action: ItemMeta.() -> Unit)
* [ItemMeta] に対して変更を加える。
*/
inline fun <reified T : ItemMeta> ItemStack.editItemMeta(action: T.() -> Unit)

/**
* NBT タグを取得する。
*/
val ItemStack.nbtTag: String
```

## Util / Component
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.github.syari.spigot.api.sample.nms

import com.github.syari.spigot.api.nms.NMS_VERSION
import com.github.syari.spigot.api.nms.getNMSClass
import com.github.syari.spigot.api.util.item.displayName
import com.github.syari.spigot.api.util.item.nbtTag
import org.bukkit.Material
import org.bukkit.inventory.ItemStack
import org.bukkit.plugin.java.JavaPlugin
Expand All @@ -17,25 +17,4 @@ class Main : JavaPlugin() {

ItemStack(Material.LAVA_BUCKET).run { logger.info("NBT Tag: $nbtTag") }
}

val ItemStack.nbtTag: String
get() {
val craftItemStack = getNMSClass("org.bukkit.craftbukkit.%s.inventory.CraftItemStack")
.getDeclaredMethod("asNMSCopy", ItemStack::class.java)
.invoke(null, this)
val nmsItemStackClass = getNMSClass("net.minecraft.server.%s.ItemStack")
val nbtTagCompoundClass = getNMSClass("net.minecraft.server.%s.NBTTagCompound")
val nbtTagCompound = nmsItemStackClass
.getDeclaredMethod("getTag")
.invoke(craftItemStack) ?: run {
val nbtTagCompound = nbtTagCompoundClass.getConstructor().newInstance()
nmsItemStackClass
.getDeclaredMethod("setTag", nbtTagCompoundClass)
.invoke(craftItemStack, nbtTagCompound)
nbtTagCompound
}
return nbtTagCompoundClass
.getDeclaredMethod("toString")
.invoke(nbtTagCompound) as String
}
}

0 comments on commit 57fa463

Please sign in to comment.