From 02fdfe29be22235e3bcbbb111583ed270bfd87c0 Mon Sep 17 00:00:00 2001 From: Pyrofab Date: Thu, 18 Jun 2020 02:16:48 +0200 Subject: [PATCH] Fix ability sources serialization --- .../io/github/ladysnake/pal/SimpleAbilityTracker.java | 11 +++++++---- .../pal/impl/mixin/ServerPlayerEntityMixin.java | 7 +++++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/main/java/io/github/ladysnake/pal/SimpleAbilityTracker.java b/src/main/java/io/github/ladysnake/pal/SimpleAbilityTracker.java index 9696ce6..bf7bc99 100644 --- a/src/main/java/io/github/ladysnake/pal/SimpleAbilityTracker.java +++ b/src/main/java/io/github/ladysnake/pal/SimpleAbilityTracker.java @@ -23,7 +23,6 @@ import net.minecraft.nbt.CompoundTag; import net.minecraft.nbt.ListTag; import net.minecraft.nbt.StringTag; -import net.minecraft.nbt.Tag; import net.minecraft.util.Identifier; import java.util.HashSet; @@ -103,15 +102,19 @@ protected boolean shouldBeEnabled() { public void save(CompoundTag tag) { ListTag list = new ListTag(); for (AbilitySource abilitySource : this.abilitySources) { - list.add(StringTag.of(abilitySource.toString())); + list.add(StringTag.of(abilitySource.getId().toString())); } tag.put("ability_sources", list); } @Override public void load(CompoundTag tag) { - for (Tag id : tag.getList("ability_sources", NbtType.STRING)) { - this.addSource(Pal.getAbilitySource(new Identifier(id.asString()))); + ListTag list = tag.getList("ability_sources", NbtType.STRING); + for (int i = 0; i < list.size(); i++) { + Identifier sourceId = Identifier.tryParse(list.getString(i)); + if (sourceId != null) { + this.addSource(Pal.getAbilitySource(sourceId)); + } } } diff --git a/src/main/java/io/github/ladysnake/pal/impl/mixin/ServerPlayerEntityMixin.java b/src/main/java/io/github/ladysnake/pal/impl/mixin/ServerPlayerEntityMixin.java index 5bf3781..e920d4a 100644 --- a/src/main/java/io/github/ladysnake/pal/impl/mixin/ServerPlayerEntityMixin.java +++ b/src/main/java/io/github/ladysnake/pal/impl/mixin/ServerPlayerEntityMixin.java @@ -97,14 +97,17 @@ private void writeAbilitiesToTag(CompoundTag tag, CallbackInfo ci) { ListTag list = new ListTag(); for (Map.Entry entry : this.palAbilities.entrySet()) { CompoundTag abilityTag = new CompoundTag(); - abilityTag.putString("ability_id", entry.getKey().toString()); + abilityTag.putString("ability_id", entry.getKey().getId().toString()); entry.getValue().save(abilityTag); list.add(abilityTag); } tag.put("playerabilitylib:abilities", list); } - @Inject(method = "readCustomDataFromTag", at = @At("RETURN")) + @Inject( + method = "readCustomDataFromTag", + at = @At(value = "INVOKE", target = "Lnet/minecraft/entity/player/PlayerEntity;readCustomDataFromTag(Lnet/minecraft/nbt/CompoundTag;)V", shift = At.Shift.AFTER) + ) private void readAbilitiesFromTag(CompoundTag tag, CallbackInfo ci) { for (Tag t : tag.getList("playerabilitylib:abilities", NbtType.COMPOUND)) { CompoundTag abilityTag = ((CompoundTag) t);