The Meaning of Life, the Universe, and Everything.
Join Date:
11/29/2015
Posts:
61
Member Details
i am working on a mod based off of the table game Dungeons and dragons. I need help with Random number gen for my weapons. I would like to have my Fire Mace do between 3 and 9 damage every time i hit i mob in the game and i don't know how to code the the right way. if you know how to then please help me I need all the help i can get on my mod plus if you help me you get access to the closed beta and live beta for both the mod and the modpack its going to be in. form what i have seen this is not d&d mod pack out there that has rng for there tools, weapons, and potions.
i also want to have the rng work for all mods that add tools, weapons, and potions and for minecrafts tools, weapons, and potions as well.
so please let me know if you want to help me on my mod and i will go into greater detail about what i am working on with this dungeons and dragons mod.
Just use java's random generator to get the random number:
int num = (int)(Math.random() * 9 + 1);
if (num < 3) {
num+=3;
}
I'm sorry, but that is terrible for a random number between 3 and 9, and will actually produce numbers between 3 and 6 more often. Not to mention that adding 1 means it's actually a range up to 10...
A lot of classes (Item, Entity and World mainly) have a public or protected Random variable which is used for a lot in the game. Since these instances get used, they will possibly give better results rather than using some new Random instance. So if you can use one, you can use that instead of the new instance in the example code below. But this code will produce a float of a value from 3 to 9:
Random rand = new Random();
float num = rand.nextFloat() * 6 + 3;
It depends on how you use that code. If you're just referring to that field num in code as the attack damage, then it will always be the same value. However, if every time you want to deal damage you use that whole above code to generate a new value every time you hit, then yes you'll have a different value when you attack a mob.
private Random fRandom;
public ItemCopperMace(ToolMaterial material)
{
super(material);
}
//@Override
//@SideOnly(Side.SERVER)
} where would i put that code in here?
You can probably put the code in the hitEntity() method of your sword. Each time you hit an entity, you get your random number from 3-9 and use target.attackEntityFrom(DamageSource.generic, randomNumber); to inflict damage on it. This may affect your tool material for the mace but that's my suggestion. You also don't need the @SideOnly.
public ItemCopperMace(ToolMaterial material)
{
super(material);
}
@Override
//@SideOnly(Side.SERVER)
public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase player)
{
Random rand = new Random();
float randomNumber = rand.nextFloat()*6 + 3;;
target.attackEntityFrom(DamageSource.generic, randomNumber);
return true;
}
} Why is this not changing the damage the mace still dose 4 damage every time not 3-9 and i want to do 3-9
How can this be a server only mod if you have items? Items must be registered on both client and server.
Unfortunately I can't help you with specifics for the item class, as I haven't modded in 1.7.10 in ages, and I don't even have an environment in that version anymore so I can't easily view the code.
Which also makes me wonder why you're still modding in such an old version...
The Meaning of Life, the Universe, and Everything.
Join Date:
11/29/2015
Posts:
61
Member Details
Because of the mods in the mod pack this mod is going in the lord of the rings mod and Sword art online Gui are not updated to 1.10.2 or higher yet and i don't think it will become one any time soon.
also how would i go about using the rng code to change the damage of weapons already in minecraft?
I wasn't talking about that exactly, but given the help we've given so far, yes it's pretty easy.
We've suggested subscribing to the LivingAttackEvent event to adjust the damage, and I've even provided you with the calculation you needed.
Events are a fundamental part of modding if you're trying to do something which you can't do with the usual Item and Block methods. So I strongly suggest you go find tutorials for events (even at least read the Forge documentation for them for the best advice).
i am working on a mod based off of the table game Dungeons and dragons. I need help with Random number gen for my weapons. I would like to have my Fire Mace do between 3 and 9 damage every time i hit i mob in the game and i don't know how to code the the right way. if you know how to then please help me I need all the help i can get on my mod plus if you help me you get access to the closed beta and live beta for both the mod and the modpack its going to be in. form what i have seen this is not d&d mod pack out there that has rng for there tools, weapons, and potions.
i also want to have the rng work for all mods that add tools, weapons, and potions and for minecrafts tools, weapons, and potions as well.
so please let me know if you want to help me on my mod and i will go into greater detail about what i am working on with this dungeons and dragons mod.
also rng must be in the mod or its not a d&d mod.
Just use java's random generator to get the random number:
int num = (int)(Math.random() * 9 + 1);
if (num < 3) {
num+=3;
}
CPU Cooler: Corsair H100i 77.0 CFM Liquid CPU Cooler
Motherboard: Asus Maximus V Extreme
Memory: G.Skill Ripjaws Z Series 32GB @1866 MHz
Storage: Samsung 840 EVO 250GB
Storage: Samsung 840 EVO 250GB
Storage: Samsung 840 EVO 500GB
Video Card: Asus GeForce GTX 780 3GB
Case: Corsair Obsidian Series 800D ATX Full Tower Case
Power Supply: Corsair AX1200i
Sound Card: Creative Labs Sound Blaster Zx 24-bit 192 KHz Sound Card
Wireless Network Adapter: Netgear WNDA4100-100NAS 802.11a/b/g/n USB 2.0 Wi-Fi Adapter
Mouse: Corsair Vengeance M95 Wired Laser Mouse
Keyboard: Corsair Gaming K95 RGB Cherry MX Brown Fully Mechanical Gaming Keyboard
I'm sorry, but that is terrible for a random number between 3 and 9, and will actually produce numbers between 3 and 6 more often. Not to mention that adding 1 means it's actually a range up to 10...
A lot of classes (Item, Entity and World mainly) have a public or protected Random variable which is used for a lot in the game. Since these instances get used, they will possibly give better results rather than using some new Random instance. So if you can use one, you can use that instead of the new instance in the example code below. But this code will produce a float of a value from 3 to 9:
If you try the code you will see that it actually works just fine. However, yes the solution you proposed does fit his needs much better.
CPU Cooler: Corsair H100i 77.0 CFM Liquid CPU Cooler
Motherboard: Asus Maximus V Extreme
Memory: G.Skill Ripjaws Z Series 32GB @1866 MHz
Storage: Samsung 840 EVO 250GB
Storage: Samsung 840 EVO 250GB
Storage: Samsung 840 EVO 500GB
Video Card: Asus GeForce GTX 780 3GB
Case: Corsair Obsidian Series 800D ATX Full Tower Case
Power Supply: Corsair AX1200i
Sound Card: Creative Labs Sound Blaster Zx 24-bit 192 KHz Sound Card
Wireless Network Adapter: Netgear WNDA4100-100NAS 802.11a/b/g/n USB 2.0 Wi-Fi Adapter
Mouse: Corsair Vengeance M95 Wired Laser Mouse
Keyboard: Corsair Gaming K95 RGB Cherry MX Brown Fully Mechanical Gaming Keyboard
Random rand = new Random();
float num = rand.nextFloat() * 6 + 3;
but will it gen a random number every time it attack a mob ingame?
It depends on how you use that code. If you're just referring to that field num in code as the attack damage, then it will always be the same value. However, if every time you want to deal damage you use that whole above code to generate a new value every time you hit, then yes you'll have a different value when you attack a mob.
ok now here is the current code for my weapon coppermace
package jmanz7.DandDWeaponsMod.item;
import java.util.Random;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
public class ItemCopperMace extends ItemSword
{
private Random fRandom;
public ItemCopperMace(ToolMaterial material)
{
super(material);
}
//@Override
//@SideOnly(Side.SERVER)
} where would i put that code in here?
You can probably put the code in the hitEntity() method of your sword. Each time you hit an entity, you get your random number from 3-9 and use target.attackEntityFrom(DamageSource.generic, randomNumber); to inflict damage on it. This may affect your tool material for the mace but that's my suggestion. You also don't need the @SideOnly.
well this mod is a server only mod
package jmanz7.DandDWeaponsMod.item;
import java.util.Random;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.item.ItemStack;
import net.minecraft.item.ItemSword;
import net.minecraft.util.DamageSource;
public class ItemCopperMace extends ItemSword
{
public ItemCopperMace(ToolMaterial material)
{
super(material);
}
@Override
//@SideOnly(Side.SERVER)
public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase player)
{
Random rand = new Random();
float randomNumber = rand.nextFloat()*6 + 3;;
target.attackEntityFrom(DamageSource.generic, randomNumber);
return true;
}
} Why is this not changing the damage the mace still dose 4 damage every time not 3-9 and i want to do 3-9
How can this be a server only mod if you have items? Items must be registered on both client and server.
Unfortunately I can't help you with specifics for the item class, as I haven't modded in 1.7.10 in ages, and I don't even have an environment in that version anymore so I can't easily view the code.
Which also makes me wonder why you're still modding in such an old version...
Because of the mods in the mod pack this mod is going in the lord of the rings mod and Sword art online Gui are not updated to 1.10.2 or higher yet and i don't think it will become one any time soon.
also how would i go about using the rng code to change the damage of weapons already in minecraft?
anyone?
Listen to the livingAttackEvent, check for the weapon, and calculate a new damage value.
If I helped you, please click the green up arrow.
i am new to making mods for minecraft. so i dont understand how to code the rng into all my weapons and minecrafts weapons.
some one help me
Oh I didn't realise you wanted this to work for all weapons, including vanilla...
In that case you should do what Alpvax has said, along with using the calculation I posted before.
ok but i am still new to making mods for minecraft
so i dont know where to put the code?
the same goes for all tools in minecraft and my mod?
Then maybe you should lookup some tutorials, because you're talking about doing some really basic modding.
Here I'll give you a start:
http://www.minecraftforum.net/forums/mapping-and-modding/minecraft-mods/modification-development/2716947-can-we-start-a-thorough-list-of-really-good
Random damage between two numbers picked at random is basic?
I wasn't talking about that exactly, but given the help we've given so far, yes it's pretty easy.
We've suggested subscribing to the LivingAttackEvent event to adjust the damage, and I've even provided you with the calculation you needed.
Events are a fundamental part of modding if you're trying to do something which you can't do with the usual Item and Block methods. So I strongly suggest you go find tutorials for events (even at least read the Forge documentation for them for the best advice).