Skip to content

Commit

Permalink
Clean up more legacy NBT (#143)
Browse files Browse the repository at this point in the history
  • Loading branch information
ALongStringOfNumbers authored Sep 11, 2021
1 parent 3d4921e commit 3de8c81
Showing 1 changed file with 2 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package gregtech.api.metatileentity;

import com.google.common.base.Preconditions;
import gregtech.api.GTValues;
import gregtech.api.GregTechAPI;
import gregtech.api.block.machines.BlockMachine;
import gregtech.api.cover.CoverBehavior;
import gregtech.api.gui.IUIHolder;
import gregtech.api.util.GTControlledRegistry;
import gregtech.api.util.GTLog;
import net.minecraft.block.state.IBlockState;
import net.minecraft.nbt.NBTTagCompound;
Expand All @@ -23,8 +21,6 @@

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.List;
import java.util.stream.Collectors;

public class MetaTileEntityHolder extends TickableTileEntityBase implements IUIHolder {

Expand Down Expand Up @@ -87,13 +83,8 @@ public void readFromNBT(@Nonnull NBTTagCompound compound) {
super.readFromNBT(compound);
if (compound.hasKey("MetaId", NBT.TAG_STRING)) {
String metaTileEntityIdRaw = compound.getString("MetaId");
ResourceLocation metaTileEntityId;
if (metaTileEntityIdRaw.indexOf(':') == -1) {
metaTileEntityId = convertMetaTileEntityId(metaTileEntityIdRaw);
} else {
metaTileEntityId = new ResourceLocation(metaTileEntityIdRaw);
}
MetaTileEntity sampleMetaTileEntity = metaTileEntityId == null ? null : GregTechAPI.META_TILE_ENTITY_REGISTRY.getObject(metaTileEntityId);
ResourceLocation metaTileEntityId = new ResourceLocation(metaTileEntityIdRaw);
MetaTileEntity sampleMetaTileEntity = GregTechAPI.META_TILE_ENTITY_REGISTRY.getObject(metaTileEntityId);
NBTTagCompound metaTileEntityData = compound.getCompoundTag("MetaTileEntity");
if (sampleMetaTileEntity != null) {
this.metaTileEntity = sampleMetaTileEntity.createMetaTileEntity(this);
Expand All @@ -105,31 +96,6 @@ public void readFromNBT(@Nonnull NBTTagCompound compound) {
}
}

private static List<String> registeredModIDs = null;

private static ResourceLocation convertMetaTileEntityId(String metaTileEntityIdOld) {
ResourceLocation gregtechId = new ResourceLocation(GTValues.MODID, metaTileEntityIdOld);
GTControlledRegistry<ResourceLocation, MetaTileEntity> registry = GregTechAPI.META_TILE_ENTITY_REGISTRY;
if (registry.containsKey(gregtechId)) {
return gregtechId; //remap to gregtech meta tile entities first
}
//try to lookup by different registry IDs
if (registeredModIDs == null) {
registeredModIDs = registry.getKeys().stream()
.map(ResourceLocation::getNamespace)
.distinct().collect(Collectors.toList());
registeredModIDs.remove(GTValues.MODID);
}
for (String registryModId : registeredModIDs) {
ResourceLocation probableId = new ResourceLocation(registryModId, metaTileEntityIdOld);
if (registry.containsKey(probableId)) {
return probableId;
}
}
GTLog.logger.error("Failed to convert old MetaTileEntity string ID " + metaTileEntityIdOld);
return null;
}

@Nonnull
@Override
public NBTTagCompound writeToNBT(@Nonnull NBTTagCompound compound) {
Expand Down

0 comments on commit 3de8c81

Please sign in to comment.