Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

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

Featured Replies

Posted

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){
        super(properties);
    }


    @Override
    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);

        //super
        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());
        if(state.isAir()){
            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){
        if(!level.isClientSide()){
            LightningBolt lb = new LightningBolt(EntityType.LIGHTNING_BOLT, level);
            lb.setPos(location);
            level.addFreshEntity(lb);
        }
    }

    //Minecraft.getInstance().player.chat('msg');

    public void spawnLightningBoltAtPlayer(Level level, Player player){
        if(!level.isClientSide()){
            LightningBolt lb = new LightningBolt(EntityType.LIGHTNING_BOLT, level);
            lb.setPos(player.getPosition(0f));
            level.addFreshEntity(lb);
        }
    }

}

 

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));

 

 

 

 

  • Author
2 hours ago, perromercenary00 said:

to understand the raytrace you have to look into te abstrab arrow class 

I do understand raytracing.

And i think it's pretty clear what i want to achieve (which i've done) the comments are clear.

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...

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.