Jump to content

Recommended Posts

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!

Posted

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.

Posted

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

Posted

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.

Posted

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

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

??

Posted

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.

Announcements



  • Recently Browsing

    • No registered users viewing this page.
  • Posts

    • Hello , when I try to launch the forge installer it just crash with a message for 0,5 secondes. I'm using java 17 to launch it. Here's the link of the error :https://cdn.corenexis.com/view/?img=d/ma24/qs7u4U.jpg  
    • You will find the crash-report or log in your minecraft directory (crash-report or logs folder)
    • Use a modpack which is using these 2 mods as working base:   https://www.curseforge.com/minecraft/modpacks/life-in-the-village-3
    • inicie un mundo donde instale Croptopia y Farmer's Delight, entonces instale el addon Croptopia Delight pero no funciona. es la version 1.18.2
    • Hello all. I'm currently grappling with the updateShape method in a custom class extending Block.  My code currently looks like this: The conditionals in CheckState are there to switch blockstate properties, which is working fine, as it functions correctly every time in getStateForPlacement.  The problem I'm running into is that when I update a state, the blocks seem to call CheckState with the position of the block which was changed updated last.  If I build a wall I can see the same change propagate across. My question thus is this: is updateShape sending its return to the neighbouring block?  Is each block not independently executing the updateShape method, thus inserting its own current position?  The first statement appears to be true, and the second false (each block is not independently executing the method). I have tried to fix this by saving the block's own position to a variable myPos at inception, and then feeding this in as CheckState(myPos) but this causes a worse outcome, where all blocks take the update of the first modified block, rather than just their neighbour.  This raises more questions than it answers, obviously: how is a different instance's variable propagating here?  I also tried changing it so that CheckState did not take a BlockPos, but had myPos built into the body - same problem. I have previously looked at neighbourUpdate and onNeighbourUpdate, but could not find a way to get this to work at all.  One post on here about updatePostPlacement and other methods has proven itself long superceded.  All other sources on the net seem to be out of date. Many thanks in advance for any help you might offer me, it's been several days now of trying to get this work and several weeks of generally trying to get round this roadblock.  - Sandermall
  • Topics

  • Who's Online (See full list)

    • There are no registered users currently online
×
×
  • Create New...

Important Information

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