Jump to content

[1.7.10] [UNSOLVED] updateTick() on a crop not working


Recommended Posts

Posted

Hi! Im trying to make an item that grows crops and breaks them. Im using the world.updateTick() and setting it to update the block when a random happens and another that breaks the block. But when I use it, nothing happens when calling if "!world.isRemote" and when I dont use that I get ghost blocks and items. This is probably a silly mistake but I tried fixing it for about 5 hours (with some additions here and there with the item :P)

 

Here's my code

 

@Override
public void onUsingTick(ItemStack itemstack, EntityPlayer player, int count) {
	super.onUsingTick(itemstack, player, count);

	Random random = new Random();
	int number = random.nextInt(5);
	int x = (int)player.serverPosX;
	int y = (int)player.serverPosY;
	int z = (int)player.serverPosZ;

	Block block1 = player.worldObj.getBlock(x, y-1, z);
	Block block2 = player.worldObj.getBlock(x+1, y-1, z);
	Block block3 = player.worldObj.getBlock(x, y-1, z+1);
	Block block4 = player.worldObj.getBlock(x+1, y-1, z+1);
	Block block5 = player.worldObj.getBlock(x+1, y-1, z-1);
	Block block6 = player.worldObj.getBlock(x-1, y-1, z+1);
	Block block7 = player.worldObj.getBlock(x-1, y-1, z-1);
	Block block8 = player.worldObj.getBlock(x-1, y-1, z);
	Block block9 = player.worldObj.getBlock(x, y-1, z-1);

	switch(itemstack.stackTagCompound.getInteger("mode")){
	case 0:
		updateThisBlock(block1, player.worldObj, x, y, z);
		updateThisBlock(block2, player.worldObj, x+1, y, z);
		updateThisBlock(block3, player.worldObj, x, y, z+1);
		updateThisBlock(block4, player.worldObj, x+1, y, z+1);
		updateThisBlock(block5, player.worldObj, x+1, y, z-1);
		updateThisBlock(block6, player.worldObj, x-1, y, z-1);
		updateThisBlock(block7, player.worldObj, x-1, y, z+1);
		updateThisBlock(block8, player.worldObj, x-1, y, z);
		updateThisBlock(block9, player.worldObj, x, y, z-1);
	break;

	case 1:
		breakThisBlock(block1, player.worldObj, x, y, z);
		breakThisBlock(block2, player.worldObj, x+1, y, z);
		breakThisBlock(block3, player.worldObj, x, y, z+1);
		breakThisBlock(block4, player.worldObj, x+1, y, z+1);
		breakThisBlock(block5, player.worldObj, x+1, y, z-1);
		breakThisBlock(block6, player.worldObj, x-1, y, z-1);
		breakThisBlock(block7, player.worldObj, x-1, y, z+1);
		breakThisBlock(block8, player.worldObj, x-1, y, z);
		breakThisBlock(block9, player.worldObj, x, y, z-1);	
	}
}

public void updateThisBlock(Block block, World world, int x, int y, int z){
	Random random = new Random();
	int r = random.nextInt(4);
	if(block instanceof IPlantable && r == 3){
		if(!world.isRemote){
			System.out.println("Updated");
			world.scheduleBlockUpdate(x, y-1, z, block, 20);
			block.updateTick(world, x, y-1, z, random);
		}
	}
}
public void breakThisBlock(Block block, World world, int x, int y, int z){
	Random random = new Random();
	int r = random.nextInt(1);
	List<ItemStack> items = new ArrayList();

	if(block instanceof IPlantable && r==0){
		items.addAll(block.getDrops(world, x, y-1, z, world.getBlockMetadata(x, y-1, z), 1));
		if(!world.isRemote){
			world.setBlockToAir(x, y-1, z);
			for(ItemStack stackerino : items){
				world.spawnEntityInWorld(new EntityItem(world, x, y, z, stackerino));
			}
		}
	}
}

 

Thanks in advance!

Posted

You should really look into the world class a bit more.

 

world.func_147480_a(x,y,z,dropItems);

 

^^^ That is the code for breaking blocks. (It was deobfusicated in 1.6)

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Posted

Yup just looked at that and it works but when I try to check if the world is not remote the code is not executed, dont really know why. Here's what I'm trying to do:

 

public void breakThisBlock(Block block, World world, int x, int y, int z){
	Random random = new Random();
	int r = random.nextInt(1);
	if(block instanceof IPlantable && r==0){
		if(!world.isRemote){
			world.func_147480_a(x, y-1, z, true);
		}
	}
}

Posted

Correct me if I'm wrong, but I'm pretty sure you don't have to do that on server side only.

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Posted

Okay, so I see that your issue is in the update method.

 

Try either updating the block or using using the schedule. Don't use both. See if that works.

 

(If it does, then the schedule might be un-updating it after it updating. If not, let's see what happens.)

-Mitchellbrine

 

Minecraft can do ANYTHING, it's coded in Java and you got the full power of Java behind you when you code. So nothing is impossible.

It may be freaking fucking hard though, but still possible ;)

 

If you create a topic on Modder Support, live by this motto:

I don't want your charity, I want your information
Posted

Having only the updateTick still causes ghost blocks, dont know why. Also looking at the updateTick it looks like it needs to have an update scheduled (?). Still no luck :/

 

Here's complete code again:

 

@Override
public void onUsingTick(ItemStack itemstack, EntityPlayer player, int count) {
	super.onUsingTick(itemstack, player, count);

	Block block1 = player.worldObj.getBlock(x, y-1, z);
	Block block2 = player.worldObj.getBlock(x+1, y-1, z);
	Block block3 = player.worldObj.getBlock(x, y-1, z+1);
	Block block4 = player.worldObj.getBlock(x+1, y-1, z+1);
	Block block5 = player.worldObj.getBlock(x+1, y-1, z-1);
	Block block6 = player.worldObj.getBlock(x-1, y-1, z+1);
	Block block7 = player.worldObj.getBlock(x-1, y-1, z-1);
	Block block8 = player.worldObj.getBlock(x-1, y-1, z);
	Block block9 = player.worldObj.getBlock(x, y-1, z-1);
	switch(itemstack.stackTagCompound.getInteger("mode")){

	case 0: //USED FOR GROWING CROPS
		updateThisBlock(block1, player.worldObj, x, y, z);
		updateThisBlock(block2, player.worldObj, x+1, y, z);
		updateThisBlock(block3, player.worldObj, x, y, z+1);
		updateThisBlock(block4, player.worldObj, x+1, y, z+1);
		updateThisBlock(block5, player.worldObj, x+1, y, z-1);
		updateThisBlock(block6, player.worldObj, x-1, y, z-1);
		updateThisBlock(block7, player.worldObj, x-1, y, z+1);
		updateThisBlock(block8, player.worldObj, x-1, y, z);
		updateThisBlock(block9, player.worldObj, x, y, z-1);
	break;

	case 1: //USED FOR DESTROYING CROPS
		breakThisBlock(block1, player.worldObj, x, y, z);
		breakThisBlock(block2, player.worldObj, x+1, y, z);
		breakThisBlock(block3, player.worldObj, x, y, z+1);
		breakThisBlock(block4, player.worldObj, x+1, y, z+1);
		breakThisBlock(block5, player.worldObj, x+1, y, z-1);
		breakThisBlock(block6, player.worldObj, x-1, y, z-1);
		breakThisBlock(block7, player.worldObj, x-1, y, z+1);
		breakThisBlock(block8, player.worldObj, x-1, y, z);
		breakThisBlock(block9, player.worldObj, x, y, z-1);	
	break;
	}
}

//GROWING CROPS
public void updateThisBlock(Block block, World world, int x, int y, int z){
	int r = world.rand.nextInt(4);
	if(block instanceof IPlantable && r == 3){
		System.out.println("Updated");
		world.scheduleBlockUpdate(x, y, z, block, 1);
		block.updateTick(world, x, y-1, z, world.rand);
	}
}

//BREAKING CROPS
public void breakThisBlock(Block block, World world, int x, int y, int z){
	Random random = new Random();
	int r = random.nextInt(1);
	List<ItemStack> items = new ArrayList();
	if(block instanceof IPlantable && r==0){
		items.addAll(block.getDrops(world, x, y-1, z, world.getBlockMetadata(x, y-1, z), 1));
		world.func_147480_a(x, y-1, z, true);
		for(ItemStack stackerino : items){
			world.spawnEntityInWorld(new EntityItem(world, x, y, z, stackerino));
		}
	}
}

 

Thanks for the help!

Posted

Try to use

onItemRightClick()

instead of

onUsingTick()

Don't ask for support per PM! They'll get ignored! | If a post helped you, click the "Thank You" button at the top right corner of said post! |

mah twitter

This thread makes me sad because people just post copy-paste-ready code when it's obvious that the OP has little to no programming experience. This is not how learning works.

Posted

I've tried using onItemRightClick, and its still growing the crops but it looks like only on the client side. It still leaves ghost blocks of the plants growing when they're not, just as the gfy that I posted ontop shows. I've tried checking world.isRemote but it's no use since its not being called, dont know why. Also I want my item to update the blocks in a 3x3 radius of the player when right click is being held :D .

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.