Jump to content

[1.7.10]Fire Rate on guns


Jetfan16ladd

Recommended Posts

I am making a few guns in my mod and I want to know if it is possible to make them fire at a slower or faster fire rate. I also want to make a rpg that push's the player back a block when they shoot. I only currently have the rpg code.

 


public class RpgClass extends Item
{
   public RpgClass()
   {
     super();
   setMaxStackSize(1).setFull3D();
   }
   @Override
   public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World,EntityPlayer par3EntityPlayer) {
   ItemStack armourPiece0 = par3EntityPlayer.inventory.armorInventory[2];
  	   if (armourPiece0 != null && armourPiece0.getItem() == Mineturnedmain.DeathStrokeChestplate&&
  			   par3EntityPlayer.inventory.consumeInventoryItem(Mineturnedmain.RpgAmmo)||par3EntityPlayer.capabilities.isCreativeMode)
    		   
       {
           par2World.playSoundAtEntity(par3EntityPlayer, "random.bow", 0.5F, 0.4F / (itemRand.nextFloat() * 0.4F + 0.8F));
           if (!par2World.isRemote)
           {
               par2World.spawnEntityInWorld(new EntityRpg(par2World, par3EntityPlayer));
           }
       }
           return par1ItemStack;
       }
@SideOnly(Side.CLIENT)
public void registerIcons(IIconRegister reg){
	this.itemIcon = reg.registerIcon("morecraftingmod:rpg");
   } }

Link to comment
Share on other sites

You will need to add NBT to your ItemStack. After shooting, get NBT and set some "cooldown" to e.g 100 (5 sec). Then you can decrement it in onUpdate().

"Called each tick as long the item is on a player inventory."

if (cooldown <= 0) can shoot, else can't.

Something close to basics: http://www.minecraftforge.net/wiki/Creating_NBT_for_items

 

As to pushback - onItemRightClick(...) has EntityPlayer argument (user) you can simply use math to get his "back side" and move him by 1.0D. Note: do it on both server and client. To see similar stuff - search for knockback around entity damaging - ready-to-use code for finding push-back side.

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

Link to comment
Share on other sites

It's an item, only thing you can store in item is ItemStack's NBT (and meta ofc). Also - your idea of "saving NBT" is wrong. Something that I got confused too earlier - NBTs are java Maps, also note that either way ItemStack is sent from server to client, sending more packets is another weight for your connection. Also - you can't send pcket and expect shit to happen, you need to store packet-gotten info somewhere, so you would need IEEP.

Also - if you are storing cooldown in item its per-item cooldown, not a global one. Idk what's the idea here but i think it's more realistic to have 3 RPGs that have separate cooldown.

 

Next fact - ItemStack's NBT is NOT saved to HDD everytime you change it, it's either saved by world-saver or current-inventory-it-is-in saver. For players it happen fairly often (on SP everyime you pause game, on MP every "some" ticks or when you disconnect).

 

Next - TickHandler will cost more than smply saving stuff to ItemStack's NBT. And btw - using IEEP+packets to store global cooldowns will also save data (unless you don't want it to).

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

Link to comment
Share on other sites

I suggest:

1) On the client side, detect when the player is holding down the fire button.  You can do this by overriding Item.onEntitySwing (return true to stop further vanilla processing).

2) Send a packet to the server to start firing the gun.

3) On the server, you have a tick handler which is called 20 times per second.

4) When the "start firing" packet arrives at the server, fire a bullet every x ticks, where x depends on your desired firing rate.  Push the player backwards too.

5) When the client side detects that the player isn't holding down the fire button any more, send a "stop firing" packet to the server.

 

You'll probably need to tweak the logic a bit to get it how you want, but the basic idea should work.

 

-TGG

Link to comment
Share on other sites

What would happen when client will not send "stop firing" packet? For exaple, player starts firing and he is kicked from server / minecraft or computer is crashing.

 

I think sending packets with specific rate when player holds item&button is better. How vanilla does.

 

Link to comment
Share on other sites

What would happen when client will not send "stop firing" packet? For exaple, player starts firing and he is kicked from server / minecraft or computer is crashing.

for sure you would need to add some extra "error" detection to the basic idea.

 

I think sending packets with specific rate when player holds item&button is better. How vanilla does.

Probably the best is a hybrid.  Four times per second the client sends a packet.  Each packet says "fire a burst for 250s" or similar.

 

A bit of experimentation on a laggy server will show the best strategy from the player's point of view..

 

-TGG

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.