So i decided to make a tutorial explaining the basics to thrown items. This isn't going to revolutionize anything but just give a good idea on how to make them. Warning: this will not give you code to copy and paste. You will have to read and ALREADY understand how to program and how to use modloader.
Current
Spear
To be added
Custom bow and arrow
grenade
any other requests
Spear
]This Tut comes out of the fact that I have not found any code to simply make a spear, and most just tell u to use the bow and arrow code. So I took the time to get one working.
Step 1 make a item, it doesn't matter what it is named. Now take this piece of code and add it to the onItemRightClick, or onPlayerStoppedUsing, or where ever you want it to launch your spear at.
(the 2.0F in the code is force so you can change it to any number you would like.)
//the 2.0F can be multiplied or done whatever to to change the force.
EntitySpear entityspear = new EntitySpear(world, entityplayer, 2.0F);
world.spawnEntityInWorld(entityspear);
This will create and spawn the spear with the set velocity.
there is more you can do but i wont get into it because it can easily be found.
also after or before this statement is where you can decrease item stack or set damage like so:
// checks if is not in creative and then consumes a spear
if (!entityplayer.capabilities.isCreativeMode){
entityplayer.inventory.consumeInventoryItem(mod_Tutorial.Spear.shiftedIndex);
}
//4 can be changed to anything entityspear.setDamage(4);
Step 2
so next you guessed it we must create the EntitySpear class.
this will easily be done by copying the arrow code then renaming it to spear.
next we must change what item it gives you back.
so change item.arrow to mod_******.Spear here
As always there is so much more that could be changed but I wont be getting into that right now.
Now if i didn't miss any changes and you followed this, when you spawn you spear it should be a white cube because it doesn't have a render class.
so again we copy the arrow's render class and of course change it to spear.
i used this spear design so every thing i do is based off this which is twice the length of the arrow or 32 pixels.
First change your image location to what image your using, this doesn't require modloader commands.
And the only other thing needed to be changed is the variable( with my version on mcp) F which is multiplied by 2, as you can see in the last row.
float f = 0.0F;
float f1 = 0.5F;
float f2 = (float)(0 + i * 10) / 32F;
float f3 = (float)(5 + i * 10) / 32F;
float f4 = 0.0F;
float f5 = 0.15625F;
float f6 = (float)(5 + i * 10) / 32F;
float f7 = (float)(10 + i * 10) / 32F;
//here f1 = f1 * 2;
Last in your mod_**** put this line to allow the render to work.
public void addRenderer(Map map)
{
map.put(EntitySpear.class, new RenderSpear());
}
And if all was done correctly(by you and me) you should have a working spear.
From there you can add many different functions to it.
No drop in spear/arrow
For this it is really simple, in basic form all you have to do is remove any part that changes the movement(X,Y,Z) so here is how i did it Warning this may not work perfectly because i have not tested it any extreme, just that it works.
So just delete anything commented out or one case change numbers.
This is all in the Entitywhatever class
im not making a spear but do you know how to make it spin in the air
No but look at the code that deals with pitch in the Entity arrow(i believe its in the on update function) . ill give it a try some time but if you figure it out i would love to add it to the tutorial.
Yeah sure, here it is (at least the code with the error);
EntitySpear entityspear = new EntitySpear(world, entityplayer, 2.0F);
world.spawnEntityInWorld(entityspear);
I think that the problem is not in that section of code but in the surrounding area. just send me your whole itemspear. i would be happy to find the problem.
It works fine in eclipse but u need to put a "set item in use" on right click if you want it to spawn a spear.
What do you mean a set item in use thing? Are you sure it worked in eclipse? the world.spawnEntityInWorld(entityspear) gave me the error "can't make static reference to the non-static method in world"
What do you mean a set item in use thing? Are you sure it worked in eclipse? the world.spawnEntityInWorld(entityspear) gave me the error "can't make static reference to the non-static method in world"
I fixed a piece so now it fires
I , in messing around got that Error when i referenced the World variable wrong(int the entity spawn in world ). the easiest way to explain it is, when you see this (, World world) you are saying that the Upper case World you are saying what ever is after that is a version of that. it is like saying EntitySpear entityspear = new EntitySpear, or int i. its a variable. so i think the name is incorrect, but i don't know why it doesn't work for you when it does for me.It is problem due to a different version of mcp so try messing with that name
eather way here is the code i edited to fire an arrow now.
package net.minecraft.src;
public class ItemSpear extends Item
{
protected ItemSpear(int par1)
{
super(par1);
}
public void onPlayerStoppedUsing(ItemStack itemstack, World world, EntityPlayer entityplayer, int i)
{
EntitySpear entityspear = new EntitySpear(world, entityplayer, 2.0F);
world.spawnEntityInWorld(entityspear);
}
public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer)
{
if (!entityplayer.capabilities.isCreativeMode){
// entityplayer.inventory.consumeInventoryItem(mod_whatever .spear.shiftedIndex);
}
entityplayer.setItemInUse(itemstack, getMaxItemUseDuration(itemstack)); // this makes it so it will use the onPlayerStoppedUsing void
return itemstack;
}
}
Warning: this will not give you code to copy and paste. You will have to read and ALREADY understand how to program and how to use modloader.
Current
Spear
To be added
Custom bow and arrow
grenade
any other requests
Spear
Step 1 make a item, it doesn't matter what it is named. Now take this piece of code and add it to the onItemRightClick, or onPlayerStoppedUsing, or where ever you want it to launch your spear at.
(the 2.0F in the code is force so you can change it to any number you would like.)
This will create and spawn the spear with the set velocity.
there is more you can do but i wont get into it because it can easily be found.
also after or before this statement is where you can decrease item stack or set damage like so:
Step 2
so next you guessed it we must create the EntitySpear class.
this will easily be done by copying the arrow code then renaming it to spear.
next we must change what item it gives you back.
so change item.arrow to mod_******.Spear here
Also change the original damage source to this
As always there is so much more that could be changed but I wont be getting into that right now.
Now if i didn't miss any changes and you followed this, when you spawn you spear it should be a white cube because it doesn't have a render class.
so again we copy the arrow's render class and of course change it to spear.
i used this spear design so every thing i do is based off this which is twice the length of the arrow or 32 pixels.
First change your image location to what image your using, this doesn't require modloader commands.
And the only other thing needed to be changed is the variable( with my version on mcp) F which is multiplied by 2, as you can see in the last row.
Last in your mod_**** put this line to allow the render to work.
And if all was done correctly(by you and me) you should have a working spear.
From there you can add many different functions to it.
No drop in spear/arrow
Warning this may not work perfectly because i have not tested it any extreme, just that it works.
So just delete anything commented out or one case change numbers.
This is all in the Entitywhatever class
Not sure what is wrong, have you had any luck fixing it?
No but look at the code that deals with pitch in the Entity arrow(i believe its in the on update function) . ill give it a try some time but if you figure it out i would love to add it to the tutorial.
Your welcome, im glad it helped
could i see you code. ill see if i can find whats wrong.
Yeah sure, here it is (at least the code with the error);
I think that the problem is not in that section of code but in the surrounding area. just send me your whole itemspear. i would be happy to find the problem.
It works fine in eclipse but u need to put a "set item in use" on right click if you want it to spawn a spear.
What do you mean a set item in use thing? Are you sure it worked in eclipse? the world.spawnEntityInWorld(entityspear) gave me the error "can't make static reference to the non-static method in world"
I fixed a piece so now it fires
I , in messing around got that Error when i referenced the World variable wrong(int the entity spawn in world ). the easiest way to explain it is, when you see this (, World world) you are saying that the Upper case World you are saying what ever is after that is a version of that. it is like saying EntitySpear entityspear = new EntitySpear, or int i. its a variable. so i think the name is incorrect, but i don't know why it doesn't work for you when it does for me.It is problem due to a different version of mcp so try messing with that name
eather way here is the code i edited to fire an arrow now.