Extended Baseblock for BlockItems, auto register blockItems

This commit is contained in:
Michael 2020-09-09 16:32:40 +01:00
parent 9d8e711d35
commit b51722e181
No known key found for this signature in database
GPG key ID: 971C5B254742488F
5 changed files with 54 additions and 40 deletions

View file

@ -10,13 +10,13 @@ import de.ellpeck.actuallyadditions.common.util.StackUtil;
import de.ellpeck.actuallyadditions.common.util.StringUtil; import de.ellpeck.actuallyadditions.common.util.StringUtil;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.client.MainWindow; import net.minecraft.client.MainWindow;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.client.util.ITooltipFlag; import net.minecraft.client.util.ITooltipFlag;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.*; import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.item.Items;
import net.minecraft.state.StateContainer; import net.minecraft.state.StateContainer;
import net.minecraft.state.properties.BlockStateProperties; import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
@ -34,7 +34,6 @@ import net.minecraft.world.IBlockReader;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.api.distmarker.Dist; import net.minecraftforge.api.distmarker.Dist;
import net.minecraftforge.api.distmarker.OnlyIn; import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.common.ToolType;
import java.util.List; import java.util.List;
@ -44,10 +43,7 @@ public class BlockAtomicReconstructor extends BlockContainerBase implements IHud
public static final int NAME_FLAVOR_AMOUNTS_2 = 14; public static final int NAME_FLAVOR_AMOUNTS_2 = 14;
public BlockAtomicReconstructor() { public BlockAtomicReconstructor() {
super(Properties.create(Material.ROCK) super(STONE_PROPS.hardnessAndResistance(10f, 80f));
.hardnessAndResistance(10f, 80f)
.harvestTool(ToolType.PICKAXE)
.sound(SoundType.STONE));
} }
@Override @Override
@ -107,16 +103,6 @@ public class BlockAtomicReconstructor extends BlockContainerBase implements IHud
} }
} }
@Override
protected BlockItemBase getItemBlock() {
return new BlockItem();
}
@Override
public Rarity getRarity(){
return Rarity.EPIC;
}
@Override @Override
protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder){ protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder){
builder.add(BlockStateProperties.FACING); builder.add(BlockStateProperties.FACING);

View file

@ -6,11 +6,8 @@ import de.ellpeck.actuallyadditions.common.tile.TileEntityBatteryBox;
import de.ellpeck.actuallyadditions.common.util.StackUtil; import de.ellpeck.actuallyadditions.common.util.StackUtil;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.BlockState; import net.minecraft.block.BlockState;
import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material;
import net.minecraft.entity.player.PlayerEntity; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.item.Rarity;
import net.minecraft.tileentity.TileEntity; import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.ActionResultType; import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand; import net.minecraft.util.Hand;
@ -20,26 +17,15 @@ import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape; import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.world.IBlockReader; import net.minecraft.world.IBlockReader;
import net.minecraft.world.World; import net.minecraft.world.World;
import net.minecraftforge.common.ToolType;
// todo add rarity RARE
public class BlockBatteryBox extends BlockContainerBase { public class BlockBatteryBox extends BlockContainerBase {
public static final VoxelShape SHAPE = Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D); public static final VoxelShape SHAPE = Block.makeCuboidShape(0.0D, 0.0D, 0.0D, 16.0D, 8.0D, 16.0D);
public BlockBatteryBox() { public BlockBatteryBox() {
super(Properties.create(Material.ROCK) super(STONE_PROPS_WITH_HARDNESS.harvestLevel(0));
.hardnessAndResistance(1.5f, 10.0f)
.harvestLevel(0)
.harvestTool(ToolType.PICKAXE)
.sound(SoundType.STONE));
} }
@Override
public Rarity getRarity(){
return Rarity.RARE;
}
@Override @Override
public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context){ public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context){
return SHAPE; return SHAPE;

View file

@ -1,16 +1,21 @@
package de.ellpeck.actuallyadditions.common.blocks; package de.ellpeck.actuallyadditions.common.blocks;
import de.ellpeck.actuallyadditions.common.ActuallyAdditions; import de.ellpeck.actuallyadditions.common.ActuallyAdditions;
import de.ellpeck.actuallyadditions.common.blocks.base.ActuallyBlockBase;
import de.ellpeck.actuallyadditions.common.blocks.base.BlockPlant; import de.ellpeck.actuallyadditions.common.blocks.base.BlockPlant;
import de.ellpeck.actuallyadditions.common.blocks.metalists.TheMiscBlocks; import de.ellpeck.actuallyadditions.common.blocks.metalists.TheMiscBlocks;
import de.ellpeck.actuallyadditions.common.items.InitItems;
import de.ellpeck.actuallyadditions.common.items.metalists.TheCrystals; import de.ellpeck.actuallyadditions.common.items.metalists.TheCrystals;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.StairsBlock; import net.minecraft.block.StairsBlock;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.item.BlockItem;
import net.minecraftforge.fml.RegistryObject; import net.minecraftforge.fml.RegistryObject;
import net.minecraftforge.registries.DeferredRegister; import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries; import net.minecraftforge.registries.ForgeRegistries;
import java.util.Objects;
public final class InitBlocks { public final class InitBlocks {
public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, ActuallyAdditions.MODID); public static final DeferredRegister<Block> BLOCKS = DeferredRegister.create(ForgeRegistries.BLOCKS, ActuallyAdditions.MODID);
@ -300,7 +305,21 @@ public final class InitBlocks {
public static final RegistryObject<Block> blockPillarQuartzSlab public static final RegistryObject<Block> blockPillarQuartzSlab
= BLOCKS.register("block_pillar_quartz_slab", () -> new BlockSlabs(blockMisc.get().getDefaultState().with(BlockMisc.TYPE, TheMiscBlocks.QUARTZ_PILLAR))); = BLOCKS.register("block_pillar_quartz_slab", () -> new BlockSlabs(blockMisc.get().getDefaultState().with(BlockMisc.TYPE, TheMiscBlocks.QUARTZ_PILLAR)));
public static void init() { static {
ActuallyAdditions.LOGGER.info("Initializing Blocks..."); for (RegistryObject<Block> entry : BLOCKS.getEntries()) {
if (!(entry.get() instanceof ActuallyBlockBase)) {
continue;
}
ActuallyBlockBase block = (ActuallyBlockBase) entry.get();
if (block.hasCustomBlockItem()) {
continue;
}
InitItems.ITEMS.register(
Objects.requireNonNull(block.getRegistryName()).getPath(),
() -> new BlockItem(block, block.getBlockItemProps())
);
}
} }
} }

View file

@ -1,21 +1,40 @@
package de.ellpeck.actuallyadditions.common.blocks.base; package de.ellpeck.actuallyadditions.common.blocks.base;
import de.ellpeck.actuallyadditions.common.ActuallyAdditions;
import net.minecraft.block.Block; import net.minecraft.block.Block;
import net.minecraft.block.SoundType; import net.minecraft.block.SoundType;
import net.minecraft.block.material.Material; import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraftforge.common.ToolType; import net.minecraftforge.common.ToolType;
public abstract class ActuallyBlockBase extends Block { public abstract class ActuallyBlockBase extends Block {
private static final Properties STONE_PROPS = Block.Properties.create(Material.ROCK) protected static final Properties STONE_PROPS = Block.Properties.create(Material.ROCK)
.harvestTool(ToolType.PICKAXE) .harvestTool(ToolType.PICKAXE)
.sound(SoundType.STONE); .sound(SoundType.STONE);
private static final Properties STONE_PROPS_WITH_TICK = STONE_PROPS.tickRandomly(); protected static final Properties STONE_PROPS_WITH_TICK = STONE_PROPS.tickRandomly();
private static final Properties STONE_PROPS_WITH_HARDNESS = STONE_PROPS.hardnessAndResistance(1.5f, 10.0f); protected static final Properties STONE_PROPS_WITH_HARDNESS = STONE_PROPS.hardnessAndResistance(1.5f, 10.0f);
public ActuallyBlockBase(Properties properties) { public ActuallyBlockBase(Properties properties) {
super(properties); super(properties);
} }
/**
* Define custom BlockItem Properties for a specific block. This is useful for
* when you need to modify the stack size. I'd recommend using super.getBlockItemProps()
* and adding to this response.
*
* @return default item properties for our blockItems
*/
public Item.Properties getBlockItemProps() {
return new Item.Properties().group(ActuallyAdditions.aaGroup);
}
/**
* If the block has a custom BlockItem to define custom behavior then
* override this method and return true to stop auto registration
*/
public boolean hasCustomBlockItem() {
return false;
}
} }

View file

@ -25,9 +25,13 @@ import net.minecraft.item.Item;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.potion.PotionEffect; import net.minecraft.potion.PotionEffect;
import net.minecraft.tileentity.TileEntityChest; import net.minecraft.tileentity.TileEntityChest;
import net.minecraftforge.registries.DeferredRegister;
import net.minecraftforge.registries.ForgeRegistries;
public final class InitItems { public final class InitItems {
public static final DeferredRegister<Item> ITEMS = DeferredRegister.create(ForgeRegistries.ITEMS, ActuallyAdditions.MODID);
public static Item itemBooklet; public static Item itemBooklet;
public static Item itemFertilizer; public static Item itemFertilizer;
public static Item itemMisc; public static Item itemMisc;