33import ca .spottedleaf .moonrise .common .PlatformHooks ;
44import com .google .common .base .Preconditions ;
55import com .google .gson .JsonObject ;
6- import com .mojang .logging .LogUtils ;
76import com .mojang .serialization .JsonOps ;
87import io .papermc .paper .entity .EntitySerializationFlag ;
98import net .minecraft .SharedConstants ;
109import net .minecraft .nbt .CompoundTag ;
10+ import net .minecraft .nbt .ListTag ;
1111import net .minecraft .nbt .NbtOps ;
1212import net .minecraft .nbt .Tag ;
1313import net .minecraft .server .MinecraftServer ;
1414import net .minecraft .server .level .ServerLevel ;
15- import net .minecraft .util .ProblemReporter ;
1615import net .minecraft .util .datafix .fixes .References ;
1716import net .minecraft .world .entity .player .Player ;
18- import net .minecraft .world .level .storage .TagValueInput ;
19- import net .minecraft .world .level .storage .TagValueOutput ;
2017import org .bukkit .World ;
2118import org .bukkit .craftbukkit .CraftWorld ;
2219import org .bukkit .craftbukkit .entity .CraftEntity ;
2320import org .bukkit .entity .Entity ;
2421import org .jetbrains .annotations .NotNull ;
25- import org .slf4j .Logger ;
2622
2723import java .util .ArrayList ;
2824import java .util .List ;
3127
3228// Based on https://github.com/Warriorrrr/Paper/commit/acba85eac5a5c37577916d4561e593d38bb0573f
3329public class JsonEntitySerializer {
34- private static final Logger LOGGER = LogUtils .getLogger ();
35-
3630 public static @ NotNull JsonObject serializeEntityAsJson (@ NotNull final Entity entity , final @ NotNull EntitySerializationFlag ... serializationFlags ) {
3731 Preconditions .checkArgument (entity != null , "entity must not be null" );
3832
3933 final CompoundTag nbt = serializeEntityToNbt (entity , serializationFlags );
40- nbt .putInt ("DataVersion" , SharedConstants .getCurrentVersion ().dataVersion ().version ());
34+ nbt .putInt ("DataVersion" , SharedConstants .getCurrentVersion ().getDataVersion ().getVersion ());
4135
4236 return NbtOps .INSTANCE .convertTo (JsonOps .INSTANCE , nbt ).getAsJsonObject ();
4337 }
@@ -57,14 +51,14 @@ private static CompoundTag serializeEntityToNbt(org.bukkit.entity.Entity entity,
5751 Preconditions .checkArgument (entity instanceof CraftEntity , "Only CraftEntities can be serialized" );
5852
5953 Set <EntitySerializationFlag > flags = Set .of (serializationFlags );
60- final boolean serializePassengers = flags .contains (EntitySerializationFlag .PASSENGERS );
54+ final boolean serializePassangers = flags .contains (EntitySerializationFlag .PASSENGERS );
6155 final boolean forceSerialization = flags .contains (EntitySerializationFlag .FORCE );
6256 final boolean allowPlayerSerialization = flags .contains (EntitySerializationFlag .PLAYER );
6357 final boolean allowMiscSerialization = flags .contains (EntitySerializationFlag .MISC );
6458 final boolean includeNonSaveable = allowPlayerSerialization || allowMiscSerialization ;
6559
6660 net .minecraft .world .entity .Entity nmsEntity = ((CraftEntity ) entity ).getHandle ();
67- (serializePassengers ? nmsEntity .getSelfAndPassengers () : Stream .of (nmsEntity )).forEach (e -> {
61+ (serializePassangers ? nmsEntity .getSelfAndPassengers () : Stream .of (nmsEntity )).forEach (e -> {
6862 // Ensure force flag is not needed
6963 Preconditions .checkArgument (
7064 (e .getBukkitEntity ().isValid () && e .getBukkitEntity ().isPersistent ()) || forceSerialization ,
@@ -91,30 +85,26 @@ private static CompoundTag serializeEntityToNbt(org.bukkit.entity.Entity entity,
9185 }
9286 });
9387
94- try (final ProblemReporter .ScopedCollector problemReporter = new ProblemReporter .ScopedCollector (
95- () -> "serialiseEntity@" + entity .getUniqueId (), LOGGER
96- )) {
97- final TagValueOutput output = TagValueOutput .createWithContext (problemReporter , nmsEntity .registryAccess ());
98- if (serializePassengers ) {
99- if (!nmsEntity .saveAsPassenger (output , true , includeNonSaveable , forceSerialization )) {
100- throw new IllegalArgumentException ("Couldn't serialize entity" );
101- }
102- } else {
103- List <net .minecraft .world .entity .Entity > pass = new ArrayList <>(nmsEntity .getPassengers ());
104- nmsEntity .passengers = com .google .common .collect .ImmutableList .of ();
105- boolean serialized = nmsEntity .saveAsPassenger (output , true , includeNonSaveable , forceSerialization );
106- nmsEntity .passengers = com .google .common .collect .ImmutableList .copyOf (pass );
107- if (!serialized ) {
108- throw new IllegalArgumentException ("Couldn't serialize entity" );
109- }
88+ CompoundTag compound = new CompoundTag ();
89+ if (serializePassangers ) {
90+ if (!nmsEntity .saveAsPassenger (compound , true , includeNonSaveable , forceSerialization )) {
91+ throw new IllegalArgumentException ("Couldn't serialize entity" );
92+ }
93+ } else {
94+ List <net .minecraft .world .entity .Entity > pass = new ArrayList <>(nmsEntity .getPassengers ());
95+ nmsEntity .passengers = com .google .common .collect .ImmutableList .of ();
96+ boolean serialized = nmsEntity .saveAsPassenger (compound , true , includeNonSaveable , forceSerialization );
97+ nmsEntity .passengers = com .google .common .collect .ImmutableList .copyOf (pass );
98+ if (!serialized ) {
99+ throw new IllegalArgumentException ("Couldn't serialize entity" );
110100 }
111- return output .buildResult ();
112101 }
102+ return compound ;
113103 }
114104
115105 private static org .bukkit .entity .Entity deserializeEntityFromNbt (CompoundTag compound , World world , boolean preserveUUID , boolean preservePassengers ) {
116- int dataVersion = compound .getIntOr ("DataVersion" , 0 );
117- compound = PlatformHooks .get ().convertNBT (References .ENTITY , MinecraftServer .getServer ().fixerUpper , compound , dataVersion , SharedConstants .getCurrentVersion ().dataVersion ().version ()); // Paper - possibly use dataconverter
106+ int dataVersion = compound .getInt ("DataVersion" );
107+ compound = PlatformHooks .get ().convertNBT (References .ENTITY , MinecraftServer .getServer ().fixerUpper , compound , dataVersion , SharedConstants .getCurrentVersion ().getDataVersion ().getVersion ()); // Paper - possibly use dataconverter
118108 if (!preservePassengers ) {
119109 compound .remove ("Passengers" );
120110 }
@@ -127,27 +117,19 @@ private static net.minecraft.world.entity.Entity deserializeEntity(CompoundTag c
127117 // Generate a new UUID, so we don't have to worry about deserializing the same entity twice
128118 compound .remove ("UUID" );
129119 }
120+ net .minecraft .world .entity .Entity nmsEntity = net .minecraft .world .entity .EntityType .create (compound , world , net .minecraft .world .entity .EntitySpawnReason .LOAD )
121+ .orElseThrow (() -> new IllegalArgumentException ("An ID was not found for the data. Did you downgrade?" ));
130122
131- final net .minecraft .world .entity .Entity nmsEntity ;
132- try (final ProblemReporter .ScopedCollector problemReporter = new ProblemReporter .ScopedCollector (
133- () -> "deserialiseEntity" , LOGGER
134- )) {
135- nmsEntity = net .minecraft .world .entity .EntityType .create (
136- TagValueInput .create (problemReporter , world .registryAccess (), compound ),
137- world ,
138- net .minecraft .world .entity .EntitySpawnReason .LOAD
139- ).orElseThrow (() -> new IllegalArgumentException ("An ID was not found for the data. Did you downgrade?" ));
140- }
141-
142- compound .getList ("Passengers" ).ifPresent (passengers -> {
143- for (final Tag tag : passengers ) {
144- if (!(tag instanceof final CompoundTag serializedPassenger )) {
123+ if (compound .contains ("Passengers" , Tag .TAG_LIST )) {
124+ ListTag passengersCompound = compound .getList ("Passengers" , Tag .TAG_COMPOUND );
125+ for (Tag tag : passengersCompound ) {
126+ if (!(tag instanceof CompoundTag serializedPassenger )) {
145127 continue ;
146128 }
147- final net .minecraft .world .entity .Entity passengerEntity = deserializeEntity (serializedPassenger , world , preserveUUID );
129+ net .minecraft .world .entity .Entity passengerEntity = deserializeEntity (serializedPassenger , world , preserveUUID );
148130 passengerEntity .startRiding (nmsEntity , true );
149131 }
150- });
132+ }
151133 return nmsEntity ;
152134 }
153135}
0 commit comments