Skip to content

Commit

Permalink
Use proper even for Struck By lighting
Browse files Browse the repository at this point in the history
  • Loading branch information
AlphaMode committed Dec 11, 2023
1 parent f47f7d7 commit c2a27d1
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,17 +71,21 @@ public Entity getEntity() {
e.onEntityEnterSection(entity, packedOldPos, packedNewPos);
});

/**
* Will be removed in 1.20.2 and the new method will be renamed back to "STRUCK_BY_LIGHTING"
*/
@Deprecated(forRemoval = true)
public static final Event<LightingStrike> STRUCK_BY_LIGHTING = EventFactory.createArrayBacked(LightingStrike.class, callbacks -> (entity, lightningBolt) -> {
for (LightingStrike callback : callbacks)
if (callback.onEntityStruckByLightning(entity, lightningBolt))
return true;
return false;
});

@Override
public void sendEvent() {

}
public static final Event<NewLightingStrike> ENTITY_STRUCK_BY_LIGHTING = EventFactory.createArrayBacked(NewLightingStrike.class, callbacks -> event -> {
for (NewLightingStrike callback : callbacks)
callback.onEntityStruckByLightning(event);
});

@FunctionalInterface
public interface EnteringSection {
Expand All @@ -108,6 +112,11 @@ public interface LightingStrike {
boolean onEntityStruckByLightning(Entity entity, LightningBolt bolt);
}

@FunctionalInterface
public interface NewLightingStrike {
void onEntityStruckByLightning(EntityStruckByLightningEvent event);
}

@FunctionalInterface
public interface Teleport {
void onTeleport(EntityTeleportEvent event);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.github.fabricators_of_create.porting_lib.entity.events;

import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LightningBolt;

public class EntityStruckByLightningEvent extends EntityEvents {
private final LightningBolt lightning;

public EntityStruckByLightningEvent(Entity entity, LightningBolt lightning) {
super(entity);
this.lightning = lightning;
}

public LightningBolt getLightning() {
return lightning;
}

@Override
public void sendEvent() {
ENTITY_STRUCK_BY_LIGHTING.invoker().onEntityStruckByLightning(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.llamalad7.mixinextras.injector.WrapWithCondition;

import io.github.fabricators_of_create.porting_lib.entity.events.EntityEvents;
import io.github.fabricators_of_create.porting_lib.entity.events.EntityStruckByLightningEvent;
import net.minecraft.server.level.ServerLevel;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LightningBolt;
Expand All @@ -14,6 +15,9 @@
public class LightningBoltMixin {
@WrapWithCondition(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/Entity;thunderHit(Lnet/minecraft/server/level/ServerLevel;Lnet/minecraft/world/entity/LightningBolt;)V"))
private boolean shouldStrikeEntity(Entity entity, ServerLevel level, LightningBolt lightningBolt) {
return !EntityEvents.STRUCK_BY_LIGHTING.invoker().onEntityStruckByLightning(entity, lightningBolt);
var event = new EntityStruckByLightningEvent(entity, lightningBolt);
event.setCanceled(EntityEvents.STRUCK_BY_LIGHTING.invoker().onEntityStruckByLightning(entity, lightningBolt));
event.sendEvent();
return !event.isCanceled();
}
}

0 comments on commit c2a27d1

Please sign in to comment.