diff --git a/fml/common/cpw/mods/fml/common/Loader.java b/fml/common/cpw/mods/fml/common/Loader.java index c4a8950a21..15524e3436 100644 --- a/fml/common/cpw/mods/fml/common/Loader.java +++ b/fml/common/cpw/mods/fml/common/Loader.java @@ -55,6 +55,7 @@ import cpw.mods.fml.common.event.FMLInterModComms; import cpw.mods.fml.common.event.FMLLoadEvent; import cpw.mods.fml.common.functions.ModIdFunction; import cpw.mods.fml.common.modloader.BaseModProxy; +import cpw.mods.fml.common.registry.GameData; import cpw.mods.fml.common.toposort.ModSorter; import cpw.mods.fml.common.toposort.ModSortingException; import cpw.mods.fml.common.toposort.TopologicalSort; @@ -674,6 +675,8 @@ public class Loader // Mod controller should be in the initialization state here modController.distributeStateMessage(LoaderState.INITIALIZATION); modController.transition(LoaderState.POSTINITIALIZATION); + // Construct the "mod object table" so mods can refer to it in IMC and postinit + GameData.buildModObjectTable(); modController.distributeStateMessage(FMLInterModComms.IMCEvent.class); modController.distributeStateMessage(LoaderState.POSTINITIALIZATION); modController.transition(LoaderState.AVAILABLE); diff --git a/fml/common/cpw/mods/fml/common/registry/GameData.java b/fml/common/cpw/mods/fml/common/registry/GameData.java index 407becba72..3bdd862d7f 100644 --- a/fml/common/cpw/mods/fml/common/registry/GameData.java +++ b/fml/common/cpw/mods/fml/common/registry/GameData.java @@ -5,7 +5,7 @@ * are made available under the terms of the GNU Lesser Public License v2.1 * which accompanies this distribution, and is available at * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html - * + * * Contributors: * cpw - implementation */ @@ -22,6 +22,8 @@ import java.util.Set; import java.util.concurrent.CountDownLatch; import java.util.logging.Level; +import net.minecraft.block.Block; +import net.minecraft.block.BlockSand; import net.minecraft.item.Item; import net.minecraft.nbt.NBTTagCompound; import net.minecraft.nbt.NBTTagList; @@ -29,10 +31,15 @@ import net.minecraft.nbt.NBTTagList; import com.google.common.base.Function; import com.google.common.base.Throwables; import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableTable; +import com.google.common.collect.ImmutableTable.Builder; import com.google.common.collect.MapDifference; +import com.google.common.collect.Tables; import com.google.common.collect.MapDifference.ValueDifference; import com.google.common.collect.Maps; import com.google.common.collect.Sets; +import com.google.common.collect.Table; +import com.google.common.collect.Table.Cell; import cpw.mods.fml.common.FMLLog; import cpw.mods.fml.common.Loader; @@ -46,6 +53,7 @@ public class GameData { private static MapDifference difference; private static boolean shouldContinue = true; private static boolean isSaveValid = true; + private static ImmutableTable modObjectTable; private static Map ignoredMods; private static boolean isModIgnoredForIdValidation(String modId) @@ -238,4 +246,50 @@ public class GameData { ItemData itemData = idMap.get(id); itemData.setName(name,modId); } + + public static void buildModObjectTable() + { + if (modObjectTable != null) + { + throw new IllegalStateException("Illegal call to buildModObjectTable!"); + } + + Map> map = Maps.transformValues(idMap, new Function>() { + public Cell apply(ItemData data) + { + return Tables.immutableCell(data.getModId(), data.getItemType(), data.getItemId()); + } + }); + + Builder tBuilder = ImmutableTable.builder(); + for (Cell c : map.values()) + { + tBuilder.put(c); + } + modObjectTable = tBuilder.build(); + } + static Item findItem(String modId, String name) + { + if (modObjectTable == null) + { + return null; + } + + return Item.field_77698_e[modObjectTable.get(modId, name)]; + } + + static Block findBlock(String modId, String name) + { + if (modObjectTable == null) + { + return null; + } + + Integer blockId = modObjectTable.get(modId, name); + if (blockId >= Block.field_71973_m.length) + { + return null; + } + return Block.field_71973_m[blockId]; + } } diff --git a/fml/common/cpw/mods/fml/common/registry/GameRegistry.java b/fml/common/cpw/mods/fml/common/registry/GameRegistry.java index d6c1b20a02..3e8f5b4fe5 100644 --- a/fml/common/cpw/mods/fml/common/registry/GameRegistry.java +++ b/fml/common/cpw/mods/fml/common/registry/GameRegistry.java @@ -12,6 +12,7 @@ package cpw.mods.fml.common.registry; +import java.lang.reflect.Constructor; import java.util.List; import java.util.Map; import java.util.Random; @@ -211,7 +212,16 @@ public class GameRegistry assert block != null : "registerBlock: block cannot be null"; assert itemclass != null : "registerBlock: itemclass cannot be null"; int blockItemId = block.field_71990_ca - 256; - Item i = itemclass.getConstructor(int.class).newInstance(blockItemId); + Constructor itemCtor; + try + { + itemCtor = itemclass.getConstructor(int.class); + } + catch (NoSuchMethodException e) + { + itemCtor = itemclass.getConstructor(int.class, Block.class); + } + Item i = itemCtor.newInstance(blockItemId, block); GameRegistry.registerItem(i,name, modId); } catch (Exception e) @@ -360,4 +370,26 @@ public class GameRegistry tracker.onPlayerRespawn(player); } + + /** + * Look up a mod block in the global "named item list" + * @param modId The modid owning the block + * @param name The name of the block itself + * @return The block or null if not found + */ + public static net.minecraft.block.Block findBlock(String modId, String name) + { + return GameData.findBlock(modId, name); + } + + /** + * Look up a mod item in the global "named item list" + * @param modId The modid owning the item + * @param name The name of the item itself + * @return The item or null if not found + */ + public static net.minecraft.item.Item findItem(String modId, String name) + { + return GameData.findItem(modId, name); + } }