Jump to content

How I can know which block I'm pointing with the mouse?


Recommended Posts

Posted

From the method onItemRightClick I want to change the block I'm pointing the cursor. From where I can get the coordinates of that block?

 

I've tried from EntityPlayer, but all I found was the method getLookvec(), but gives me values ​​that I do not understand

 

Thank you very much in advance

 

 

Posted

Thanks for your help Atrain99, but this method look for 2 args, distance and partialTickTime and I don't have the distance.

 

I can calculate the distance to a Entity but not to a Block.

 

I'll keep looking for any clues :) Thanks a lot.

 

(Sorry. I not speak English and is very difficult for me to explain properly. I'm from Spain)

 

EDIT:

I found in Minecraft class a object called "objectMouseOver". This object have 3 variables called blockX, blockY and blockZ. This object stores the coordinates of the block where the mouse are.

 

To acces to this object, I create a Minecraft instance in my item class, like this:

public class ItemCreator extends Item
{
@Instance
private Minecraft instance;

protected ItemCreator(int id) 
{
	super(id);

}

    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer player)
    {
    	if(Minecraft.getMinecraft().objectMouseOver!=null)
    	{
    		int xm=Minecraft.getMinecraft().objectMouseOver.blockX;
    		int ym=Minecraft.getMinecraft().objectMouseOver.blockY;
    		int zm=Minecraft.getMinecraft().objectMouseOver.blockZ;   		
    	
    		player.addChatMessage("X: "+xm+" Y:"+ym+"Z: "+zm);
    	}

    	return par1ItemStack;
}

 

It's the good way to do it or I,m create a monster?

Posted

You aren't creating a Minecraft instance, you are just getting the singleton instance. It's also a bit silly that you have a variable to hold the minecraft instance but then you never assign or use it ;).

Posted

You aren't creating a Minecraft instance, you are just getting the singleton instance. It's also a bit silly that you have a variable to hold the minecraft instance but then you never assign or use it ;).

 

The code is only a example to view the result. I'm only learning about mods development with Forge.

 

My intention is that when using an object on a block, that block is a reference to create a structure. Then I need the values ​​x, y and z of the block to which I am pointing the cursor and objectMouseOver gives me these values​​.

 

My question is if I'm using the correct method. Thanks for your answer :)

Posted

You aren't creating a Minecraft instance, you are just getting the singleton instance. It's also a bit silly that you have a variable to hold the minecraft instance but then you never assign or use it ;).

 

The code is only a example to view the result. I'm only learning about mods development with Forge.

 

My intention is that when using an object on a block, that block is a reference to create a structure. Then I need the values ​​x, y and z of the block to which I am pointing the cursor and objectMouseOver gives me these values​​.

 

My question is if I'm using the correct method. Thanks for your answer :)

It's rayTrace(distance to scan for blocks, and I don't know what this arg is);

So, what would happen if I did push that shiny red button over there? ... Really? ... Can I try it? ... Damn.

Posted

It's rayTrace(distance to scan for blocks, and I don't know what this arg is);

 

    @SideOnly(Side.CLIENT)

    /**
     * Performs a ray trace for the distance specified and using the partial tick time. Args: distance, partialTickTime
     */
    public MovingObjectPosition rayTrace(double par1, float par3)
    {
        Vec3 var4 = this.getPosition(par3);
        Vec3 var5 = this.getLook(par3);
        Vec3 var6 = var4.addVector(var5.xCoord * par1, var5.yCoord * par1, var5.zCoord * par1);
        return this.worldObj.rayTraceBlocks(var4, var6);
    }

 

The second argument is partialTickTime. I do not know how to get those values. I will do some tests with this method.

 

Regards

Posted

Items can use MovingObjectPosition movingobjectposition =  this.getMovingObjectPositionFromPlayer(world, player, true);

movingobjectposition should have the info you need.

 

The downfall is, its limited to the players reach.

But using it as an example you can easily make one where you set the reach. (and allow its use outside of the Item class)

 

Posted

var3.rayTrace(200, 1.0F).blockX

 

var3 is entityplayer

 

200 is "reach" so if you want you can have the raytrace work for let's say 100 blocks, change that to 100

 

edit: I'm awful at explaining, so here's my code, it's easy to figure out

(I'm telling minecraft to send a packet to the location I'm looking at with the variables positionx positiony positionz and strength)

 

            if (var2.currentScreen == null && !Keyboard.isKeyDown(pulseboltKey) && pulseboltKeyDown)
            {
            	this.sendExplosion((int)var3.rayTrace(200, 1.0F).blockX, (int)var3.rayTrace(200, 1.0F).blockY, (int)var3.rayTrace(200, 1.0F).blockZ, 6);
            	this.explosionTimer = 0;
            }

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.