-
Posts
107 -
Joined
-
Last visited
Posts posted by Maxi07
-
-
What part of my code is wrong? The block are initialized in the registry event, and then I try get the block outside the registry event with .get()
-
9 hours ago, Draco18s said:
do not store the result of get(), just call get() when you need the block/item/etc.
Now it is crashing again, but a bit differently:
RegisterBlocks.java:
package maxi.ores_cores.init; @EventBusSubscriber(modid = OresCores.MODID, bus = Bus.MOD) public class RegisterBlocks { @SubscribeEvent public static void registerBlock(Register<Block> event) { event.getRegistry().registerAll( new SimpleBlock(OresCores.MODID, "emoulrite_block", Properties.create(Material.IRON, MaterialColor.GOLD).sound(SoundType.METAL).hardnessAndResistance(5f, 6f).harvestTool(ToolType.PICKAXE).harvestLevel(2)), new SimpleOreBlock(OresCores.MODID, "emoulrite_ore", Properties.create(Material.ROCK, MaterialColor.STONE).sound(SoundType.STONE).hardnessAndResistance(3f, 3f).harvestTool(ToolType.PICKAXE).harvestLevel(2)), new SimpleBlock(OresCores.MODID, "telerite_block", Properties.create(Material.IRON, MaterialColor.PURPLE).sound(SoundType.METAL).hardnessAndResistance(5f, 6f).harvestTool(ToolType.PICKAXE).harvestLevel(2)), new SimpleOreBlock(OresCores.MODID, "telerite_ore", Properties.create(Material.ROCK, MaterialColor.SAND).sound(SoundType.STONE).hardnessAndResistance(4f, 9f).harvestTool(ToolType.PICKAXE).harvestLevel(2)) ); } @SubscribeEvent public static void registerItem(Register<Item> event) { event.getRegistry().registerAll( new SimpleBlockItem(Blocks.EMOULRITE_BLOCK.get(), ItemGroup.BUILDING_BLOCKS), new SimpleBlockItem(Blocks.EMOULRITE_ORE.get(), ItemGroup.BUILDING_BLOCKS), new SimpleBlockItem(Blocks.TELERITE_BLOCK.get(), ItemGroup.BUILDING_BLOCKS), new SimpleBlockItem(Blocks.TELERITE_ORE.get(), ItemGroup.BUILDING_BLOCKS) ); } }
Blocks.java:
package maxi.ores_cores.blocks; public class Blocks { public static final RegistryObject<Block> EMOULRITE_BLOCK = RegistryObject.of(new ResourceLocation(OresCores.MODID, "emoulrite_block"), ForgeRegistries.BLOCKS); public static final RegistryObject<Block> EMOULRITE_ORE = RegistryObject.of(new ResourceLocation(OresCores.MODID, "emoulrite_ore"), ForgeRegistries.BLOCKS); public static final RegistryObject<Block> TELERITE_BLOCK = RegistryObject.of(new ResourceLocation(OresCores.MODID, "telerite_block"), ForgeRegistries.BLOCKS); public static final RegistryObject<Block> TELERITE_ORE = RegistryObject.of(new ResourceLocation(OresCores.MODID, "telerite_ore"), ForgeRegistries.BLOCKS); }
-
9 hours ago, Draco18s said:
In general do not store the result of get(), just call get() when you need the block/item/etc.
Thanks!
-
After I remade my registration system again (for causes see my comments at [1.16.1] The Book of Minecraft Modding - complete guide for beginners),
the game now crashes with the main problem: java.lang.NullPointerException: Registry Object not present: ores_cores:emoulrite_block
debug.log:
I cut out the imports in the source code
RegisterBlocks.java:
package maxi.ores_cores.init; @EventBusSubscriber(modid = OresCores.MODID, bus = Bus.MOD) public class RegisterBlocks { @SubscribeEvent public static void registerBlock(Register<Block> event) { event.getRegistry().registerAll( new SimpleBlock(OresCores.MODID, "emoulrite_block", Properties.create(Material.IRON, MaterialColor.GOLD).sound(SoundType.METAL).hardnessAndResistance(5f, 6f).harvestTool(ToolType.PICKAXE).harvestLevel(2)), new SimpleOreBlock(OresCores.MODID, "emoulrite_ore", Properties.create(Material.ROCK, MaterialColor.STONE).sound(SoundType.STONE).hardnessAndResistance(3f, 3f).harvestTool(ToolType.PICKAXE).harvestLevel(2)), new SimpleBlock(OresCores.MODID, "telerite_block", Properties.create(Material.IRON, MaterialColor.PURPLE).sound(SoundType.METAL).hardnessAndResistance(5f, 6f).harvestTool(ToolType.PICKAXE).harvestLevel(2)), new SimpleOreBlock(OresCores.MODID, "telerite_ore", Properties.create(Material.ROCK, MaterialColor.SAND).sound(SoundType.STONE).hardnessAndResistance(4f, 9f).harvestTool(ToolType.PICKAXE).harvestLevel(2)) ); } @SubscribeEvent public static void registerItem(Register<Item> event) { event.getRegistry().registerAll( new SimpleBlockItem(Blocks.EMOULRITE_BLOCK, ItemGroup.BUILDING_BLOCKS), new SimpleBlockItem(Blocks.EMOULRITE_ORE, ItemGroup.BUILDING_BLOCKS), new SimpleBlockItem(Blocks.TELERITE_BLOCK, ItemGroup.BUILDING_BLOCKS), new SimpleBlockItem(Blocks.TELERITE_ORE, ItemGroup.BUILDING_BLOCKS) ); } }
SimpleBlock.java (SimpleOreBlock is similar):
package maxi.ores_cores.blocks; public class SimpleBlock extends Block { public SimpleBlock(Properties properties) { super(properties); } public SimpleBlock(String modid, String name, Properties properties) { super(properties); this.setRegistryName(modid, name); } }
Blocks.java:
package maxi.ores_cores.blocks; public class Blocks { public static final Block EMOULRITE_BLOCK = RegistryObject.of(new ResourceLocation(OresCores.MODID, "emoulrite_block"), ForgeRegistries.BLOCKS).get(); public static final Block EMOULRITE_ORE = RegistryObject.of(new ResourceLocation(OresCores.MODID, "emoulrite_ore"), ForgeRegistries.BLOCKS).get(); public static final Block TELERITE_BLOCK = RegistryObject.of(new ResourceLocation(OresCores.MODID, "telerite_block"), ForgeRegistries.BLOCKS).get(); public static final Block TELERITE_ORE = RegistryObject.of(new ResourceLocation(OresCores.MODID, "telerite_ore"), ForgeRegistries.BLOCKS).get(); }
SimpleBlockItem.java:
package maxi.ores_cores.items; public class SimpleBlockItem extends BlockItem { public SimpleBlockItem(Block block, ItemGroup group) { super(block, new Properties().group(group)); this.setRegistryName(block.getRegistryName()); } }
My guess is that the static fields at Blocks.java are initialized before the block registry event is fired, therefore the blocks that I try to get doesn´t exist.
-
3 hours ago, eqeqeqeqeqeqeeqqeq said:
I Have The Optiforge mod
Why are you using OptiForge? OptiFine itself is compatible with forge.
-
Now it finally works! Every tutorial I've seen used static initializers. What are the problems, why I should avoid them (I know you said something about timing above)? And by the way, is my code now finally not bad?
@EventBusSubscriber(modid = OresCores.MODID, bus = Bus.MOD) public class RegisterBlocks { @SubscribeEvent public static void registerBlock(Register<Block> event) { final Block EXAMPLE_BLOCK = new Block(Properties.create(Material.ROCK, MaterialColor.STONE).sound(SoundType.STONE).hardnessAndResistance(3, 3).harvestTool(ToolType.PICKAXE).harvestLevel(2)); final IForgeRegistry<Block> registry = event.getRegistry(); registry.register(EXAMPLE_BLOCK.setRegistryName(OresCores.MODID, "example_block")); } @SubscribeEvent public static void registerItem(Register<Item> event) { final IForgeRegistry<Item> registry = event.getRegistry(); registry.register(new BasicBlockItem(Blocks.EXAMPLE_BLOCK, ItemGroup.BUILDING_BLOCKS)); } }
Blocks.java:
@ObjectHolder(value = OresCores.MODID) public class Blocks { public static final Block EXAMPLE_BLOCK = null; }
-
Of course, that makes a lot of sense! Excuse me for this silly question, but how I obtain my instance outside the registration?
-
Okay, then what I have to change? I want to have one instance of my block. Or is this stupid?
Sorry for being a bit annoying
-
So will this already work?
package maxi.ores_cores.init; import maxi.ores_cores.OresCores; import maxi.ores_cores.items.BasicBlockItem; import net.minecraft.block.Block; import net.minecraft.block.SoundType; import net.minecraft.block.Block.Properties; import net.minecraft.block.material.Material; import net.minecraft.block.material.MaterialColor; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraftforge.common.ToolType; import net.minecraftforge.event.RegistryEvent.Register; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; import net.minecraftforge.registries.IForgeRegistry; import net.minecraftforge.registries.ObjectHolder; @EventBusSubscriber(modid = OresCores.MODID, bus = Bus.MOD) @ObjectHolder(value = OresCores.MODID) public class RegisterBlocks { public static final Block EXAMPLE_BLOCK = new Block(Properties.create(Material.ROCK, MaterialColor.STONE).sound(SoundType.STONE).hardnessAndResistance(3, 3).harvestTool(ToolType.PICKAXE).harvestLevel(2)); @SubscribeEvent public static void registerBlock(Register<Block> event) { final IForgeRegistry<Block> registry = event.getRegistry(); registry.register(EXAMPLE_BLOCK.setRegistryName(OresCores.MODID, "emoulrite_block")); } @SubscribeEvent public static void registerItem(Register<Item> event) { final IForgeRegistry<Item> registry = event.getRegistry(); registry.register(new BasicBlockItem(EXAMPLE_BLOCK, ItemGroup.BUILDING_BLOCKS)); } }
-
On 7/4/2020 at 4:11 AM, ChampionAsh5357 said:
Please use object holder to "hold" the block and item instances when registering or use deferred register if you're going to statically initialize anything.
Okay, now I am finally continue with modding. I am changing my whole registration system and how I add blocks and items. I dont want the static intializer problem, but I dont want to use DefferedRegisries either (please dont ask why). So I looked at the minecraft classes net.minecraft.block.Blocks and net.minecraft.item.Items where the minecraft blocks and items are registered. I want to do this similar. I should use ObjectHolders. I searched how they work but I couldn't find something that could help me. And now I am asking here. And please say if you have an idea how i could make my registration system without DefferedRegisries and problems.
Thanks!
-
You need to change to change the forge version and the mappings in your build.gradle (look at the build.gradle of the 1.16 MDK). And you also need to change a few values in your mod.toml to make FML able to load your mod.
-
2 hours ago, Maxi07 said:
public static final Block EMOULRITE_BLOCK = new Block(Properties .create(Material.IRON, MaterialColor.GOLD).sound(SoundType.METAL) .hardnessAndResistance(5, 6) .harvestTool(ToolType.PICKAXE).harvestLevel(2));
Oh okay, you mean this, right?
EDIT: Thanks for helping me improving my code @diesieben07
-
3 minutes ago, diesieben07 said:
Registration must occur during a very specific time (when the registry event is fired). By using a static initializer you are completely bypassing this and doing it when your class is initialized, which is not easy to know when it is.
So I should make the methods that register the blocks and items non static?
-
6 hours ago, ChampionAsh5357 said:
Please use object holder to "hold" the block and item instances when registering or use deferred register if you're going to statically initialize anything. What you have written for custom blocks is just wrong.
I register my items and blocks similar:
RegisterBlocks.java
package maxi.ores_cores.init; import maxi.ores_cores.OresCores; import maxi.ores_cores.blocks.*; import maxi.ores_cores.items.BasicBlockItem; import net.minecraft.block.Block; import net.minecraft.item.Item; import net.minecraft.item.ItemGroup; import net.minecraftforge.event.RegistryEvent.Register; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; import net.minecraftforge.registries.IForgeRegistry; @EventBusSubscriber(modid = OresCores.MODID, bus = Bus.MOD) public class RegisterBlocks { @SubscribeEvent public static void registerBlock(Register<Block> event) { final IForgeRegistry<Block> registry = event.getRegistry(); registry.register(MetalBlock.EMOULRITE_BLOCK.setRegistryName(OresCores.MODID, "emoulrite_block")); registry.register(Ore.EMOULRITE_ORE.setRegistryName(OresCores.MODID, "emoulrite_ore")); registry.register(MetalBlock.TELERITE_BLOCK.setRegistryName(OresCores.MODID, "telerite_block")); registry.register(Ore.TELERITE_ORE.setRegistryName(OresCores.MODID, "telerite_ore")); } @SubscribeEvent public static void registerItem(Register<Item> event) { final IForgeRegistry<Item> registry = event.getRegistry(); registry.register(new BasicBlockItem(MetalBlock.EMOULRITE_BLOCK, ItemGroup.BUILDING_BLOCKS)); registry.register(new BasicBlockItem(Ore.EMOULRITE_ORE, ItemGroup.BUILDING_BLOCKS)); registry.register(new BasicBlockItem(MetalBlock.TELERITE_BLOCK, ItemGroup.BUILDING_BLOCKS)); registry.register(new BasicBlockItem(Ore.TELERITE_ORE, ItemGroup.BUILDING_BLOCKS)); } }
MetalBlock.java (Ore.java is similar)
package maxi.ores_cores.blocks; import net.minecraft.block.SoundType; import net.minecraft.block.Block; import net.minecraft.block.Block.Properties; import net.minecraft.block.material.Material; import net.minecraft.block.material.MaterialColor; import net.minecraftforge.common.ToolType; public class MetalBlock { public static final Block EMOULRITE_BLOCK = new Block(Properties .create(Material.IRON, MaterialColor.GOLD).sound(SoundType.METAL) .hardnessAndResistance(5, 6) .harvestTool(ToolType.PICKAXE).harvestLevel(2)); public static final Block TELERITE_BLOCK = new Block(Properties .create(Material.IRON, MaterialColor.PURPLE).sound(SoundType.METAL) .hardnessAndResistance(5, 6) .harvestTool(ToolType.PICKAXE).harvestLevel(2)); }
BasicBlockItem.java
package maxi.ores_cores.items; import net.minecraft.block.Block; import net.minecraft.item.BlockItem; import net.minecraft.item.ItemGroup; public class BasicBlockItem extends BlockItem { public BasicBlockItem(Block block, ItemGroup group) { super(block, new Properties().group(group)); setRegistryName(block.getRegistryName()); } }
Whats bad about this? I am new to modding and I appreciate every suggestion for improvement that I get
-
On 6/27/2020 at 12:10 AM, Curle said:
MCP is outdated and isn't used any more. We have a newer system in place.
See the Discord/IRC.
But now, the newest mappings are from mcpbot, right? And when mcpbot has 1.16 mappings I should prefer them, if I want newer mappings, right?
-
Please post logs or describe more exactly what happened
-
Your IDE is marking an error in picture 2
Remove this method and the try again
-
Where did you register your blockitem?
-
Sorry, but Forge 1.8 is not supported on this forum.
We can only help you with the new and supported versions.
-
Is this right?:
- The 1.16 mapping are custom / adjusted 1.15.1 mappings from mcpbot.
- If mcpbot has 1.16 mapping I should prefer them, if I want newer mappings.
-
I recently made a mod which adds a few new blocks. Now I want to use this blocks in others mods. How I can use the blocks in the code of other mods?
-
Are the 1.16 mappings are custom / adjusted 1.15.1 mappings from http://mcpbot.bspk.rs? And when 1.16 mappings from mcpbot are available I should prefer this, if want newer mappings, right?
-
Is this right? My IDE marks all as warnings because DeferredWorkQueue is deprecated. Should I just use @SuppressWarnings("deprecation")?
package maxi.ores_cores.init; import maxi.ores_cores.OresCores; import maxi.ores_cores.world.gen.*; import net.minecraftforge.eventbus.api.SubscribeEvent; import net.minecraftforge.fml.DeferredWorkQueue; import net.minecraftforge.fml.common.Mod.EventBusSubscriber; import net.minecraftforge.fml.common.Mod.EventBusSubscriber.Bus; import net.minecraftforge.fml.event.lifecycle.FMLCommonSetupEvent; @EventBusSubscriber(modid = OresCores.MODID, bus = Bus.MOD) public class RegisterWorldGen { @SubscribeEvent public static void registerWorldGen(FMLCommonSetupEvent event) { DeferredWorkQueue.runLater(new Runnable() { @Override public void run() { EmoulriteOreGen.generateOre(); TeleriteOreGen.generateOre(); } }); } }
-
I think your main class NewDawn has to annotated with @EventBusSubscriber(modid = [your_modid], bus = Bus.MOD), but leave "@Mod"
Then the methods with @SubribeEvent (like your ore gen) will be executed
- 1
- 1
[Solved] [1.16.1] Block Registration
in Modder Support
Posted · Edited by Maxi07
.get() or
?