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



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • They were already updated, and just to double check I even did a cleanup and fresh update from that same page. I'm quite sure drivers are not the problem here. 
    • i tried downloading the drivers but it says no AMD graphics hardware has been detected    
    • Update your AMD/ATI drivers - get the drivers from their website - do not update via system  
    • As the title says i keep on crashing on forge 1.20.1 even without any mods downloaded, i have the latest drivers (nvidia) and vanilla minecraft works perfectly fine for me logs: https://pastebin.com/5UR01yG9
    • Hello everyone, I'm making this post to seek help for my modded block, It's a special block called FrozenBlock supposed to take the place of an old block, then after a set amount of ticks, it's supposed to revert its Block State, Entity, data... to the old block like this :  The problem I have is that the system breaks when handling multi blocks (I tried some fix but none of them worked) :  The bug I have identified is that the function "setOldBlockFields" in the item's "setFrozenBlock" function gets called once for the 1st block of multiblock getting frozen (as it should), but gets called a second time BEFORE creating the first FrozenBlock with the data of the 1st block, hence giving the same data to the two FrozenBlock :   Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=head] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@73681674 BlockEntityData : id:"minecraft:bed",x:3,y:-60,z:-6} Old Block Fields set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=3, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} Frozen Block Entity set BlockState : Block{minecraft:black_bed}[facing=east,occupied=false,part=foot] BlockPos{x=2, y=-60, z=-6} BlockEntity : net.minecraft.world.level.block.entity.BedBlockEntity@6d1aa3da BlockEntityData : {id:"minecraft:bed",x:2,y:-60,z:-6} here is the code inside my custom "freeze" item :    @Override     public @NotNull InteractionResult useOn(@NotNull UseOnContext pContext) {         if (!pContext.getLevel().isClientSide() && pContext.getHand() == InteractionHand.MAIN_HAND) {             BlockPos blockPos = pContext.getClickedPos();             BlockPos secondBlockPos = getMultiblockPos(blockPos, pContext.getLevel().getBlockState(blockPos));             if (secondBlockPos != null) {                 createFrozenBlock(pContext, secondBlockPos);             }             createFrozenBlock(pContext, blockPos);             return InteractionResult.SUCCESS;         }         return super.useOn(pContext);     }     public static void createFrozenBlock(UseOnContext pContext, BlockPos blockPos) {         BlockState oldState = pContext.getLevel().getBlockState(blockPos);         BlockEntity oldBlockEntity = oldState.hasBlockEntity() ? pContext.getLevel().getBlockEntity(blockPos) : null;         CompoundTag oldBlockEntityData = oldState.hasBlockEntity() ? oldBlockEntity.serializeNBT() : null;         if (oldBlockEntity != null) {             pContext.getLevel().removeBlockEntity(blockPos);         }         BlockState FrozenBlock = setFrozenBlock(oldState, oldBlockEntity, oldBlockEntityData);         pContext.getLevel().setBlockAndUpdate(blockPos, FrozenBlock);     }     public static BlockState setFrozenBlock(BlockState blockState, @Nullable BlockEntity blockEntity, @Nullable CompoundTag blockEntityData) {         BlockState FrozenBlock = BlockRegister.FROZEN_BLOCK.get().defaultBlockState();         ((FrozenBlock) FrozenBlock.getBlock()).setOldBlockFields(blockState, blockEntity, blockEntityData);         return FrozenBlock;     }  
  • Topics

×
×
  • Create New...

Important Information

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