Jump to content

How to make a weapon [gun] fire faster than default speed?


HappleAcks

Recommended Posts

  • Replies 50
  • Created
  • Last Reply

Top Posters In This Topic

Hi

 

Do you mean - the bullets fire more frequently, or the bullets travel faster?

 

-TGG

More frequently, I already have them moving fast/not affected by gravity. :) 

 

- I do have a problem with slower firing weapons though, where you need to hold down the right mouse button for a second before it fires. I think this is because I set it so that it reduces the cool-down on right click, but not while just sitting there.

Link to comment
Share on other sites

Is it just a singular item that's shooting it, or a consumable? If it's a singluar item then if the shooting is happening in the items onUsingTick method I think that's the fastest it can get, seeing it's called every tick and you can't really do anything between ticks... (Could be wrong though)

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

Is it just a singular item that's shooting it, or a consumable? If it's a singluar item then if the shooting is happening in the items onUsingTick method I think that's the fastest it can get, seeing it's called every tick and you can't really do anything between ticks... (Could be wrong though)

It's a singular item that is consuming a different item. Also, it currently seems to be once/tick, but I've seen mods where mobs can shoot like an arrow every 1/4 of a tick just at ridiculous rates so I was wondering if that is possible to transfer to a weapon.

Link to comment
Share on other sites

80 times a second? What do you need that kind of speed for... I think I have a workaround solution though.... Say you want to make it fire every half tick, you could make it spawn 2 nearly identical projectiles every tick, one of which would have special code in it's onupdate meathod to move it forward half the difference it would travel in a tick as soon as it spawns, staggering them. This technique could be modified for any RoF you need.

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

80 times a second? What do you need that kind of speed for... I think I have a workaround solution though.... Say you want to make it fire every half tick, you could make it spawn 2 nearly identical projectiles every tick, one of which would have special code in it's onupdate meathod to move it forward half the difference it would travel in a tick as soon as it spawns, staggering them. This technique could be modified for any RoF you need.

Oh, is that how long a tick is? My current fire rate is like 1 shot every half second.

Link to comment
Share on other sites

Yep, if post your item class here I might be able to give you a better solution

 

An example of a weapon firing default speed that I want to make fire faster:

 

 

 

public class Rifle extends ItemSword{

 

  public Rifle(int i, ToolMaterial p_i45356_1_){

super(p_i45356_1_);

setFull3D();

    this.setCreativeTab(CreativeTabs.tabCombat);

  }

 

  public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World,EntityPlayer par3EntityPlayer) {

  if(par3EntityPlayer.capabilities.isCreativeMode||par3EntityPlayer.inventory.consumeInventoryItem(darkMod.bullet))

  {

    par2World.playSoundAtEntity(par3EntityPlayer, "darkmod:gun", 1.0F, 1.0F);

    if (!par2World.isRemote)

    {

      par2World.spawnEntityInWorld(new EntityBullet(par2World, par3EntityPlayer, 15.0F));

    }

  }

  return par1ItemStack;

  }

 

 

}

 

 

 

An example of my method for making a weapon fire slower:

 

 

public class Sniper extends ItemSword{

private int firetick;

private int firemax;

 

  public Sniper(int i, ToolMaterial p_i45356_1_){

super(p_i45356_1_);

setFull3D();

    this.firemax = 4;

    this.firetick = this.firemax;

    this.setCreativeTab(CreativeTabs.tabCombat);

  }

 

  public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World,EntityPlayer par3EntityPlayer) {

  if(!par2World.isRemote) {

if (this.firetick == this.firemax && this.firemax != 0) {

if(par3EntityPlayer.capabilities.isCreativeMode||par3EntityPlayer.inventory.consumeInventoryItem(darkMod.bullet)){

par2World.playSoundAtEntity(par3EntityPlayer, "darkMod:Sniper", 1.0F, 1.0F);

par2World.spawnEntityInWorld(new EntityBullet(par2World, par3EntityPlayer, 10.0F));

}

 

this.firetick = 0;

} else {

++this.firetick;

}

if (this.firemax == 0) {

                        if(par3EntityPlayer.capabilities.isCreativeMode||par3EntityPlayer.inventory.consumeInventoryItem(darkMod.bullet)){

par2World.playSoundAtEntity(par3EntityPlayer, "darkMod:Sniper", 1.0F, 1.0F);

par2World.spawnEntityInWorld(new EntityBullet(par2World, par3EntityPlayer, 10.0F));

}

}

   

  }

  return par1ItemStack;

  }

 

  @Override

public void onPlayerStoppedUsing(ItemStack var1, World var2, EntityPlayer var3, int var4) {

this.firetick = this.firemax;

}

 

 

 

 

Link to comment
Share on other sites

Huh, I thought that ran every tick... Well there is another method, but this one is more complicated. You have to use a tickhandler to check if your player is holding your item and right clicking, and if so spawn the bullet and do the stuff. Here's a snippit of code as an example (this would be in your tickhandler):

//Make sure the player is holding an item
if (player.getHeldItem() != null) {
                                 //Make sure that item is your item
			if (player.getHeldItem().getItem() instanceof YOURITEM) {
                                       //Cast the held item to your item so you can do stuff with it
				youritem = (YOURITEM) player.getHeldItem().getItem();


				if(Mouse.isButtonDown(1)){

					//Spawn the bullets here, logic to wait X ticks between shots goes here too
                                                //You also have access to the instance of your item the player is holding here, so you
                                                //can modify variables and such
				}
          }
}

 

 

EDIT: It MIGHT be a little buggy depending on your setup, should work though

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

Note that you cannot check mouse and keyboard states on the server, and they are thus not suitable for spawning entities without sending packets, since entities must spawn on the server.

 

Besides, the using item method already IS a tick handler, being called every single tick while the item is in use, and should be the preferred method if you want a fast rate of fire (20 bullets per second, about). On right click, you need to set your item in use and make sure to give it a max item use duration, then in the update tick spawn an entity every tick, every other tick, or however many ticks you want.

 

@Override
public void onUsingTick(ItemStack stack, EntityPlayer player, int count) {
// count starts at the max duration and counts down
int ticksInUse = getMaxItemUseDuration(stack) - count;

// this will fire every 2 ticks; change or remove to suit your needs
if (ticksInUse % 2 == 0) {
// consume bullet and spawn projectile on server
}
}

If you want faster than one bullet per tick, you can use a render tick handler/event and send a packet to the server each render tick to consume and spawn the bullet, but I think 20 per second should be plenty fast.

Link to comment
Share on other sites

Doesn't that slow the player down while they're firing though? Not sure if that's be a problem, just wondering... Also I have a setup that uses the code I showed to spawn entities and it works (it's a bit erratic though and only seems to run through the code completely half the time, but I don't think it's because of that.... Could be wrong though, I suck at this :P)

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

Note that you cannot check mouse and keyboard states on the server, and they are thus not suitable for spawning entities without sending packets, since entities must spawn on the server.

 

Besides, the using item method already IS a tick handler, being called every single tick while the item is in use, and should be the preferred method if you want a fast rate of fire (20 bullets per second, about). On right click, you need to set your item in use and make sure to give it a max item use duration, then in the update tick spawn an entity every tick, every other tick, or however many ticks you want.

 

@Override
public void onUsingTick(ItemStack stack, EntityPlayer player, int count) {
// count starts at the max duration and counts down
int ticksInUse = getMaxItemUseDuration(stack) - count;

// this will fire every 2 ticks; change or remove to suit your needs
if (ticksInUse % 2 == 0) {
// consume bullet and spawn projectile on server
}
}

If you want faster than one bullet per tick, you can use a render tick handler/event and send a packet to the server each render tick to consume and spawn the bullet, but I think 20 per second should be plenty fast.

This confused me a little bit when it came to the implementation, since I haven't worked with ticks much. Could you clarify how to implement this code?

Link to comment
Share on other sites

Note that you cannot check mouse and keyboard states on the server, and they are thus not suitable for spawning entities without sending packets, since entities must spawn on the server.

 

Besides, the using item method already IS a tick handler, being called every single tick while the item is in use, and should be the preferred method if you want a fast rate of fire (20 bullets per second, about). On right click, you need to set your item in use and make sure to give it a max item use duration, then in the update tick spawn an entity every tick, every other tick, or however many ticks you want.

 

@Override
public void onUsingTick(ItemStack stack, EntityPlayer player, int count) {
// count starts at the max duration and counts down
int ticksInUse = getMaxItemUseDuration(stack) - count;

// this will fire every 2 ticks; change or remove to suit your needs
if (ticksInUse % 2 == 0) {
// consume bullet and spawn projectile on server
}
}

If you want faster than one bullet per tick, you can use a render tick handler/event and send a packet to the server each render tick to consume and spawn the bullet, but I think 20 per second should be plenty fast.

This confused me a little bit when it came to the implementation, since I haven't worked with ticks much. Could you clarify how to implement this code?

Still waiting on this, since I don't really know how to implement it along with my current weapon code.

Link to comment
Share on other sites

This confused me a little bit when it came to the implementation, since I haven't worked with ticks much. Could you clarify how to implement this code?

That was the entire implementation, aside from consuming the bullet and spawning the entity; what more do you need? It's a method in the Item class, just put it in your rifle class and add what you need. 20 ticks is roughly one second.

Link to comment
Share on other sites

This confused me a little bit when it came to the implementation, since I haven't worked with ticks much. Could you clarify how to implement this code?

That was the entire implementation, aside from consuming the bullet and spawning the entity; what more do you need? It's a method in the Item class, just put it in your rifle class and add what you need. 20 ticks is roughly one second.

Well I tried implementing according to the comments left, and when I right click while holding the rifle it just holds it on a weird angle and doesn't fire anything.

 

The code - @Override had to be removed to allow me to use the World parameter to create the entity.

 

 

  public void onUsingTick(ItemStack par1ItemStack, EntityPlayer player, World par2World, int count) {

  // count starts at the max duration and counts down

  int ticksInUse = getMaxItemUseDuration(par1ItemStack) - count;

 

  // this will fire every 2 ticks; change or remove to suit your needs

  if (ticksInUse % 2 == 0) {

  if(player.capabilities.isCreativeMode||player.inventory.consumeInventoryItem(darkMod.bullet))

  {

  par2World.playSoundAtEntity(player, "darkMod:Rifle", 1.0F, 1.0F);

    if (!par2World.isRemote)

    {

      par2World.spawnEntityInWorld(new EntityBullet(par2World, player, 9.0F));

    }

  }

  // consume bullet and spawn projectile on server

  }

 

 

Link to comment
Share on other sites

Anytime you elect to remove an @Override annotation, you break your code. Seriously, is this not posted multiple times on these boards?

If you have to remove that, it means you are no longer overriding any superclass method, and your method is likely never to be called or used at all. The superclass method will still be called in every case that you don't specifically call your method on an instance of your object.

 

Overriding methods is basic java. If you do not understand how to override a method, learn java first. The IDE does not teach java and it will only offer to remove syntax errors, it cannot "fix" your code or even figure out what you want to do.

 

 

All methods in java have a signature. Multiple methods can have the exact same names because the parameter (argument) types and number of them determine which is the correct method.

If you override a method and then change any parameter type or add or remove parameters, you no longer override that method. You just created a completely independent method with the same name.

 

 

Link to comment
Share on other sites

Well I tried implementing according to the comments left, and when I right click while holding the rifle it just holds it on a weird angle and doesn't fire anything.

 

The code - @Override had to be removed to allow me to use the World parameter to create the entity.

Anytime you elect to remove an @Override annotation, you break your code. Seriously, is this not posted multiple times on these boards?

If you have to remove that, it means you are no longer overriding any superclass method, and your method is likely never to be called or used at all. The superclass method will still be called in every case that you don't specifically call your method on an instance of your object.

 

Overriding methods is basic java. If you do not understand how to override a method, learn java first. The IDE does not teach java and it will only offer to remove syntax errors, it cannot "fix" your code or even figure out what you want to do.

 

 

All methods in java have a signature. Multiple methods can have the exact same names because the parameter (argument) types and number of them determine which is the correct method.

If you override a method and then change any parameter type or add or remove parameters, you no longer override that method. You just created a completely independent method with the same name.

 

 

^^ Exactly what Sequituri said.

 

The method I posted is the 1.7.2 version; since I have not seen anywhere that you specify for which version you are coding, I assume you are coding for 1.7.2. If that's not the case, you should either say so in your topic title or first post, and learn to use your IDE and Java to find what the method might be called in whatever version you happen to be using.

Link to comment
Share on other sites

Well I tried implementing according to the comments left, and when I right click while holding the rifle it just holds it on a weird angle and doesn't fire anything.

 

The code - @Override had to be removed to allow me to use the World parameter to create the entity.

Anytime you elect to remove an @Override annotation, you break your code. Seriously, is this not posted multiple times on these boards?

If you have to remove that, it means you are no longer overriding any superclass method, and your method is likely never to be called or used at all. The superclass method will still be called in every case that you don't specifically call your method on an instance of your object.

 

Overriding methods is basic java. If you do not understand how to override a method, learn java first. The IDE does not teach java and it will only offer to remove syntax errors, it cannot "fix" your code or even figure out what you want to do.

 

 

All methods in java have a signature. Multiple methods can have the exact same names because the parameter (argument) types and number of them determine which is the correct method.

If you override a method and then change any parameter type or add or remove parameters, you no longer override that method. You just created a completely independent method with the same name.

 

 

^^ Exactly what Sequituri said.

 

The method I posted is the 1.7.2 version; since I have not seen anywhere that you specify for which version you are coding, I assume you are coding for 1.7.2. If that's not the case, you should either say so in your topic title or first post, and learn to use your IDE and Java to find what the method might be called in whatever version you happen to be using.

I am using 1.7.2. As I mentioned though, to keep the Override I must retain the original parameters which don't include the World, but I don't know how to spawn an entity without the world parameter. When I just remove that parameter and above add a declaration (public World par2World;) then the game crashes when I fire the weapon.

 

Crash report:

 

 

Reported exception thrown!

net.minecraft.util.ReportedException: Ticking entity

at net.minecraft.world.World.updateEntities(World.java:2111) ~[World.class:?]

at net.minecraft.client.Minecraft.runTick(Minecraft.java:2104) ~[Minecraft.class:?]

at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1036) ~[Minecraft.class:?]

at net.minecraft.client.Minecraft.run(Minecraft.java:951) [Minecraft.class:?]

at net.minecraft.client.main.Main.main(Main.java:112) [Main.class:?]

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_51]

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51]

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[?:1.7.0_51]

at java.lang.reflect.Method.invoke(Unknown Source) ~[?:1.7.0_51]

at net.minecraft.launchwrapper.Launch.launch(Launch.java:134) [launchwrapper-1.9.jar:?]

at net.minecraft.launchwrapper.Launch.main(Launch.java:28) [launchwrapper-1.9.jar:?]

Caused by: java.lang.NullPointerException

at com.darkMod.item.weapon.Rifle.onUsingTick(Rifle.java:41) ~[Rifle.class:?]

at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:306) ~[EntityPlayer.class:?]

at net.minecraft.client.entity.EntityClientPlayerMP.onUpdate(EntityClientPlayerMP.java:106) ~[EntityClientPlayerMP.class:?]

at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2286) ~[World.class:?]

at net.minecraft.world.World.updateEntity(World.java:2246) ~[World.class:?]

at net.minecraft.world.World.updateEntities(World.java:2096) ~[World.class:?]

... 10 more

 

Description: Ticking entity

 

java.lang.NullPointerException: Ticking entity

at com.darkMod.item.weapon.Rifle.onUsingTick(Rifle.java:41)

at net.minecraft.entity.player.EntityPlayer.onUpdate(EntityPlayer.java:306)

at net.minecraft.client.entity.EntityClientPlayerMP.onUpdate(EntityClientPlayerMP.java:106)

at net.minecraft.world.World.updateEntityWithOptionalForce(World.java:2286)

at net.minecraft.world.World.updateEntity(World.java:2246)

at net.minecraft.world.World.updateEntities(World.java:2096)

at net.minecraft.client.Minecraft.runTick(Minecraft.java:2104)

at net.minecraft.client.Minecraft.runGameLoop(Minecraft.java:1036)

at net.minecraft.client.Minecraft.run(Minecraft.java:951)

at net.minecraft.client.main.Main.main(Main.java:112)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)

at java.lang.reflect.Method.invoke(Unknown Source)

at net.minecraft.launchwrapper.Launch.launch(Launch.java:134)

at net.minecraft.launchwrapper.Launch.main(Launch.java:28)

 

 

 

Link to comment
Share on other sites

To get the world just use player.worldObj

Removing the override it breaks it, you should work around it instead of remove it (for example, just use something else that runs every tick and already gives you the world object to check if the player is right clicking with your item and spawn the entity.

 

 

Creator of Metroid Cubed! Power Suits, Beams, Hypermode and more!

width=174 height=100http://i.imgur.com/ghgWmA3.jpg[/img]

Link to comment
Share on other sites

To get the world just use player.worldObj

Removing the override it breaks it, you should work around it instead of remove it (for example, just use something else that runs every tick and already gives you the world object to check if the player is right clicking with your item and spawn the entity.

This worked! - ALMOST, the only problem is that now the gun is still held on a funny angle when shooting. Is there a way to make it so it doesn't visually change when shooting? It didn't with my old method, but this new method allows me to make it shoot faster.

Link to comment
Share on other sites

The held position is caused because you set your EnumAction to .block; change it to .none and your gun position will not change.

I never touched touched that though.

 

My code:

 

 

public class Rifle extends ItemSword{

 

  public Rifle(int i, ToolMaterial p_i45356_1_){

super(p_i45356_1_);

setFull3D();

    this.setCreativeTab(CreativeTabs.tabCombat);

  }

 

  @Override

  public void onUsingTick(ItemStack par1ItemStack, EntityPlayer player, int count) {

  // count starts at the max duration and counts down

  int ticksInUse = getMaxItemUseDuration(par1ItemStack) - count;

 

  // this will fire every 2 ticks; change or remove to suit your needs

  if (ticksInUse % 2 == 0) {

    if(player.capabilities.isCreativeMode||player.inventory.consumeInventoryItem(darkMod.bullet))

      {

    player.worldObj.playSoundAtEntity(player, "darkMod:Rifle", 1.0F, 1.0F);

        if (!player.worldObj.isRemote)

        {

        player.worldObj.spawnEntityInWorld(new EntityBullet(player.worldObj, player, 9.0F));

        }

      }

  }

  }

  }

 

 

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.