Jump to content

[SOLVED]How do I Call a Part of a Script Every 3 Seconds


Caleb6801

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.

Announcements



×
×
  • Create New...

Important Information

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