Jump to content
View in the app

A better way to browse. Learn more.

Forge Forums

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Posted

Hi, so I have a item that when held adds the regeneration effect to you. But for the regeneration to work it needs to tick down, so I want to call the script that adds the potion every three seconds when the item is held.

This is the script I want called every 3 seconds:

 

@Override

public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5){

super.onUpdate(stack, world, entity, par4, par5);

EntityPlayer player = (EntityPlayer) entity;

ItemStack equipped = player.getCurrentEquippedItem();

if(equipped == stack){

player.addPotionEffect(new PotionEffect(Potion.regeneration.getId(), 60, 2));

}

}

 

Thanks in advanced!

Well you could use livingupdate event. Use a counter so every 60 ticks you would Check if the entity is a Player and then Check if hes holding the item.

  • Author

I am not sure how to do that, if you could explain, because right now my code just adds regeneration every tick.

I have no code access right now so i can just give ideas because I dont know the functions.

You want something to happen every three seconds. Three seconds Are 60 ticks.

I See two ways.

Acess the buffs of the Player and Check if the Regen buff is already applied at least three seconds. (acutalDuration<maxDuration-60)

Check every three seconds if the Player is Holding ur item. That means saving the last tick where u checked, get the actual tick and Check if its 60 ticks later

Two ways:

 

1. Per-item solution:

- Use ItemStack's NBT to store how many ticks ago buff was added.

- +1 with every check.

- If value reaches 60, add buff.

Flaws: Vanilla is retarded and can't handle ItemStacks' NBT changes like normal human being would, and "recreates" ItemStack with every NBT change, instead of just changing value - which, in many cases, is stupid.

Opinion: Don't do this, too easy, too cheesy.

 

2. Per-entity solution

- Use IExtendedEntityProperties to store integer (ticks elapsed) and utilize Tick event to ++ it.

- Same as in 1. when tick reaches 60, do update (add effect).

Pros: Good and efficient, proper thing to do.

 

How-to:

Decide if effect should be for all entities or only players, pick LivingUpdateEvent or PlayerTickEvent.

Create class that implements IExtendedEntityProperties and save your value.

Use that value in previously created event.

 

How to use events: google has like million examples/tuts.

How to IEEP:

Working, long: http://www.minecraftforum.net/forums/mapping-and-modding/mapping-and-modding-tutorials/1571567-1-7-2-1-6-4-eventhandler-and

Short: It's quite easy, look at code :D

 

EDIT

3. There is also other per-item solution that includes saving timestamp the moment when buff was last added, then instead of incrementing tick-count in NBT every tick, you simply check timestamp and see if 60 ticks passed - if so - save new timestamp and re-apply.

For timestamp you can sue currentWorldTime.

Flaws: Timestamps are not always cool to work with.

Pros: Better than 1; still worse than 2.

1.7.10 is no longer supported by forge, you are on your own.

  • Author

I'm new to this and don't get any of what you just said. I'm probably just going to change the potion effect of that item :(

  • Author

Is there an easier way to add a delay in my script after I do:

player.addPotionEffect(new PotionEffect(Potion.regeneration.getId(), 100, 0));

??

If you can't do it, it's most likely lack of Java experience, in which case i suggest learning and not give up :D

 

Even if you can't Java good - just by googling it you would be able to get it working, I told you what to look for.

 

If you are not planning to use that info:

Know this:

- Potion effects should be added server-side.

@Override
   public void onUpdate(ItemStack stack, World world, Entity entity, int par4, boolean par5){
      super.onUpdate(stack, world, entity, par4, par5);
      if (!world.isRemote) // This will run on server
     {
         EntityPlayer player = (EntityPlayer) entity;
         ItemStack equipped = player.getCurrentEquippedItem();
         if(equipped == stack){
            player.addPotionEffect(new PotionEffect(Potion.regeneration.getId(), 60, 2));
         }
      }
  }

- Applying potion effect every tick is NOT BAD. Vanilla handles it on it's own, so as long as you want to keep potion effect all the time when item is held, you can leave code as it is.

- Setting potion duration to 1 will cause glitches, always use 2+ values (this has to do with update of effects, that might occur before or after you added the potion, thus - effect can act weird).

 

 

Is there an easier way to add a delay

No, I told you all possible proper ways, anything out of this (3 ways) is probably weird or same thing wrapped in different code (or is it?)

1.7.10 is no longer supported by forge, you are on your own.

Guest
This topic is now closed to further replies.

Important Information

By using this site, you agree to our Terms of Use.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.