Jump to content

[1.10.2] Help with pulling data from loop


Altemio

Recommended Posts

Hello! The question is a little vague and might not be what I'm asking, as I'm not really sure as to what I'm actually asking. Sorry if this is the case!

 

This is my first mod & first time with Java (no experience at all in any language, so I'm a lost lamb in this), so this might come across as a dumb/silly question, as I suspect it isn't something I can't get around the way I want to, but so far my oh-so-terrible google-fu hasn't really given me much in the way of answers.

 

I'm generating items and ores from an enummap and running the enummap through a FOR loop. Everything works perfectly, and all the objects (items and ore) I wish to create are being created without any trouble. However, the issue I'm having is interfacing the results of these loops with each other, i.e. getting recipes to work.

 

If I merge the FOR blocks together (such as nuggets and ignots), I can get recipes nugget<->ingot to generate as intended, but my issue is with getting different FOR loops to interact with those loops, if that makes sense. My nuggets and Ingots use the same enum map, so they could be put in the same block, however my chunks use a different map (my base enummap, which does not include alloy materials). This results in an issue where I am unable to have my chunks smelted into ingots / nuggets, as the only value I can catch is the very last to be calculated in the nugget / ingot FOR loops, i.e Zinc.

 

I could probably overcome this by using the same enummap for my chunks as my nuggets/ingots (therefore generating alloy chunks, which I don't want) but the issue still persists because I need to pull my chunks out of that FOR loop and into another FOR loop in a different class, as I have ores being generated using the same method and I need them to drop my chunks.

 

I'm starting to think I need to go back to the original method I was using, where I was using static assignments so I could reference the objects. I was just hoping by doing it this way I could centralise everything and make my code more compact and easier to modify. If there is a way around this that anyone is able and willing to share, that would be awesome! If the news is bad (and the way I'm doing it is just outright wrong/bad), then that is also good as I can drop this way of 'doing it'. Thanks in advance!

 

 

I've removed all of my comments from the following code snippet (my entire class).

 

Spoiler

public class ItemGeneric {    
	static public ItemBase item_nugget = null;
	static public ItemBase item_ingot = null;
	static public ItemBase item_chunk = null;
	
	public static void initItems() {		
		
		for(EnumMaterialMetal enummaterial:EnumMaterialMetal.values())
		{
			{
				item_nugget = new ItemBase(enummaterial);

				item_nugget.setUnlocalizedName("nugget" + enummaterial.suffix);

				GameRegistry.register(item_nugget.setRegistryName("nugget" + enummaterial.suffix));
				OreDictionary.registerOre("nugget" + enummaterial.suffix, new ItemStack(item_nugget, 1, 0));

				item_nugget.setCreativeTab(IndifferentOres.altTabItems);
				IndifferentOres.proxy.registerItemRenderer(item_nugget);
			}
		}

		for(EnumMaterialMetal enummaterial:EnumMaterialMetal.values())
		{
			{

				item_ingot = new ItemBase(enummaterial);

				item_ingot.setUnlocalizedName("ingot" + enummaterial.suffix);

				GameRegistry.register(item_ingot.setRegistryName("ingot" + enummaterial.suffix));
				OreDictionary.registerOre("ingot" + enummaterial.suffix, new ItemStack(item_ingot, 1, 0));

				item_ingot.setCreativeTab(IndifferentOres.altTabItems);
				IndifferentOres.proxy.registerItemRenderer(item_ingot);
			}
		}
		
		for(EnumMaterialOre enummaterial:EnumMaterialOre.values())
		{
			{
				item_chunk = new ItemBase(enummaterial);

				item_chunk.setMaxStackSize(enummaterial.MAX_STACK_SIZE);
				item_chunk.setUnlocalizedName("chunk" + enummaterial.suffix);

				GameRegistry.register(item_chunk.setRegistryName("chunk" + enummaterial.suffix));
				OreDictionary.registerOre("chunk" + enummaterial.suffix, new ItemStack(item_chunk, 1, 0));
				
				item_chunk.setCreativeTab(IndifferentOres.altTabChunks);
				IndifferentOres.proxy.registerItemRenderer(item_chunk);
			}
		}
		
		//Does not actually work, only the last value to be passed through the loop is used.
		GameRegistry.addRecipe(new ItemStack(item_ingot), new Object[] {"###", "###", "###", '#', item_nugget}); // Nine Nuggets will make one ingot.
		GameRegistry.addShapelessRecipe(new ItemStack(item_nugget, 9), new ItemStack(item_ingot, 1)); // One Ingot will make nine nuggets
		GameRegistry.addSmelting(item_chunk, new ItemStack(item_nugget, 3), 1.0F); //One ore Chunk will make three nuggets.
	}
}

 

 

Link to comment
Share on other sites

With each loop, you are constantly overwriting the ItemBase fields, thus resulting in the field only containing the latest iteration of each loop. This is because a field can only hold 1 instance at a time.

 

I recommend you use metadata to store each different nugget in 1 Item, each different nugget in 1 Item etc. You can also add every nugget and ingot to a single Item, but that would clutter your code a lot.

 

Also, as you've stated, you currently don't have any experience in Java or another OOP language. While you may be able to pull it off, a lot of people couldn't and came to the forums with non-Minecraft, pure Java related questions, which sometimes is a PITA. So I, and a lot of other people on the forums, really recommend you to get a basic grasp of Java by any means other than Minecraft, just to get a better understanding of OOP in general.

  • Like 1

Don't PM me with questions. They will be ignored! Make a thread on the appropriate board for support.

 

1.12 -> 1.13 primer by williewillus.

 

1.7.10 and older versions of Minecraft are no longer supported due to it's age! Update to the latest version for support.

 

http://www.howoldisminecraft1710.today/

Link to comment
Share on other sites

Basically you just need to move all the code into a single for loop

 

public class ItemGeneric {    
	static public ItemBase item_nugget = null;
	static public ItemBase item_ingot = null;
	static public ItemBase item_chunk = null;
	
	public static void initItems() {		
		
		for(EnumMaterialMetal enummaterial:EnumMaterialMetal.values())
		{
				item_nugget = new ItemBase(enummaterial);

				item_nugget.setUnlocalizedName("nugget" + enummaterial.suffix);

				GameRegistry.register(item_nugget.setRegistryName("nugget" + enummaterial.suffix));
				OreDictionary.registerOre("nugget" + enummaterial.suffix, new ItemStack(item_nugget, 1, 0));

				item_nugget.setCreativeTab(IndifferentOres.altTabItems);
				IndifferentOres.proxy.registerItemRenderer(item_nugget);

				item_ingot = new ItemBase(enummaterial);

				item_ingot.setUnlocalizedName("ingot" + enummaterial.suffix);

				GameRegistry.register(item_ingot.setRegistryName("ingot" + enummaterial.suffix));
				OreDictionary.registerOre("ingot" + enummaterial.suffix, new ItemStack(item_ingot, 1, 0));

				item_ingot.setCreativeTab(IndifferentOres.altTabItems);
				IndifferentOres.proxy.registerItemRenderer(item_ingot);

				item_chunk = new ItemBase(enummaterial);

				item_chunk.setMaxStackSize(enummaterial.MAX_STACK_SIZE);
				item_chunk.setUnlocalizedName("chunk" + enummaterial.suffix);

				GameRegistry.register(item_chunk.setRegistryName("chunk" + enummaterial.suffix));
				OreDictionary.registerOre("chunk" + enummaterial.suffix, new ItemStack(item_chunk, 1, 0));
				
				item_chunk.setCreativeTab(IndifferentOres.altTabChunks);
				IndifferentOres.proxy.registerItemRenderer(item_chunk);
              
              GameRegistry.addRecipe(new ItemStack(item_ingot), new Object[] {"###", "###", "###", '#', item_nugget}); // Nine Nuggets will make one ingot.
		GameRegistry.addShapelessRecipe(new ItemStack(item_nugget, 9), new ItemStack(item_ingot, 1)); // One Ingot will make nine nuggets
		GameRegistry.addSmelting(item_chunk, new ItemStack(item_nugget, 3), 1.0F); //One ore Chunk will make three nuggets.
		}
	}
}

 

Which could be optimised:

public class ItemGeneric {
  
   protected static ItemBase getItem(EnumMaterialMetal enummaterial, String _type)
   {
		ItemBase item = new ItemBase(enummaterial);

		item.setMaxStackSize(enummaterial.MAX_STACK_SIZE);
		item.setUnlocalizedName(_type + enummaterial.suffix);

		GameRegistry.register(item.setRegistryName(_type + enummaterial.suffix));
		OreDictionary.registerOre(_type + enummaterial.suffix, new ItemStack(item, 1, 0));
				
		chunk.setCreativeTab(IndifferentOres.altTabChunks);
		IndifferentOres.proxy.registerItemRenderer(item);
		return item;
   }
  
	public static void initItems() {		
		
		ItemBase item_nugget;
		ItemBase item_ingot;
		ItemBase item_chunk;
      
		for(EnumMaterialMetal enummaterial:EnumMaterialMetal.values())
		{
			item_nugget = getItem(enummaterial,"nugget");
			item_ingot = getItem(enummaterial,"ingot");
			item_chunk = getItem(enummaterial,"chunk");
              
			GameRegistry.addRecipe(new ItemStack(item_ingot), new Object[] {"###", "###", "###", '#', item_nugget}); // Nine Nuggets will make one ingot.
			GameRegistry.addShapelessRecipe(new ItemStack(item_nugget, 9), new ItemStack(item_ingot, 1)); // One Ingot will make nine nuggets
			GameRegistry.addSmelting(item_chunk, new ItemStack(item_nugget, 3), 1.0F); //One ore Chunk will make three nuggets.
		}
	}
}

 

Note that if you wanted to access the items you would need to store them in an array or list in the getItem code or ask the registry for them using their registry name.

  • Like 1
Link to comment
Share on other sites

edit: ninja'd

 

34 minutes ago, larsgerrits said:

With each loop, you are constantly overwriting the ItemBase fields, thus resulting in the field only containing the latest iteration of each loop. This is because a field can only hold 1 instance at a time.

 

I recommend you use metadata to store each different nugget in 1 Item, each different nugget in 1 Item etc. You can also add every nugget and ingot to a single Item, but that would clutter your code a lot.

 

Also, as you've stated, you currently don't have any experience in Java or another OOP language. While you may be able to pull it off, a lot of people couldn't and came to the forums with non-Minecraft, pure Java related questions, which sometimes is a PITA. So I, and a lot of other people on the forums, really recommend you to get a basic grasp of Java by any means other than Minecraft, just to get a better understanding of OOP in general.

 

Many thanks for the response!

 

I realised that was going on, hence putting the recipes in the loop block works (each pass sets the recipe when the item is created) but outside of it does not (because only one value is being passed out of the loop, the last).

 

Guess I need more background, so to speak, rather than jumping in and hoping it will work (I'm surprised I got as far as I did). Probably tried doing something far too complex without the background for it. As for metadata, I'll be sure to look into that as it isn't something I've touched on yet.

 

9 minutes ago, stucuk said:

Basically you just need to move all the code into a single for loop


-snip-

 

Which could be optimised:

 

-snip-

 

Note that if you wanted to access the items you would need to store them in an array or list in the getItem code or ask the registry for them using their registry name.

 

Thanks for the detailed response! Will have a look at your suggestions and see what I can do. Using an array/list and getItem is certainly something I'm going to have to research.

Edited by Altemio
ninja'd (response to stucuk)
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.



×
×
  • Create New...

Important Information

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