Jump to content

[solved] Lightning


scoob27

Recommended Posts

Hi everyone,

 

Like Jetfan16ladd i do have a problem when spawning lightnings. In my first try i have successfully spawned lightnings which dealt damages on other entities and put the ground on fire but that only works in single player. When i tried in multiplayer the server crashed. I looked in my code and found that rayTrace(double,float) is client side only.

 

So i tried to use the isRemote() method. The lightning appears in the world but it looks like it's only on the client side so the lighting don't deal any damage or put things on fire. From what i've read the client renders the lightning but it's the server's job to handle the damages (which is logic :) ).

 

So does someone know how to make the lightning deal damages without crashing the server ?

 

Here is the code i'm using:

 

 

@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) 
{
	if(world.isRemote)
	{
		MovingObjectPosition objectMouseOver = player.rayTrace(200, 1.0F);

		if (objectMouseOver != null && objectMouseOver.typeOfHit == MovingObjectType.BLOCK) 
		{
			int x = objectMouseOver.blockX;
			int y = objectMouseOver.blockY;
			int z = objectMouseOver.blockZ;
			world.addWeatherEffect(new EntityLightningBolt(world, x, y, z));
			//world.spawnEntityInWorld(new EntityLightningBolt(world, x, y, z));
		}
	}
	return itemStack;
}

 

 

Link to comment
Share on other sites

Thanks, it looks like it works :)

 

I have 2 (minor) questions thought :

 

1) It appears that in single player i can burn the grass but not in multiplayer. Is there a reason to that ?

 

2) With the getMovingObjectPositionFromPlayer method it works but only when i right-click near the player. Do you know if there is a way to extend the distance ?

 

If not, no problem, i can still work with that !!

 

Anyway, thank you :)

Link to comment
Share on other sites

Well for my code it's the same. I just replace the ray method with the one you gave me.

 

 

 

@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) 
{
		MovingObjectPosition objectMouseOver = getMovingObjectPositionFromPlayer(world, player, true);

		if (objectMouseOver != null && objectMouseOver.typeOfHit == MovingObjectType.BLOCK) 
		{
			int x = objectMouseOver.blockX;
			int y = objectMouseOver.blockY;
			int z = objectMouseOver.blockZ;
			world.addWeatherEffect(new EntityLightningBolt(world, x, y, z));
			//world.spawnEntityInWorld(new EntityLightningBolt(world, x, y, z));
		}
	return itemStack;
}

 

 

 

By copy, you mean i override the getMovingObjectPositionFromPlayer method right ? I'll try that.

Link to comment
Share on other sites

Ok it works great now, the entities take damages and are set to fire (the grass not but it's not a big deal) and i can adjust the distance !

 

Thank you very much :)

 

For those who are interested, here is my final code to spawn lightning when that player right-clicks an item :

 

 

 

@Override
public ItemStack onItemRightClick(ItemStack itemStack, World world, EntityPlayer player) 
{
	if(!world.isRemote)
	{
		MovingObjectPosition objectMouseOver = getDistanceFromPlayer(world, player, true);

		if (objectMouseOver != null && objectMouseOver.typeOfHit == MovingObjectType.BLOCK) 
		{
			int x = objectMouseOver.blockX;
			int y = objectMouseOver.blockY;
			int z = objectMouseOver.blockZ;
			world.addWeatherEffect(new EntityLightningBolt(world, x, y, z));
		}
	}
	return itemStack;
}

protected MovingObjectPosition getDistanceFromPlayer(World p_77621_1_, EntityPlayer p_77621_2_, boolean p_77621_3_)
    {
        float f = 1.0F;
        float f1 = p_77621_2_.prevRotationPitch + (p_77621_2_.rotationPitch - p_77621_2_.prevRotationPitch) * f;
        float f2 = p_77621_2_.prevRotationYaw + (p_77621_2_.rotationYaw - p_77621_2_.prevRotationYaw) * f;
        double d0 = p_77621_2_.prevPosX + (p_77621_2_.posX - p_77621_2_.prevPosX) * (double)f;
        double d1 = p_77621_2_.prevPosY + (p_77621_2_.posY - p_77621_2_.prevPosY) * (double)f + (double)(p_77621_1_.isRemote ? p_77621_2_.getEyeHeight() - p_77621_2_.getDefaultEyeHeight() : p_77621_2_.getEyeHeight()); // isRemote check to revert changes to ray trace position due to adding the eye height clientside and player yOffset differences
        double d2 = p_77621_2_.prevPosZ + (p_77621_2_.posZ - p_77621_2_.prevPosZ) * (double)f;
        Vec3 vec3 = Vec3.createVectorHelper(d0, d1, d2);
        float f3 = MathHelper.cos(-f2 * 0.017453292F - (float)Math.PI);
        float f4 = MathHelper.sin(-f2 * 0.017453292F - (float)Math.PI);
        float f5 = -MathHelper.cos(-f1 * 0.017453292F);
        float f6 = MathHelper.sin(-f1 * 0.017453292F);
        float f7 = f4 * f5;
        float f8 = f3 * f5;
        double reachDistance = 20.0D;
    
        Vec3 vec31 = vec3.addVector((double)f7 * reachDistance, (double)f6 * reachDistance, (double)f8 * reachDistance);
        return p_77621_1_.func_147447_a(vec3, vec31, p_77621_3_, !p_77621_3_, false);
    }

 

 

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
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.



×
×
  • Create New...

Important Information

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