How do I find what block/coordinates the player is looking at?

Recommended Posts

I'm trying to make lightning sword (cliché, I know) that calls down lightning at the position the player is looking at on right click. I already have it really close to done, and I can call lightning down at a BlockPos (I've tested this by calling lightning on the player themselves).

So all of that works, I just need to find the coordinates the player is looking at from either a Player or ServerPlayer object

Share on other sites

Forgot to mention in the post. I am making this in 1.19.3

Share on other sites

some math you need

```	// #########################################################################3
// devuelve el punto en el espacio a la distancia especificada en la direccion
// hacia donde esta
// mirando el player
public Vec3 calcular_vo() {
return calcular_vo(this.vi, this.subject, this.distancia, this.precision);
}
public Vec3 calcular_vo(Vec3 vi, Entity player, int distancia, float precision) { // int distancia, float
// precision
// System.out.println( "calcularVectores(Vec3 " + vi + ", Entity player, int " +
// distancia + ", float " + precision + ")" );
// vi = player.getEyePosition(0.0F);
float gpitch = player.getViewXRot(0.0F);
float gyaw = player.getViewYRot(0.0F);
// precision = 0.0F => perfect aim
// precision = 1.0f => 45º ramdom error any direction
precision = (precision > 1.0F) ? 1.0F : ((precision < 0.0F) ? 0.0F : precision);
float factor = (90F * precision);
gpitch = (float) ((gpitch - (factor / 2)) + (factor * Math.random()));
gyaw = (float) ((gyaw - (factor / 2)) + (factor * Math.random()));
double tmpryaw = ryaw - 1.5707964D;
Vec3 vo = new Vec3(vi.x - ((Math.cos(tmpryaw)) * (Math.cos(rpitch) * distancia)),
vi.y - (Math.sin(rpitch) * distancia), vi.z - ((Math.sin(tmpryaw)) * (Math.cos(rpitch) * distancia)));
return vo;
}
// #########################################################################3
return (float) ((x * Math.PI) / 180F);
}

// #########################################################################3
return (float) ((x * 180F) / Math.PI);
}
// #########################################################################3
public static float fixGrad(float D) {
D %= 360.0F;
if (D < -179F) {
D += 360F;
}
if (D > 180F) {
D -= 360F;
}
return D;
}

```

Share on other sites

player.pick() - method is defined in the Entity class, does ray tracing for an entity

On the client, some of the calculations are cached for the player in the Minecraft class,

e.g. see how GameRenderer.pick() calculates Minecraft.hitResult or Minecraft.crosshairPickEntity

Boilerplate:

If you don't post your logs/debug.log we can't help you. For curseforge you need to enable the forge debug.log in its minecraft settings. You should also post your crash report if you have one.

If there is no error in the log file and you don't have a crash report then post the launcher_log.txt from the minecraft folder. Again for curseforge this will be in your curseforge/minecraft/Install

Large files should be posted to a file sharing site like https://gist.github.com  You should also read the support forum sticky post.

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.

Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.

×   Pasted as rich text.   Restore formatting

Only 75 emoji are allowed.

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×

• Posts

• idk how to reply here but no im not coding any mod, all of them are downloaded but its only gfs computer, on mine its ok and i was looking all around the internet and never seen anything similiar atleast
• I changed it to public static void but I can't find anyway to get level from the event even if it isn't a listener parameter. @JimiIT92
• I didn't yet see this happening on 1.20.2 forge, but sadly, I noticed that my mod is incompatible with this version because of this event handler: @SubscribeEvent public static void onPlayerRightClickItem(PlayerInteractEvent.RightClickItem e) { if (e.getItemStack().getItem() instanceof EggItem) { List<ThrownEgg> eggs = e.getLevel().getEntitiesOfClass(ThrownEgg.class, new AABB(e.getEntity().blockPosition().immutable() .below(5) .north(5) .west(5), e.getEntity().blockPosition().immutable() .above(10) .south(5) .east(5))); if (!eggs.isEmpty()) { eggs.forEach(ThrownEgg::discard); if (!e.getEntity().getAbilities().instabuild) e.getItemStack().grow(1); } if (!e.getEntity().level().isClientSide) { ((ServerPlayer) e.getEntity()).connection.send( new ClientboundStopSoundPacket(SoundEvents.EGG_THROW.getLocation(), SoundSource.PLAYERS)); // Minecraft Forge 1.20.2 crashes because of NoSuchMethodError here } e.setCancellationResult(InteractionResult.SUCCESS); e.setCanceled(true); } }
• It might just be a misleading log, if you run the command while debugging what's the outcome?  I've tried checking the item inside the RightClickBlock event and it works just fine private static final Ingredient FOOD_ITEMS = Ingredient.of(Items.WHEAT_SEEDS, Items.MELON_SEEDS, Items.PUMPKIN_SEEDS, Items.BEETROOT_SEEDS, Items.TORCHFLOWER_SEEDS, Items.PITCHER_POD); @SubscribeEvent public static void onRightClickBlock(final PlayerInteractEvent.RightClickBlock event) { final ItemStack itemStack = event.getItemStack(); LOGGER.info(FOOD_ITEMS.test(itemStack) + ""); } When I right click a block with an Item it checks if is one of the ingredients of FOOD_ITEMS. Clicking with an empty hand (air) logs false, as expected. Tip: please use the "spoiler" tags for long code snippets and don't merge all your classes into one code snippet EDIT: just noticed you are on 1.20.1. Maybe is just a bug with the Forge version? Try update to the latest Forge 1.20.2 and see if it still occurs

• Who's Online (See full list)

×

• Activity

×
• Create New...