Jump to content

[1-15] doing something when selected


matt1999rd

Recommended Posts

Hello everyone,

in my mod I need to do some action when selected by entity using ISelectionContext and EntitySelectionContext I try to do it with getShape but it is not called when the player is selecting the block and when the voxels appear.

Is there a another way to do this as i just wanted the block to detect the player when it is in front of the block?

 

 

Link to comment
Share on other sites

Hi 

The DrawHighlightEvent might help you, if you only want to do this on the client side.  It is the event called when Minecraft draws the box around the block or entity you're currently looking at.

 

If you need it on the server side, you could perhaps perform a raytrace yourself on the server, say in a PlayerTick event.  I'm not 100% sure that will work because I'm not certain that the server knows which way the player is facing.

If not, and your server needs to know, you could use the DrawHighlightEvent and send a custom packet to the Server every time a new block is highlighted.

 

Easier: if you don't care whether the player is looking at the block, just whether the player is near to the block, then you could use the PlayerTick event and check within a radius of the player to see if there are any of your block nearby.

 

-TGG

Link to comment
Share on other sites

20 minutes ago, TheGreyGhost said:

Hi 

The DrawHighlightEvent might help you, if you only want to do this on the client side.  It is the event called when Minecraft draws the box around the block or entity you're currently looking at.

 

If you need it on the server side, you could perhaps perform a raytrace yourself on the server, say in a PlayerTick event.  I'm not 100% sure that will work because I'm not certain that the server knows which way the player is facing.

If not, and your server needs to know, you could use the DrawHighlightEvent and send a custom packet to the Server every time a new block is highlighted.

 

Easier: if you don't care whether the player is looking at the block, just whether the player is near to the block, then you could use the PlayerTick event and check within a radius of the player to see if there are any of your block nearby.

 

-TGG

Good idea with the raytrace, 1.13 and above implemented a new ray-casting system with command blocks, if you can do this with commands, Im sure its easily possible with mods too. *shrug*

Link to comment
Share on other sites

Ok this function looks interesting but in fact I don't know how to implement the event and what I really want to avoid is using method IEntityReader#getClosestPlayer because it is searching amongst all player and this is done in each tick which will lower the speed of minecraft

Link to comment
Share on other sites

If it's your own block, you can override Block#getShape:

  @Override
  public VoxelShape getShape(BlockState state, IBlockReader worldIn, BlockPos pos, ISelectionContext context) {
    if(context instanceof EntitySelectionContext) {
      Entity e = context.getEntity();
      if (e != null) {
        // Perform your logic
      }
    }
    return super.getShape(state, worldIn, pos, context);
  }

 

Edited by Alpvax
misspelling
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.

Announcements



×
×
  • Create New...

Important Information

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