Jump to content

Recommended Posts

Posted

I'm creating an item that strikes the target with lightning. I want to grant the attacker with temporary immunity to lightning. The way I plan to do this is setting an NBT tag on the ItemStack when an entity is attacked and removing it after 20 ticks or after the player is struck by lightning. I need help putting some sort of timer after an entity is attacked to set the NBT tag to false.

Event Handler class:

@Mod.EventBusSubscriber(modid = Main.MODID, bus = Mod.EventBusSubscriber.Bus.FORGE)
public class EventHandler {
    @SubscribeEvent
    public static void onStruckByLightning(EntityStruckByLightningEvent e) {
        if (e.getEntity() instanceof PlayerEntity) {
            if (((PlayerEntity) e.getEntity()).isHolding(ItemInit.LIGHTNING_AXE.get())) {
                ItemStack stack = ((PlayerEntity) e.getEntity()).getMainHandItem();
                if (stack.hasTag() && stack.getTag().contains(LIGHTNING_IMMUNITY_TAG) && stack.getTag().getBoolean(LIGHTNING_IMMUNITY_TAG)) {
                    e.setCanceled(true);
                    stack.getTag().putBoolean(LIGHTNING_IMMUNITY_TAG, false);
                }
            }
        }
    }

    @SubscribeEvent
    public static void onAttackEntity(AttackEntityEvent e) {
        if (e.getTarget() instanceof LivingEntity) {
            PlayerEntity attacker = e.getPlayer();

            if (attacker.level.dimension() == World.OVERWORLD && attacker.isHolding(ItemInit.LIGHTNING_AXE.get())) {
                attacker.getMainHandItem().getCapability(CapabilityEnergy.ENERGY, null).ifPresent(energyStorage -> {
                    if (energyStorage.getEnergyStored() >= 500 && attacker.getAttackStrengthScale(0) >= 1.0F) {
                        CompoundNBT nbt = attacker.getMainHandItem().getTag();

                        if (nbt == null) {
                            nbt = new CompoundNBT();
                            attacker.getMainHandItem().setTag(nbt);
                        }

                        nbt.putBoolean(LIGHTNING_IMMUNITY_TAG, true); // I want to put some sort of timer here to eventually set this to false.

                        LightningBoltEntity lightningBoltEntity = new LightningBoltEntity(EntityType.LIGHTNING_BOLT, e.getTarget().level);
                        lightningBoltEntity.setPos(e.getTarget().getX(), e.getTarget().getY(), e.getTarget().getZ());
                        e.getTarget().level.addFreshEntity(lightningBoltEntity);
                        e.getTarget().setSecondsOnFire(2);

                        energyStorage.extractEnergy(500, false);

                        attacker.addEffect(new EffectInstance(Effects.FIRE_RESISTANCE, 120, 0, false, false));
                    }
                });
            }
        }
    }
}

 

Other code can be found on my GitHub repo: https://github.com/65-7a/EnergyBasedGear/tree/lightning-axe

Thanks for reading.

Posted

Store the world time of the time you want the condition to end.

When the entity is struck by lightning, check to see if that time is in the future, or the past.

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
51 minutes ago, Draco18s said:

Store the world time of the time you want the condition to end.

When the entity is struck by lightning, check to see if that time is in the future, or the past.

Thanks for responding.

Is this the correct way to do it?

    @SubscribeEvent
    public static void onStruckByLightning(EntityStruckByLightningEvent e) {
        if (e.getEntity() instanceof PlayerEntity) {
            if (((PlayerEntity) e.getEntity()).isHolding(ItemInit.LIGHTNING_AXE.get())) {
                ItemStack stack = ((PlayerEntity) e.getEntity()).getMainHandItem();
                if (stack.hasTag() && stack.getTag().contains(LIGHTNING_IMMUNITY_TAG) && stack.getTag().getLong(LIGHTNING_IMMUNITY_TAG) > e.getEntity().level.getGameTime()) {
                    e.setCanceled(true);
                }
            }
        }
    }

    @SubscribeEvent
    public static void onAttackEntity(AttackEntityEvent e) {
        if (e.getTarget() instanceof LivingEntity) {
            PlayerEntity attacker = e.getPlayer();

            if (attacker.level.dimension() == World.OVERWORLD && attacker.isHolding(ItemInit.LIGHTNING_AXE.get())) {
                attacker.getMainHandItem().getCapability(CapabilityEnergy.ENERGY, null).ifPresent(energyStorage -> {
                    if (energyStorage.getEnergyStored() >= 500 && attacker.getAttackStrengthScale(0) >= 1.0F) {
                        CompoundNBT nbt = attacker.getMainHandItem().getOrCreateTag();
                        nbt.putLong(LIGHTNING_IMMUNITY_TAG, e.getTarget().level.getGameTime() + 20L);

                        LightningBoltEntity lightningBoltEntity = new LightningBoltEntity(EntityType.LIGHTNING_BOLT, e.getTarget().level);
                        lightningBoltEntity.setPos(e.getTarget().getX(), e.getTarget().getY(), e.getTarget().getZ());
                        e.getTarget().level.addFreshEntity(lightningBoltEntity);
                        e.getTarget().setSecondsOnFire(2);

                        energyStorage.extractEnergy(500, false);

                        attacker.addEffect(new EffectInstance(Effects.FIRE_RESISTANCE, 120, 0, false, false));
                    }
                });
            }
        }
    }

 

Posted

And to me.

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.

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.