Jump to content

[1.7.10] How to make lightning bolt deal no damage to an entity?


Recommended Posts

Posted

I have this enchantment effect on a sword, however it damages the attacker too.

How can I prevent the lightning damage to the holder?

Thanks.

 

public void func_151368_a(EntityLivingBase entLB, Entity entity, int level)
{
    	EntityLightningBolt lightning = new EntityLightningBolt(entity.worldObj, entity.posX, entity.posY, entity.posZ);
    	entity.worldObj.addWeatherEffect(lightning);
}

Posted

U should be using LivingHurtEvent, check if the damage source is a lightning and if the entity taking damage is the attacker, if it is dont damage him

Okay, I will look at it.

Posted

When I did this:

@SubscribeEvent
public void onEntityStruckByLightning(EntityStruckByLightningEvent event)
{		
	 if(((EntityLivingBase) event.entity).getHeldItem().getItem() == ExtraTools.redSword)
     {
    	 event.setCanceled(true);
     }
}

Minecraft crashed when spider I attacked was dying.

 

 

net.minecraft.util.ReportedException: Ticking entity

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:719) ~[MinecraftServer.class:?]

at net.minecraft.server.MinecraftServer.tick(MinecraftServer.java:624) ~[MinecraftServer.class:?]

at net.minecraft.server.integrated.IntegratedServer.tick(IntegratedServer.java:118) ~[integratedServer.class:?]

at net.minecraft.server.MinecraftServer.run(MinecraftServer.java:495) [MinecraftServer.class:?]

at net.minecraft.server.MinecraftServer$2.run(MinecraftServer.java:762) [MinecraftServer$2.class:?]

Caused by: java.lang.NullPointerException

at cz.extratools.ETEventHandler.onEntityStruckByLightning(ETEventHandler.java:17) ~[ETEventHandler.class:?]

at cpw.mods.fml.common.eventhandler.ASMEventHandler_18_ETEventHandler_onEntityStruckByLightning_EntityStruckByLightningEvent.invoke(.dynamic) ~[?:?]

at cpw.mods.fml.common.eventhandler.ASMEventHandler.invoke(ASMEventHandler.java:51) ~[ASMEventHandler.class:?]

at cpw.mods.fml.common.eventhandler.EventBus.post(EventBus.java:122) ~[EventBus.class:?]

at net.minecraftforge.event.ForgeEventFactory.onEntityStruckByLightning(ForgeEventFactory.java:142) ~[ForgeEventFactory.class:?]

at net.minecraft.entity.effect.EntityLightningBolt.onUpdate(EntityLightningBolt.java:117) ~[EntityLightningBolt.class:?]

at net.minecraft.world.World.updateEntities(World.java:1984) ~[World.class:?]

at net.minecraft.world.WorldServer.updateEntities(WorldServer.java:517) ~[WorldServer.class:?]

at net.minecraft.server.MinecraftServer.updateTimeLightAndEntities(MinecraftServer.java:713) ~[MinecraftServer.class:?]

... 4 more

 

 

EDIT: My Forge # is 1199

EDIT2: Realised it now, the spider has no items.

EDIT3: Made it working:

@SubscribeEvent
public void onEntityStruckByLightning(EntityStruckByLightningEvent event)
{		
	 if(event.entity instanceof EntityLivingBase)
	 if(((EntityLivingBase) event.entity).getHeldItem() != null)
	 if(((EntityLivingBase) event.entity).getHeldItem().getItem() == ExtraTools.redSword)
     {
    	 event.setCanceled(true);
     }
}

Now i just need to fix the problem when someone else is in the radius and has that sword (BTW it's the only sword that you can get the enchantment on)

Posted

maybe you can add a tag to the player when he starts hitting, in the death event check for the tag and remove it, so you know who should be the one not taking damage

Posted

Thank you guys, it looks like I finally did it.

 

Is this the best soulution?

 

@SubscribeEvent
public void onEntityStruckByLightning(EntityStruckByLightningEvent event)
{		
	 if(event.entity.getUniqueID().toString().equals(((LightningBoltExtended) event.lightning.getExtendedProperties("ChargedStrikeCreator")).getCreator()))
     {
    	 event.setCanceled(true);
     }
}

 

public void func_151368_a(EntityLivingBase entLB, Entity entity, int level)
    {
    	EntityLightningBolt lightning = new EntityLightningBolt(entity.worldObj, entity.posX, entity.posY, entity.posZ);
    	entity.worldObj.addWeatherEffect(lightning);
    	lightning.registerExtendedProperties("ChargedStrikeCreator", new LightningBoltExtended());
    	LightningBoltExtended prop = (LightningBoltExtended) lightning.getExtendedProperties("ChargedStrikeCreator");
    	prop.setCreator(entLB.getUniqueID().toString());
    }

 

public class LightningBoltExtended implements IExtendedEntityProperties 
{
    public final static String subCompound = "chargedStrikeCreator";
    protected EntityLightningBolt theEntity;
    protected World theWorld;
    public String playerId;

    @Override
    public void saveNBTData(NBTTagCompound parCompound)
    {
    	NBTTagCompound compound = new NBTTagCompound();
        parCompound.setTag(subCompound, compound);
        
        compound.setString("owner", playerId);
    }

    @Override
    public void loadNBTData(NBTTagCompound parCompound)

    {
        NBTTagCompound compound = (NBTTagCompound) 
        parCompound.getTag(subCompound);
        
        playerId = parCompound.getString("owner");	        
   }

   @Override
   public void init(Entity entity, World world)
   {
        theEntity = (EntityLightningBolt)entity;
        theWorld = world;
   }
   
   public void setCreator(String creator)
   {
	   playerId = creator;
   }
   
   public String getCreator()
   {
	   return playerId;
   }
}

 

Sorry about the look of the code, I am quite messy.

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.