Jump to content

[1.12.2] [SOLVED] Add Custom Woods made with Variants to Ore Dictionary


DiogoRod

Recommended Posts

Hi there! 

I've started my own mod and after following a couple of forum posts, tutorials and looking at the vanilla code of the BlockPlanks, BlockLogs, (etc)... I was able to create my own trees and generate them all around but now I'm having a very specific problem. I want my custom planks to have the same functionalities, in terms of crafting, like all the other planks (create sticks, crafting tables, chests, etc...) and so after researching for a while and looking at the forge documentation I tried to register the planks on the Ore Dictionary, on my BlockInit class after registering all the blocks, like so: 

	OreDictionary.registerOre("plankWood", new ItemStack(ModBlocks.MODPLANKS.getDefaultState().withProperty(BlockPlankBase.VARIANT, BlockPlankBase.EnumType.SPRING).getBlock()));
	OreDictionary.registerOre("plankWood", new ItemStack(ModBlocks.MODPLANKS.getDefaultState().withProperty(BlockPlankBase.VARIANT, BlockPlankBase.EnumType.AUTUMN).getBlock()));
	OreDictionary.registerOre("plankWood", new ItemStack(ModBlocks.MODPLANKS.getDefaultState().withProperty(BlockPlankBase.VARIANT, BlockPlankBase.EnumType.SUMMER).getBlock()));
	OreDictionary.registerOre("plankWood", new ItemStack(ModBlocks.MODPLANKS.getDefaultState().withProperty(BlockPlankBase.VARIANT, BlockPlankBase.EnumType.WINTER).getBlock()));

also tried to instead of creating a new ItemStack just use the block but it also failed. The problem is (I think) that I'm using variants to create the different types of planks. Inside the Planks class I have the following to make this possible:

public static enum EnumType implements IStringSerializable {

		AUTUMN(0, "autumn"), SPRING(1, "spring"), WINTER(2, "winter"), SUMMER(3, "summer");

		private static final BlockPlankBase.EnumType[] META_LOOKUP = new BlockPlankBase.EnumType[values().length];
		private final int meta;
		private final String name, unlocalizedName;

		private EnumType(int meta, String name) {
			this(meta, name, name);
		}

		private EnumType(int meta, String name, String unlocalizedName) {
			this.name = name;
			this.meta = meta;
			this.unlocalizedName = unlocalizedName;
		}

		@Override
		public String getName() {
			return this.name;
		}

		public int getMeta() {
			return this.meta;
		}

		public String unlocalizedName() {
			return this.unlocalizedName;
		}

		@Override
		public String toString() {
			return this.name;
		}

		public static BlockPlankBase.EnumType byMetadata(int meta) {
			return META_LOOKUP[meta];
		}

		static {
			for (BlockPlankBase.EnumType blockplankbase$enumtype : values()) {
				META_LOOKUP[blockplankbase$enumtype.getMeta()] = blockplankbase$enumtype;
			}
		}
	}

So as you can see it is heavily inspired on the vanilla code (BlockPlanks).

Coming back to the problem, when I try to register the blocks on the Ore Dictionary it only works for the autumn planks, because it is the first one declared on the EnumType in the BlockCustomPlanks class that I made. I've tried to change the declarations inside the EnumType and put, for exempla, Winter first instead of Autumn and it then only works for the Winter planks. I'm guessing that it only works for the objects of this class with data = 0. 

I've been around this problem for a couple of days and I can't resolve the issue.

I would appreciate if anyone could shine a light on this problem!

Thank you! :))

Edited by DiogoRod
Link to comment
Share on other sites

Have called hasSubtypes(true) in your item class constructor?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

27 minutes ago, Draco18s said:

Have called hasSubtypes(true) in your item class constructor?

Nop, I've tried to do that after your suggestion but it just tells me that there is no function with that name and asks me if I want to create it...  should I? Cause I have no clue how...  I'm a bit confused ahah

Maybe it will be easier to understand the problem if I show the class itself:

public class BlockPlankBase extends Block{

	public static final PropertyEnum<BlockPlankBase.EnumType> VARIANT = PropertyEnum.<BlockPlankBase.EnumType>create(
			"variant", BlockPlankBase.EnumType.class);

	public BlockPlankBase(String name) {
		super(Material.WOOD);
		this.setUnlocalizedName(name);
		this.setRegistryName(name);
		this.setSoundType(SoundType.WOOD);
		this.setHardness(1.8F);
		this.setDefaultState(this.blockState.getBaseState().withProperty(VARIANT, BlockPlankBase.EnumType.AUTUMN));
	}

	@Override
	public int damageDropped(IBlockState state) {
		return ((BlockPlankBase.EnumType) state.getValue(VARIANT)).getMeta();
	}

	@Override
	public void getSubBlocks(CreativeTabs intemIn, NonNullList<ItemStack> items) {
		for (BlockPlankBase.EnumType blockplankbase$enumtype : BlockPlankBase.EnumType.values()) {
			items.add(new ItemStack(this, 1, blockplankbase$enumtype.getMeta()));
		}
	}

	@Override
	public IBlockState getStateFromMeta(int meta) {
		return this.getDefaultState().withProperty(VARIANT, BlockPlankBase.EnumType.byMetadata(meta));
	}

	@Override
	public int getMetaFromState(IBlockState state) {
		return state.getValue(VARIANT).getMeta();
	}

	@Override
	public ItemStack getPickBlock(IBlockState state, RayTraceResult target, World world, BlockPos pos,
			EntityPlayer player) {
		return new ItemStack(Item.getItemFromBlock(this), 1, getMetaFromState(world.getBlockState(pos)));
	}

	@Override
	protected BlockStateContainer createBlockState() {
		return new BlockStateContainer(this, new IProperty[] { VARIANT });
	}

	public static enum EnumType implements IStringSerializable {

		AUTUMN(0, "autumn"), SPRING(1, "spring"), WINTER(2, "winter"), SUMMER(3, "summer");

		private static final BlockPlankBase.EnumType[] META_LOOKUP = new BlockPlankBase.EnumType[values().length];
		private final int meta;
		private final String name, unlocalizedName;

		private EnumType(int meta, String name) {
			this(meta, name, name);
		}

		private EnumType(int meta, String name, String unlocalizedName) {
			this.name = name;
			this.meta = meta;
			this.unlocalizedName = unlocalizedName;
		}

		@Override
		public String getName() {
			return this.name;
		}

		public int getMeta() {
			return this.meta;
		}

		public String unlocalizedName() {
			return this.unlocalizedName;
		}

		@Override
		public String toString() {
			return this.name;
		}

		public static BlockPlankBase.EnumType byMetadata(int meta) {
			return META_LOOKUP[meta];
		}

		static {
			for (BlockPlankBase.EnumType blockplankbase$enumtype : values()) {
				META_LOOKUP[blockplankbase$enumtype.getMeta()] = blockplankbase$enumtype;
			}
		}
	}
}

 

Thanks for the attention btw.

Edited by DiogoRod
Link to comment
Share on other sites

1 hour ago, Draco18s said:

your item class

 

1 hour ago, DiogoRod said:

extends Block

 

That's a block. You need an Item to call that method. 

https://github.com/Draco18s/ReasonableRealism/blob/1.12.1/src/main/java/com/draco18s/ores/item/ItemOreBlock.java#L12

Edited by Draco18s

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

 
 
 
 
18 minutes ago, Draco18s said:

That's a block. You need an Item to call that method. 

Ahhhh yeah, on the Item it is indeed on the constructor. Sorry, got confused for a second.

public class ItemTreeVariants extends ItemBlock{

	public ItemTreeVariants(Block block) {
		super(block);
		setHasSubtypes(true);
		setMaxDamage(0);
	}
	
	@Override
	public String getUnlocalizedName(ItemStack stack) {
		return super.getUnlocalizedName() + "_" + ((IMetaName) this.block).getSpecialName(stack);
	}
	
	@Override
	public int getMetadata(int damage) {
		return damage;
	}
}

 At the time when I created the BlockPlankBase that I showed before I also created this class that on the BlockInit class is called in order to register the item block that corresponds to the BlockPlank itself, like so:

 

	// Tree
	public static final Block MODPLANKS = new BlockPlankBase("mod_planks");

	public static void register() {
		registerBlockVariants(MODPLANKS, new ItemTreeVariants(MODPLANKS));
		regPlanks();
	}

	public static void registerBlockVariants(Block block, ItemBlock itemblock) {
		ForgeRegistries.BLOCKS.register(block);
		block.setCreativeTab(Reference.MODTAB);
		itemblock.setRegistryName(block.getRegistryName());
		ForgeRegistries.ITEMS.register(itemblock);
	}

	public static void regPlanks() {

		OreDictionary.registerOre("plankWood", new ItemStack(ModBlocks.MODPLANKS.getDefaultState()
				.withProperty(BlockPlankBase.VARIANT, BlockPlankBase.EnumType.SPRING).getBlock()));
		OreDictionary.registerOre("plankWood", new ItemStack(ModBlocks.MODPLANKS.getDefaultState()
				.withProperty(BlockPlankBase.VARIANT, BlockPlankBase.EnumType.AUTUMN).getBlock()));
		OreDictionary.registerOre("plankWood", new ItemStack(ModBlocks.MODPLANKS.getDefaultState()
				.withProperty(BlockPlankBase.VARIANT, BlockPlankBase.EnumType.SUMMER).getBlock()));
		OreDictionary.registerOre("plankWood", new ItemStack(ModBlocks.MODPLANKS.getDefaultState()
				.withProperty(BlockPlankBase.VARIANT, BlockPlankBase.EnumType.WINTER).getBlock()));
	}

 

I hope I'm not posting to much code, but sometimes I feel like it's easier to explain by posting the code and English is not my first language so I do apologize if say anything that sounds a bit confusing from time to time. Either way, thanks for now!  

Link to comment
Share on other sites

31 minutes ago, DiogoRod said:

sometimes I feel like it's easier to explain by posting the code and English is not my first language so I do apologize if say anything that sounds a bit confusing from time to time.

No worries, you're doing pretty good so far.

31 minutes ago, DiogoRod said:

Either way, thanks for now!  

Got it working?

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

 
 
 
 
Quote

Got it working?

Nop, it still is doing the same thing, only one of the planks is being registered (the one with data=0), I'm back to the same initial problem. I was thinking that maybe I could use the ItemBlock class as so:

OreDictionary.registerOre("plankWood", new ItemTreeVariants(MODPLANKS.getDefaultState().withProperty(BlockPlankBase.VARIANT, BlockPlankBase.EnumType.SPRING).getBlock()));


also tried


OreDictionary.registerOre("plankWood", new ItemTreeVariants(MODPLANKS));

But it also doesn't work so I'm out of ideas.

Any suggestions are appreciated, thank you! 

Edited by DiogoRod
Link to comment
Share on other sites

1 hour ago, DiogoRod said:

ForgeRegistries.BLOCKS.register(block);

block.setCreativeTab(Reference.MODTAB);

itemblock.setRegistryName(block.getRegistryName());

ForgeRegistries.ITEMS.register(itemblock);

You should be using the Registry events to register your blocks and items, not this.

	@SubscribeEvent
	public void registerItems(RegistryEvent.Register<Item> event) {
	    event.getRegistry().registerAll(
			//items here
		);
	}

 

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

2 hours ago, Draco18s said:

You should be using the Registry events to register your blocks and items, not this.


	@SubscribeEvent
	public void registerItems(RegistryEvent.Register<Item> event) {
	    event.getRegistry().registerAll(
			//items here
		);
	}

 

Ok, so... I've made some progress and I am now using the registry events for everything else except the plank blocks which are the one with the variants because everytime I try it just... doesn't work. 

@SubscribeEvent
	public static void onItemRegister(RegistryEvent.Register<Item> event) {
		
		event.getRegistry().registerAll(ModItems.ITEMS.toArray(new Item[0]));
	}
	
	@SubscribeEvent
	public static void onBlockRegister(RegistryEvent.Register<Block> event) {
		
		event.getRegistry().registerAll(ModBlocks.BLOCKS.toArray(new Block[0]));
	}
	
	@SubscribeEvent
	@SideOnly(Side.CLIENT)
	public static void onModelRegister(ModelRegistryEvent event) {
		for(Item item : ModItems.ITEMS) {
			if(item instanceof IHasModel) {
				((IHasModel)item).registerModels();
			}
		}
		
		for(Block block : ModBlocks.BLOCKS) {
			if(block instanceof IHasModel) {
				((IHasModel)block).registerModels();
			}
		}
	}

 

I know why it does not work, it's because I'm not adding the planks to the Arrays BLOCKS and ITEMS but I can't seem to be able to add the planks... how can I explain this... Everytime I try to add them to the Arrays, both in the constructor in the BlockPlanksBase class or in BlockInit I can only add one instead of 4, meaning that I can't add the 4 different planks only the block "mod_planks" instead of "mod_planks_autumn", "mod_planks_spring", etc... and so later it asks for the model and textures for "mod_planks" instead of the others.

Thank you for the attention!

Edited by DiogoRod
Link to comment
Share on other sites

2 minutes ago, DiogoRod said:

Blocks. It's an ArrayList of Blocks

Show how you're adding them to the list.

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

12 minutes ago, Animefan8888 said:

Show how you're adding them to the list.

I'm doing it like this on the constructor of the BlockPlankBase class:

	public BlockPlankBase(String name) {
		super(Material.WOOD);
		this.setUnlocalizedName(name);
		this.setRegistryName(name);
		this.setSoundType(SoundType.WOOD);
		this.setHardness(1.8F);
		
		ModBlocks.BLOCKS.add(this);
		ModItems.ITEMS.add(new ItemTreeVariants(this).setRegistryName(this.getRegistryName()));
		this.setDefaultState(this.blockState.getBaseState().withProperty(VARIANT, BlockPlankBase.EnumType.AUTUMN));		
	}

 

Link to comment
Share on other sites

28 minutes ago, DiogoRod said:

meaning that I can't add the 4 different planks

Have you overriden Item#getSubItems?

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

5 minutes ago, Animefan8888 said:

Have you overriden Item#getSubItems?

yes, I did it like so:

	@Override
	public void getSubBlocks(CreativeTabs intemIn, NonNullList<ItemStack> items) {
		for (BlockPlankBase.EnumType blockplankbase$enumtype : BlockPlankBase.EnumType.values()) {
			items.add(new ItemStack(this, 1, blockplankbase$enumtype.getMeta()));
		}
	}

the whole class is up on my second post in this thread :)) 

 

 

Link to comment
Share on other sites

44 minutes ago, DiogoRod said:

if(block instanceof IHasModel) {

Don't use IHasModel. This is your problem. For one your planks block doesn't use it. So no models are applied. Second all Items have a model. So you can just do something like
for (Item item : ITEMS) {

   ModelLoader.setCustomModelLocation(item, 0, new ModelResourceLocation(item.getRegistryName(), "inventory"));

}

And in your case since you have meta items don't add them to the list and instead do them directly like.
for (Enum enum : EnumType.values()) {

   ModelLoader.setCustomModelLocation(Item.getItemFromBlock(ModBlocks.SOME_PLANK), enum.getMeta(), new ModelResourceLocation(ModBlocks.SOME_PLANK.getRegistryName().toString() + "_" + enum.getName(), "inventory"));

Something like that.
 

VANILLA MINECRAFT CLASSES ARE THE BEST RESOURCES WHEN MODDING

I will be posting 1.15.2 modding tutorials on this channel. If you want to be notified of it do the normal YouTube stuff like subscribing, ect.

Forge and vanilla BlockState generator.

Link to comment
Share on other sites

I found the problem and it is now working properly! Apparently I was not calling the function OreDictionary.registerOre with the proper arguments. It seems that when you have variants you need to use the constant WILDCARD_VALUE.

All is good now. Thank you so much to everyone who tried to help me!

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

    • Please help. It was working perfectly fine last night when I was on, and now it's crashing?   report: https://pastebin.com/DjF1UUb8
    • Gfgvtbhhj   Hddhdhdhhddhd
    • Mam ten błąd również w GeckoLib Seven Days of Fingees i Fingees Dinozar I also have this error in GeckoLib Seven Days of Fingees and Fingees Dinozar
    • Whether you are a fan of Hypixel Bedwars, SkyWars and PvP gamemodes like that, well you would enjoy this server! We have a very fun and unique style of PvP that a lot of our players really enjoy and we want to bring this server to more players like you! Yes you reading this post haha. Introducing, the Minezone Network, home of SUPER CRAFT BLOCKS. We've been working on this server for over 4 years now. Here is what we have to offer: SUPER CRAFT BLOCKS: This has 3 different gamemodes you can play, Classic, Duels and Frenzy. Each mode offers over 60 kits to choose from, along with a total of over 60 maps, allowing for various different playstyles on each map. There are also random powerups that spawn on the map which can include Health Pots, Bazookas, Nukes, Extra Lives and way way more! There is also double jump in this gamemode as well, which makes PvP a lot more fun & unique. You only need a minimum of 2 players to start any mode! Classic: Choose a kit, 5 lives for each player, fight it out and claim the #1 spot! Look out for lightning as they can spawn powerups to really give you an advantage in the game! Duels: Fight against another random player or one of your friends and see who is the best! Frenzy: Your kit is randomly selected for you, each life you will have a different kit. You can fight with up to 100 players in this mode and lets see who will be the best out of that 100! All the other stuff from Classic/Duels apply to this mode as well like powerups. We have 2 ranks on this server too, VIP and CAPTAIN which has a bunch of different perks for SCB and other things like Cosmetics and more.   SERVER IP: If this server has caught your interest in any way, please consider joining and you will NOT regret it! Bring some of your friends online for an even better experience and join in on the fun at: IP: minezone.club Hope to see you online!   SERVER TRAILER: https://www.youtube.com/watch?v=0phpMgu1mH0
  • Topics

  • Who's Online (See full list)

×
×
  • Create New...

Important Information

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