Skip to content

Commit

Permalink
feat: Automatically try to migrate BalmData between NeoForge/Forge/Fa…
Browse files Browse the repository at this point in the history
…bric #105
  • Loading branch information
BlayTheNinth committed Jan 11, 2025
1 parent a1e288c commit f9cc651
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ public interface BalmEntity {

void setFabricBalmData(CompoundTag tag);

CompoundTag getForgeBalmData();

void setForgeBalmData(CompoundTag tag);

CompoundTag getNeoForgeBalmData();

void setNeoForgeBalmData(CompoundTag tag);

}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
23 changes: 23 additions & 0 deletions forge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
}

Expand All @@ -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;
}
}
31 changes: 23 additions & 8 deletions neoforge/src/main/java/net/blay09/mods/balm/mixin/EntityMixin.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CompoundTag> callbackInfo) {
if (!fabricBalmData.isEmpty()) {
compound.put("BalmData", fabricBalmData);
} else if (compound.contains("ForgeData")) {
forgeBalmData = compound.getCompound("ForgeData").getCompound("PlayerPersisted").getCompound("BalmData");
}
}

Expand All @@ -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.");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit f9cc651

Please sign in to comment.