Jump to content

[1.16.4] Make custom BlockStateProperty


Eleocraft

Recommended Posts

I want to make a block, that works similar to an end portal frame block in minecraft. The player should be able to rightclick on it, and the block should change to an active state.

From all I know minecraft uses Blockstateproberties for that, but up now I didn't had to make my own property, I only used the standart ones (like FACING for example). I have no idea how to register a new Property.

For testing purposes I tried to give my block the property "EYE", the one that is used for end portal frames, but it didn't seem to work.

My code:

package eleocraft.ancientguardian.objects.blocks;

import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.state.BooleanProperty;
import net.minecraft.state.properties.BlockStateProperties;
import net.minecraft.util.ActionResultType;
import net.minecraft.util.Hand;
import net.minecraft.util.ResourceLocation;
import net.minecraft.util.math.BlockPos;
import net.minecraft.util.math.BlockRayTraceResult;
import net.minecraft.util.math.shapes.ISelectionContext;
import net.minecraft.util.math.shapes.VoxelShape;
import net.minecraft.util.math.vector.Vector3d;
import net.minecraft.world.IBlockReader;
import net.minecraft.world.World;
import net.minecraftforge.registries.ForgeRegistries;

public class BaseBlock extends Block
{
	 private static final Vector3d BASE_MIN_CORNER = new Vector3d(0.0, 0.0, 0.0);
	 private static final Vector3d BASE_MAX_CORNER = new Vector3d(16.0, 8.0, 16.0);
	
	 private static final VoxelShape BASE = Block.makeCuboidShape(BASE_MIN_CORNER.getX(), BASE_MIN_CORNER.getY(), BASE_MIN_CORNER.getZ(), BASE_MAX_CORNER.getX(), BASE_MAX_CORNER.getY(), BASE_MAX_CORNER.getZ());
	 
	 public static BooleanProperty ACTIVE = BlockStateProperties.EYE;
	 
	 public BaseBlock(Properties properties)
	 {
		super(properties);
		this.setDefaultState(this.stateContainer.getBaseState().with(ACTIVE, Boolean.valueOf(false)));
	 }
	
	 @Override
	 public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) 
	 {
	    return BASE;
	 }
	 
	 @Override
	 public ActionResultType onBlockActivated(BlockState state, World worldIn, BlockPos pos, PlayerEntity player, Hand handIn, BlockRayTraceResult hit)
	 {
		 if (player.inventory.getCurrentItem().getItem() == ForgeRegistries.ITEMS.getValue(new ResourceLocation("ancientguardian:ancient_crystal")) && state.get(ACTIVE).equals(Boolean.valueOf(false)))
		 {
			 System.out.println("It worked");
			 worldIn.setBlockState(pos, this.stateContainer.getBaseState().with(ACTIVE, Boolean.valueOf(true)));
			 player.inventory.getCurrentItem().setCount(player.inventory.getCurrentItem().getCount() - 1);
			 return ActionResultType.SUCCESS;
		 }
		 return ActionResultType.FAIL;
	 }
}

 

Link to comment
Share on other sites

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Link to comment
Share on other sites

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
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...

Important Information

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