Jump to content

Recommended Posts

Posted

So I'm coding a sort of medicine mod, and I'm trying to make my Ibuprofen item give a "Painkiller Sickness" after using it once, and if it is used again during the time of the status effect, the player dies, or takes massive amounts of damage. I'm new to the whole Java coding thing, so any help would be appreciated. Thanks in advance!

Posted

Java != JavaScript

Apparently I'm a complete and utter jerk and come to this forum just like to make fun of people, be confrontational, and make your personal life miserable.  If you think this is the case, JUST REPORT ME.  Otherwise you're just going to get reported when you reply to my posts and point it out, because odds are, I was trying to be nice.

 

Exception: If you do not understand Java, I WILL NOT HELP YOU and your thread will get locked.

 

DO NOT PM ME WITH PROBLEMS. No help will be given.

Posted

You'd want the sickness to be a

Potion

. When the player uses the Ibuprofen, check if they have the

Potion

active: if they do, damage or kill them; else heal them and apply the

Potion

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

Again, I have no clue how to do this, so I think I made the Effect class correctly, but how do I make the item kill them when they have the effect, and how do I make the Ibuprofen give the effect?

Posted

If using the Ibuprofen is instant, override

Item#onItemRightClick

to apply the damage or potion effect.

 

If you need to eat it like a potion, override the following methods (look at

ItemFood

and

ItemPotion

to see examples of this):

  • Item#getMaxItemUseDuration

    to return 32 (the same use duration as food and potions)

  • Item#getItemUseAction

    to return

    EnumAction.drink

    (the same action as potions) or

    EnumAction.eat

    (the same action as food)

  • Item#onItemRightClick

    to call

    EntityPlayer#setItemInUse

    with the

    ItemStack

    argument and the result of calling

    this.getMaxItemUseDuration

    with the

    ItemStack

    argument (this makes the player start using the item when they right click)

  • Item#onEaten

    to apply the damage or potion effect

 

To check if a player has a potion effect active, use

EntityLivingBase#isPotionActive

(

EntityPlayer

extends from

EntityLivingBase

). To apply a potion effect, use

EntityLivingBase#addPotionEffect

. To damage a player, use

Entity#attackEntityFrom

.

Please don't PM me to ask for help. Asking your question in a public thread preserves it for people who are having the same problem in the future.

Posted

I tried to do something with the code you gave me, but every time I use the item, I just take damage, with or without the potion effect. Here is the bottom portion of code for my Ibuprofen item.

 

 

 

public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer)

{

  itemstack.damageItem(1, entityplayer);

 

  entityplayer.addPotionEffect((new PotionEffect(RandomC.Ibp.getId(), 5, 0)));

 

  if (entityplayer.isPotionActive(RandomC.Ibp));

  entityplayer.attackEntityFrom(DamageSource.causePlayerDamage(entityplayer), 5);

 

 

  if (entityplayer.prevHealth >= 20) return itemstack;

  entityplayer.heal(healAmount);

  return itemstack;

 

 

 

 

Posted

The 2nd parameter of PotionEffect is duration, in ticks; 20 ticks is approximately 1 second, so 5 ticks is 1/4 second - not very long.

 

The reason you take damage every time is that you apply your Ibp PotionEffect to the player first, and then check if it is active. Well, you just applied it, so it is going to be active very single time.

 

Just swap the bits of code around and you'll be fine: check if active first to cause damage, THEN add the potion effect.

 

Also, I don't see what the point of the prevHealth check is - #heal will not surpass the player's max health no matter what value you put in there, and 20 is usually the max health. What are you trying to accomplish with that bit of code?

Posted

I switched the causePlayerDamage lines and the addPotionEffect lines around, but it would still damage me if I had the effect or not. The last section of code was to give the item back if the player used it at full health, but it didn't work. And, do you know how to name the potion effect in the en_US.lang file? Thanks in advance

 

Posted

You can't 'give the item back' because you didn't take it away - all you did in your code was damage the stack by 1. Shouldn't you be reducing the stack size by 1 instead?

 

If you take a look at some vanilla Item classes, such as ItemPotion or ItemFood, you will get a better idea of how to structure your method. For example, you should check if the player's current health is less than their max health before using and consuming the item, and don't consume it if the player is in Creative mode.

 

In pseudo-code:

#onItemRightClick {
  if (player.getCurrentHealth() < player.getMaxHealth()) {
     // we know for sure the item is going to be used now, but why restrict it? don't you want it to damage players if they take too many?
     // how will they take too many if they can't keep popping pills once they reach full health?
     if (player is NOT in creative mode) {
        --stack.stackSize;
     }
     if (the potion effect is already active on the player) {
        damage the player;
     } else {
        add the potion effect;
        heal the player;
     }
  }
  return stack;
}

 

For the language file, when you are in game, what does the name come up as? It should be something like 'potion.effect.name' - just take whatever that string is and put it in your en_US.lang file with a translation:

potion.effect.name=My Potion Effect

Posted

This is what I could get out of your code, and the potion effect game in game is potion/IbpEffect . The item still hurts me when I use it.

 

 

public ItemStack onItemRightClick(ItemStack itemstack, World world, EntityPlayer entityplayer) {

if (entityplayer.getHealth() < entityplayer.getMaxHealth() ){

itemstack.damageItem(1, entityplayer);

if (entityplayer.isPotionActive(RandomC.Ibp));{

entityplayer.attackEntityFrom(DamageSource.causePlayerDamage(entityplayer), 5);

}

} else {

entityplayer.addPotionEffect((new PotionEffect(RandomC.Ibp.getId(), 100, 0)));

entityplayer.heal(healAmount);

 

 

 

}

Posted

This is what I could get out of your code, and the potion effect game in game is potion/IbpEffect . The item still hurts me when I use it.

itemstack.damageItem(1, entityplayer); // I thought we covered this already...

if (entityplayer.isPotionActive(RandomC.Ibp));{ // what ?! a semi-colon followed by bracket?!?!?!?!
  stuff
} // huh? don't you want this to go into the else below? but you have an extra bracket again...
} else {

Sorry to say, but you need to brush up on your Java skills. That code is a mess.

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.