Jump to content

Recommended Posts

Posted

Just a quick question that has been bugging me for the past two or so days: I've managed create and item that spawns lightning onRightClick(), and have a "cool down" before use again, something like 5 seconds. It works fine, but there is something odd that I've noticed. Whenever I right click and spawn the lightning, say 15m (blocks) away, the ground below *me* and not the block I pointed at, erupts into flame, and I get struck by lightning. Which is not the intended effect. However, whenever I am in the air (creative) the lightning strike works fine, causing the block I pointed at to erupt into flame and damage any near-by creatures in the area.

 

What's stumped me is why it decides to inflict damage where I am standing, instead of where the actual bolt is rendering/striking.

 

Any advice or speculation or help is greatly appreciated!

 

As usual, my code for the Item in question:

 

package aceofkings.mod;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class TestItem extends Item {

public TestItem(){
	super();
	setMaxDamage(100);
}

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player){

	if(stack.getItemDamage() == 0){

		//ray trace
		MovingObjectPosition mop = player.rayTrace(75.0D, 1.0F);
		if(mop == null){
			return stack;
		}

		//getting coords from ray trace
		Vec3 vec3 = mop.hitVec;
		double x = vec3.xCoord;
		double y = vec3.yCoord;
		double z = vec3.zCoord;
		int i = MathHelper.floor_double(x);
		int j = MathHelper.floor_double(y);
		int k = MathHelper.floor_double(z);

		//Dont want to spawn lightning in caves...
		if(!world.canBlockSeeTheSky(i, j, k)){
			return stack;
		}

		//intance of bolt, and and spawning of bolt
		EntityLightningBolt bolt = new EntityLightningBolt(world, i, j, k);
		world.spawnEntityInWorld(bolt);

		stack.setItemDamage(99);
	}
	return stack;
}

@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean flag){

	if(!world.isRemote){
		//Cool down
		if(stack.getItemDamage() > 0){
			stack.damageItem(-1, (EntityLiving)entity);
		}
	}

}
}

 

 

If you want, I can post the main class too, but I don't think that the issue lies there, so just let me know.

Posted

Well first all, so you can get an idea of my credibility, i'm new here, and new to modding ( or even coding in java ). I do have a lot of experience in other coding languages though, so I'm just going to through out my opinion.

 

You probably already know this, but you problem is most likely here

 

Vec3 vec3 = mop.hitVec;

double x = vec3.xCoord;

double y = vec3.yCoord;

double z = vec3.zCoord;

int i = MathHelper.floor_double(x);

int j = MathHelper.floor_double(y);

int k = MathHelper.floor_double(z);

 

 

In playing minecraft, i've also seen that in creative, mobs don't even acknowledge you, unlike survival. So what if, in survival, the ray is starting in you, and ending in you. Sort of, detecting you as the collision point. Try start the line off in front the the player, like at a blocks distance.

 

Posted

Well first all, so you can get an idea of my credibility, i'm new here, and new to modding ( or even coding in java ). I do have a lot of experience in other coding languages though, so I'm just going to through out my opinion.

 

You probably already know this, but you problem is most likely here

 

Vec3 vec3 = mop.hitVec;

double x = vec3.xCoord;

double y = vec3.yCoord;

double z = vec3.zCoord;

int i = MathHelper.floor_double(x);

int j = MathHelper.floor_double(y);

int k = MathHelper.floor_double(z);

 

 

In playing minecraft, i've also seen that in creative, mobs don't even acknowledge you, unlike survival. So what if, in survival, the ray is starting in you, and ending in you. Sort of, detecting you as the collision point. Try start the line off in front the the player, like at a blocks distance.

 

Thanks! I'll have to tinker around with it. And now that you mention it, it very well could be intercepting me instead of where I want it to. Didn't think of that before. I'll report back later with my findings.

Posted

OH OH OH... I think I figured it out... Turns out someone else had the same issue here. Oops.

 

EDIT/UPDATE:

 

Okay, so after some thread surfing and a few smashed keyboards, I've found a solution to my problem, and it works quite nicely, other than the fact the Lightning bolt isn't rendering. As it turns out, in the ItemBoat.java, there is code that places the boat where you are looking, within a five block radius! So all I did was copy and paste bits and pieces that I needed, changed a variable or two, etc etc. I even got it to teleport me to the block I was looking at (with some more checks and such, this could be used quite safely in survival, ie not suffocating in a wall)!

 

For those who wish to see the code, I shall paste it here:

 


package yourpackage.name;

import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.effect.EntityLightningBolt;
import net.minecraft.entity.item.EntityBoat;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Blocks;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.MovingObjectPosition.MovingObjectType;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class LightningRod extends Item {

public LightningRod(){
	super();
	setMaxDamage(100);
}

@Override
public ItemStack onItemRightClick(ItemStack stack, World world, EntityPlayer player){

	//Copied ItemBoat.java
	float f = 1.0F;
        float f1 = player.prevRotationPitch + (player.rotationPitch - player.prevRotationPitch) * f;
        float f2 = player.prevRotationYaw + (player.rotationYaw - player.prevRotationYaw) * f;
        double d0 = player.prevPosX + (player.posX - player.prevPosX) * (double)f;
        double d1 = player.prevPosY + (player.posY - player.prevPosY) * (double)f + 1.62D - (double)player.yOffset;
        double d2 = player.prevPosZ + (player.posZ - player.prevPosZ) * (double)f;
        Vec3 vec3 = world.getWorldVec3Pool().getVecFromPool(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;
        
        //Range from player
        double d3 = 15.0D;
        Vec3 vec31 = vec3.addVector((double)f7 * d3, (double)f6 * d3, (double)f8 * d3);
        MovingObjectPosition movingobjectposition = world.rayTraceBlocks(vec3, vec31, true);
        
        if (movingobjectposition == null)
        {
            return stack;
        }
        
        //Checks if you hit a block
        if (movingobjectposition.typeOfHit == MovingObjectPosition.MovingObjectType.BLOCK)
        {
            int i = movingobjectposition.blockX;
            int j = movingobjectposition.blockY;
            int k = movingobjectposition.blockZ;
            
            //Accounts for snow layer.
            if (world.getBlock(i, j, k) == Blocks.snow_layer)
            {
                --j;
            }
            
            //Originally was + 0.5F for i and k, removed so it wont spawn in a corner.
            EntityLightningBolt entitybolt = new EntityLightningBolt(world, (double)((float)i), (double)((float)j + 1.0F), (double)((float)k)); 

            //Server/Client check
            if (!world.isRemote)
            {
                world.spawnEntityInWorld(entitybolt);

                //100% OTIONAL! Was used to test player teleporting
                //Needs further work to be completely survival safe, ie no spawning in walls
                player.setPositionAndUpdate(i, j+1, k);
            }
        //Used for cooldown method below   
        stack.setItemDamage(100);
        }
	return stack;
}


//Cooldown method using onUpdate
@Override
public void onUpdate(ItemStack stack, World world, Entity entity, int i, boolean flag){

	//Needs to be here, otherwise doesnt work.
	if(!world.isRemote){
		//Cool down
		if(stack.getItemDamage() > 0){
			stack.damageItem(-1, (EntityLiving)entity);
		}
	}
}
}

 

 

On a final note, I'll tinker around with the code and see if I can get the bolt to actually render so the player can see it, and not just the effects of it. If anyone has any suggestions/input, I'm listening.

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.