跳到主要内容

21两个高块作物

玉米块代码

public class CornCropBlock extends CropBlock {
public static final int FIRST_STAGE_MAX_AGE = 7;
public static final int SECOND_STAGE_MAX_AGE = 1;
private static final VoxelShape[] AGE_TO_SHAPE = new VoxelShape[]{
Block.createCuboidShape(0.0, 0.0, 0.0, 16.0, 2.0, 16.0),
Block.createCuboidShape(0.0, 0.0, 0.0, 16.0, 4.0, 16.0),
Block.createCuboidShape(0.0, 0.0, 0.0, 16.0, 6.0, 16.0),
Block.createCuboidShape(0.0, 0.0, 0.0, 16.0, 8.0, 16.0),
Block.createCuboidShape(0.0, 0.0, 0.0, 16.0, 10.0, 16.0),
Block.createCuboidShape(0.0, 0.0, 0.0, 16.0, 12.0, 16.0),
Block.createCuboidShape(0.0, 0.0, 0.0, 16.0, 14.0, 16.0),
Block.createCuboidShape(0.0, 0.0, 0.0, 16.0, 16.0, 16.0),
Block.createCuboidShape(0.0, 0.0, 0.0, 16.0, 16.0, 16.0)};

public static final IntProperty AGE = IntProperty.of("age", 0, 8);

public CornCropBlock(Settings settings) {
super(settings);
}

@Override
public VoxelShape getOutlineShape(BlockState state, BlockView world, BlockPos pos, ShapeContext context) {
return AGE_TO_SHAPE[this.getAge(state)];
}

@Override
public void randomTick(BlockState state, ServerWorld world, BlockPos pos, Random random) {
if (world.getBaseLightLevel(pos, 0) >= 9) {
int currentAge = this.getAge(state);
if (currentAge < this.getMaxAge()) {
float f = getAvailableMoisture(this, world, pos);
if (random.nextInt((int)(25.0F / f) + 1) == 0) {
if(currentAge == FIRST_STAGE_MAX_AGE) {
if(world.getBlockState(pos.up(1)).isOf(Blocks.AIR)) {
world.setBlockState(pos.up(1), this.withAge(currentAge + 1), 2);
}
} else {
world.setBlockState(pos, this.withAge(currentAge + 1), 2);
}
}
}
}
}

@Override
public void applyGrowth(World world, BlockPos pos, BlockState state) {
int nextAge = this.getAge(state) + this.getGrowthAmount(world);
int maxAge = this.getMaxAge();
if(nextAge > maxAge) {
nextAge = maxAge;
}

if(this.getAge(state) == FIRST_STAGE_MAX_AGE && world.getBlockState(pos.up(1)).isOf(Blocks.AIR)) {
world.setBlockState(pos.up(1), this.withAge(nextAge), 2);
} else {
world.setBlockState(pos, this.withAge(nextAge - 1), 2);
}
}

@Override
public boolean canPlaceAt(BlockState state, WorldView world, BlockPos pos) {
return super.canPlaceAt(state, world, pos) || (world.getBlockState(pos.down(1)).isOf(this) &&
world.getBlockState(pos.down(1)).get(AGE) == 7);
}

@Override
public int getMaxAge() {
return FIRST_STAGE_MAX_AGE + SECOND_STAGE_MAX_AGE;
}

@Override
protected ItemConvertible getSeedsItem() {
return ModItems.CORN_SEEDS;
}

@Override
protected IntProperty getAgeProperty() {
return AGE;
}

@Override
protected void appendProperties(StateManager.Builder<Block, BlockState> builder) {
builder.add(AGE);
}
}

在ModBlocks中注册块

public static final Block CORN_CROP = Registry.register(Registries.BLOCK, new Identifier(TutorialMod.MOD_ID, "corn_crop"),
new CornCropBlock(FabricBlockSettings.copyOf(Blocks.WHEAT)));

在ModItems中注册物品

public static final Item CORN_SEEDS = registerItem("corn_seeds",
new AliasedBlockItem(ModBlocks.CORN_CROP, new FabricItemSettings()));
public static final Item CORN = registerItem("corn",
new Item(new FabricItemSettings()));

在ModItemGroups中添加到物品组中

entries.add(ModItems.CORN_SEEDS);
entries.add(ModItems.CORN);

在TutorialModClient中,设

BlockRenderLayerMap.INSTANCE.putBlock(ModBlocks.CORN_CROP, RenderLayer.getCutout());

代码生成:

在ModLootTableProvider中:

AnyOfLootCondition.Builder builder2 =
BlockStatePropertyLootCondition.builder(ModBlocks.CORN_CROP).properties(StatePredicate.Builder.create()
.exactMatch(CornCropBlock.AGE, 7))
.or(BlockStatePropertyLootCondition.builder(ModBlocks.CORN_CROP).properties(StatePredicate.Builder.create()
.exactMatch(CornCropBlock.AGE, 8)));
addDrop(ModBlocks.CORN_CROP, cropDrops(ModBlocks.CORN_CROP, ModItems.CORN, ModItems.CORN_SEEDS, builder2));

在ModModelProvider中:

blockStateModelGenerator.registerCrop(ModBlocks.CORN_CROP, CornCropBlock.AGE, 0, 1, 2, 3, 4, 5, 6, 7, 8);

itemModelGenerator.register(ModItems.CORN, Models.GENERATED);

效果

玉米可以涨2格子高

image-20240825083711102