Change ProjectileHitEvent to return a result instead of being cancelable. Closes #9642

This commit is contained in:
JustRed23 authored and LexManos committed 2023-07-13 17:21:13 -07:00
1 parent 54b08d2711
commit 2f229c5bb1
4 files changed
+117 -11

No files matched your search

@@ -667,9 +667,20 @@ public class ForgeEventFactory
return event.getCharge();
}
public static ProjectileImpactEvent.ImpactResult onProjectileImpactResult(Projectile projectile, HitResult ray)
{
ProjectileImpactEvent event = new ProjectileImpactEvent(projectile, ray);
// Remove this when the event is no longer cancelable
if (MinecraftForge.EVENT_BUS.post(event))
return ProjectileImpactEvent.ImpactResult.SKIP_ENTITY;
return event.getImpactResult();
}
public static boolean onProjectileImpact(Projectile projectile, HitResult ray)
{
return MinecraftForge.EVENT_BUS.post(new ProjectileImpactEvent(projectile, ray));
return onProjectileImpactResult(projectile, ray) != ProjectileImpactEvent.ImpactResult.DEFAULT;
}
public static LootTable loadLootTable(ResourceLocation name, LootTable table)
@@ -10,6 +10,9 @@ import net.minecraft.world.phys.HitResult;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.ForgeEventFactory;
import net.minecraftforge.eventbus.api.Cancelable;
import org.jetbrains.annotations.NotNull;
import java.util.Objects;
/**
* This event is fired on the {@link MinecraftForge#EVENT_BUS}.<br>
@@ -19,6 +22,8 @@ import net.minecraftforge.eventbus.api.Cancelable;
* 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.
* You can also directly set the {@link ImpactResult} to change the impact behaviour.
* @see #setImpactResult(ImpactResult)
*/
@Cancelable
public class ProjectileImpactEvent extends EntityEvent
@@ -26,6 +31,8 @@ public class ProjectileImpactEvent extends EntityEvent
private final HitResult ray;
private final Projectile projectile;
private ImpactResult result = ImpactResult.DEFAULT;
public ProjectileImpactEvent(Projectile projectile, HitResult ray)
{
super(projectile);
@@ -33,6 +40,16 @@ public class ProjectileImpactEvent extends EntityEvent
this.projectile = projectile;
}
/**
* {@inheritDoc}
* @deprecated Use {@link #setImpactResult(ImpactResult)} instead.
*/
@Deprecated(forRemoval = true, since = "1.20.1")
public void setCanceled(boolean cancel)
{
super.setCanceled(cancel);
}
public HitResult getRayTraceResult()
{
return ray;
@@ -42,4 +59,34 @@ public class ProjectileImpactEvent extends EntityEvent
{
return projectile;
}
public void setImpactResult(@NotNull ImpactResult newResult)
{
this.result = Objects.requireNonNull(newResult, "ImpactResult cannot be null");
}
public ImpactResult getImpactResult()
{
return result;
}
public enum ImpactResult
{
/**
* The default behaviour, the projectile will be destroyed and the hit will be processed.
*/
DEFAULT,
/**
* The projectile will pass through the current entity as if it wasn't there. This will return default behaviour if there is no entity.
*/
SKIP_ENTITY,
/**
* Damage the entity and stop the projectile here, the projectile will not pierce.
*/
STOP_AT_CURRENT,
/**
* Cancel the piercing aspect of the projectile, and do not damage the entity.
*/
STOP_AT_CURRENT_NO_DAMAGE
}
}