Skip to content
This repository was archived by the owner on May 26, 2018. It is now read-only.

Commit 5bfaf7c

Browse files
committed
Global object registry, also, support the new itemblockwithmetadata constructor
1 parent 2d98835 commit 5bfaf7c

File tree

3 files changed

+91
-2
lines changed

3 files changed

+91
-2
lines changed

common/cpw/mods/fml/common/Loader.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import cpw.mods.fml.common.event.FMLLoadEvent;
5656
import cpw.mods.fml.common.functions.ModIdFunction;
5757
import cpw.mods.fml.common.modloader.BaseModProxy;
58+
import cpw.mods.fml.common.registry.GameData;
5859
import cpw.mods.fml.common.toposort.ModSorter;
5960
import cpw.mods.fml.common.toposort.ModSortingException;
6061
import cpw.mods.fml.common.toposort.TopologicalSort;
@@ -674,6 +675,8 @@ public void initializeMods()
674675
// Mod controller should be in the initialization state here
675676
modController.distributeStateMessage(LoaderState.INITIALIZATION);
676677
modController.transition(LoaderState.POSTINITIALIZATION);
678+
// Construct the "mod object table" so mods can refer to it in IMC and postinit
679+
GameData.buildModObjectTable();
677680
modController.distributeStateMessage(FMLInterModComms.IMCEvent.class);
678681
modController.distributeStateMessage(LoaderState.POSTINITIALIZATION);
679682
modController.transition(LoaderState.AVAILABLE);

common/cpw/mods/fml/common/registry/GameData.java

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* are made available under the terms of the GNU Lesser Public License v2.1
66
* which accompanies this distribution, and is available at
77
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
8-
*
8+
*
99
* Contributors:
1010
* cpw - implementation
1111
*/
@@ -22,17 +22,24 @@
2222
import java.util.concurrent.CountDownLatch;
2323
import java.util.logging.Level;
2424

25+
import net.minecraft.block.Block;
26+
import net.minecraft.block.BlockSand;
2527
import net.minecraft.item.Item;
2628
import net.minecraft.nbt.NBTTagCompound;
2729
import net.minecraft.nbt.NBTTagList;
2830

2931
import com.google.common.base.Function;
3032
import com.google.common.base.Throwables;
3133
import com.google.common.collect.ImmutableMap;
34+
import com.google.common.collect.ImmutableTable;
35+
import com.google.common.collect.ImmutableTable.Builder;
3236
import com.google.common.collect.MapDifference;
37+
import com.google.common.collect.Tables;
3338
import com.google.common.collect.MapDifference.ValueDifference;
3439
import com.google.common.collect.Maps;
3540
import com.google.common.collect.Sets;
41+
import com.google.common.collect.Table;
42+
import com.google.common.collect.Table.Cell;
3643

3744
import cpw.mods.fml.common.FMLLog;
3845
import cpw.mods.fml.common.Loader;
@@ -46,6 +53,7 @@ public class GameData {
4653
private static MapDifference<Integer, ItemData> difference;
4754
private static boolean shouldContinue = true;
4855
private static boolean isSaveValid = true;
56+
private static ImmutableTable<String, String, Integer> modObjectTable;
4957
private static Map<String,String> ignoredMods;
5058

5159
private static boolean isModIgnoredForIdValidation(String modId)
@@ -238,4 +246,50 @@ static void setName(Item item, String name, String modId)
238246
ItemData itemData = idMap.get(id);
239247
itemData.setName(name,modId);
240248
}
249+
250+
public static void buildModObjectTable()
251+
{
252+
if (modObjectTable != null)
253+
{
254+
throw new IllegalStateException("Illegal call to buildModObjectTable!");
255+
}
256+
257+
Map<Integer, Cell<String, String, Integer>> map = Maps.transformValues(idMap, new Function<ItemData,Cell<String,String,Integer>>() {
258+
public Cell<String,String,Integer> apply(ItemData data)
259+
{
260+
return Tables.immutableCell(data.getModId(), data.getItemType(), data.getItemId());
261+
}
262+
});
263+
264+
Builder<String, String, Integer> tBuilder = ImmutableTable.builder();
265+
for (Cell<String, String, Integer> c : map.values())
266+
{
267+
tBuilder.put(c);
268+
}
269+
modObjectTable = tBuilder.build();
270+
}
271+
static Item findItem(String modId, String name)
272+
{
273+
if (modObjectTable == null)
274+
{
275+
return null;
276+
}
277+
278+
return Item.field_77698_e[modObjectTable.get(modId, name)];
279+
}
280+
281+
static Block findBlock(String modId, String name)
282+
{
283+
if (modObjectTable == null)
284+
{
285+
return null;
286+
}
287+
288+
Integer blockId = modObjectTable.get(modId, name);
289+
if (blockId >= Block.field_71973_m.length)
290+
{
291+
return null;
292+
}
293+
return Block.field_71973_m[blockId];
294+
}
241295
}

common/cpw/mods/fml/common/registry/GameRegistry.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
package cpw.mods.fml.common.registry;
1414

15+
import java.lang.reflect.Constructor;
1516
import java.util.List;
1617
import java.util.Map;
1718
import java.util.Random;
@@ -211,7 +212,16 @@ public static void registerBlock(net.minecraft.block.Block block, Class<? extend
211212
assert block != null : "registerBlock: block cannot be null";
212213
assert itemclass != null : "registerBlock: itemclass cannot be null";
213214
int blockItemId = block.field_71990_ca - 256;
214-
Item i = itemclass.getConstructor(int.class).newInstance(blockItemId);
215+
Constructor<? extends ItemBlock> itemCtor;
216+
try
217+
{
218+
itemCtor = itemclass.getConstructor(int.class);
219+
}
220+
catch (NoSuchMethodException e)
221+
{
222+
itemCtor = itemclass.getConstructor(int.class, Block.class);
223+
}
224+
Item i = itemCtor.newInstance(blockItemId, block);
215225
GameRegistry.registerItem(i,name, modId);
216226
}
217227
catch (Exception e)
@@ -360,4 +370,26 @@ public static void onPlayerRespawn(EntityPlayer player)
360370
tracker.onPlayerRespawn(player);
361371
}
362372

373+
374+
/**
375+
* Look up a mod block in the global "named item list"
376+
* @param modId The modid owning the block
377+
* @param name The name of the block itself
378+
* @return The block or null if not found
379+
*/
380+
public static net.minecraft.block.Block findBlock(String modId, String name)
381+
{
382+
return GameData.findBlock(modId, name);
383+
}
384+
385+
/**
386+
* Look up a mod item in the global "named item list"
387+
* @param modId The modid owning the item
388+
* @param name The name of the item itself
389+
* @return The item or null if not found
390+
*/
391+
public static net.minecraft.item.Item findItem(String modId, String name)
392+
{
393+
return GameData.findItem(modId, name);
394+
}
363395
}

0 commit comments

Comments
 (0)