One thing I noticed when I was trying to make a mod, was that there was no tutorials for creating a gun. So now that I know how to do it myself, I thought I'd create a tutorial for it!
First things first:
-I assume you have Java knowledge. I'm not saying you should leave now if you don't know Java very well, but I will use terms in Java, because program in Java for Java game development, with Minecraft being a bonus
-I assume you know the basics of ModLoader, how to add texture overrides to items, etc.
-I assume you know (at least a little) about how Minecraft works in the code so that you know why you're extend from other classes like 'Item' or 'Entity'.
Now, it's hard to make something when you don't know what you're making. Because of this I'm going to briefly go over how the gun will work in the game.
The gun will be crafted (Recipes are completely up to you) as well as a clip of ammo for that gun. When the gun shoots it looses durability. When the gun shoots and only has one durability left then it doesn't do anything unless you have a clip in your inventory. If you do have a clip then the gun will play a countdown timer, and once the gun is done with the reload counter it removes the clip from the inventory, resets the timer, and the guns durability, as well as playing a reload sound. Once a person asked me why the gun looses durability instead of the clip. Well there are two reasons. First of all, I am unsure if I have the correct object access to actually damage the clip. (AKA, I don't think I could even if I wanted to) Second of all, the gun loosing durability creates a sort of 'ammo' bar, that is always visible, even if you need to put your clips in the upper half of your inventory, instead of the hotbar. When the gun shoots it creates an invisible projectile. This projectile is very accurate and doesn't not loose velocity like a bow. Because the projectile is invisible we will be playing a custom sound effect to help the user regonize that he/she just shot a bullet. You can use this link to learn more about using custom sounds without using extra mods like AudioMod. (I'm not saying this is the best way, but I know some people like me would definately prefer it)
I'm also going to (try) and use good Object-Orientated programming practices in this tutorial. For those of you who aren't entirely sure what I mean by this, it basically means we're going to create a gun class that can be used to create an infinate amount of guns, each with their own fire speed, ammo per clip count, damage delt against entities, required item for a clip, fireing sound, and reloading sound.
Ok! Now that we got all that out of the way we can (finally) start coding!
First let's create a new class called EntityBullet. I'm not as farmiliar with the bullet class as I am with the gun class because I'm not very good at projectiles, so I will explain to you guys what you need to know about the bullet class, and if you're any good at Java you could try and figure out how Notchs velocity methods and whatnot work.
Special thanks to hiloser12221 for getting me this code. It was the only thing I couldn't get working right. If for some reason you end up picking up arrows from where your invisible bullets hit the ground, try deleting lines 315-320. (I just realized that there is code for arrow retrival, though IDK if it actually works or not)
Notice that the 4th constructor (the constructors are the ones that say 'public EntityBullet()', and are called whenever we create a bullet) has 4 parameters. This constructor is what we use to create bullets in our gun class. The first parameter will be the current world, the second parameter will be the player, the third parameter is the damage, and the fourth parameter is the accuracy, which hiloser12221 said didn't seem to have any effect so I just set it to '1'.
Now we get to the fun stuff. I highly encourage you to experiment with this gun code, adding modifications to make it your own. You could have no reloading and make it so that it takes away bullets like arrows. You could have multiple bullets come out for a shotgun type thing. You could have an accuracy parameter in the constructor and use position methods for modify the x, y, and z values of the bullet slightly in order to add accuracy or knockback. It's all up to you! First let's learn about making the basic gun, however.
ItemGun.java
package net.minecraft.src;
public class ItemGun extends Item{
//These are all of our base variables for our gun
private int damage; //Damage in half-hearts
private int reloadtick; //The current value of our reloading timer
private int reloadmax; //The value that the reloadtick variable needs to be in order for the gun to reload
private int clipid; //The item id of our clip
private int ammo; //The amount of ammo avaliable per clip (used in setting durability)
private int firetick; //The current value of our shooting timer
private int firemax; //The value that the firetick variable needs to be in order for the gun to fire
//Fire delay is completely ignored when firemax is set to 0
private String firesound; //The String that notch uses in his code to figure out what sound to play when we shoot our gun
private String reloadsound; //Firesound but with reloading
//The parameters for our constructor are:
//int i (the id), int damage (damage in half-hearts), int ammo (how much ammo per clip)
//int clipid (the item id of the clip this gun uses), int firedelay (the value of firemax)
//String firesound (the value of firesound), String reloadsound (the value of reloadsound)
public ItemGun(int i, int damage, int ammo, int clipid, int firedelay, String firesound, String reloadsound){
super(i); //calls the Item.java constructor and passes in the item id for a parameter
this.damage = damage; //sets the damage value
firemax = firedelay; //sets the firemax value
firetick = firemax; //sets the firetick value equal to firemax (so you don't need to wait for the delay on the first shot)
reloadmax = 5; //sets the reload max
reloadtick = 0; //sets the reloadtick to 0
this.ammo = ammo; //sets the ammo
this.clipid = clipid; //sets the clipid
this.firesound = firesound; //sets the firesound
this.reloadsound = reloadsound; //sets the reloadsound
setMaxStackSize(1); //sets the max stack size to one
setMaxDamage(ammo + 1); //sets the durability of our gun to the ammo count + 1
}
public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer){
//Checks if the player has enough ammo
if(!par2World.isRemote && par1ItemStack.getItemDamage() < ammo){
//if the firemax isn't 0, and the fireing counter is done, then this is true
if(firetick == firemax && firemax != 0){
//spawns the bullet
par2World.spawnEntityInWorld(new EntityBullet(par2World, par3EntityPlayer, damage, 1));
//plays the sound effect
par2World.playSoundAtEntity(par3EntityPlayer, firesound, 1F, 1F);
//damages the gun
par1ItemStack.damageItem(1, par3EntityPlayer);
//resets the fire delay counter
firetick = 0;
}else{
//if the fire delay counter isn't done, then this is called, which increases the fire delay counter
++firetick;
}
//if firemax is 0 none of the above was called, so call this instead
if(firemax == 0){
par2World.spawnEntityInWorld(new EntityBullet(par2World, par3EntityPlayer, damage, 1));
par2World.playSoundAtEntity(par3EntityPlayer, firesound, 1F, 1F);
par1ItemStack.damageItem(1, par3EntityPlayer);
//already explained...
}
//If the player is out of ammo in the current clip, and the player has the correct clip, run this code
}else if(!par2World.isRemote && par3EntityPlayer.inventory.hasItem(clipid) && par1ItemStack.getItemDamage() == ammo){
//checks for the reload timer to complete
if(reloadtick == reloadmax){
//resets the reload timer
reloadtick = 0;
//plays the reload sound
par2World.playSoundAtEntity(par3EntityPlayer, reloadsound, 1F, 1F);
//consumes the item of the clipid
par3EntityPlayer.inventory.consumeInventoryItem(clipid);
//resets the ammo of the gun
par1ItemStack.setItemDamage(0);
}else{
//just like with firetick and firemax...
++reloadtick;
}
}
//just some housekeeping stuff
return par1ItemStack;
}
public void onPlayerStoppedUsing(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer, int i){
//this resets the firetick. I originally planned on using this method for non-automatic weapons
//but the method didn't seem to end up being called at all... if someone could clarify why this is
//happening I would appreciate it...
firetick = firemax;
}
}
You may have noticed I like to structure my curly braces a little differently then most people on the Minecraft forums. This is simply a personal preference, you can go either way and it doesn't really matter what way you pick, it's all up to you. I personally think this way looks a little cleaner. I also have a certain way that I like to space out my code. This is also a personal preference but I recommend you stay consistant with these things as I mostly do them to make my code easier to read. (Not being consistant will definately make things harder on you)
Finally we create the mod_***** file, which is pretty simple stuff provided you haven't just been copy/pasting your way through the whole tutorial...
package net.minecraft.src;
import java.io.File;
import net.minecraft.client.Minecraft;
public class mod_**** extends BaseMod{
Minecraft mc = ModLoader.getMinecraftInstance();
public static final Item Clip = new Item(499).setMaxStackSize(1).setItemName("Clip");
public static final Item Gun = new ItemGun(500, 5, 30, Clip.shiftedIndex, 0, "gun.fire", "gun.reload").setItemName("Gun);
public void load(){
mc.installResource("newsound/gun/shoot.ogg", new File(mc.mcDataDir, "resources/newsound/gun/shoot.ogg");
mc.installResource("newsound/gun/reload.ogg", new File(mc.mcDataDir, "resources/newsound/gun/reload.ogg");
Clip.iconIndex = ModLoader.addOverride("/gui/items.png", "/yourmod/clip.png");
Gun.iconIndex = ModLoader.addOverride("/gui/items.png", "/yourmod/gun.png");
ModLoader.addName(Clip, "Clip");
ModLoader.addName(Gun, "Gun");
ModLoader.addRecipe(new ItemStack(Clip, 1), new Object[]{
"#", Character.valueOf('#'), Block.dirt
};
ModLoader.addRecipe(new ItemStack(Gun, 1), new Object[]{
"#", Character.valueOf('#'), Block.planks
}
}
public String getVersion(){
return "1.2.5";
}
}
And I think that's it! You should now have a fully functioning gun in Minecraft!
I hope you guys enjoyed this tutorial, and if you have any questions please feel free to post, I will get to them we I can. Finally, if you have any suggestions for new tutorials, I would like to hear them. Nothing huge like dimensions, but if I get enough requests I could do a grenade tutorial!
how i change the gun's delay?and give sprite to the bullet?the bullet stay invisible
If you would have read the tutorial and meet the requirements you would have known that when you create a gun you use the following code:
public static final Item MyGun = new ItemGun(int i, int damage, int ammo, int clipid, int firedelay, String firesound, String reloadsound).setItemName("Gun");
Except instead of int i you put the item id of your gun. Instead of 'int damage' you but the damage the gun does in half-hearts. Instead of 'int ammo' you put the amount of ammunition you gun has in one clip. Instead of 'int firedelay' you put the fire delay (the bigger the number, the less shots per second) And the last two are your sound effects.
Sorry for the late reply, I've been taking a break with modding and waiting for MCP 1.7 to come out XD
Anyways you need to do something like this in your mod_**** file:
package net.minecraft.src;
import java.io.File;
import net.minecraft.client.Minecraft;
public class mod_****{
private Minecraft mc = ModLoader.getMinecraftInstance();
public void load(){
mc.installResource("newsound/yourmodfolder/yoursound.ogg", new File("resource/newsound/yourmodfolder/yoursound.ogg"));
//do this for every new sound effect
//make sure to add a folder for your mod
//sounds and put all mod sounds in it!
}
public String getVersion(){
return "1.2.5";
}
}
I left out the part about sound in the code. You need to add two String variables in the constructor for the gun. And then use the method:
par2World.playSoundAtEntity()
in the code for reloading and shooting. I'm sorry I couldn't make answers that are avaliable for non-Java people, but I don't really feel like posting revised versions of the code :\
Also, please note that this code is out of date and I haven't gotten it to work with Multiplayer in 1.2.5 so idk if it will work in 1.3.1. idk if ModLoader has even been updated for 1.2.5 ;D (haven't checked in a while tho)
i want to make a kamehameha wave (do u know dragon ball z?), so i want to make the 'gun' appear in your inventory when u get a certain amount of experience points. however, there would be a timer in between uses (a few seconds) but it wouldnt eat up any bullets. also it should explode when it hits an object.
confused yet?
here is an example:
i want to make it shoot out that particle, OR a blue beam
the tutorial is pretty good but i have some problems.
- is there a way to give the weapon a durability and let it consume a clip after each x shoots instead of durability 0 = reload?
- when i shoot enemy's they don't aggro on me
- when you kill something with the gun they also don't drop experience
- sound is not working (its not installing the files where they belong...) when i put them by myself its working
idk how to fix these issues. The first one is not possible in 1.2.5.
2 and 3 you would have to look at arrow code or something to figure that out.
And the sound, that is something I'm not very experienced with so you might want to ask someone else about it. It could be a problem with your ogg files not working.
@adlzman
I can't code your mod for you. Not even part of it. *shrugs*.
AND REMEMBER TO ANYBODY ELSE READING THIS: This code only works for 1.2.5, SINGLE PLAYER. This code is therefore incompatible with Minecraft 1.3.1 in a lot of ways. I also don't do much modding anymore, so unless you're not some guy who has less then 20 posts, and wants me to basically code the entire mod for you, I am not going to do that. If you have a decent amount of posts, and have a good idea of what your mod is going to be, then ask me and I will consider it. But I am not going to help you make a mod based off of some franchise when you have no real work done on how its going to work and look, etc. (not to be rude or anything, just saying that I would rather be "employed" by someone who knows what they're doing...)
Vec3D was changed to Vec3, and I believe the createVector method is now createVectorHelper. So anywhere you see Vec3D.createVector, you need to edit it to Vec3.createVectorHelper.
im curious about a zoom function, im close and i have another persons gun mod based on the bow, im close to figuring it out and have the code set up, i just need to figure out how the gl codes work in entityrenderer (lines 530 and 1075) i tried just using them and referencing the code but it doesnt work, i'll pm you the code if you want and if you help me with it you can feel free to include it in your tutorial
ps im working in mc ver 1.2.5
Hello, im having some trouble with my Entity. I fallowed your tutorial exact and i get only one error and it happens to be the vec3D one. Although i am using forge it still works(not the vec3D some reason)
If you could help me that would be great!
When i hover my mouse over the error (eclipse) its says that it has not bin initialized?
Oh, nvm i figured it out, i had to change it to .getVec3Pool and add some different code
Hello,
I've actually got a question to help me along with a mod that I'm making. I have some small experience with Java, and I am having a competition with a friend to see who can develop the better mod after two months time. One item, the "fireball emblem", creates a fireball entity (not the same that a ghast shoots, that was too complex) that works a lot like a snowball. On right click with the emblem, the entity is created and moves just like a snowball would. It lands and explodes, leaving behind a fiery crater like a ghast, only slightly smaller. HOWEVER- here is where my problem is. To create the entity, I copied and pasted the code used by the EntitySnowball.class file, and edited what I wanted to get it how it is. I currently am UNABLE to see the entity when it spawns, only the destruction that it leaves behind. I believe that there should be a render function somewhere that I need to add in using Modloader, but I can't find any tutorials that would help me. Do you have any idea how to get this to work?
TLDR: How would I get a throwable object to render the entity, like a snowball that is my own class?
First things first:
-I assume you have Java knowledge. I'm not saying you should leave now if you don't know Java very well, but I will use terms in Java, because program in Java for Java game development, with Minecraft being a bonus
-I assume you know the basics of ModLoader, how to add texture overrides to items, etc.
-I assume you know (at least a little) about how Minecraft works in the code so that you know why you're extend from other classes like 'Item' or 'Entity'.
Now, it's hard to make something when you don't know what you're making. Because of this I'm going to briefly go over how the gun will work in the game.
The gun will be crafted (Recipes are completely up to you) as well as a clip of ammo for that gun. When the gun shoots it looses durability. When the gun shoots and only has one durability left then it doesn't do anything unless you have a clip in your inventory. If you do have a clip then the gun will play a countdown timer, and once the gun is done with the reload counter it removes the clip from the inventory, resets the timer, and the guns durability, as well as playing a reload sound. Once a person asked me why the gun looses durability instead of the clip. Well there are two reasons. First of all, I am unsure if I have the correct object access to actually damage the clip. (AKA, I don't think I could even if I wanted to) Second of all, the gun loosing durability creates a sort of 'ammo' bar, that is always visible, even if you need to put your clips in the upper half of your inventory, instead of the hotbar. When the gun shoots it creates an invisible projectile. This projectile is very accurate and doesn't not loose velocity like a bow. Because the projectile is invisible we will be playing a custom sound effect to help the user regonize that he/she just shot a bullet. You can use this link to learn more about using custom sounds without using extra mods like AudioMod. (I'm not saying this is the best way, but I know some people like me would definately prefer it)
I'm also going to (try) and use good Object-Orientated programming practices in this tutorial. For those of you who aren't entirely sure what I mean by this, it basically means we're going to create a gun class that can be used to create an infinate amount of guns, each with their own fire speed, ammo per clip count, damage delt against entities, required item for a clip, fireing sound, and reloading sound.
Ok! Now that we got all that out of the way we can (finally) start coding!
First let's create a new class called EntityBullet. I'm not as farmiliar with the bullet class as I am with the gun class because I'm not very good at projectiles, so I will explain to you guys what you need to know about the bullet class, and if you're any good at Java you could try and figure out how Notchs velocity methods and whatnot work.
EntityBullet.java:
Notice that the 4th constructor (the constructors are the ones that say 'public EntityBullet()', and are called whenever we create a bullet) has 4 parameters. This constructor is what we use to create bullets in our gun class. The first parameter will be the current world, the second parameter will be the player, the third parameter is the damage, and the fourth parameter is the accuracy, which hiloser12221 said didn't seem to have any effect so I just set it to '1'.
Now we get to the fun stuff. I highly encourage you to experiment with this gun code, adding modifications to make it your own. You could have no reloading and make it so that it takes away bullets like arrows. You could have multiple bullets come out for a shotgun type thing. You could have an accuracy parameter in the constructor and use position methods for modify the x, y, and z values of the bullet slightly in order to add accuracy or knockback. It's all up to you! First let's learn about making the basic gun, however.
ItemGun.java
Finally we create the mod_***** file, which is pretty simple stuff provided you haven't just been copy/pasting your way through the whole tutorial...
I hope you guys enjoyed this tutorial, and if you have any questions please feel free to post, I will get to them we I can. Finally, if you have any suggestions for new tutorials, I would like to hear them. Nothing huge like dimensions, but if I get enough requests I could do a grenade tutorial!
-I assume you know the basics of ModLoader, how to add texture overrides to items, etc.
sorry i had to copy that lol
If you would have read the tutorial and meet the requirements you would have known that when you create a gun you use the following code:
Except instead of int i you put the item id of your gun. Instead of 'int damage' you but the damage the gun does in half-hearts. Instead of 'int ammo' you put the amount of ammunition you gun has in one clip. Instead of 'int firedelay' you put the fire delay (the bigger the number, the less shots per second) And the last two are your sound effects.
Anyways you need to do something like this in your mod_**** file:
par2World.playSoundAtEntity()
in the code for reloading and shooting. I'm sorry I couldn't make answers that are avaliable for non-Java people, but I don't really feel like posting revised versions of the code :\
Also, please note that this code is out of date and I haven't gotten it to work with Multiplayer in 1.2.5 so idk if it will work in 1.3.1. idk if ModLoader has even been updated for 1.2.5 ;D (haven't checked in a while tho)
confused yet?
here is an example:
i want to make it shoot out that particle, OR a blue beam
think u could help?
idk how to fix these issues. The first one is not possible in 1.2.5.
2 and 3 you would have to look at arrow code or something to figure that out.
And the sound, that is something I'm not very experienced with so you might want to ask someone else about it. It could be a problem with your ogg files not working.
@adlzman
I can't code your mod for you. Not even part of it. *shrugs*.
AND REMEMBER TO ANYBODY ELSE READING THIS: This code only works for 1.2.5, SINGLE PLAYER. This code is therefore incompatible with Minecraft 1.3.1 in a lot of ways. I also don't do much modding anymore, so unless you're not some guy who has less then 20 posts, and wants me to basically code the entire mod for you, I am not going to do that. If you have a decent amount of posts, and have a good idea of what your mod is going to be, then ask me and I will consider it. But I am not going to help you make a mod based off of some franchise when you have no real work done on how its going to work and look, etc. (not to be rude or anything, just saying that I would rather be "employed" by someone who knows what they're doing...)
Help please...
You're my last hope........
ps im working in mc ver 1.2.5
If you could help me that would be great!
When i hover my mouse over the error (eclipse) its says that it has not bin initialized?
Oh, nvm i figured it out, i had to change it to .getVec3Pool and add some different code
I've actually got a question to help me along with a mod that I'm making. I have some small experience with Java, and I am having a competition with a friend to see who can develop the better mod after two months time. One item, the "fireball emblem", creates a fireball entity (not the same that a ghast shoots, that was too complex) that works a lot like a snowball. On right click with the emblem, the entity is created and moves just like a snowball would. It lands and explodes, leaving behind a fiery crater like a ghast, only slightly smaller. HOWEVER- here is where my problem is. To create the entity, I copied and pasted the code used by the EntitySnowball.class file, and edited what I wanted to get it how it is. I currently am UNABLE to see the entity when it spawns, only the destruction that it leaves behind. I believe that there should be a render function somewhere that I need to add in using Modloader, but I can't find any tutorials that would help me. Do you have any idea how to get this to work?
TLDR: How would I get a throwable object to render the entity, like a snowball that is my own class?
http://www.youtube.com/user/006ruler
It still works for 1.3.2, you might just have to figure it out yourself