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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hi,  I'm using Forge 47.3.0 for Minecraft 1.20.1 I apologise if this is obvious I am very new to modding for Minecraft. I sucessfully made a mod that launched without errors or crashes (without it doing anything) but in order to add the features I need, I need to add "Custom Portal API [Forge]" as a dependency. However no matter the way I've tried to acheive this, it crashes. I am pretty sure it's not the way I'm putting it in the repositories, the dependencies or the way I'm refrencing it, as I've a hundred diffrent combinations and multiple Maven methods. And on all those diffrent variations I still get this crash: pastebin.com/UhumzZCZ Any tips would be invaluable as I've been loosing my mind over this!
    • Hi, i'm really having problems trying to set the texture to my custom item. I thought i'm doing everything correctly, but all i see is the missing texture block for my item. I am trying this for over a week now and getting really frustrated. The only time i could make the texture work, was when i used an older Forge version (52.0.1) for Minecraft (1.21.4). Was there a fundamental change for textures and models somewhere between versions that i'm missing? I started with Forge 54.1.0 and had this problem, so in my frustration i tried many things: Upgrading to Forge 54.1.1, created multiple new projects, workspaces, redownloaded everything and setting things up multiple times, as it was suggested in an older thread. Therea are no errors in the console logs, but maybe i'm blind, so i pasted the console logs to pastebin anyway: https://pastebin.com/zAM8RiUN The only time i see an error is when i change the models JSON file to an incorrect JSON which makes sense and that suggests to me it is actually reading the JSON file.   I set the github repository to public, i would be so thankful if anyone could take a look and tell me what i did wrong: https://github.com/xLorkin/teleport_pug_forge   As a note: i'm pretty new to modding, this is my first mod ever. But i'm used to programming. I had some up and downs, but through reading the documentation, using google and experimenting, i could solve all other problems. I only started modding for Minecraft because my son is such a big fan and wanted this mod.
    • Please read the FAQ (link in orange bar at top of page), and post logs as described there.
    • Hello fellow Minecrafters! I recently returned to Minecraft and realized I needed a wiki that displays basic information easily and had great user navigation. That’s why I decided to build: MinecraftSearch — a site by a Minecraft fan, for Minecraft fans. Key Features So Far Straight-to-the-Point Info: No extra fluff; just the essentials on items, mobs, recipes, loot and more. Clean & Intuitive Layout: Easy navigation so you spend less time scrolling and more time playing. Optimized Search: Search for anything—items, mobs, blocks—and get results instantly. What I’m Thinking of Adding More data/information: Catch chances for fishing rod, traveling villager trades, biomes info and a lot more. The website is still under development and need a lot more data added. Community Contributions: Potential for user-uploaded tips for items/mobs/blocks in the future. Feature Requests Welcome: Your ideas could shape how the wiki evolves! You can see my roadmap at the About page https://minecraftsearch.com/about I’d love for you to check out MinecraftSearch and see if it helps you find the info you need faster. Feedback is crucial—I want to develop this further based on what the community needs most, so please let me know what you think. Thanks, and happy crafting!
    • Instructions on how to install newer Java can be found in the FAQ
  • Topics

×
×
  • Create New...

Important Information

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