diff --git a/common/src/main/java/net/blay09/mods/balm/api/entity/BalmEntity.java b/common/src/main/java/net/blay09/mods/balm/api/entity/BalmEntity.java index 52ee3848..44fa70b3 100644 --- a/common/src/main/java/net/blay09/mods/balm/api/entity/BalmEntity.java +++ b/common/src/main/java/net/blay09/mods/balm/api/entity/BalmEntity.java @@ -8,4 +8,12 @@ public interface BalmEntity { void setFabricBalmData(CompoundTag tag); + CompoundTag getForgeBalmData(); + + void setForgeBalmData(CompoundTag tag); + + CompoundTag getNeoForgeBalmData(); + + void setNeoForgeBalmData(CompoundTag tag); + } diff --git a/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalmHooks.java b/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalmHooks.java index d4eb1da1..6ca06c9a 100644 --- a/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalmHooks.java +++ b/fabric/src/main/java/net/blay09/mods/balm/fabric/FabricBalmHooks.java @@ -55,7 +55,16 @@ public boolean growCrop(ItemStack itemStack, Level level, BlockPos pos, @Nullabl @Override public CompoundTag getPersistentData(Entity entity) { - return ((BalmEntity) entity).getFabricBalmData(); + var balmData = ((BalmEntity) entity).getFabricBalmData(); + if (balmData.isEmpty()) { + // If we have no data, try to import from NeoForge in case the world was migrated + balmData = ((BalmEntity) entity).getNeoForgeBalmData(); + } + if (balmData.isEmpty()) { + // If we still have no data, try to import from Forge in case the world was migrated + balmData = ((BalmEntity) entity).getForgeBalmData(); + } + return balmData; } @Override diff --git a/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalmHooks.java b/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalmHooks.java index f7104bed..75fa3d45 100644 --- a/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalmHooks.java +++ b/forge/src/main/java/net/blay09/mods/balm/forge/ForgeBalmHooks.java @@ -81,6 +81,10 @@ public CompoundTag getPersistentData(Entity entity) { // If we have no data, try to import from Fabric in case the world was migrated balmData = ((BalmEntity) entity).getFabricBalmData(); } + if (balmData.isEmpty()) { + // If we still have no data, try to import from NeoForge in case the world was migrated + balmData = ((BalmEntity) entity).getNeoForgeBalmData(); + } if (!balmData.isEmpty()) { persistentData.put("BalmData", balmData); } diff --git a/forge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java b/forge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java index 7cfe267d..877286cb 100644 --- a/forge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java +++ b/forge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java @@ -13,11 +13,14 @@ public class EntityMixin implements BalmEntity { private CompoundTag fabricBalmData = new CompoundTag(); + private CompoundTag neoforgeBalmData = new CompoundTag(); @Inject(method = "load(Lnet/minecraft/nbt/CompoundTag;)V", at = @At("HEAD")) private void load(CompoundTag compound, CallbackInfo callbackInfo) { if (compound.contains("BalmData")) { fabricBalmData = compound.getCompound("BalmData"); + } else if (compound.contains("NeoForgeData")) { + neoforgeBalmData = compound.getCompound("NeoForgeData").getCompound("PlayerPersisted").getCompound("BalmData"); } } @@ -35,4 +38,24 @@ public CompoundTag getFabricBalmData() { public void setFabricBalmData(CompoundTag tag) { this.fabricBalmData = tag; } + + @Override + public CompoundTag getForgeBalmData() { + throw new UnsupportedOperationException("This method should not have been called. Report this issue to Balm."); + } + + @Override + public void setForgeBalmData(CompoundTag tag) { + throw new UnsupportedOperationException("This method should not have been called. Report this issue to Balm."); + } + + @Override + public CompoundTag getNeoForgeBalmData() { + return neoforgeBalmData; + } + + @Override + public void setNeoForgeBalmData(CompoundTag tag) { + this.neoforgeBalmData = tag; + } } diff --git a/neoforge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java b/neoforge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java index 95330016..104d2ae3 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java @@ -7,24 +7,19 @@ import org.spongepowered.asm.mixin.injection.At; import org.spongepowered.asm.mixin.injection.Inject; import org.spongepowered.asm.mixin.injection.callback.CallbackInfo; -import org.spongepowered.asm.mixin.injection.callback.CallbackInfoReturnable; @Mixin(Entity.class) public class EntityMixin implements BalmEntity { private CompoundTag fabricBalmData = new CompoundTag(); + private CompoundTag forgeBalmData = new CompoundTag(); @Inject(method = "load(Lnet/minecraft/nbt/CompoundTag;)V", at = @At("HEAD")) private void load(CompoundTag compound, CallbackInfo callbackInfo) { if (compound.contains("BalmData")) { fabricBalmData = compound.getCompound("BalmData"); - } - } - - @Inject(method = "saveWithoutId(Lnet/minecraft/nbt/CompoundTag;)Lnet/minecraft/nbt/CompoundTag;", at = @At("HEAD")) - private void saveWithoutId(CompoundTag compound, CallbackInfoReturnable callbackInfo) { - if (!fabricBalmData.isEmpty()) { - compound.put("BalmData", fabricBalmData); + } else if (compound.contains("ForgeData")) { + forgeBalmData = compound.getCompound("ForgeData").getCompound("PlayerPersisted").getCompound("BalmData"); } } @@ -37,4 +32,24 @@ public CompoundTag getFabricBalmData() { public void setFabricBalmData(CompoundTag tag) { this.fabricBalmData = tag; } + + @Override + public CompoundTag getForgeBalmData() { + return forgeBalmData; + } + + @Override + public void setForgeBalmData(CompoundTag tag) { + this.forgeBalmData = tag; + } + + @Override + public CompoundTag getNeoForgeBalmData() { + throw new UnsupportedOperationException("This method should not have been called. Report this issue to Balm."); + } + + @Override + public void setNeoForgeBalmData(CompoundTag tag) { + throw new UnsupportedOperationException("This method should not have been called. Report this issue to Balm."); + } } diff --git a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmHooks.java b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmHooks.java index 67c5a44d..7ade9682 100644 --- a/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmHooks.java +++ b/neoforge/src/main/java/net/blay09/mods/balm/neoforge/NeoForgeBalmHooks.java @@ -73,6 +73,10 @@ public CompoundTag getPersistentData(Entity entity) { // If we have no data, try to import from Fabric in case the world was migrated balmData = ((BalmEntity) entity).getFabricBalmData(); } + if (balmData.isEmpty()) { + // If we still have no data, try to import from Forge in case the world was migrated + balmData = ((BalmEntity) entity).getForgeBalmData(); + } if (!balmData.isEmpty()) { persistentData.put("BalmData", balmData); }