Skip to content

Commit

Permalink
support loading legacy side config for tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
thiakil committed Aug 22, 2024
1 parent e4ba3cc commit 3fd900a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@

@NothingNullByDefault
public enum TransmissionType implements IHasTranslationKey, StringRepresentable {
ENERGY("EnergyNetwork", "energy", MekanismLang.TRANSMISSION_TYPE_ENERGY),
FLUID("FluidNetwork", "fluids", MekanismLang.TRANSMISSION_TYPE_FLUID),
CHEMICAL("ChemicalNetwork", "chemicals", MekanismLang.TRANSMISSION_TYPE_CHEMICALS),
ITEM("InventoryNetwork", "items", MekanismLang.TRANSMISSION_TYPE_ITEM),
HEAT("HeatNetwork", "heat", MekanismLang.TRANSMISSION_TYPE_HEAT);
ENERGY("EnergyNetwork", "energy", MekanismLang.TRANSMISSION_TYPE_ENERGY, 0),
FLUID("FluidNetwork", "fluids", MekanismLang.TRANSMISSION_TYPE_FLUID, 1),
CHEMICAL("ChemicalNetwork", "chemicals", MekanismLang.TRANSMISSION_TYPE_CHEMICALS, 2),//3,4,5 deleted
ITEM("InventoryNetwork", "items", MekanismLang.TRANSMISSION_TYPE_ITEM, 6),
HEAT("HeatNetwork", "heat", MekanismLang.TRANSMISSION_TYPE_HEAT, 7);

public static final Codec<TransmissionType> CODEC = StringRepresentable.fromEnum(TransmissionType::values);
public static final IntFunction<TransmissionType> BY_ID = ByIdMap.continuous(TransmissionType::ordinal, values(), ByIdMap.OutOfBoundsStrategy.WRAP);
Expand All @@ -29,11 +29,13 @@ public enum TransmissionType implements IHasTranslationKey, StringRepresentable
private final String name;
private final String transmission;
private final ILangEntry langEntry;
private final int legacyOrdinal;

TransmissionType(String name, String transmission, ILangEntry langEntry) {
TransmissionType(String name, String transmission, ILangEntry langEntry, int legacyOrdinal) {
this.name = name;
this.transmission = transmission;
this.langEntry = langEntry;
this.legacyOrdinal = legacyOrdinal;
}

public String getName() {
Expand Down Expand Up @@ -65,4 +67,8 @@ public boolean checkTransmissionType(TileEntityTransmitter transmitter) {
public String getSerializedName() {
return transmission;
}

public int getLegacyOrdinal() {
return legacyOrdinal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@

public class TileComponentConfig implements ITileComponent, ISpecificContainerTracker {

public static final String LEGACY_ITEM_EJECT_KEY = SerializationConstants.EJECT + TransmissionType.ITEM.getLegacyOrdinal();
public static final String LEGACY_ITEM_CONFIG_KEY = SerializationConstants.CONFIG + TransmissionType.ITEM.getLegacyOrdinal();
public final TileEntityMekanism tile;
private final Map<TransmissionType, ConfigInfo> configInfo = new EnumMap<>(TransmissionType.class);
private final Map<TransmissionType, List<Consumer<Direction>>> configChangeListeners = new EnumMap<>(TransmissionType.class);
Expand Down Expand Up @@ -297,23 +299,39 @@ public static void read(CompoundTag configNBT, Map<TransmissionType, ConfigInfo>
}

public static void read(CompoundTag configNBT, Map<TransmissionType, ConfigInfo> configInfo, BiConsumer<TransmissionType, RelativeSide> onChange) {
//todo 1.22 remove backcompat - check for old ITEM ordinal, switch to legacy ordinals if found
boolean isLegacyData = configNBT.contains(LEGACY_ITEM_CONFIG_KEY) || configNBT.contains(LEGACY_ITEM_EJECT_KEY);
for (Entry<TransmissionType, ConfigInfo> entry : configInfo.entrySet()) {
TransmissionType type = entry.getKey();
ConfigInfo info = entry.getValue();
NBTUtils.setBooleanIfPresent(configNBT, SerializationConstants.EJECT + type.ordinal(), info::setEjecting);
String configKey = SerializationConstants.CONFIG + type.ordinal();
int ordinalToUse = isLegacyData ? type.getLegacyOrdinal() : type.ordinal();
NBTUtils.setBooleanIfPresent(configNBT, SerializationConstants.EJECT + ordinalToUse, info::setEjecting);
String configKey = SerializationConstants.CONFIG + ordinalToUse;
if (configNBT.contains(configKey, Tag.TAG_INT_ARRAY)) {
int[] sideData = configNBT.getIntArray(configKey);
for (int i = 0; i < sideData.length && i < EnumUtils.SIDES.length; i++) {
RelativeSide side = EnumUtils.SIDES[i];
if (info.setDataType(DataType.BY_ID.apply(sideData[i]), side)) {
onChange.accept(type, side);
readConfigSides(configNBT, onChange, configKey, info, type);
} else if (isLegacyData && type == TransmissionType.CHEMICAL) {
//fallback to try load other types in case a machine didn't have GAS
for (int legacyOrdinal = TransmissionType.CHEMICAL.getLegacyOrdinal() + 1; legacyOrdinal < TransmissionType.ITEM.getLegacyOrdinal(); legacyOrdinal++) {
configKey = SerializationConstants.CONFIG + legacyOrdinal;
if (configNBT.contains(configKey, Tag.TAG_INT_ARRAY)) {
readConfigSides(configNBT, onChange, configKey, info, type);
break;
}
}
}
}
}

private static void readConfigSides(CompoundTag configNBT, BiConsumer<TransmissionType, RelativeSide> onChange, String configKey, ConfigInfo info, TransmissionType type) {
int[] sideData = configNBT.getIntArray(configKey);
for (int i = 0; i < sideData.length && i < EnumUtils.SIDES.length; i++) {
RelativeSide side = EnumUtils.SIDES[i];
if (info.setDataType(DataType.BY_ID.apply(sideData[i]), side)) {
onChange.accept(type, side);
}
}
}

@Override
public CompoundTag serialize(HolderLookup.Provider provider) {
return write(configInfo, true);
Expand Down

0 comments on commit 3fd900a

Please sign in to comment.