So, I've been creating a mod, and while going through food, I found that making one piece of food randomize between two effects was unheard of to me. If anyone knows how to do this, I would appreciate it if you showed me how:D
All you need to do is add code like this to the code that applies the effects:
if (random.nextBoolean())
{
// Apply effect #1
}
else
{
// Apple effect #2
}
This will give a 50% chance of either effect; you could extend this by using nextInt(number) and a switch statement to select between cases, or leave the else empty so the effect is only applied sometimes, as with rotten flesh and Hunger.
More specifically, this is the code in ItemFood which applies a potion effect; you can extend that class and override this method to account for multiple cases (as e.g. golden apples do); for a simple case like rotten flesh you can just call "setPotionEffect" to set the effect and another useful method is "setAlwaysEdible" which lets you eat the food even if you are full:
(note that this is from 1.6.4 but the code in later versions should be very similar. Also, note that the duration of an effect is in ticks, 20 * seconds (the "potionDuration" field is multiplied by 20 so this field should be set to seconds) and the amplifier starts at 0 for level 1)
So, I've been creating a mod, and while going through food, I found that making one piece of food randomize between two effects was unheard of to me. If anyone knows how to do this, I would appreciate it if you showed me how:D
Thanks in Advance,
CodeOne
All you need to do is add code like this to the code that applies the effects:
This will give a 50% chance of either effect; you could extend this by using nextInt(number) and a switch statement to select between cases, or leave the else empty so the effect is only applied sometimes, as with rotten flesh and Hunger.
More specifically, this is the code in ItemFood which applies a potion effect; you can extend that class and override this method to account for multiple cases (as e.g. golden apples do); for a simple case like rotten flesh you can just call "setPotionEffect" to set the effect and another useful method is "setAlwaysEdible" which lets you eat the food even if you are full:
(note that this is from 1.6.4 but the code in later versions should be very similar. Also, note that the duration of an effect is in ticks, 20 * seconds (the "potionDuration" field is multiplied by 20 so this field should be set to seconds) and the amplifier starts at 0 for level 1)
TheMasterCaver's First World - possibly the most caved-out world in Minecraft history - includes world download.
TheMasterCaver's World - my own version of Minecraft largely based on my views of how the game should have evolved since 1.6.4.
Why do I still play in 1.6.4?