Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- object SerializeEntity {
- private val getHandleMethod = obcClass("entity.CraftEntity").getMethod("getHandle")
- private val nbtClass = nmsClass("NBTTagCompound")
- private val nbtConstructor = nbtClass.getConstructor()
- private val nbtCloneMethod = nbtClass.getMethod("clone")
- private val entityClass = nmsClass("Entity")
- private val serializeEntityMethod = entityClass.getMethod("serializeEntity",nbtClass)
- /**
- * @return Compound
- * */
- fun getNBTCompoundData(entity : Entity): Any? {
- val entityNMS = getHandleMethod.invoke(entity)
- val compound = nbtConstructor.newInstance()
- serializeEntityMethod.invoke(entityNMS,compound)
- return compound
- }
- fun getEntityFromNBT(compound:String,world: World,changeUUID : Boolean = true): Entity?{
- val mojangsonParserClass = nmsClass("MojangsonParser")
- val nbtTagCompound = mojangsonParserClass.getMethod("parse",Class.forName("java.lang.String")).invoke(null,compound)
- val clone = nbtCloneMethod.invoke(nbtTagCompound)
- val worldServer = obcClass("CraftWorld").getMethod("getHandle").invoke(world)
- val entity = nmsClass("EntityTypes").getMethod("a",
- nbtClass, Class.forName("net.minecraft.world.level.World"), Class.forName("java.util.function.Function")
- ).invoke(null,clone,worldServer, EntityFunction<Entity,Entity>())
- entityClass.getDeclaredField("dead").set(entity, false)
- entityClass.getField("uniqueID").set(entity,UUID.randomUUID())
- if (entity != null){
- nmsClass("EntityInsentient").getMethod(
- "prepare",
- nmsClass("WorldAccess"),
- nmsClass("DifficultyDamageScaler"),
- nmsClass("EnumMobSpawn"),
- nmsClass("GroupDataEntity"),
- nmsClass("NBTTagCompound")
- ).invoke(
- entity,
- worldServer,
- nmsClass("WorldServer").getMethod("getDamageScaler", nmsClass("BlockPosition"))
- .invoke(worldServer,
- entityClass.getMethod("getChunkCoordinates").invoke(entity)
- ),
- nmsClass("EnumMobSpawn").enumConstants[13],
- null,
- null
- )
- return entityClass.getMethod("getBukkitEntity").invoke(entity) as Entity
- }
- return null
- }
- class EntityFunction<TE,RE> :Function<TE,RE>{
- override fun apply(t: TE): RE {
- try {
- entityClass.getDeclaredField("dead").set(t, false)
- entityClass.getField("uniqueID").set(t,UUID.randomUUID())
- }catch (e: NoSuchFieldException){
- //pass
- }
- return t as RE
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment