- package tech.cookiepower.blueberrytools.util
- import org.bukkit.entity.Entity
- import taboolib.module.nms.nmsClass
- import taboolib.module.nms.obcClass
- object SerializeEntity {
- private val getHandleMethod = obcClass("entity.CraftEntity").getMethod("getHandle")
- private val nbtClass = nmsClass("NBTTagCompound")
- private val nbtConstructor = nbtClass.getConstructor()
- private val entityClass = nmsClass("Entity")
- private val serializeEntityMethod = entityClass.getMethod("serializeEntity",nbtClass)
- /**
- * [url=home.php?mod=space&uid=491268]@Return[/url] Compound
- * */
- fun getNBTCompoundData(entity : Entity): Any? {
- val entityNMS = getHandleMethod.invoke(entity)
- val compound = nbtConstructor.newInstance()
- serializeEntityMethod.invoke(entityNMS,compound)
- return compound
- }
- }
复制代码
- package tech.cookiepower.blueberrytools.util
- import org.bukkit.World
- import org.bukkit.entity.Entity
- import org.bukkit.event.entity.CreatureSpawnEvent
- import taboolib.module.nms.nmsClass
- import taboolib.module.nms.obcClass
- import tech.cookitpower.blueberrytools.unit.PrimitiveType
- import java.lang.reflect.Method
- import java.util.*
- import java.util.function.Function
- class DeSerializeEntity(compound:String, private val world: World) {
- private var changeUUID : Boolean = false
- private var entity : Any? = null
- private var worldServer : Any? = null
- companion object{
- private val nbtClass = nmsClass("NBTTagCompound")
- private val nbtCloneMethod = nbtClass.getMethod("clone")
- private val entityClass = nmsClass("Entity")
- private val parseMethod = nmsClass("MojangsonParser").getMethod("parse",Class.forName("java.lang.String"))
- private val getHandleWorldMethod = obcClass("CraftWorld").getMethod("getHandle")
- private val entityA = nmsClass("EntityTypes").getMethod("a",
- nbtClass, Class.forName("net.minecraft.world.level.World"), Class.forName("java.util.function.Function")
- )
- private val prepare = nmsClass("EntityInsentient").getMethod(
- "prepare",
- nmsClass("WorldAccess"),
- nmsClass("DifficultyDamageScaler"),
- nmsClass("EnumMobSpawn"),
- nmsClass("GroupDataEntity"),
- nmsClass("NBTTagCompound")
- )
- val addEntity: Method = obcClass("CraftWorld").getMethod("addEntity",nmsClass("Entity"), CreatureSpawnEvent.SpawnReason::class.java)
- }
-
- init {
- val nbtTagCompound = parseMethod.invoke(null,compound)
- nbtClass.getMethod("setFloat", Class.forName("java.lang.String"),PrimitiveType.FLOAT)
- .invoke(nbtTagCompound,"Health",1.0F)
- val clone = nbtCloneMethod.invoke(nbtTagCompound)
- this.worldServer = getHandleWorldMethod.invoke(world)
- this.entity = entityA.invoke(null,clone,worldServer,
- EntityFunction<Entity, Entity>(changeUUID)
- )
- }
- fun changeUUID(changeUUID: Boolean){
- this.changeUUID = changeUUID
- }
- fun build():Entity?{
- if (entity != null){
- prepare.invoke(
- entity,
- worldServer,
- nmsClass("WorldServer").getMethod("getDamageScaler", nmsClass("BlockPosition"))
- .invoke(worldServer,
- entityClass.getMethod("getChunkCoordinates").invoke(entity)
- ),
- nmsClass("EnumMobSpawn").enumConstants[13],
- null,
- null
- )
- addEntity.invoke(world, entity, CreatureSpawnEvent.SpawnReason.CUSTOM)
- return entityClass.getMethod("getBukkitEntity").invoke(entity) as Entity
- }
- return null
- }
- class EntityFunction<TE,RE>(private val changeUUID: Boolean) : Function<TE, RE> {
- override fun apply(t: TE): RE {
- if (changeUUID)
- entityClass.getMethod("a_",UUID::class.java).invoke(t,UUID.randomUUID())
- return t as RE
- }
- }
- }
复制代码 |