Jump to content

Opinions on this entity spawning item and some questions. (1.19)


Recommended Posts

For now it's working as intended. What are your opinions on the code?


- How could things be done better? Let me know what you think.

- Am i right in assuming ClipContext is for ray tracing?

- I couldn't find the RayTracingContext, it took me some time of digging around blind to finally stumble upon this ClipContext class. How could i have investigated the topic faster? By digging through minecraft and other mods? By asking you here? 

package net.ja.testmod.item;

import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResultHolder;
import net.minecraft.world.entity.EntityType;
import net.minecraft.world.entity.LightningBolt;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.level.ClipContext;
import net.minecraft.world.level.Level;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.phys.BlockHitResult;
import net.minecraft.world.phys.Vec3;

public class ThunderWand extends Item {
    public ThunderWand(Item.Properties properties){

    public InteractionResultHolder<ItemStack> use(Level level, Player player, InteractionHand interactionHand) {

        //if main hand, lightningbolt at crosshair
        if(interactionHand == InteractionHand.MAIN_HAND) {
            BlockHitResult result = getCrosshairLocation(level, player, 50f);
            Vec3 location = correctLightningBoltSpawnHeight(level, result);
            spawnLightningBoltAt(level, location);

        //if offhand, lightningbolt at player
        if(interactionHand == InteractionHand.OFF_HAND) spawnLightningBoltAtPlayer(level, player);

        return super.use(level, player, interactionHand);

    public Vec3 correctLightningBoltSpawnHeight(Level level, BlockHitResult prevResult){

        //avoid spawning lightningbolts in the air, spawn them straight down instead
        BlockState state = level.getBlockState(prevResult.getBlockPos());
            Vec3 down = new Vec3(0, -1, 0);
            ClipContext cc = new ClipContext(prevResult.getLocation(), prevResult.getLocation().add(down.scale(500)), ClipContext.Block.COLLIDER, ClipContext.Fluid.ANY, null);
            BlockHitResult result = level.clip(cc);

            return result.getLocation();

        return prevResult.getLocation();

    public BlockHitResult getCrosshairLocation(Level level, Player player, float distance){
        Vec3 view = player.getViewVector(0f);
        Vec3 from = player.getEyePosition();
        Vec3 to = from.add(view.scale(distance));

        ClipContext cc = new ClipContext(from, to, ClipContext.Block.COLLIDER, ClipContext.Fluid.ANY, null);
        BlockHitResult result = level.clip(cc);

        return result;

    public void spawnLightningBoltAt(Level level, Vec3 location){
            LightningBolt lb = new LightningBolt(EntityType.LIGHTNING_BOLT, level);


    public void spawnLightningBoltAtPlayer(Level level, Player player){
            LightningBolt lb = new LightningBolt(EntityType.LIGHTNING_BOLT, level);



Link to comment
Share on other sites

what are trying to achive op 
to understand the raytrace you have to look into te abstrab arrow class 

mostly is the same but they change the names of things  

package net.minecraft.world.phys.HitResult;

i do this when i want to search things ignoring liquids but stoping on solid blocks 

        this.hitresult = this.warudo.clip(new ClipContext(this.vi, this.vo, ClipContext.Block.COLLIDER, ClipContext.Fluid.NONE, this.player_entity));





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.

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.

  • Create New...

Important Information

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