Mod Item Manager
Now that we can get all of our JSON data, we need to parse that data and create items. We use the ModItemManager to do this. The meat of the code is in the loadItems method:
public void loadItems(IResourceManager resourceManager) {
ModJsonLoader jsonLoader = new ModJsonLoader();
Map<ResourceLocation, JsonObject> itemResources = jsonLoader.loadJsonResources(resourceManager, ITEMS_FOLDER);
mItems = Sets.newHashSet();
for(Entry<ResourceLocation, JsonObject> entry : itemResources.entrySet()) {
ResourceLocation resourceLocation = entry.getKey();
if (resourceLocation.getPath().startsWith("_")) { continue; }
try {
Item item = deserializeItem(entry.getValue());
if (item == null) {
LOGGER.info("Skipping loading item {} as it's serializer returned null", resourceLocation);
continue;
}
mItems.add(item);
} catch (IllegalArgumentException | JsonParseException exception) {
LOGGER.error("Parsing error loading item {}", resourceLocation, exception);
}
}
LOGGER.info("Loaded {} items", mItems.size());
}
This uses ModJsonLoader to get all the JSON files, then parses them one by one using deserializeItem:
private Item deserializeItem(JsonObject json) {
String typeValue = JSONUtils.getString(json, "type");
ItemType type = ItemType.valueOf(typeValue.toUpperCase());
switch (type) {
case ITEM:
return deserializeBasicItem(json);
default:
LOGGER.info("Item type {} not supported", typeValue);
return null;
}
}
We select a different deserialize method based on the item’s type. At the time of writing we only have an implementation for a very basic item, but this will be updated later. If you look at the enumeration for item types you can see the other item types that will be added in the future:
private enum ItemType {
ITEM,
FOOD,
DURABLE,
DURABLE_BUCKET,
BOWL_FOOD,
BLOCK_NAMED,
BLOCK,
STONE_BOWL_FOOD,
STONE
}
The one method we do have is deserializeBasicItem:
private Item deserializeBasicItem(JsonObject json) {
String itemGroup = JSONUtils.getString(json, "itemGroup");
String registryName = JSONUtils.getString(json, "name");
ItemGroup group = getItemGroup(itemGroup);
return new ModItem(group, registryName);
}
We just pull out the item group and registry name then create a very basic ModItem. By default this will be loaded into the mod’s registry so we don’t need to set the Mod ID in the registry name here.
The penultimate method of interest is getItemGroup. This method simply looks up the item group based on the name set in the JSON attribute:
private ItemGroup getItemGroup(String name) {
for (ItemGroup group : ItemGroup.GROUPS) {
if (group.getTabLabel().equals(name)) {
return group;
}
}
throw new IllegalArgumentException("Invalid Item Group:" + name);
}
By using the names already there we don’t need to define them ourselves and other modders should already be familiar with them.
Finally we just need to make sure we have an accessor so we can use the list of items in our registry method:
public Item[] getItems() {
return mItems.toArray(new Item[0]);
}
We’re almost done. We just need to register the items generated by this class.