generated from TropheusJ/fabric-example-mod
-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ProjectileImpactEvent and deprecate ProjectileImpactCallback
- Loading branch information
Showing
11 changed files
with
234 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
...java/io/github/fabricators_of_create/porting_lib/entity/events/ProjectileImpactEvent.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package io.github.fabricators_of_create.porting_lib.entity.events; | ||
|
||
import net.minecraft.world.entity.projectile.Projectile; | ||
import net.minecraft.world.phys.HitResult; | ||
|
||
/** | ||
* This event is fired when a projectile entity impacts something.<br> | ||
* This event is fired for all vanilla projectiles by Porting Lib, | ||
* custom projectiles should fire this event and check the result in a similar fashion. | ||
* This event is cancelable. When canceled, the impact will not be processed and the projectile will continue flying. | ||
* Killing or other handling of the entity after event cancellation is up to the modder. | ||
*/ | ||
public class ProjectileImpactEvent extends EntityEvents { | ||
private final HitResult ray; | ||
private final Projectile projectile; | ||
|
||
public ProjectileImpactEvent(Projectile projectile, HitResult ray) { | ||
super(projectile); | ||
this.ray = ray; | ||
this.projectile = projectile; | ||
} | ||
|
||
public HitResult getRayTraceResult() { | ||
return ray; | ||
} | ||
|
||
public Projectile getProjectile() { | ||
return projectile; | ||
} | ||
|
||
@Override | ||
public void sendEvent() { | ||
// setCanceled(ProjectileImpactCallback.EVENT.invoker().onImpact(getProjectile(), getRayTraceResult())); ProjectileImpactCallback fires for all projectiles including none vanilla | ||
PROJECTILE_IMPACT.invoker().onProjectileImpact(this); | ||
} | ||
} |
46 changes: 46 additions & 0 deletions
46
...a/io/github/fabricators_of_create/porting_lib/entity/mixin/common/AbstractArrowMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package io.github.fabricators_of_create.porting_lib.entity.mixin.common; | ||
|
||
import com.llamalad7.mixinextras.injector.WrapWithCondition; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.Operation; | ||
import com.llamalad7.mixinextras.injector.wrapoperation.WrapOperation; | ||
import com.llamalad7.mixinextras.sugar.Share; | ||
|
||
import com.llamalad7.mixinextras.sugar.ref.LocalBooleanRef; | ||
|
||
import com.llamalad7.mixinextras.sugar.ref.LocalRef; | ||
|
||
import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactEvent; | ||
import net.minecraft.world.entity.Entity; | ||
import net.minecraft.world.entity.EntityType; | ||
import net.minecraft.world.entity.projectile.AbstractArrow; | ||
|
||
import net.minecraft.world.level.Level; | ||
import net.minecraft.world.phys.HitResult; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
@Mixin(AbstractArrow.class) | ||
public abstract class AbstractArrowMixin extends Entity { | ||
public AbstractArrowMixin(EntityType<?> variant, Level world) { | ||
super(variant, world); | ||
} | ||
|
||
@WrapWithCondition(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/AbstractArrow;onHit(Lnet/minecraft/world/phys/HitResult;)V")) | ||
private boolean onImpact(AbstractArrow arrow, HitResult result, @Share("event") LocalRef<ProjectileImpactEvent> eventRef, @Share("state") LocalBooleanRef hasImpulseState) { | ||
ProjectileImpactEvent event = new ProjectileImpactEvent(arrow, result); | ||
event.sendEvent(); | ||
eventRef.set(event); | ||
hasImpulseState.set(this.hasImpulse); | ||
return !event.isCanceled(); | ||
} | ||
|
||
@WrapOperation(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/AbstractArrow;getPierceLevel()B")) | ||
private byte shouldBreak(AbstractArrow instance, Operation<Byte> original, @Share("event") LocalRef<ProjectileImpactEvent> eventRef, @Share("state") LocalBooleanRef hasImpulseState) { | ||
if (eventRef.get().isCanceled()) { | ||
this.hasImpulse = hasImpulseState.get(); | ||
return 0; | ||
} | ||
return original.call(instance); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...fabricators_of_create/porting_lib/entity/mixin/common/AbstractHurtingProjectileMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.github.fabricators_of_create.porting_lib.entity.mixin.common; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import com.llamalad7.mixinextras.injector.WrapWithCondition; | ||
|
||
import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactEvent; | ||
import net.minecraft.world.entity.projectile.AbstractHurtingProjectile; | ||
import net.minecraft.world.phys.HitResult; | ||
|
||
@Mixin(AbstractHurtingProjectile.class) | ||
public class AbstractHurtingProjectileMixin { | ||
@WrapWithCondition(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/AbstractHurtingProjectile;onHit(Lnet/minecraft/world/phys/HitResult;)V")) | ||
private boolean onImpact(AbstractHurtingProjectile projectile, HitResult result) { | ||
ProjectileImpactEvent event = new ProjectileImpactEvent(projectile, result); | ||
event.sendEvent(); | ||
return !event.isCanceled(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...thub/fabricators_of_create/porting_lib/entity/mixin/common/FireworkRocketEntityMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.fabricators_of_create.porting_lib.entity.mixin.common; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import com.llamalad7.mixinextras.injector.WrapWithCondition; | ||
|
||
import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactEvent; | ||
import net.minecraft.world.entity.projectile.FireworkRocketEntity; | ||
import net.minecraft.world.phys.HitResult; | ||
|
||
@Mixin(FireworkRocketEntity.class) | ||
public class FireworkRocketEntityMixin { | ||
@WrapWithCondition(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/FireworkRocketEntity;onHit(Lnet/minecraft/world/phys/HitResult;)V")) | ||
private boolean onImpact(FireworkRocketEntity projectile, HitResult result) { | ||
if (result.getType() == HitResult.Type.MISS) | ||
return true; | ||
ProjectileImpactEvent event = new ProjectileImpactEvent(projectile, result); | ||
event.sendEvent(); | ||
return !event.isCanceled(); | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
...ava/io/github/fabricators_of_create/porting_lib/entity/mixin/common/FishingHookMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package io.github.fabricators_of_create.porting_lib.entity.mixin.common; | ||
|
||
import com.llamalad7.mixinextras.injector.WrapWithCondition; | ||
|
||
import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactEvent; | ||
import net.minecraft.world.entity.projectile.AbstractHurtingProjectile; | ||
import net.minecraft.world.entity.projectile.FishingHook; | ||
|
||
import net.minecraft.world.phys.HitResult; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
@Mixin(FishingHook.class) | ||
public class FishingHookMixin { | ||
@WrapWithCondition(method = "checkCollision", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/FishingHook;onHit(Lnet/minecraft/world/phys/HitResult;)V")) | ||
private boolean onImpact(FishingHook projectile, HitResult result) { | ||
if (result.getType() == HitResult.Type.MISS) | ||
return true; | ||
ProjectileImpactEvent event = new ProjectileImpactEvent(projectile, result); | ||
event.sendEvent(); | ||
return !event.isCanceled(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
.../java/io/github/fabricators_of_create/porting_lib/entity/mixin/common/LlamaSpitMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.fabricators_of_create.porting_lib.entity.mixin.common; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import com.llamalad7.mixinextras.injector.WrapWithCondition; | ||
|
||
import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactEvent; | ||
import net.minecraft.world.entity.projectile.LlamaSpit; | ||
import net.minecraft.world.phys.HitResult; | ||
|
||
@Mixin(LlamaSpit.class) | ||
public class LlamaSpitMixin { | ||
@WrapWithCondition(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/LlamaSpit;onHit(Lnet/minecraft/world/phys/HitResult;)V")) | ||
private boolean onImpact(LlamaSpit projectile, HitResult result) { | ||
if (result.getType() == HitResult.Type.MISS) | ||
return true; // NeoForge prevents any misses here, however vanilla doesn't so we keep vanilla behavior | ||
ProjectileImpactEvent event = new ProjectileImpactEvent(projectile, result); | ||
event.sendEvent(); | ||
return !event.isCanceled(); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
...a/io/github/fabricators_of_create/porting_lib/entity/mixin/common/ShulkerBulletMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package io.github.fabricators_of_create.porting_lib.entity.mixin.common; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
import com.llamalad7.mixinextras.injector.WrapWithCondition; | ||
|
||
import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactEvent; | ||
import net.minecraft.world.entity.projectile.ShulkerBullet; | ||
import net.minecraft.world.phys.HitResult; | ||
|
||
@Mixin(ShulkerBullet.class) | ||
public class ShulkerBulletMixin { | ||
@WrapWithCondition(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/ShulkerBullet;onHit(Lnet/minecraft/world/phys/HitResult;)V")) | ||
private boolean onImpact(ShulkerBullet projectile, HitResult result) { | ||
ProjectileImpactEvent event = new ProjectileImpactEvent(projectile, result); | ||
event.sendEvent(); | ||
return !event.isCanceled(); | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...ithub/fabricators_of_create/porting_lib/entity/mixin/common/ThrowableProjectileMixin.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package io.github.fabricators_of_create.porting_lib.entity.mixin.common; | ||
|
||
import com.llamalad7.mixinextras.injector.WrapWithCondition; | ||
|
||
import io.github.fabricators_of_create.porting_lib.entity.events.ProjectileImpactEvent; | ||
import net.minecraft.world.entity.projectile.ShulkerBullet; | ||
import net.minecraft.world.entity.projectile.ThrowableProjectile; | ||
|
||
import net.minecraft.world.phys.HitResult; | ||
|
||
import org.spongepowered.asm.mixin.Mixin; | ||
import org.spongepowered.asm.mixin.injection.At; | ||
|
||
@Mixin(ThrowableProjectile.class) | ||
public class ThrowableProjectileMixin { | ||
@WrapWithCondition(method = "tick", at = @At(value = "INVOKE", target = "Lnet/minecraft/world/entity/projectile/ThrowableProjectile;onHit(Lnet/minecraft/world/phys/HitResult;)V")) | ||
private boolean onImpact(ThrowableProjectile projectile, HitResult result) { | ||
ProjectileImpactEvent event = new ProjectileImpactEvent(projectile, result); | ||
event.sendEvent(); | ||
return !event.isCanceled(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters