Global object registry, also, support the new itemblockwithmetadata constructor

This commit is contained in:
Christian 2013-03-08 13:15:09 -05:00
parent 304e717aea
commit 0de7554d8b
3 changed files with 91 additions and 2 deletions

View File

@ -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);

View File

@ -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<Integer, ItemData> difference;
private static boolean shouldContinue = true;
private static boolean isSaveValid = true;
private static ImmutableTable<String, String, Integer> modObjectTable;
private static Map<String,String> 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<Integer, Cell<String, String, Integer>> map = Maps.transformValues(idMap, new Function<ItemData,Cell<String,String,Integer>>() {
public Cell<String,String,Integer> apply(ItemData data)
{
return Tables.immutableCell(data.getModId(), data.getItemType(), data.getItemId());
}
});
Builder<String, String, Integer> tBuilder = ImmutableTable.builder();
for (Cell<String, String, Integer> 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];
}
}

View File

@ -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<? extends ItemBlock> 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);
}
}