Trying to create a custom lever, but I just can't seem to nail it

Started by JJ_007 on

Topic category: Advanced modding

Joined Apr 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Trying to create a custom lever, but I just can't seem to nail it
Mon, 08/03/2020 - 10:09 (edited)

Warning, there's a lot of custom code ahead.

I'm trying to make a new lever. It has the same model, and functions exactly the same as a vanilla lever, only it has a different texture. I've imported the models and textures already into MCreator, along with the blockstates. Using the Java code for the vanilla lever, I tried to use custom code to make mine work. Two hours of changing up the code to remove some errors later, and I'm down to one error. However, if I fix this, I'm sure there'll be many more. If someone who is experienced with this kind of thing could just look over it, and tell me if anything's wrong, it'd be greatly appreciated. The custom code is as follows, with the line with the error in bold and italics.

 

package net.mcreator.vanilla_improvements.block;

import net.minecraftforge.registries.ObjectHolder;
import net.minecraftforge.api.distmarker.OnlyIn;
import net.minecraftforge.api.distmarker.Dist;

import net.minecraft.world.storage.loot.LootContext;
import net.minecraft.world.World;
import net.minecraft.world.IWorld;
import net.minecraft.world.IBlockReader;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.SoundEvents;
import net.minecraft.util.SoundCategory;
import net.minecraft.util.Hand;
import net.minecraft.util.Direction;
import net.minecraft.util.ActionResultType;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.state.properties.AttachFace;
import net.minecraft.state.StateContainer;
import net.minecraft.state.BooleanProperty;
import net.minecraft.particles.RedstoneParticleData;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemGroup;
import net.minecraft.item.Item;
import net.minecraft.item.BlockItem;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.block.material.Material;
import net.minecraft.block.SoundType;
import net.minecraft.block.BlockState;
import net.minecraft.block.Block;

import net.mcreator.vanilla_improvements.VanillaImprovementsModElements;

import java.util.Random;
import java.util.List;
import java.util.Collections;

@VanillaImprovementsModElements.ModElement.Tag
public class BlackstoneLeverBlock extends VanillaImprovementsModElements.ModElement {
    @ObjectHolder("vanilla_improvements:blackstone_lever")
    public static final Block block = null;
    public BlackstoneLeverBlock(VanillaImprovementsModElements instance) {
        super(instance, 355);
    }
    public static final BooleanProperty POWERED = BlockStateProperties.POWERED;
    protected static final VoxelShape BLACKSTONE_LEVER_NORTH_AABB = Block.makeCuboidShape(5.0D, 4.0D, 10.0D, 11.0D, 12.0D, 16.0D);
    protected static final VoxelShape BLACKSTONE_LEVER_SOUTH_AABB = Block.makeCuboidShape(5.0D, 4.0D, 0.0D, 11.0D, 12.0D, 6.0D);
    protected static final VoxelShape BLACKSTONE_LEVER_WEST_AABB = Block.makeCuboidShape(10.0D, 4.0D, 5.0D, 16.0D, 12.0D, 11.0D);
    protected static final VoxelShape BLACKSTONE_LEVER_EAST_AABB = Block.makeCuboidShape(0.0D, 4.0D, 5.0D, 6.0D, 12.0D, 11.0D);
    protected static final VoxelShape FLOOR_Z_SHAPE = Block.makeCuboidShape(5.0D, 0.0D, 4.0D, 11.0D, 6.0D, 12.0D);
    protected static final VoxelShape FLOOR_X_SHAPE = Block.makeCuboidShape(4.0D, 0.0D, 5.0D, 12.0D, 6.0D, 11.0D);
    protected static final VoxelShape CEILING_Z_SHAPE = Block.makeCuboidShape(5.0D, 10.0D, 4.0D, 11.0D, 16.0D, 12.0D);
    protected static final VoxelShape CEILING_X_SHAPE = Block.makeCuboidShape(4.0D, 10.0D, 5.0D, 12.0D, 16.0D, 11.0D);
    protected BlackstoneLeverBlock(Block.VanillaImprovementsModElements builder) {
        super(builder);
        this.setDefaultState(this.stateContainer.getBaseState().with(HORIZONTAL_FACING, Direction.NORTH).with(POWERED, Boolean.valueOf(false))
                .with(FACE, AttachFace.WALL));
    }

    public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
        switch ((AttachFace) state.get(FACE)) {
            case FLOOR :
                switch (state.get(HORIZONTAL_FACING).getAxis()) {
                    case X :
                        return FLOOR_X_SHAPE;
                    case Z :
                    default :
                        return FLOOR_Z_SHAPE;
                }
            case WALL :
                switch ((Direction) state.get(HORIZONTAL_FACING)) {
                    case EAST :
                        return BLACKSTONE_LEVER_EAST_AABB;
                    case WEST :
                        return BLACKSTONE_LEVER_WEST_AABB;
                    case SOUTH :
                        return BLACKSTONE_LEVER_SOUTH_AABB;
                    case NORTH :
                    default :
                        return BLACKSTONE_LEVER_NORTH_AABB;
                }
            case CEILING :
            default :
                switch (state.get(HORIZONTAL_FACING).getAxis()) {
                    case X :
                        return CEILING_X_SHAPE;
                    case Z :
                    default :
                        return CEILING_Z_SHAPE;
                }
        }
    }

    public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn,
            BlockRayTraceResult hit) {
        if (worldIn.isRemote) {
            BlockState blockstate1 = state.cycle(POWERED);
            if (blockstate1.get(POWERED)) {
                addParticles(blockstate1, worldIn, pos, 1.0F);
            }
            return ActionResultType.SUCCESS;
        } else {
            BlockState blockstate = this.func_226939_d_(state, worldIn, pos);
            float f = blockstate.get(POWERED) ? 0.6F : 0.5F;
            worldIn.playSound((PlayerEntity) null, pos, SoundEvents.BLOCK_LEVER_CLICK, SoundCategory.BLOCKS, 0.3F, f);
            return ActionResultType.SUCCESS;
        }
    }

    public BlockState func_226939_d_(BlockState p_226939_1_, World p_226939_2_, BlockPos p_226939_3_) {
        p_226939_1_ = p_226939_1_.cycle(POWERED);
        p_226939_2_.setBlockState(p_226939_3_, p_226939_1_, 3);
        this.updateNeighbors(p_226939_1_, p_226939_2_, p_226939_3_);
        return p_226939_1_;
    }

    private static void addParticles(BlockState state, IWorld worldIn, BlockPos pos, float alpha) {
        Direction direction = state.get(HORIZONTAL_FACING).getOpposite();
        Direction direction1 = getFacing(state).getOpposite();
        double d0 = (double) pos.getX() + 0.5D + 0.1D * (double) direction.getXOffset() + 0.2D * (double) direction1.getXOffset();
        double d1 = (double) pos.getY() + 0.5D + 0.1D * (double) direction.getYOffset() + 0.2D * (double) direction1.getYOffset();
        double d2 = (double) pos.getZ() + 0.5D + 0.1D * (double) direction.getZOffset() + 0.2D * (double) direction1.getZOffset();
        worldIn.addParticle(new RedstoneParticleData(1.0F, 0.0F, 0.0F, alpha), d0, d1, d2, 0.0D, 0.0D, 0.0D);
    }

    @Override
    public void initElements() {
        elements.blocks.add(() -> new CustomBlock());
        elements.items.add(() -> new BlockItem(block, new Item.Properties().group(ItemGroup.REDSTONE)).setRegistryName(block.getRegistryName()));
    }

    @Override
    @OnlyIn(Dist.CLIENT)
    public void animateTick(BlockState stateIn, World worldIn, BlockPos pos, Random rand) {
        if (stateIn.get(POWERED) && rand.nextFloat() < 0.25F) {
            addParticles(stateIn, worldIn, pos, 0.5F);
        }
    }
    public static class CustomBlock extends Block {
        public CustomBlock() {
            super(Block.Properties.create(Material.REDSTONE_LIGHT).sound(SoundType.STONE).hardnessAndResistance(1f, 10f).lightValue(0).notSolid());
            setRegistryName("blackstone_lever");
        }

        public void onReplaced(BlockState state, World worldIn, BlockPos pos, BlockState newState, boolean isMoving) {
            if (!isMoving && state.getBlock() != newState.getBlock()) {
                if (state.get(POWERED)) {
                    this.updateNeighbors(state, worldIn, pos);
                }
                super.onReplaced(state, worldIn, pos, newState, isMoving);
            }
        }

        public int getWeakPower(BlockState blockState, IBlockReader blockAccess, BlockPos pos, Direction side) {
            return blockState.get(POWERED) ? 15 : 0;
        }

        public int getStrongPower(BlockState blockState, IBlockReader blockAccess, BlockPos pos, Direction side) {
            return blockState.get(POWERED) && getFacing(blockState) == side ? 15 : 0;
        }

        public boolean canProvidePower(BlockState state) {
            return true;
        }

        private void updateNeighbors(BlockState p_196378_1_, World p_196378_2_, BlockPos p_196378_3_) {
            p_196378_2_.notifyNeighborsOfStateChange(p_196378_3_, this);
            p_196378_2_.notifyNeighborsOfStateChange(p_196378_3_.offset(getFacing(p_196378_1_).getOpposite()), this);
        }

        protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) {
            builder.add(FACE, HORIZONTAL_FACING, POWERED);
        }

        @Override
        public boolean isNormalCube(BlockState state, IBlockReader worldIn, BlockPos pos) {
            return false;
        }

        @Override
        public boolean propagatesSkylightDown(BlockState state, IBlockReader reader, BlockPos pos) {
            return true;
        }

        @Override
        public List<ItemStack> getDrops(BlockState state, LootContext.Builder builder) {
            List<ItemStack> dropsOriginal = super.getDrops(state, builder);
            if (!dropsOriginal.isEmpty())
                return dropsOriginal;
            return Collections.singletonList(new ItemStack(this, 1));
        }
    }
}
 

And the console error;

 

error: cannot find symbol 
protected BlackstoneLeverBlock(Block.VanillaImprovementsModElements builder) {

                                                              ^

symbol:   class VanillaImprovementsModElements
location: class Block

 

If anyone has any idea how to solve this, please let me know!

Edited by JJ_007 on Mon, 08/03/2020 - 10:09
Joined Apr 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
Aye I figured it out. I was…
Tue, 08/04/2020 - 09:43

Aye I figured it out. I was really overcomplicating things there, wow

Joined Jun 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
S0 what was the fix? I'm…
Fri, 12/03/2021 - 17:02

S0 what was the fix? I'm also making a lever but am having troubles when it comes to the wall and ceiling placement.

Joined Apr 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
When opening the java custom…
Fri, 12/10/2021 - 07:26

When opening the java custom code of the block, look for where it says 'CustomBlock extends Block' and replace 'Block' with 'Lever'.

Joined Apr 2020
Points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • MCreator plugins:
  • Comments:
LeverBlock* not Lever, sorry
Fri, 12/10/2021 - 07:26

LeverBlock* not Lever, sorry