Skip to content

Instantly share code, notes, and snippets.

@Toma400
Last active April 21, 2022 09:47
Show Gist options
  • Save Toma400/8f9336027caec6b2fd46b7b7b3032617 to your computer and use it in GitHub Desktop.
Save Toma400/8f9336027caec6b2fd46b7b7b3032617 to your computer and use it in GitHub Desktop.
Custom block class for fuel blocks
package toma400.cobr.core;
import net.minecraft.world.item.BlockItem;
import net.minecraft.world.item.CreativeModeTab;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.crafting.RecipeType;
import net.minecraft.world.level.block.Block;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.material.Material;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
import net.minecraftforge.registries.RegistryObject;
import toma400.cobr.Cobr;
import javax.annotation.Nullable;
import java.util.function.Supplier;
public class CobrFuelBlocks {
public static final DeferredRegister<Block> BLOCKS =
DeferredRegister.create(ForgeRegistries.BLOCKS, Cobr.MOD_ID);
public static final RegistryObject<Block> DUNE_COAL_BLOCK = registerBlock("dune_coal_block",
() -> new Block(BlockBehaviour.Properties.of(Material.WOOD).strength(6f, 8f)), CobrTab.COBR_TAB, 18000); //that last part with 18000 is fuel time of the block item, you can specify it for each block separately
private static <T extends Block>RegistryObject<T> registerBlock(String name, Supplier<T> block, CreativeModeTab tab, int FuelTime) {
RegistryObject<T> toReturn = BLOCKS.register(name, block);
registerBlockItem(name, toReturn, tab, FuelTime);
return toReturn;
}
private static <T extends Block>RegistryObject<Item> registerBlockItem(String name, RegistryObject<T> block, CreativeModeTab tab, int FuelTime) {
return CobrItems.ITEMS.register (name, () -> new BlockItem(block.get(),
new Item.Properties().tab(tab)) {
@Override
public int getBurnTime(ItemStack itemStack, @Nullable RecipeType<?> recipeType) {
return FuelTime;
}
});
}
public static void register(IEventBus eventBus) {
BLOCKS.register(eventBus);
}
}
@Toma400
Copy link
Author

Toma400 commented Feb 10, 2022

Workaround for adding fuel blocks without actual need to make too much code for them. It simply adds another file/class, so it should not be inside YourModItems nor YourModBlocks. It's simply third one, so you can call it YourModFuelBlocks.
Code used is mixed tutorial on blocks and tutorial on fuels, both by Kaupenjoe. This workaround is continuation of what I came up with for items being managed by only one class instead of separate one for each (gist here).

Remember that you have to add CobrFuelBlocks.register(eventBus); part to your main class, just like you did with Items and Blocks (FuelBlocks are just another registry class)!

@Toma400
Copy link
Author

Toma400 commented Apr 21, 2022

You can also include this in your main class for whole registering system, but then remember that you need to put "0" for all blocks that are not fuel-related. It really depends on how you want to wrote it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment