This repository has been archived by the owner on Feb 16, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
148 additions
and
23 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
api/src/main/kotlin/com/github/syari/spigot/api/nms/CraftItemStackWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
api/src/main/kotlin/com/github/syari/spigot/api/nms/NBTTagCompoundWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" } | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
api/src/main/kotlin/com/github/syari/spigot/api/nms/NMSItemStackWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
35
api/src/main/kotlin/com/github/syari/spigot/api/nms/NMSWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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<*> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters