Welcome. Here I'll show how to automatically set block and item ids (or indices) with Forge. The result should be: your mod searches for free indices, finds them and sets default values to each item and block, thus removing the need to manually adjust them and preventing Id conflicts.
Say you have a mod "AutoDemo" with block "BlockSub" and an item "ItemSub".
BlockSub.java
public class BlockSub extends Block {
protected BlockSub(int index)
{
super(index, Material.iron);
// TODO Auto-generated constructor stub
}
ItemSub.java
public class ItemSub extends Item
{
public ItemAssigner(int id) {
super(id);
}
}
AutoDemo.java
@Mod(modid="AD",name="AD",version="1")
public class AutoDemo{
public static Block subBlock;
public static Item subItem;
public static int itemIndex,blockIndex;
@PreInit
public void preLoad(FMLPreInitializationEvent event)
{
Configuration confi=new Configuration(new File(event.getModConfigurationDirectory(), "Autoassign demonstration.rtf"));
confi.load();
confi.save();
}
@Init
public void load(FMLInitializationEvent event)
{
}
}
Now one needs to create 2 methods for getting free indices. This one is for blocks:
int getFreeBlockIndex()
{
for (short index=257;index<Block.blocksList.length;index++)
{
if(Block.blocksList[index]==null)
{
return index;
}
}
return 0;
}
The next is for items:
int getFreeItemIndex()
{
for (short index=4097;index<Item.itemsList.length;index++)
{
if(Item.itemsList[index+256]==null)
{
return index-256;
}
}
return 0;
}
You can put these methods anywhere. I think it's better to put them in your package of shared stuff. Here I'll put them into AutoDemo.java.
Then you have to set default ids int the @PreInit:
Check whether all works and that should be finish. If successful, your items and blocks will automatically get free default ids and won't conflict with other mods' blocks and items; this prevents item overwriting.
Final AutoDemo.java:
@Mod(modid="AD",name="AD",version="1")
public class AutoDemo {
public static Block subBlock;
public static Item subItem;
public static int itemIndex,blockIndex;
int getFreeBlockIndex()
{
for (short index=257;index<Block.blocksList.length;index++)
{
if(Block.blocksList[index]==null)
{
return index;
}
}
return 0;
}
int getFreeItemIndex()
{
for (short index=4097;index<Item.itemsList.length;index++)
{
if(Item.itemsList[index]==null)
{
return index;
}
}
return 0;
@PreInit
public void preLoad(FMLPreInitializationEvent event)
{
Configuration confi=new Configuration(new File(event.getModConfigurationDirectory(), "Autoassign demonstration.rtf"));
confi.load();
blockIndex=confi.getBlock("Block",getFreeBlockIndex()).getInt();
itemIndex=confi.getItem("Item",getFreeItemIndex()).getInt();
confi.save();
}
@Init
public void load(FMLInitializationEvent event)
{
subBlock=new BlockSub(blockIndex).setUnlocalisedName("block").setCreativeTab(CreativeTabs.tabMisc);
ModLoader.addName(subBlock,"en_US","Sub Block");
GameRegistry.registerBlock(subBlock, "blocksub");
subItem=new ItemSub(itemIndex).setUnlocalizedName("item").setCreativeTab(CreativeTabs.tabMisc);
GameRegistry.registerItem(subItem,"itemsub");
ModLoader.addName(subItem,"en_US","Sub Item");
}
That's pretty slick. However, since you store the next free index in the config file, once the file is created it will keep the same index, which may not necessarily be free if you add another mod (unless they also used this method ;)). Wouldn't it be better to use getFreeItemIndex() every time you declare a new item / block?
Yeah. Like that. In my mods I always assign a mod item index from config which is the starting index for all my mod item ids, then when I create new items I use:
item1Name = new Item(modItemIndex++);
item2Name = new Item(modItemIndex++);
etc.
That just puts them all in a continuous block; the user can specify where it starts in the config. But using your method, switch 'modItemIndex++' for 'getFreeItemIndex()' would accomplish the same thing, but the Item IDs wouldn't necessarily be all together, which isn't usually important anyway.
I meant that you could make it a choice for the person inside the config to choose if they want auto ID assignments or not?
Then they would get what they want no matter what their preference is?
Say you have a mod "AutoDemo" with block "BlockSub" and an item "ItemSub".
BlockSub.java
ItemSub.java
AutoDemo.java
Now one needs to create 2 methods for getting free indices. This one is for blocks:
The next is for items:
You can put these methods anywhere. I think it's better to put them in your package of shared stuff. Here I'll put them into AutoDemo.java.
Then you have to set default ids int the @PreInit:
Now finish the item and block:
Check whether all works and that should be finish. If successful, your items and blocks will automatically get free default ids and won't conflict with other mods' blocks and items; this prevents item overwriting.
Final AutoDemo.java:
That just puts them all in a continuous block; the user can specify where it starts in the config. But using your method, switch 'modItemIndex++' for 'getFreeItemIndex()' would accomplish the same thing, but the Item IDs wouldn't necessarily be all together, which isn't usually important anyway.
I meant that you could make it a choice for the person inside the config to choose if they want auto ID assignments or not?
Then they would get what they want no matter what their preference is?