Guest User

Untitled

a guest
Feb 4th, 2022
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 2.80 KB | None | 0 0
  1. object SerializeEntity {
  2.  
  3.     private val getHandleMethod = obcClass("entity.CraftEntity").getMethod("getHandle")
  4.     private val nbtClass = nmsClass("NBTTagCompound")
  5.     private val nbtConstructor = nbtClass.getConstructor()
  6.     private val nbtCloneMethod = nbtClass.getMethod("clone")
  7.     private val entityClass = nmsClass("Entity")
  8.     private val serializeEntityMethod = entityClass.getMethod("serializeEntity",nbtClass)
  9.     /**
  10.      * @return Compound
  11.      * */
  12.     fun getNBTCompoundData(entity : Entity): Any? {
  13.         val entityNMS = getHandleMethod.invoke(entity)
  14.         val compound = nbtConstructor.newInstance()
  15.         serializeEntityMethod.invoke(entityNMS,compound)
  16.         return compound
  17.     }
  18.  
  19.     fun getEntityFromNBT(compound:String,world: World,changeUUID : Boolean = true): Entity?{
  20.         val mojangsonParserClass = nmsClass("MojangsonParser")
  21.         val nbtTagCompound = mojangsonParserClass.getMethod("parse",Class.forName("java.lang.String")).invoke(null,compound)
  22.         val clone = nbtCloneMethod.invoke(nbtTagCompound)
  23.         val worldServer = obcClass("CraftWorld").getMethod("getHandle").invoke(world)
  24.         val entity = nmsClass("EntityTypes").getMethod("a",
  25.             nbtClass, Class.forName("net.minecraft.world.level.World"), Class.forName("java.util.function.Function")
  26.         ).invoke(null,clone,worldServer, EntityFunction<Entity,Entity>())
  27.  
  28.         entityClass.getDeclaredField("dead").set(entity, false)
  29.         entityClass.getField("uniqueID").set(entity,UUID.randomUUID())
  30.  
  31.         if (entity != null){
  32.             nmsClass("EntityInsentient").getMethod(
  33.                 "prepare",
  34.                 nmsClass("WorldAccess"),
  35.                 nmsClass("DifficultyDamageScaler"),
  36.                 nmsClass("EnumMobSpawn"),
  37.                 nmsClass("GroupDataEntity"),
  38.                 nmsClass("NBTTagCompound")
  39.             ).invoke(
  40.                 entity,
  41.                 worldServer,
  42.                 nmsClass("WorldServer").getMethod("getDamageScaler", nmsClass("BlockPosition"))
  43.                     .invoke(worldServer,
  44.                             entityClass.getMethod("getChunkCoordinates").invoke(entity)
  45.                         ),
  46.                 nmsClass("EnumMobSpawn").enumConstants[13],
  47.                 null,
  48.                 null
  49.             )
  50.             return entityClass.getMethod("getBukkitEntity").invoke(entity) as Entity
  51.         }
  52.         return null
  53.     }
  54.  
  55.     class EntityFunction<TE,RE> :Function<TE,RE>{
  56.         override fun apply(t: TE): RE {
  57.             try {
  58.                 entityClass.getDeclaredField("dead").set(t, false)
  59.                 entityClass.getField("uniqueID").set(t,UUID.randomUUID())
  60.             }catch (e: NoSuchFieldException){
  61.                 //pass
  62.             }
  63.             return t as RE
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment