Jump to content

I need help making my arrows destroy the blocks they hit


cooperwl1

Recommended Posts

For my computer science class I'm making a Fortnite Minecraft copy, I am making a gun that is pretty simple and I want the gun to destroy a certain block type which I make myself when the arrow from the gun hits the block. How would  I go about doing this? 

 

This is my code so far

 

GUN CODE

 

package tealsmc.mods.items;

import java.util.Random;

import cpw.mods.fml.common.eventhandler.SubscribeEvent;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.projectile.EntityArrow;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.event.entity.player.ArrowNockEvent;

public class FortnitePistol extends Item {
    
    public FortnitePistol() {
        setCreativeTab(CreativeTabs.tabTools);
        this.setFull3D();
        this.setMaxStackSize(1);
        this.setMaxDamage(10);
    }
    
    
    
    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer) {
        
        EntityArrow var8 = new EntityArrow(par2World, par3EntityPlayer, 1.0F);
        
        par2World.spawnEntityInWorld(var8);
                 
        
        
        return par1ItemStack;
        
    }
   
    
}
 

 

BLOCK CODE

 

package tealsmc.mods.blocks;


import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;

public class FortniteWood extends Block{
    
    public FortniteWood(Material mat) {
        super(mat);
        setCreativeTab(CreativeTabs.tabBlock);
    }
    
    
    
}
 

 

 

 

I am also using the TEALS educational modpack but it is just Forge, just to clear up any confusion on the package.

Link to comment
Share on other sites

Don't take this too seriously, but I'd go for the following method:

 

@SubscribeEvent
    public void regiterItems(ProjectileImpactEvent event) {
        if (event.getRayTraceResult().typeOfHit == RayTraceResult.Type.BLOCK){
            //Destroy block here
        }
    }

This would be the best event to go for, in my opinion.

 

EDIT:

 

For the block destruction, I'd try 

event.getEntity().getEntityWorld().setBlockToAir(event.getRayTraceResult().getBlockPos());

But I haven't tested it. I'm not even sure if event.getEntity() returns the player that shot the arrow....

Edited by RekTek249
Link to comment
Share on other sites

  • Guest locked this topic
Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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