3232import net .minecraft .server .MinecraftServer ;
3333import net .minecraft .server .network .ServerPlayerEntity ;
3434import net .minecraft .server .world .ServerWorld ;
35+ import net .minecraft .storage .NbtReadView ;
36+ import net .minecraft .storage .NbtWriteView ;
37+ import net .minecraft .storage .ReadView ;
38+ import net .minecraft .storage .WriteView ;
39+ import net .minecraft .util .ErrorReporter ;
3540import net .minecraft .util .Identifier ;
36- import net .minecraft .util .math .BlockPos ;
3741import net .minecraft .world .World ;
3842import org .spongepowered .asm .mixin .Mixin ;
3943import org .spongepowered .asm .mixin .Unique ;
@@ -50,8 +54,8 @@ public abstract class ServerPlayerEntityMixin extends PlayerEntity implements Pl
5054 @ Unique
5155 private final Map <PlayerAbility , AbilityTracker > palAbilities = new LinkedHashMap <>();
5256
53- public ServerPlayerEntityMixin (World world , BlockPos pos , float yaw , GameProfile gameProfile ) {
54- super (world , pos , yaw , gameProfile );
57+ public ServerPlayerEntityMixin (World world , GameProfile gameProfile ) {
58+ super (world , gameProfile );
5559 }
5660
5761
@@ -85,10 +89,10 @@ public void refreshAllPalAbilities(boolean syncVanilla) {
8589 @ Inject (method = "copyFrom" , at = @ At (value = "INVOKE" , target = "Lnet/minecraft/server/network/ServerPlayerInteractionManager;setGameMode(Lnet/minecraft/world/GameMode;Lnet/minecraft/world/GameMode;)V" , shift = At .Shift .AFTER ))
8690 private void copyAbilitiesAfterRespawn (ServerPlayerEntity oldPlayer , boolean alive , CallbackInfo ci ) {
8791 if (alive ) {
88- NbtCompound nbt = new NbtCompound ( );
92+ var writeView = NbtWriteView . create ( ErrorReporter . EMPTY , oldPlayer . getRegistryManager () );
8993 //noinspection ConstantConditions
90- ((ServerPlayerEntityMixin ) (Object ) oldPlayer ).writeAbilitiesToTag ( nbt , null );
91- this .readAbilitiesFromTag ( nbt , null );
94+ ((ServerPlayerEntityMixin ) (Object ) oldPlayer ).writeAbilitiesToData ( writeView , null );
95+ this .readAbilitiesFromData ( NbtReadView . create ( ErrorReporter . EMPTY , oldPlayer . getRegistryManager (), writeView . getNbt ()) , null );
9296 }
9397 }
9498
@@ -102,33 +106,29 @@ private void checkAbilityConsistency(CallbackInfo ci) {
102106 }
103107 }
104108
105- @ Inject (method = "writeCustomDataToNbt " , at = @ At ("RETURN" ))
106- private void writeAbilitiesToTag ( NbtCompound tag , CallbackInfo ci ) {
107- NbtList list = new NbtList ( );
109+ @ Inject (method = "writeCustomData " , at = @ At ("RETURN" ))
110+ private void writeAbilitiesToData ( WriteView view , CallbackInfo ci ) {
111+ var list = view . getList ( "playerabilitylib:abilities" );
108112 for (Map .Entry <PlayerAbility , AbilityTracker > entry : this .palAbilities .entrySet ()) {
109- NbtCompound abilityTag = new NbtCompound ();
113+ var abilityTag = list . add ();
110114 abilityTag .putString ("ability_id" , entry .getKey ().getId ().toString ());
111115 entry .getValue ().save (abilityTag );
112- list .add (abilityTag );
113116 }
114- tag .put ("playerabilitylib:abilities" , list );
115117 }
116118
117119 @ Inject (
118- method = "readCustomDataFromNbt " ,
119- at = @ At (value = "INVOKE" , target = "Lnet/minecraft/entity/player/PlayerEntity;readCustomDataFromNbt (Lnet/minecraft/nbt/NbtCompound ;)V" , shift = At .Shift .AFTER )
120+ method = "readCustomData " ,
121+ at = @ At (value = "INVOKE" , target = "Lnet/minecraft/entity/player/PlayerEntity;readCustomData (Lnet/minecraft/storage/ReadView ;)V" , shift = At .Shift .AFTER )
120122 )
121- private void readAbilitiesFromTag (NbtCompound tag , CallbackInfo ci ) {
122- for (NbtElement t : tag .getListOrEmpty ("playerabilitylib:abilities" )) {
123- if (t instanceof NbtCompound abilityTag ) {
124- if (abilityTag .contains ("ability_id" )) {
125- String abilityId = abilityTag .getString ("ability_id" , "" );
126- AbilityTracker tracker = this .palAbilities .get (PalInternals .getAbility (Identifier .tryParse (abilityId )));
127- if (tracker != null ) {
128- tracker .load (abilityTag );
129- } else {
130- PalInternals .LOGGER .warn ("Encountered unknown ability {} while deserializing data for {}" , abilityId , this );
131- }
123+ private void readAbilitiesFromData (ReadView view , CallbackInfo ci ) {
124+ for (var abilityTag : view .getListReadView ("playerabilitylib:abilities" )) {
125+ String abilityId = abilityTag .getString ("ability_id" , "" );
126+ if (!abilityId .isEmpty ()) {
127+ AbilityTracker tracker = this .palAbilities .get (PalInternals .getAbility (Identifier .tryParse (abilityId )));
128+ if (tracker != null ) {
129+ tracker .load (abilityTag );
130+ } else {
131+ PalInternals .LOGGER .warn ("Encountered unknown ability {} while deserializing data for {}" , abilityId , this );
132132 }
133133 }
134134 }
0 commit comments