How to create an item that refreshes a potion effect after a set interval by holding it

Started by RedEssence5 on Wed, 11/28/2018 - 17:22

Topic category: User side tutorials

Last seen on 22:54, 29. Nov 2018
Joined Jan 2016
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
How to create an item that refreshes a potion effect after a set interval by holding it
Wed, 11/28/2018 - 17:22

So Im trying to make an item that when you hold it in your off hand it gives regen, problem is it is getting refreshed so fast the effect doesnt actually regen anything so ive been messing around with the procedure editor but i can't seem to create a functional item, what i want it to do is it gives you regen 2 for 3 seconds and then refreshes it every 2 seconds

 

Last seen on 22:54, 29. Nov 2018
Joined Jan 2016
User points:

User statistics:

  • Modifications:
  • Forum topics:
  • Wiki pages:
  • Tracker tickets:
  • MCreator plugins:
  • Comments:
Ok so the effect is being…
Wed, 11/28/2018 - 18:53

Ok so the effect is being given to me at the correct intervals, problem is Regen 3 isnt even healing me and it kinda just stays there even though its at 0 seconds

https://youtu.be/AHrFw_Jc-to
heres a video link to the issue

 

Heres the code

package mod.mcreator;

import net.minecraftforge.items.ItemHandlerHelper;
import net.minecraftforge.fml.common.gameevent.TickEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.common.MinecraftForge;

import net.minecraft.potion.PotionEffect;
import net.minecraft.init.MobEffects;

import net.minecraft.world.World;
import net.minecraft.item.ItemStack;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.EntityLivingBase;
import net.minecraft.entity.Entity;

import java.util.HashMap;

public class mcreator_soulOfBodyItemInInventoryTick extends testenvironmentmod.ModElement 
{

    static double Counter1 = 0;

    public static void executeProcedure(java.util.HashMap<String, Object> dependencies) 
    {
        if (dependencies.get("entity") == null) 
        {
            System.err.println("Failed to load dependency entity for procedure soulOfBodyItemInInventoryTick!");
            return;
        }
        
        Entity entity = (Entity) dependencies.get("entity");
        
        if ((new ItemStack(((entity instanceof EntityLivingBase) ? ((EntityLivingBase) entity).getHeldItemOffhand().getItem() : null), (int) (1))
                .getItem() == new ItemStack(mcreator_soulOfBody.block, (int) (1)).getItem())) 
        {
            
            Counter1 = ((double) (Counter1 + 1));
            
            if ((Counter1 >= 320)) 
            {
                if (entity instanceof EntityLivingBase)
                    ((EntityLivingBase) entity).addPotionEffect(new PotionEffect(MobEffects.REGENERATION, (int) 120, (int) 2));
                Counter1 = ((double) 0);
            }
        }
        else
            Counter1 = 0;
    }

    @SubscribeEvent
    public void onPlayerTick(TickEvent.PlayerTickEvent event) 
    {
        
        if (event.phase == TickEvent.Phase.END) 
        {
            Entity entity = event.player;
            World world = entity.world;
            int i = (int) entity.posX;
            int j = (int) entity.posY;
            int k = (int) entity.posZ;
            java.util.HashMap<String, Object> dependencies = new java.util.HashMap<>();
            dependencies.put("x", i);
            dependencies.put("y", j);
            dependencies.put("z", k);
            dependencies.put("world", world);
            dependencies.put("entity", entity);
            this.executeProcedure(dependencies);
        }
        
    }

    @Override
    public void load(FMLInitializationEvent event) {
        MinecraftForge.EVENT_BUS.register(this);
    }
}