Jump to content

[1.8.x] Shooting Custom Bullets while riding mobs


Darkflame

Recommended Posts

Hi!

 

I've been working on a Rideable Mech mod, and I looked into making the  battle mech shoot. I looked at some gun tutorials, and followed them. So I have a gun Item. However, I tried to make it so when the person was riding the mech, they would be able to right click and the bullet entity would be created, and work as if it was fired from a gun. Naturally, this didn't work. How could it work? Maybe if I made an item that only was able to create the bullet entity when I was riding the mech? but how would I make it recognize that I was in the mech?  :-\ Help Please! D:>

 

Here's my mob class:

package com.mech.entity;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.SharedMonsterAttributes;
import net.minecraft.entity.monster.EntityMob;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.MathHelper;
import net.minecraft.world.World;

public class BattleMechMob extends EntityMob {

public BattleMechMob(World worldIn) {
	super(worldIn);
	// TODO Auto-generated constructor stub
}

public boolean interact(EntityPlayer entityplayer)
  {
    if (riddenByEntity == null || riddenByEntity == entityplayer)
    {
      entityplayer.mountEntity(this);
      
      return true;
    }
    else
    {
      return false;
    }
  }

public void moveEntityWithHeading(float p_70612_1_, float p_70612_2_)
{
if (this.riddenByEntity != null && this.riddenByEntity instanceof EntityLivingBase)
{
this.prevRotationYaw = this.rotationYaw = this.riddenByEntity.rotationYaw;
this.rotationPitch = this.riddenByEntity.rotationPitch * 0.5F;
this.setRotation(this.rotationYaw, this.rotationPitch);
this.rotationYawHead = this.renderYawOffset = this.rotationYaw;
//this.posY = this.rotationPitch; new addition
//this.posY = ((EntityLivingBase)this.riddenByEntity).moveFlying(p_70612_1_, p_70612_2_, );
//this.posY = ((EntityLivingBase)this.riddenByEntity).moveFlying(strafe, forward, friction);
p_70612_1_ = ((EntityLivingBase)this.riddenByEntity).moveStrafing * 0.1F; //initial value 0.5F
p_70612_2_ = ((EntityLivingBase)this.riddenByEntity).moveForward * 0.50F; //Forward move speed

if (p_70612_2_ <= 0.0F)
{
  p_70612_2_ *= 0.25F; //Reverse move speed
}

this.stepHeight = 1.0F;
this.jumpMovementFactor = this.getAIMoveSpeed() * 0.1F;

if (!this.worldObj.isRemote)
{
  this.setAIMoveSpeed((float)this.getEntityAttribute(SharedMonsterAttributes.movementSpeed).getAttributeValue());
  super.moveEntityWithHeading(p_70612_1_, p_70612_2_);
}

this.prevLimbSwingAmount = this.limbSwingAmount;
double d1 = this.posX - this.prevPosX;
double d0 = this.posZ - this.prevPosZ;
float f4 = MathHelper.sqrt_double(d1 * d1 + d0 * d0) * 4.0F;

if (f4 > 1.0F)
{
  f4 = 1.0F;
}

this.limbSwingAmount += (f4 - this.limbSwingAmount) * 0.4F;
this.limbSwing += this.limbSwingAmount;
}
else
{
this.stepHeight = 0.5F;
this.jumpMovementFactor = 0.02F;
super.moveEntityWithHeading(p_70612_1_, p_70612_2_);
}
}

}

 

Here's my gun class:

package com.mech.item;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource;
import net.minecraft.world.World;

import com.mech.entity.EntityBlasterBolt;

public class ItemBlasterRifle extends Item
{
  public ItemBlasterRifle()
  {
    super();
    setCreativeTab(CreativeTabs.tabCombat);
    setUnlocalizedName("blasterRifle");
  }
  
  @Override
  public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World,EntityPlayer par3EntityPlayer) {
      if(par3EntityPlayer.capabilities.isCreativeMode||par3EntityPlayer.inventory.consumeInventoryItem(Items.redstone))
      {
          par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
          if (!par2World.isRemote)
          {
              par2World.spawnEntityInWorld(new EntityBlasterBolt(par2World, par3EntityPlayer));
          }
          
      }
          return par1ItemStack;
  }
}

 

And here's my bullet class:

package com.mech.entity;

import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.projectile.EntityThrowable;
import net.minecraft.util.DamageSource;
import net.minecraft.util.EnumParticleTypes;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.world.World;

public class EntityBlasterBolt extends EntityThrowable
{
    private float explosionRadius = 1.5F;
public EntityBlasterBolt(World par1World)
    {
        super(par1World);
    }
    public EntityBlasterBolt(World par1World, EntityLivingBase par2EntityLivingBase)
    {
        super(par1World, par2EntityLivingBase);
    }
    public EntityBlasterBolt(World par1World, double par2, double par4, double par6)
    {
        super(par1World, par2, par4, par6);
    }
    
    @Override
    protected void onImpact(MovingObjectPosition par1MovingObjectPosition) 
    {
    	if(true){
    		this.attackEntityFrom(DamageSource.generic, 100);
    	}
        this.worldObj.createExplosion(this, this.posX, this.posY, this.posZ, (float)this.explosionRadius , false);
        this.setDead();
    }
    
    public void onUpdate()
    {
    	super.onUpdate();
    	this.worldObj.spawnParticle(EnumParticleTypes.CRIT_MAGIC, this.posX, this.posY, this.posZ, 100.0D, 100.0D, 100.0D, new int[100]);

    }

}

 

Thank you to anyone that helps <:3

I. Am. A. Noob.

Noobs. :P

Link to comment
Share on other sites

i didnt read much of the post tbh but i found youre using a method called "onRightClickItem" and i would use this one ?

public boolean onItemUse(ItemStack is, EntityPlayer eplayer, World world, BlockPos pos, EnumFacing facing, float f1, float f2, float f3)
{
return false;
}

Link to comment
Share on other sites

Break your problem down into smaller problems, and describe it in more detail.

 

Do you want the player to shoot the bullet, or the mech?

 

Can the mech shoot on its own, or does it only fire when controlled by a player?

 

Do you really want the player to have to be holding a certain item to shoot while riding the mech, or would you rather it just happens when the player right clicks like they entered some commands on the mech's control panel?

 

Always explain EXACTLY what you want to happen and how you want it to happen so people can help you more effectively.

Link to comment
Share on other sites

Break your problem down into smaller problems, and describe it in more detail.

 

Do you want the player to shoot the bullet, or the mech?

 

Can the mech shoot on its own, or does it only fire when controlled by a player?

 

Do you really want the player to have to be holding a certain item to shoot while riding the mech, or would you rather it just happens when the player right clicks like they entered some commands on the mech's control panel?

 

Always explain EXACTLY what you want to happen and how you want it to happen so people can help you more effectively.

 

I want to make the bullet shoot when the player is riding the mech, and he right clicks.

I. Am. A. Noob.

Noobs. :P

Link to comment
Share on other sites

You didn't answer all questions, and yes - they are all important and will decide how to code it.

 

If the mech would shoot, the mech will be the one spawning enitity, if it's player - it would be player.

Both would trigger differently. Does mech have same rotation as player? What are height differences? Do you want to shoot from mech's arm, or center?

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

To shoot on right-click, you will need to use Events, specifically the MouseEvent and KeyInputEvent (to handle keyUseItem in the case that it is bound to a key other than the mouse).

 

Both of those events are CLIENT only, meaning you will have to send a packet to the server letting it know that the player right-clicked; from there, perform all necessary checks (e.g. is player riding mech? can he shoot this tick? etc.) and spawn the bullet based on decisions you make in response to Ernio's reply.

 

Just be aware that if you have never used Events or Packets before, it can be a daunting task as it seems far more complex at first than it actually is. Once you get the hang of them, they really are quite simple.

Link to comment
Share on other sites

To shoot on right-click, you will need to use Events, specifically the MouseEvent and KeyInputEvent (to handle keyUseItem in the case that it is bound to a key other than the mouse).

 

Both of those events are CLIENT only, meaning you will have to send a packet to the server letting it know that the player right-clicked; from there, perform all necessary checks (e.g. is player riding mech? can he shoot this tick? etc.) and spawn the bullet based on decisions you make in response to Ernio's reply.

 

Just be aware that if you have never used Events or Packets before, it can be a daunting task as it seems far more complex at first than it actually is. Once you get the hang of them, they really are quite simple.

 

I followed the event tutorial, but what event should I use? I can't use the interact event because that would shoot a bullet every time I mounted the mob.

I. Am. A. Noob.

Noobs. :P

Link to comment
Share on other sites

Client side:

1. Subscribe MouseEvent

2. Check if mouse event == left/right click.

3. Check if you are mounting mech entity

4. If yes - send packet to server using SimpleNetworkWrapper with simple "action ID", nothing more, nothing less (it can even be empty packet in this case).

Server side:

1. Receive and hanlde packet:

- Check AGAIN if player who send packet is mounting mech.

- Check any other requirements (server decides if you can shoot).

- Get that mech, use math to calculate arm position.

- Offset created bullet entity to position of hand.

- Spawn new bullet entity with some velocity at mech's entity (entities have constructor taking entity-to-spawn-on argument)

- BOOM, you spawned bullet from hand of mech!

1.7.10 is no longer supported by forge, you are on your own.

Link to comment
Share on other sites

To shoot on right-click, you will need to use Events, specifically the MouseEvent and KeyInputEvent (to handle keyUseItem in the case that it is bound to a key other than the mouse).

 

Both of those events are CLIENT only, meaning you will have to send a packet to the server letting it know that the player right-clicked; from there, perform all necessary checks (e.g. is player riding mech? can he shoot this tick? etc.) and spawn the bullet based on decisions you make in response to Ernio's reply.

 

Just be aware that if you have never used Events or Packets before, it can be a daunting task as it seems far more complex at first than it actually is. Once you get the hang of them, they really are quite simple.

 

I followed the event tutorial, but what event should I use? I can't use the interact event because that would shoot a bullet every time I mounted the mob.

I have now put the events needed in bold. Please read more carefully in the future.

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.