Jump to content

Recommended Posts

Posted (edited)

Hi, I added a new type of block "weeds" that should be able to spread to nearby crops. The block itself works fine, but I don't know how I should go about the spreading mechanic.

public void tick(IBlockState state, World worldIn, BlockPos pos, Random random) {
        super.tick(state, worldIn, pos, random);
        Map<BlockPos, IBlockState> blockStates = new HashMap();
        blockStates.put(pos.north(), worldIn.getBlockState(pos.north()));
        blockStates.put(pos.south(), worldIn.getBlockState(pos.south()));
        blockStates.put(pos.east(), worldIn.getBlockState(pos.east()));
        blockStates.put(pos.west(), worldIn.getBlockState(pos.west()));

        for(Map.Entry<BlockPos, IBlockState> blockState : blockStates.entrySet()) {
            String registryName = blockState.getValue().getBlock().getRegistryName().toString();
            if(registryName.equals("wheat")
                    || registryName.equals("beetroots")
                    || registryName.equals("carrots")
                    || registryName.equals("potatoes")) {
                worldIn.setBlockState(blockState.getKey(), BlockList.weeds.getDefaultState());
            }
        }
    }

I tried something like that, but that doesn't seem to work. How do I find out what type of block is next to the ticked blocked and how do I replace blocks in the world? 

Edited by Yamahari
Posted
35 minutes ago, Yamahari said:

            if(registryName.equals("wheat")
                    || registryName.equals("beetroots")
                    || registryName.equals("carrots")
                    || registryName.equals("potatoes")) {

Ok, so, two problems:

 

1) Comparing strings in order to identify blocks is The Worst idea. Blocks.WHEAT, Blocks.BEETROOTBlocks.CARROTS, and Blocks.POTATOES are a thing. Just check if(block == Blocks.WHEAT), etc.

2) This can't handle mod added crops of any kind, you should instance check if(block instanceof BlockCrops)

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.

Posted (edited)

Thanks for the feedback, it's been a long long time since I last touched modding x)

public void tick(IBlockState state, World worldIn, BlockPos pos, Random random) {
        super.tick(state, worldIn, pos, random);
        Map<BlockPos, IBlockState> blockStates = new HashMap();
        blockStates.put(pos.north(), worldIn.getBlockState(pos.north()));
        blockStates.put(pos.south(), worldIn.getBlockState(pos.south()));
        blockStates.put(pos.east(), worldIn.getBlockState(pos.east()));
        blockStates.put(pos.west(), worldIn.getBlockState(pos.west()));

        for(Map.Entry<BlockPos, IBlockState> blockState : blockStates.entrySet()) {
            Block block = blockState.getValue().getBlock();
            if(block instanceof BlockCrops && block != BlockList.weeds && random.nextInt(4) == 0) {
                worldIn.setBlockState(blockState.getKey(), BlockList.weeds.getDefaultState());
            }
        }
    }

Can you help me with this: how do I get the age property from the block that may be replaced? 

 

Nvm i got it:

BlockCrops blockCrops = (BlockCrops) block;
if(blockState.getValue().get(blockCrops.getAgeProperty()) < blockCrops.getMaxAge()) {
	worldIn.setBlockState(blockState.getKey(), BlockList.weeds.getDefaultState());
}

 

Edited by Yamahari
Posted
public void tick(IBlockState state, World worldIn, BlockPos pos, Random random) {
        super.tick(state, worldIn, pos, random);
        Map<BlockPos, IBlockState> blockStates = new HashMap();
        blockStates.put(pos.north(), worldIn.getBlockState(pos.north()));
        blockStates.put(pos.south(), worldIn.getBlockState(pos.south()));
        blockStates.put(pos.east(), worldIn.getBlockState(pos.east()));
        blockStates.put(pos.west(), worldIn.getBlockState(pos.west()));

        for(Map.Entry<BlockPos, IBlockState> blockState : blockStates.entrySet()) {
            Block block = blockState.getValue().getBlock();
            if(block instanceof BlockCrops && block != BlockList.weeds && random.nextInt(4) == 0) {
                worldIn.setBlockState(blockState.getKey(), BlockList.weeds.getDefaultState());
            }
        }
    }

what is that

do it like so

public void tick(IBlockState state, World worldIn, BlockPos pos, Random random) {
		super.tick(state, worldIn, pos, random);
		for (EnumFacing facing : EnumFacing.HORIZONTALS){
			BlockPos offSet = pos.offset(facing);
			IBlockState blockState = worldIn.getBlockState(offSet);
			Block block = blockState.getBlock();
			if(block instanceof BlockCrops && block != BlockList.weeds && random.nextInt(4) == 0) {
				worldIn.setBlockState(blockState.getKey(), BlockList.weeds.getDefaultState());
			}
		}
	}

 

  • Like 1
Posted

Hm, what version of forge is this? I am modding for 1.13.2 ( maybe should have added that x) ) and there doesn't seem to be a member "HORIZONTALS" in EnumFacing.

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
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
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.

Announcements



×
×
  • Create New...

Important Information

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