Jump to content

[1.8] [Solved] Iterating Item List to init, register, or registerRenders?


DavidTriphon

Recommended Posts

I was trying to simplify my code to save work in the future, but it appears I made a mistake in how I assumed Forge & FML worked or else I'm making a dumb mistake. I wanted to make a list of say, all my items in my modItems file, and then have the preinit function just iterate through all of the items by adding them to the right creativeTab. I also attempted to do this for registering and render registering functions since they all have the same line of code. What am I doing wrong? Is the easiest way just to plug in the functions into private functions instead and then call each of those individually for each item/block?

 

My modItems file: CubularItems.java

 

package davidt.cubularmod.init;

import java.awt.List;
import java.util.ArrayList;

import davidt.cubularmod.Reference;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class CubularItems {

//Item declarations go here

//boards
public static Item oak_board;
public static Item birch_board;
public static Item spruce_board;
public static Item jungle_board;
public static Item big_oak_board;
public static Item acacia_board;

//rocks
public static Item stone_rock;
public static Item granite_rock;
public static Item diorite_rock;
public static Item andesite_rock;
public static Item lapis_rock;
public static Item iron_rock;
public static Item gold_rock;

//bricks
public static Item stone_brick;
public static Item granite_brick;
public static Item diorite_brick;
public static Item andesite_brick;
public static Item lapis_brick;

//slates
public static Item stone_slate;
public static Item granite_slate;
public static Item diorite_slate;
public static Item andesite_slate;
public static Item lapis_slate;

//list
public static Item[] itemList = {
	oak_board, birch_board, spruce_board, jungle_board, big_oak_board, acacia_board,	//boards
	stone_rock, granite_rock, diorite_rock, andesite_rock, lapis_rock, iron_rock, gold_rock,	//rocks
	stone_brick, granite_brick, diorite_brick, andesite_brick, lapis_brick,	//bricks
	stone_slate, granite_slate, diorite_slate, andesite_slate, lapis_slate, //slates
};

//Item initializing goes here
public static void init()
{
	//boards
	oak_board = new Item().setUnlocalizedName("oak_board").setCreativeTab(CubularTabs.tabCubularItems);
	birch_board = new Item().setUnlocalizedName("birch_board").setCreativeTab(CubularTabs.tabCubularItems);
	spruce_board = new Item().setUnlocalizedName("spruce_board").setCreativeTab(CubularTabs.tabCubularItems);
	jungle_board = new Item().setUnlocalizedName("jungle_board").setCreativeTab(CubularTabs.tabCubularItems);
	big_oak_board = new Item().setUnlocalizedName("big_oak_board").setCreativeTab(CubularTabs.tabCubularItems);
	acacia_board = new Item().setUnlocalizedName("acacia_board").setCreativeTab(CubularTabs.tabCubularItems);

	//rocks
	stone_rock = new Item().setUnlocalizedName("stone_rock").setCreativeTab(CubularTabs.tabCubularItems);
	granite_rock = new Item().setUnlocalizedName("granite_rock").setCreativeTab(CubularTabs.tabCubularItems);
	diorite_rock = new Item().setUnlocalizedName("diorite_rock").setCreativeTab(CubularTabs.tabCubularItems);
	andesite_rock = new Item().setUnlocalizedName("andesite_rock").setCreativeTab(CubularTabs.tabCubularItems);
	lapis_rock = new Item().setUnlocalizedName("lapis_rock").setCreativeTab(CubularTabs.tabCubularItems);
	iron_rock = new Item().setUnlocalizedName("iron_rock").setCreativeTab(CubularTabs.tabCubularItems);
	gold_rock = new Item().setUnlocalizedName("gold_rock").setCreativeTab(CubularTabs.tabCubularItems);

	//bricks
	stone_brick = new Item().setUnlocalizedName("stone_brick").setCreativeTab(CubularTabs.tabCubularItems);
	granite_brick = new Item().setUnlocalizedName("granite_brick").setCreativeTab(CubularTabs.tabCubularItems);
	diorite_brick = new Item().setUnlocalizedName("diorite_brick").setCreativeTab(CubularTabs.tabCubularItems);
	andesite_brick = new Item().setUnlocalizedName("andesite_brick").setCreativeTab(CubularTabs.tabCubularItems);
	lapis_brick = new Item().setUnlocalizedName("lapis_brick").setCreativeTab(CubularTabs.tabCubularItems);

	//slates
	stone_slate = new Item().setUnlocalizedName("stone_slate").setCreativeTab(CubularTabs.tabCubularItems);
	granite_slate = new Item().setUnlocalizedName("granite_slate").setCreativeTab(CubularTabs.tabCubularItems);
	diorite_slate = new Item().setUnlocalizedName("diorite_slate").setCreativeTab(CubularTabs.tabCubularItems);
	andesite_slate = new Item().setUnlocalizedName("andesite_slate").setCreativeTab(CubularTabs.tabCubularItems);
	lapis_slate = new Item().setUnlocalizedName("lapis_slate").setCreativeTab(CubularTabs.tabCubularItems);
}

//Item registering goes here
public static void register()
{
	for (Item item : itemList)
	{
		System.out.println(item);
		GameRegistry.registerItem(item, item.getUnlocalizedName().substring(5));
	}
}

//Item render registering goes here
public static void registerRenders()
{
	for (Item item : itemList)
	{
		System.out.println(item);
		Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
	}
}
}

 

 

My ModBlocks file: CubularBlocks.java

 

package davidt.cubularmod.init;

import davidt.cubularmod.Reference;
import davidt.cubularmod.blocks.BlockStoneBricks;
import davidt.cubularmod.blocks.BlockStoneSlates;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class CubularBlocks {

//Block declarations go here
// public static [blockClass] [blockName]";

//slate blocks
public static Block stone_slates;
public static Block granite_slates;
public static Block diorite_slates;
public static Block andesite_slates;
public static Block lapis_slates;

//brick blocks
public static Block stone_bricks;
public static Block granite_bricks;
public static Block diorite_bricks;
public static Block andesite_bricks;
public static Block lapis_bricks;

//list
public static Block[] blockList =
{
	stone_slates, granite_slates, diorite_slates, andesite_slates, lapis_slates,	//slates
	stone_bricks, granite_bricks, diorite_bricks, andesite_bricks, lapis_bricks,	//bricks
};

//Block initializing goes here
// [blockName] = new [blockClass]([Material]).setUnlocalizedName("[blockName]");
public static void init()
{
	//slate blocks
	stone_slates = new BlockStoneSlates(Material.rock).setUnlocalizedName("stone_slates");
	granite_slates = new BlockStoneSlates(Material.rock).setUnlocalizedName("granite_slates");
	diorite_slates = new BlockStoneSlates(Material.rock).setUnlocalizedName("diorite_slates");
	andesite_slates = new BlockStoneSlates(Material.rock).setUnlocalizedName("andesite_slates");
	lapis_slates = new BlockStoneSlates(Material.rock).setUnlocalizedName("lapis_slates");

	//brick blocks
	stone_bricks = new BlockStoneBricks(Material.rock).setUnlocalizedName("stone_bricks");
	granite_bricks = new BlockStoneBricks(Material.rock).setUnlocalizedName("granite_bricks");
	diorite_bricks = new BlockStoneBricks(Material.rock).setUnlocalizedName("diorite_bricks");
	andesite_bricks = new BlockStoneBricks(Material.rock).setUnlocalizedName("andesite_bricks");
	lapis_bricks = new BlockStoneBricks(Material.rock).setUnlocalizedName("lapis_bricks");

	//list
	for (Block block : blockList)
	{
		block.setCreativeTab(CubularTabs.tabCubularBlocks);
	}
}

//Block registering goes here
public static void register()
{
	for (Block block : blockList)
	{
		GameRegistry.registerBlock(block, block.getUnlocalizedName().substring(5));
	}
}

//Block render registering goes here
public static void registerRenders()
{
	for (Block block : blockList)
	{
		Item item = Item.getItemFromBlock(block);
		Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
	}
}
}

 

 

SOLVED

 

I didn't want to go with manual initialization of the item/block fields, but I eventually went with it. I'm just glad everything else is automated now.

 

Here are the resulting corrected files for future reference

 

CubularBlocks.java:

 

package davidt.cubularmod.init;

import java.lang.reflect.Field;

import davidt.cubularmod.Reference;
import davidt.cubularmod.blocks.BlockStoneBricks;
import davidt.cubularmod.blocks.BlockStoneSlates;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class CubularBlocks {

//Block declarations go here

//slate blocks
public static Block stone_slates;
public static Block granite_slates;
public static Block diorite_slates;
public static Block andesite_slates;
public static Block lapis_slates;

//brick blocks
public static Block stone_bricks;
public static Block granite_bricks;
public static Block diorite_bricks;
public static Block andesite_bricks;
public static Block lapis_bricks;

//Block initializing goes here
public static void init()
{
	//slate blocks
	stone_slates = new BlockStoneSlates(Material.rock).setUnlocalizedName("stone_slates").setCreativeTab(CubularTabs.Blocks);
	granite_slates = new BlockStoneSlates(Material.rock).setUnlocalizedName("granite_slates").setCreativeTab(CubularTabs.Blocks);
	diorite_slates = new BlockStoneSlates(Material.rock).setUnlocalizedName("diorite_slates").setCreativeTab(CubularTabs.Blocks);
	andesite_slates = new BlockStoneSlates(Material.rock).setUnlocalizedName("andesite_slates").setCreativeTab(CubularTabs.Blocks);
	lapis_slates = new BlockStoneSlates(Material.rock).setUnlocalizedName("lapis_slates").setCreativeTab(CubularTabs.Blocks);

	//brick blocks
	stone_bricks = new BlockStoneBricks(Material.rock).setUnlocalizedName("stone_bricks").setCreativeTab(CubularTabs.Blocks);
	granite_bricks = new BlockStoneBricks(Material.rock).setUnlocalizedName("granite_bricks").setCreativeTab(CubularTabs.Blocks);
	diorite_bricks = new BlockStoneBricks(Material.rock).setUnlocalizedName("diorite_bricks").setCreativeTab(CubularTabs.Blocks);
	andesite_bricks = new BlockStoneBricks(Material.rock).setUnlocalizedName("andesite_bricks").setCreativeTab(CubularTabs.Blocks);
	lapis_bricks = new BlockStoneBricks(Material.rock).setUnlocalizedName("lapis_bricks").setCreativeTab(CubularTabs.Blocks);
}

//Block registering goes here
public static void register()
{
	try
	{
		for (Field field : CubularBlocks.class.getDeclaredFields())
		{
			if (Block.class.isAssignableFrom(field.getType()))
			{
				Block block = (Block) field.get(null);
				if (block != null)
				{
					GameRegistry.registerBlock(block, block.getUnlocalizedName().substring(5));
				}
			}
		}
	}
	catch (Exception exception)
	{
		System.out.println("ERROR (Block Registration): " + exception.toString());
	}
}

//Block render registering goes here
public static void registerRenders()
{
	try
	{
		for (Field field : CubularBlocks.class.getDeclaredFields())
		{
			if (Block.class.isAssignableFrom(field.getType()))
			{
				Block block = (Block) field.get(null);
				if (block != null)
				{
					Item item = Item.getItemFromBlock(block);
					Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
				}
			}
		}
	}
	catch (Exception exception)
	{
		System.out.println("ERROR (Block Registration): " + exception.toString());
	}
}
}

 

 

CubularItems.java:

 

package davidt.cubularmod.init;

import java.util.List;
import java.lang.reflect.Field;
import java.util.ArrayList;

import davidt.cubularmod.Reference;
import net.minecraft.client.Minecraft;
import net.minecraft.client.resources.model.ModelResourceLocation;
import net.minecraft.item.Item;
import net.minecraftforge.fml.common.registry.GameRegistry;

public class CubularItems {

//Item declarations go here

//boards
public static Item oak_board;
public static Item birch_board;
public static Item spruce_board;
public static Item jungle_board;
public static Item big_oak_board;
public static Item acacia_board;

//rocks
public static Item stone_rock;
public static Item granite_rock;
public static Item diorite_rock;
public static Item andesite_rock;
public static Item lapis_rock;
public static Item iron_rock;
public static Item gold_rock;

//bricks
public static Item stone_brick;
public static Item granite_brick;
public static Item diorite_brick;
public static Item andesite_brick;
public static Item lapis_brick;

//slates
public static Item stone_slate;
public static Item granite_slate;
public static Item diorite_slate;
public static Item andesite_slate;
public static Item lapis_slate;

//Item initializing goes here
public static void init()
{	
	//boards
	oak_board = new Item().setUnlocalizedName("oak_board").setCreativeTab(CubularTabs.Items);
	birch_board = new Item().setUnlocalizedName("birch_board").setCreativeTab(CubularTabs.Items);
	spruce_board = new Item().setUnlocalizedName("spruce_board").setCreativeTab(CubularTabs.Items);
	jungle_board = new Item().setUnlocalizedName("jungle_board").setCreativeTab(CubularTabs.Items);
	big_oak_board = new Item().setUnlocalizedName("big_oak_board").setCreativeTab(CubularTabs.Items);
	acacia_board = new Item().setUnlocalizedName("acacia_board").setCreativeTab(CubularTabs.Items);

	//rocks
	stone_rock = new Item().setUnlocalizedName("stone_rock").setCreativeTab(CubularTabs.Items);
	granite_rock = new Item().setUnlocalizedName("granite_rock").setCreativeTab(CubularTabs.Items);
	diorite_rock = new Item().setUnlocalizedName("diorite_rock").setCreativeTab(CubularTabs.Items);
	andesite_rock = new Item().setUnlocalizedName("andesite_rock").setCreativeTab(CubularTabs.Items);
	lapis_rock = new Item().setUnlocalizedName("lapis_rock").setCreativeTab(CubularTabs.Items);
	iron_rock = new Item().setUnlocalizedName("iron_rock").setCreativeTab(CubularTabs.Items);
	gold_rock = new Item().setUnlocalizedName("gold_rock").setCreativeTab(CubularTabs.Items);

	//bricks
	stone_brick = new Item().setUnlocalizedName("stone_brick").setCreativeTab(CubularTabs.Items);
	granite_brick = new Item().setUnlocalizedName("granite_brick").setCreativeTab(CubularTabs.Items);
	diorite_brick = new Item().setUnlocalizedName("diorite_brick").setCreativeTab(CubularTabs.Items);
	andesite_brick = new Item().setUnlocalizedName("andesite_brick").setCreativeTab(CubularTabs.Items);
	lapis_brick = new Item().setUnlocalizedName("lapis_brick").setCreativeTab(CubularTabs.Items);

	//slates
	stone_slate = new Item().setUnlocalizedName("stone_slate").setCreativeTab(CubularTabs.Items);
	granite_slate = new Item().setUnlocalizedName("granite_slate").setCreativeTab(CubularTabs.Items);
	diorite_slate = new Item().setUnlocalizedName("diorite_slate").setCreativeTab(CubularTabs.Items);
	andesite_slate = new Item().setUnlocalizedName("andesite_slate").setCreativeTab(CubularTabs.Items);
	lapis_slate = new Item().setUnlocalizedName("lapis_slate").setCreativeTab(CubularTabs.Items);
}

//Item registering goes here
public static void register()
{
	try
	{
		for (Field field : CubularItems.class.getDeclaredFields())
		{
			if (Item.class.isAssignableFrom(field.getType()))
			{
				Item item = (Item) field.get(null);
				if (item != null)
				{
					GameRegistry.registerItem(item, item.getUnlocalizedName().substring(5));
				}
			}
		}
	}
	catch (Exception exception)
	{
		System.out.println("ERROR (Item Registration): " + exception.toString());
	}
}

//Item render registering goes here
public static void registerRenders()
{
	try
	{
		for (Field field : CubularItems.class.getDeclaredFields())
		{
			if (Item.class.isAssignableFrom(field.getType()))
			{
				Item item = (Item) field.get(null);
				if (item != null)
				{
					Minecraft.getMinecraft().getRenderItem().getItemModelMesher().register(item, 0, new ModelResourceLocation(Reference.MOD_ID + ":" + item.getUnlocalizedName().substring(5), "inventory"));
				}
			}
		}
	}
	catch (Exception exception)
	{
		System.out.println("ERROR (Item Render Registration): " + exception.toString());
	}
}
}

 

Always, RESEARCH before asking a question. It's likely someone has already asked your question in the past. Filter your google searches with "site:www.minecraftforge.net/forum" to have a better chance finding your problem.

Link to comment
Share on other sites

I reckon your problem is that when you create the static array, all of the references are null. After initializing them later on, the array is not automatically updated, since it contains only null values and has no idea how to change what they point to.

 

The best way to automate registration is via Reflection and, if your registrations are not all cookie-cutter look-alikes, probably with one or two interfaces.

 

I have this in my mod's Items class, for example:

private static void registerItems() {
	try {
		for (Field f: ZSSItems.class.getFields()) {
			if (Item.class.isAssignableFrom(f.getType())) {
				Item item = (Item) f.get(null);
				if (item != null) {
					ZSSItems.registerItemComparatorMapping(item);
					String name = item.getUnlocalizedName();
					GameRegistry.registerItem(item, name.substring(name.lastIndexOf(".") + 1));
					if (item instanceof ICustomDispenserBehavior) {
						BlockDispenser.dispenseBehaviorRegistry.putObject(item, ((ICustomDispenserBehavior) item).getNewDispenserBehavior());
					}
				}
			}
		}
	} catch(Exception e) {
		ZSSMain.logger.warn("Caught exception while registering items: " + e.toString());
		e.printStackTrace();
	}
}

The above only makes use of one (custom) interface, but that should suffice as an example. There is similar code in the ClientProxy for renderers, as well as for my blocks, and those both involve a few more interfaces.

 

As for automating creative tabs... what's the point? If you're worried about extra lines of code, then why not just inline the call to setCreativeTabs when you initialize your block/item?

Link to comment
Share on other sites

Alright, I looked at that earlier and thought it was too complicated simply because I've never used a try/catch block before, but I think I understand the basics enough to use only what I need from that code and nothing else. Thanks for the help.

Always, RESEARCH before asking a question. It's likely someone has already asked your question in the past. Filter your google searches with "site:www.minecraftforge.net/forum" to have a better chance finding your problem.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi, I want to make a client-only mod, everything is ok, but when I use shaders, none of the textures rendered in RenderLevelStageEvent nor the crow entity model are rendered, I want them to be visible, because it's a horror themed mod Here is how i render the crow model in the CrowEntityRenderer<CrowEntity>, by the time i use this method, i know is not the right method but i don't think this is the cause of the problem, the renderType i'm using is entityCutout @Override public void render(CrowEntity p_entity, float entityYaw, float partialTick, PoseStack poseStack, MultiBufferSource bufferSource, int packedLight) { super.render(p_entity, entityYaw, partialTick, poseStack, bufferSource, packedLight); ClientEventHandler.getClient().crow.renderToBuffer(poseStack, bufferSource.getBuffer(ClientEventHandler.getClient().crow .renderType(TEXTURE)), packedLight, OverlayTexture.NO_OVERLAY, Utils.rgb(255, 255, 255)); } Here renderLevelStage @Override public void renderWorld(RenderLevelStageEvent e) { horrorEvents.draw(e); } Here is how i render every event public void draw(RenderLevelStageEvent e) { for (HorrorEvent event : currentHorrorEvents) { event.tick(e.getPartialTick()); event.draw(e); } } Here is how i render the crow model on the event @Override public void draw(RenderLevelStageEvent e) { if(e.getStage() == RenderLevelStageEvent.Stage.AFTER_ENTITIES) { float arcProgress = getArcProgress(0.25f); int alpha = (int) Mth.lerp(arcProgress, 0, 255); int packedLight = LevelRenderer.getLightColor(Minecraft.getInstance().level, blockPos); VertexConsumer builder = ClientEventHandler.bufferSource.getBuffer(crow); Crow<CreepyBirdHorrorEvent> model = ClientEventHandler .getClient().crow; model.setupAnim(this); RenderHelper.renderModelInWorld(model, position, offset, e.getCamera(), e.getPoseStack(), builder, packedLight, OverlayTexture.NO_OVERLAY, alpha); builder = ClientEventHandler.bufferSource.getBuffer(eyes); RenderHelper.renderModelInWorld(model, position, offset, e.getCamera(), e.getPoseStack(), builder, 15728880, OverlayTexture.NO_OVERLAY, alpha); } } How i render the model public static void renderModelInWorld(Model model, Vector3f pos, Vector3f offset, Camera camera, PoseStack matrix, VertexConsumer builder, int light, int overlay, int alpha) { matrix.pushPose(); Vec3 cameraPos = camera.getPosition(); double finalX = pos.x - cameraPos.x + offset.x; double finalY = pos.y - cameraPos.y + offset.y; double finalZ = pos.z - cameraPos.z + offset.z; matrix.pushPose(); matrix.translate(finalX, finalY, finalZ); matrix.mulPose(Axis.XP.rotationDegrees(180f)); model.renderToBuffer(matrix, builder, light, overlay, Utils .rgba(255, 255, 255, alpha)); matrix.popPose(); matrix.popPose(); } Thanks in advance
    • Here's the link: https://mclo.gs/7L5FibL Here's the link: https://mclo.gs/7L5FibL
    • Also the mod "Connector Extras" modifies Reach-entity-attributes and can cause fatal errors when combined with ValkrienSkies mod. Disable this mod and continue to use Syntra without it.
    • Hi everyone. I was trying modify the vanilla loot of the "short_grass" block, I would like it drops seeds and vegetal fiber (new item of my mod), but I don't found any guide or tutorial on internet. Somebody can help me?
    • On 1.20.1 use ValkrienSkies mod version 2.3.0 Beta 1. I had the same issues as you and it turns out the newer beta versions have tons of unresolved incompatibilities. If you change the version you will not be required to change the versions of eureka or any other additions unless prompted at startup. This will resolve Reach-entity-attributes error sound related error and cowardly errors.
  • Topics

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.