Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

I've been working on a 1.13.2 mod that's still in its infancy. When adding blocks, I wanted to be able to add just one line of code for the Block itself and the ItemBlock. I wrote some code to do this, and it seems to work well. However, I want to be sure I'm not causing issues with compatibility or going against a style rule. Additionally, I'm not positive if I should be declaring my blocks and registering them in the same file or not. Here is my code:

 

package henryrichard.ewot.init;

import henryrichard.ewot.util.EwotReference;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.material.MaterialColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ObjectHolder;

@Mod.EventBusSubscriber(modid = EwotReference.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
@ObjectHolder(EwotReference.MODID)
public abstract class EwotBlocks {

    public static final Block amethyst_ore = null;
    public static final Block endust_ore = null;
    public static final Block aluminum_ore = null;
    public static final Block flint_ore = null;
    public static final Block amethyst_block = null;
    public static final Block endust_block = null;
    public static final Block aluminum_block = null;
    public static final Block unalloyed_endite_block = null;
    public static final Block endite_block = null;
    public static final Block nether_planks = null;
    public static final Block glowwool = null;
    public static final Block jeb_wool = null;

    private static final BlockCreator[] bcrArray = {
            new BlockCreator("amethyst_ore", Block.Properties.create(Material.ROCK, MaterialColor.NETHERRACK).hardnessAndResistance(3.0F, 3.0F)),
            new BlockCreator("endust_ore", Block.Properties.create(Material.ROCK, MaterialColor.SAND).hardnessAndResistance(5.0F, 9.0F)),
            new BlockCreator("aluminum_ore", Block.Properties.create(Material.ROCK).hardnessAndResistance(3.0F, 3.0F)),
            new BlockCreator("flint_ore", Block.Properties.create(Material.ROCK).hardnessAndResistance(3.0F, 3.0F)),
            new BlockCreator("amethyst_block", Block.Properties.create(Material.IRON, MaterialColor.PURPLE).hardnessAndResistance(5.0F, 6.0F).sound(SoundType.METAL)),
            new BlockCreator("endust_block", Block.Properties.create(Material.IRON, MaterialColor.CYAN).hardnessAndResistance(5.0F, 6.0F).sound(SoundType.METAL)),
            new BlockCreator("aluminum_block", Block.Properties.create(Material.IRON, MaterialColor.IRON).hardnessAndResistance(5.0F, 6.0F).sound(SoundType.METAL)),
            new BlockCreator("unalloyed_endite_block", Block.Properties.create(Material.IRON, MaterialColor.LIGHT_BLUE_TERRACOTTA).hardnessAndResistance(6.0F, 10.0F).sound(SoundType.METAL)),
            new BlockCreator("endite_block", Block.Properties.create(Material.IRON, MaterialColor.CYAN).hardnessAndResistance(6.0F, 10.0F).sound(SoundType.METAL)),
            new BlockCreator("nether_planks", Block.Properties.create(Material.WOOD, MaterialColor.RED_TERRACOTTA).hardnessAndResistance(2.0F, 3.0F).sound(SoundType.WOOD)),
            new BlockCreator("glowwool", Block.Properties.create(Material.CLOTH, MaterialColor.YELLOW).hardnessAndResistance(0.8F).sound(SoundType.CLOTH).lightValue(15)),
            new BlockCreator("jeb_wool", Block.Properties.create(Material.CLOTH, MaterialColor.YELLOW).hardnessAndResistance(0.8F).sound(SoundType.CLOTH)),
    };

    @SubscribeEvent
    public static void registerBlocks(RegistryEvent.Register<Block> event) {

        Block[] blocks = new Block[bcrArray.length];
        for(int i=0; i<bcrArray.length; i++) { blocks[i] = bcrArray[i].theBlock; }

        event.getRegistry().registerAll(blocks);
    }

    @SubscribeEvent
    public static void registerItemBlocks(RegistryEvent.Register<Item> event) {
        Item[] items = new Item[bcrArray.length];
        for(int i=0; i<bcrArray.length; i++) { items[i] = bcrArray[i].theItem; }

        event.getRegistry().registerAll(items);
    }

    //Nested class to create blocks more easily
    private static class BlockCreator {

        private Block.Properties blockProp;
        private Item.Properties itemProp;

        private Block theBlock;
        private Item theItem;

        private BlockCreator(String r, Block.Properties b, Item.Properties i) {
            blockProp = b;
            itemProp = i;

            theBlock = new Block(blockProp).setRegistryName(r);
            theItem = new ItemBlock(theBlock, itemProp).setRegistryName(r);
        }

        private BlockCreator(String r, Block.Properties b) {
            this(r, b, new Item.Properties().group(EwotItemGroups.EWOT_BLOCKS));
        }
    }

}

 

I'll put something here when I have something of value I need to put at the end of every post. For now it's this mostly pointless text.

  • Author

Is this better?

 

package henryrichard.ewot.init;

import henryrichard.ewot.util.EwotReference;
import net.minecraft.block.Block;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.block.material.MaterialColor;
import net.minecraft.item.Item;
import net.minecraft.item.ItemBlock;
import net.minecraftforge.event.RegistryEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
import net.minecraftforge.registries.ObjectHolder;

@Mod.EventBusSubscriber(modid = EwotReference.MODID, bus = Mod.EventBusSubscriber.Bus.MOD)
@ObjectHolder(EwotReference.MODID)
public abstract class EwotBlocks {

    public static final Block amethyst_ore = null;
    public static final Block endust_ore = null;
    public static final Block aluminum_ore = null;
    public static final Block flint_ore = null;
    public static final Block amethyst_block = null;
    public static final Block endust_block = null;
    public static final Block aluminum_block = null;
    public static final Block unalloyed_endite_block = null;
    public static final Block endite_block = null;
    public static final Block nether_planks = null;
    public static final Block glowwool = null;
    public static final Block jeb_wool = null;

    private static final BlockCreator[] bcrArray = {
            new BlockCreator("amethyst_ore", Block.Properties.create(Material.ROCK, MaterialColor.NETHERRACK).hardnessAndResistance(3.0F, 3.0F)),
            new BlockCreator("endust_ore", Block.Properties.create(Material.ROCK, MaterialColor.SAND).hardnessAndResistance(5.0F, 9.0F)),
            new BlockCreator("aluminum_ore", Block.Properties.create(Material.ROCK).hardnessAndResistance(3.0F, 3.0F)),
            new BlockCreator("flint_ore", Block.Properties.create(Material.ROCK).hardnessAndResistance(3.0F, 3.0F)),
            new BlockCreator("amethyst_block", Block.Properties.create(Material.IRON, MaterialColor.PURPLE).hardnessAndResistance(5.0F, 6.0F).sound(SoundType.METAL)),
            new BlockCreator("endust_block", Block.Properties.create(Material.IRON, MaterialColor.CYAN).hardnessAndResistance(5.0F, 6.0F).sound(SoundType.METAL)),
            new BlockCreator("aluminum_block", Block.Properties.create(Material.IRON, MaterialColor.IRON).hardnessAndResistance(5.0F, 6.0F).sound(SoundType.METAL)),
            new BlockCreator("unalloyed_endite_block", Block.Properties.create(Material.IRON, MaterialColor.LIGHT_BLUE_TERRACOTTA).hardnessAndResistance(6.0F, 10.0F).sound(SoundType.METAL)),
            new BlockCreator("endite_block", Block.Properties.create(Material.IRON, MaterialColor.CYAN).hardnessAndResistance(6.0F, 10.0F).sound(SoundType.METAL)),
            new BlockCreator("nether_planks", Block.Properties.create(Material.WOOD, MaterialColor.RED_TERRACOTTA).hardnessAndResistance(2.0F, 3.0F).sound(SoundType.WOOD)),
            new BlockCreator("glowwool", Block.Properties.create(Material.CLOTH, MaterialColor.YELLOW).hardnessAndResistance(0.8F).sound(SoundType.CLOTH).lightValue(15)),
            new BlockCreator("jeb_wool", Block.Properties.create(Material.CLOTH, MaterialColor.SNOW).hardnessAndResistance(0.8F).sound(SoundType.CLOTH)),
    };

    @SubscribeEvent
    public static void registerBlocks(RegistryEvent.Register<Block> event) {

        Block[] blocks = new Block[bcrArray.length];
        for(int i=0; i<bcrArray.length; i++) {
            blocks[i] = bcrArray[i].getBlock();
        }

        event.getRegistry().registerAll(blocks);
    }

    @SubscribeEvent
    public static void registerItemBlocks(RegistryEvent.Register<Item> event) {
        Item[] items = new Item[bcrArray.length];
        for(int i=0; i<bcrArray.length; i++) {
            items[i] = bcrArray[i].getItem();
        }

        event.getRegistry().registerAll(items);
    }

    //Nested class to create blocks more easily
    private static class BlockCreator {

        private Block.Properties blockProp;
        private Item.Properties itemProp;
        private String registryName;

        private Block theBlock;
        private Item theItem;

        private BlockCreator(String r, Block.Properties b, Item.Properties i) {
            blockProp = b;
            itemProp = i;
            registryName = r;
        }

        private BlockCreator(String r, Block.Properties b) {
            this(r, b, new Item.Properties().group(EwotItemGroups.EWOT_BLOCKS));
        }

        private Block getBlock() {
            if(theBlock == null) {
                theBlock = new Block(blockProp).setRegistryName(registryName);
            }
            return theBlock;
        }

        private Item getItem() {
            if(theItem == null) {
                theItem = new ItemBlock(getBlock(), itemProp).setRegistryName(registryName);
            }
            return theItem;
        }
    }

}

 

I'll put something here when I have something of value I need to put at the end of every post. For now it's this mostly pointless text.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.