From 3fd900a23087491df3990867396eb7127eb53f65 Mon Sep 17 00:00:00 2001 From: Thiakil Date: Thu, 22 Aug 2024 21:10:43 +0800 Subject: [PATCH] support loading legacy side config for tiles --- .../lib/transmitter/TransmissionType.java | 18 +++++++---- .../tile/component/TileComponentConfig.java | 32 +++++++++++++++---- 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/src/main/java/mekanism/common/lib/transmitter/TransmissionType.java b/src/main/java/mekanism/common/lib/transmitter/TransmissionType.java index b6d8268f6a3..f33600398dd 100644 --- a/src/main/java/mekanism/common/lib/transmitter/TransmissionType.java +++ b/src/main/java/mekanism/common/lib/transmitter/TransmissionType.java @@ -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 CODEC = StringRepresentable.fromEnum(TransmissionType::values); public static final IntFunction BY_ID = ByIdMap.continuous(TransmissionType::ordinal, values(), ByIdMap.OutOfBoundsStrategy.WRAP); @@ -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() { @@ -65,4 +67,8 @@ public boolean checkTransmissionType(TileEntityTransmitter transmitter) { public String getSerializedName() { return transmission; } + + public int getLegacyOrdinal() { + return legacyOrdinal; + } } \ No newline at end of file diff --git a/src/main/java/mekanism/common/tile/component/TileComponentConfig.java b/src/main/java/mekanism/common/tile/component/TileComponentConfig.java index 2df5300b741..bc35231db3c 100644 --- a/src/main/java/mekanism/common/tile/component/TileComponentConfig.java +++ b/src/main/java/mekanism/common/tile/component/TileComponentConfig.java @@ -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 configInfo = new EnumMap<>(TransmissionType.class); private final Map>> configChangeListeners = new EnumMap<>(TransmissionType.class); @@ -297,23 +299,39 @@ public static void read(CompoundTag configNBT, Map } public static void read(CompoundTag configNBT, Map configInfo, BiConsumer 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 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 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);