Posted August 30, 20205 yr Right now I have block classes that are all identical except for their voxel shape, defined inside each class. I'd like to somehow pass this in during registry so that I can boil it down to one reusable class. Is this possible? I know it can't be passed into the super since the parent class "Block" doesn't work that way; I could extend AbstractBlock instead and add code for that, but that seems like more work. Edited August 30, 20205 yr by urbanxx001
August 30, 20205 yr You can use anonymous class. https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
September 2, 20205 yr Author So I realized that VoxelShape can be passed into the registry like this: public static final VoxelShape[] CROP_SHAPE = new VoxelShape[] { Block.makeCuboidShape(0.0D,0.0D,0.0D,16.0D,2.0D,16.0D), Block.makeCuboidShape(0.0D,0.0D,0.0D,16.0D,4.0D,16.0D), Block.makeCuboidShape(0.0D,0.0D,0.0D,16.0D,6.0D,16.0D), Block.makeCuboidShape(0.0D,0.0D,0.0D,16.0D,8.0D,16.0D) }; public static final Block PEANUTS = register(Reference.MOD_ID + ":peanut", new ModCropsBlock(CROP_SHAPE, ModItems.PEANUT_ITEM, Block.Properties.create(Material.PLANTS).doesNotBlockMovement().tickRandomly().zeroHardnessAndResistance().sound(SoundType.CROP))); With the class: public class ModCropsBlock extends CropsBlock { public final VoxelShape[] SHAPE; public final Item SEEDS; public ModCropsBlock(VoxelShape[] shape, Item seeds, Properties builder) { super(builder); this.SHAPE = shape; this.SEEDS = seeds; } @Override public int getMaxAge() { return SHAPE.length - 1; } @Override public IntegerProperty getAgeProperty() { return IntegerProperty.create("age", 0, getMaxAge()); } @Override protected IItemProvider getSeedsItem() { return SEEDS; } @Override public void randomTick(BlockState state, ServerWorld worldIn, BlockPos pos, Random random) { if (random.nextInt(getMaxAge()) != 0) { super.randomTick(state, worldIn, pos, random); } } @Override protected int getBonemealAgeIncrease(World worldIn) { return super.getBonemealAgeIncrease(worldIn) / getMaxAge(); } @Override protected void fillStateContainer(StateContainer.Builder<Block, BlockState> builder) { builder.add(getAgeProperty()); } @Override public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) { return SHAPE[state.get(this.getAgeProperty())]; } } However during compilation I get a null pointer error for getMaxAge and getAgeProperty, which means the VoxelShape isn't being retrieved. Is this an easy fix or do I actually need anonymous functions? Sorry if this is a simple Java question.
September 2, 20205 yr Each time you call getAgeProperty() you creates a new property, which I think it makes the property you added to the state container and the property you use in the getShape() is different... but I'm not sure about this (There's already a property in CropsBlock, you should probably use it instead of create your own one). Also the reason I recommended you anonymous class is because I thought you don't want to create a class just for custom shapes, but apparently I misread your question : ( Edited September 2, 20205 yr by poopoodice
September 3, 20205 yr Author True creating a new property each time might be affecting it. The reason I think it's an issue with getting the shape is that getMaxAge returns an error separately, which is only dependent on that. I'm actually just overriding the current methods that are in place so that both properties generate automatically based on the shape array. In CropsBlock, these fields are defined outside the methods, but when I try to do it that way with something like Max_Age = Shapes.length - 1 then it says that Shapes isn't initialized.
September 3, 20205 yr ...then do it in the constructor? Your Shapes field isn't static, but defined in the constructor, so do the same with your age property. 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.
September 3, 20205 yr Author Ah it's the scope.I made those changes and it resolved the max age, but there's still a null error with the age property, saying it's invalid: public final VoxelShape[] SHAPE; public final Item SEEDS; public final int MAX_AGE; public final IntegerProperty AGE_PROPERTY; public ModCropsBlock(VoxelShape[] shape, Item seeds, Properties builder) { super(builder); this.SHAPE = shape; this.SEEDS = seeds; this.MAX_AGE = SHAPE.length - 1; this.AGE_PROPERTY = IntegerProperty.create("age", 0, MAX_AGE); } Edited September 3, 20205 yr by urbanxx001
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.