Jump to content

[1.12.2] Where can I get a list of all the blocks registered in game?


Recommended Posts

Posted

As for now I tried a "not so nice way" and by subscribing to onBlockRegister I just called event.getRegistry().getEntries() and made a list that way.

 

My "problem" is that I only get the block, and I don't think I can get the variants for each block from there.

 

Where can I get those like for planks get {minecraft:planks:0},{minecraft:planks:1},{minecraft:planks:2},{minecraft:planks:3},{minecraft:planks:4},{minecraft:planks:5}

 

And same for all the blocks.

 

PS: Is ok to get them that way, or should I get the variants and not the meta? For example in the case I later need to call it from my code.

Posted

Well, this is how I did it in order to print them out:

	/**
	 * This will list all mobs, using there unlocalized names, writing 
	 * the data to the file lists/mobs.txt.
	 */
	public static void listMobs() {	
		Set<ResourceLocation> mobrl = EntityList.getEntityNameList();
		ArrayList<String> mobs = new ArrayList<String>();
		for(ResourceLocation mob : mobrl) {
				mobs.add(mob.toString());
		}
		Collections.sort(mobs);
		
		BufferedWriter outstream = null;
		File moblist = new File(listsDir.toString() + File.separator + "entities.txt");
		if(moblist.exists()) moblist.delete(); 
		try {
			outstream = new BufferedWriter(new 
					FileWriter(moblist.toString()));			
			
			for(String mob : mobs){ 
				outstream.write(mob.toString());
				outstream.newLine();
			}
			
			if(outstream != null) outstream.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	
	/**
	 * This will list all items, using their complete unlocalized names 
	 * with mod id's, and write them the file lists/items.txt.  This 
	 * is useful for writing theme files.
	 */
	public static void listItems() {	
		BufferedWriter outstream = null;
		File itemlist = new File(listsDir.toString() + File.separator + "items.txt");
		if(itemlist.exists()) itemlist.delete(); 
		try {
			outstream = new BufferedWriter(new 
					FileWriter(itemlist.toString()));
			
			for(Object item : Item.REGISTRY){ 
				String name = Item.REGISTRY.getNameForObject((Item) item).toString();
				if(true) {
					outstream.write(name);
					outstream.newLine();
				}
			}
			
			if(outstream != null) outstream.close();
		} catch (IOException e) {
			System.err.println("[DLDUNGEONS] Error: Could not write file items.txt");
			e.printStackTrace();
		}		
	}
	
	
	/**
	 * This will list all blocks using their correct, unlocalized names, complete with 
	 * mod id's, and write them to the file lists/blocks.txt.  This is useful for editing 
	 * theme files.
	 */
	public static void listBlocks() {	
		BufferedWriter outstream = null;
		File itemlist = new File(listsDir.toString() + File.separator + "blocks.txt");
		if(itemlist.exists()) itemlist.delete(); 
		try {
			outstream = new BufferedWriter(new 
					FileWriter(itemlist.toString()));	
			
			for(Object block : Block.REGISTRY){ 
				String name = Block.REGISTRY.getNameForObject((Block)block).toString();
				if(true) {;
					outstream.write(name);
					outstream.newLine();
				}
			}
			
			if(outstream != null) outstream.close();
		} catch (IOException e) {
			System.err.println("[DLDUNGEONS] Error: Could not write file blocks.txt");
			e.printStackTrace();
		}		
	}

 

I hope it helps.

  • Like 1

Developer of Doomlike Dungeons.

Posted (edited)
43 minutes ago, JaredBGreat said:

Well, this is how I did it in order to print them out:


	/**
	 * This will list all mobs, using there unlocalized names, writing 
	 * the data to the file lists/mobs.txt.
	 */
	public static void listMobs() {	
		Set<ResourceLocation> mobrl = EntityList.getEntityNameList();
		ArrayList<String> mobs = new ArrayList<String>();
		for(ResourceLocation mob : mobrl) {
				mobs.add(mob.toString());
		}
		Collections.sort(mobs);
		
		BufferedWriter outstream = null;
		File moblist = new File(listsDir.toString() + File.separator + "entities.txt");
		if(moblist.exists()) moblist.delete(); 
		try {
			outstream = new BufferedWriter(new 
					FileWriter(moblist.toString()));			
			
			for(String mob : mobs){ 
				outstream.write(mob.toString());
				outstream.newLine();
			}
			
			if(outstream != null) outstream.close();
		} catch (IOException e) {
			e.printStackTrace();
		} 
	}
	
	
	/**
	 * This will list all items, using their complete unlocalized names 
	 * with mod id's, and write them the file lists/items.txt.  This 
	 * is useful for writing theme files.
	 */
	public static void listItems() {	
		BufferedWriter outstream = null;
		File itemlist = new File(listsDir.toString() + File.separator + "items.txt");
		if(itemlist.exists()) itemlist.delete(); 
		try {
			outstream = new BufferedWriter(new 
					FileWriter(itemlist.toString()));
			
			for(Object item : Item.REGISTRY){ 
				String name = Item.REGISTRY.getNameForObject((Item) item).toString();
				if(true) {
					outstream.write(name);
					outstream.newLine();
				}
			}
			
			if(outstream != null) outstream.close();
		} catch (IOException e) {
			System.err.println("[DLDUNGEONS] Error: Could not write file items.txt");
			e.printStackTrace();
		}		
	}
	
	
	/**
	 * This will list all blocks using their correct, unlocalized names, complete with 
	 * mod id's, and write them to the file lists/blocks.txt.  This is useful for editing 
	 * theme files.
	 */
	public static void listBlocks() {	
		BufferedWriter outstream = null;
		File itemlist = new File(listsDir.toString() + File.separator + "blocks.txt");
		if(itemlist.exists()) itemlist.delete(); 
		try {
			outstream = new BufferedWriter(new 
					FileWriter(itemlist.toString()));	
			
			for(Object block : Block.REGISTRY){ 
				String name = Block.REGISTRY.getNameForObject((Block)block).toString();
				if(true) {;
					outstream.write(name);
					outstream.newLine();
				}
			}
			
			if(outstream != null) outstream.close();
		} catch (IOException e) {
			System.err.println("[DLDUNGEONS] Error: Could not write file blocks.txt");
			e.printStackTrace();
		}		
	}

 

I hope it helps.

 

Thanks a lot! Sure it helps ;)

 

Problem is getting the different variants for each block found, but I gonna take a look around and see what I can find there that can provide me that information.

Edited by American2050
Posted
10 minutes ago, diesieben07 said:

If you want all possible block states of a Block use Block::getBlockState().getValidStates(), which will give you a List<IBlockState>. If you want all valid states of all blocks you can use flatMap:

ForgeRegistries.BLOCKS.getValues().stream().flatMap(block -> block.getBlockState().getValidStates()).collect(Collectors.toList())

Thanks, I knew there was something like the getValidStates, just wasn't sure where and the actual name of it ;)

That should do it. Thanks once again.

Posted
18 minutes ago, diesieben07 said:

If you want all possible block states of a Block use Block::getBlockState().getValidStates(), which will give you a List<IBlockState>. If you want all valid states of all blocks you can use flatMap:

ForgeRegistries.BLOCKS.getValues().stream().flatMap(block -> block.getBlockState().getValidStates()).collect(Collectors.toList())

And that could be helpful for me -- perhaps I don't have to use meta-data in my configs if users can get all the correct state labels.

  • Like 1

Developer of Doomlike Dungeons.

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



×
×
  • Create New...

Important Information

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