public static List<String> processIdRematches( Iterable<MissingMapping> missedMappings, boolean isLocalWorld, GameData gameData, Map<String, Integer[]> remapBlocks, Map<String, Integer[]> remapItems) { List<String> failed = Lists.newArrayList(); List<String> ignored = Lists.newArrayList(); List<String> warned = Lists.newArrayList(); List<String> defaulted = Lists.newArrayList(); for (MissingMapping remap : missedMappings) { FMLMissingMappingsEvent.Action action = remap.getAction(); if (action == FMLMissingMappingsEvent.Action.REMAP) { // block/item re-mapped, finish the registration with the new name/object, but the old id int currId, newId; String newName; if (remap.type == Type.BLOCK) { currId = getMain().iBlockRegistry.getId((Block) remap.getTarget()); newName = getMain().iBlockRegistry.getNameForObject(remap.getTarget()).toString(); FMLLog.fine("The Block %s is being remapped to %s.", remap.name, newName); newId = gameData.registerBlock((Block) remap.getTarget(), newName, remap.id); gameData.iBlockRegistry.addAlias(remap.name, newName); } else { currId = getMain().iItemRegistry.getId((Item) remap.getTarget()); newName = getMain().iItemRegistry.getNameForObject(remap.getTarget()).toString(); FMLLog.fine("The Item %s is being remapped to %s.", remap.name, newName); newId = gameData.registerItem((Item) remap.getTarget(), newName, remap.id); gameData.iItemRegistry.addAlias(remap.name, newName); } if (newId != remap.id) throw new IllegalStateException(); if (currId != newId) { FMLLog.info( "Fixed %s id mismatch %s: %d (init) -> %d (map).", remap.type == Type.BLOCK ? "block" : "item", newName, currId, newId); (remap.type == Type.BLOCK ? remapBlocks : remapItems) .put(newName, new Integer[] {currId, newId}); } } else if (action == FMLMissingMappingsEvent.Action.BLOCKONLY) { // Pulled out specifically so the block doesn't get reassigned a new ID just because it's // Item block has gone away FMLLog.fine( "The ItemBlock %s is no longer present in the game. The residual block will remain", remap.name); } else { // block item missing, warn as requested and block the id if (action == FMLMissingMappingsEvent.Action.DEFAULT) { defaulted.add(remap.name); } else if (action == FMLMissingMappingsEvent.Action.IGNORE) { ignored.add(remap.name); } else if (action == FMLMissingMappingsEvent.Action.FAIL) { failed.add(remap.name); } else if (action == FMLMissingMappingsEvent.Action.WARN) { warned.add(remap.name); } gameData.block(remap.id); // prevent the id from being reused later } } if (!defaulted.isEmpty()) { String text = "Forge Mod Loader detected missing blocks/items.\n\n" + "There are " + defaulted.size() + " missing blocks and items in this save.\n" + "If you continue the missing blocks/items will get removed.\n" + "A world backup will be automatically created in your saves directory.\n\n" + "Missing Blocks/Items:\n"; for (String s : defaulted) text += s + "\n"; boolean confirmed = StartupQuery.confirm(text); if (!confirmed) StartupQuery.abort(); try { String skip = System.getProperty("fml.doNotBackup"); if (skip == null || !"true".equals(skip)) { ZipperUtil.backupWorld(); } else { for (int x = 0; x < 10; x++) FMLLog.severe("!!!!!!!!!! UPDATING WORLD WITHOUT DOING BACKUP !!!!!!!!!!!!!!!!"); } } catch (IOException e) { StartupQuery.notify("The world backup couldn't be created.\n\n" + e); StartupQuery.abort(); } warned.addAll(defaulted); } if (!failed.isEmpty()) { FMLLog.severe( "This world contains blocks and items that refuse to be remapped. The world will not be loaded"); return failed; } if (!warned.isEmpty()) { FMLLog.severe("This world contains block and item mappings that may cause world breakage"); return failed; } else if (!ignored.isEmpty()) { FMLLog.fine("There were %d missing mappings that have been ignored", ignored.size()); } return failed; }
/** * Fix IDs improperly allocated by early versions of the registry, best-effort. * * <p>Items sharing the same ID with a block, but not sharing the same registry name will be * mapped to an unused id. Losing items instead of blocks should minimize the damage. * * @param dataList List containing the IDs to fix */ public static void fixBrokenIds( GameDataSnapshot.Entry blocks, GameDataSnapshot.Entry items, Set<Integer> blockedIds) { BitSet availabilityMap = new BitSet(MAX_ITEM_ID + 1); // reserve all ids occupied by blocks for (Entry<String, Integer> entry : blocks.ids.entrySet()) { availabilityMap.set(entry.getValue()); } Set<Integer> newBlockedIds = new HashSet<Integer>(); Set<String> itemsToRemove = new HashSet<String>(); Map<String, Integer> itemsToRelocate = new HashMap<String, Integer>(); // check all ids occupied by items for (Entry<String, Integer> entry : items.ids.entrySet()) { int oldId = entry.getValue(); String name = entry.getKey(); Item item = getMain().iItemRegistry.getRaw(name); boolean blockThisId = false; // block oldId unless it's used by a block if (item == null) // item no longer available { // can't fix items without reliably checking if they are ItemBlocks FMLLog.warning( "Item %s (old id %d) is no longer available and thus can't be fixed.", name, oldId); itemsToRemove.add(name); blockThisId = true; } else if (item instanceof ItemBlock) { if (blocks.ids.containsKey(name)) // the item was an ItemBlock before { int blockId = blocks.ids.get(name); if (blockId != oldId) // mis-located ItemBlock { // relocate to the matching block FMLLog.warning( "ItemBlock %s (old id %d) doesn't have the same id as its block (%d).", name, oldId, blockId); itemsToRelocate.put(name, blockId); blockThisId = true; } else // intact ItemBlock { availabilityMap.set(oldId); // occupy id } } else // the item hasn't been an ItemBlock before, but it's now { // can't fix these, drop them FMLLog.warning( "Item %s (old id %d) has been migrated to an ItemBlock and can't be fixed.", name, oldId); itemsToRemove.add(name); blockThisId = true; } } else if (availabilityMap.get(oldId)) // normal item, id is already occupied { // remove the item mapping FMLLog.warning( "Item %s (old id %d) is conflicting with another block/item and can't be fixed.", name, oldId); itemsToRemove.add(name); } else // intact Item { availabilityMap.set(oldId); // occupy id } // handle blocking the id from future use if possible (i.e. not used by a conflicting block) // blockThisId requests don't modify availabilityMap, it could only be set by a block (or // another item, which isn't being handled) if (blockThisId && !availabilityMap.get(oldId)) { // there's no block occupying this id, thus block the id from future use // as there may still be ItemStacks in the world referencing it newBlockedIds.add(oldId); availabilityMap.set(oldId); } } if (itemsToRemove.isEmpty() && itemsToRelocate.isEmpty()) return; // nothing to do // confirm String text = "Forge Mod Loader detected that this save is damaged.\n\n" + "It's likely that an automatic repair can successfully restore\n" + "most of it, except some items which may get swapped with others.\n\n" + "A world backup will be created as a zip file in your saves\n" + "directory automatically.\n\n" + itemsToRemove.size() + " items need to be removed.\n" + itemsToRelocate.size() + " items need to be relocated."; boolean confirmed = StartupQuery.confirm(text); if (!confirmed) StartupQuery.abort(); // confirm missing mods causing item removal Set<String> modsMissing = new HashSet<String>(); for (String name : itemsToRemove) { modsMissing.add(name.substring(0, name.indexOf(':'))); } for (Iterator<String> it = modsMissing.iterator(); it.hasNext(); ) { String mod = it.next(); if (mod.equals("minecraft") || Loader.isModLoaded(mod)) it.remove(); } if (!modsMissing.isEmpty()) { text = "Forge Mod Loader detected that " + modsMissing.size() + " mods are missing.\n\n" + "If you continue items previously provided by those mods will be\n" + "removed while repairing this world save.\n\n" + "Missing mods:\n"; for (String mod : modsMissing) text += mod + "\n"; confirmed = StartupQuery.confirm(text); if (!confirmed) StartupQuery.abort(); } // backup try { String skip = System.getProperty("fml.doNotBackup"); if (skip == null || !"true".equals(skip)) { ZipperUtil.backupWorld(); } else { for (int x = 0; x < 10; x++) FMLLog.severe("!!!!!!!!!! UPDATING WORLD WITHOUT DOING BACKUP !!!!!!!!!!!!!!!!"); } } catch (IOException e) { StartupQuery.notify("The world backup couldn't be created.\n\n" + e); StartupQuery.abort(); } // apply fix for (String name : itemsToRemove) { FMLLog.warning("Removed Item %s, old id %d.", name, items.ids.remove(name)); } for (Map.Entry<String, Integer> entry : itemsToRelocate.entrySet()) { int newId = entry.getValue(); int oldId = items.ids.put(entry.getKey(), newId); FMLLog.warning("Remapped Item %s to id %d, old id %d.", entry.getKey(), newId, oldId); } blockedIds.addAll(newBlockedIds); }