[1.18.x] Add the projectile search event. (#8322)

Co-authored-by: noeppi_noeppi <[email protected]>
This commit is contained in:
Jonathan Colmenaresandnoeppi_noeppi authored and GitHub committed 2022-03-24 21:52:58 +00:00
1 parent 02fd859d5c
commit c3bd365baa
7 files changed
+173

No files matched your search

@@ -150,6 +150,7 @@ import net.minecraftforge.event.entity.player.AnvilRepairEvent;
import net.minecraftforge.event.entity.player.AttackEntityEvent;
import net.minecraftforge.event.entity.player.CriticalHitEvent;
import net.minecraftforge.event.entity.player.PlayerEvent;
import net.minecraftforge.event.entity.living.LivingGetProjectileEvent;
import net.minecraftforge.event.entity.player.PlayerInteractEvent;
import net.minecraftforge.event.world.BiomeLoadingEvent;
import net.minecraftforge.event.world.BlockEvent;
@@ -1014,6 +1015,16 @@ public class ForgeHooks
return event.getModifiers();
}
/**
* Hook to fire {@link LivingGetProjectileEvent}. Returns the ammo to be used.
*/
public static ItemStack getProjectile(LivingEntity entity, ItemStack projectileWeaponItem, ItemStack projectile)
{
LivingGetProjectileEvent event = new LivingGetProjectileEvent(entity, projectileWeaponItem, projectile);
MinecraftForge.EVENT_BUS.post(event);
return event.getProjectileItemStack();
}
/**
* Used as the default implementation of {@link Item#getCreatorModId}. Call that method instead.
*/
@@ -0,0 +1,70 @@
/*
* Minecraft Forge - Forge Development LLC
* SPDX-License-Identifier: LGPL-2.1-only
*/
package net.minecraftforge.event.entity.living;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.item.ItemStack;
/**
* This event is fired when a living entity attempts to get a projectile with the
* {@link LivingEntity#getProjectile(ItemStack)} method. The item stack given is usually the item stack of a
* {@link net.minecraft.world.item.ProjectileWeaponItem} and the item stack returned is usually the item stack of a
* {@link net.minecraft.world.entity.projectile.Projectile}.
* <p>
* This event is not {@link net.minecraftforge.eventbus.api.Cancelable}.
* <p>
* This event does not have a result. {@link net.minecraftforge.eventbus.api.Event.HasResult}
* <p>
* This event is fired on the {@link net.minecraftforge.common.MinecraftForge#EVENT_BUS}.
*/
public class LivingGetProjectileEvent extends LivingEvent
{
private final ItemStack projectileWeaponItemStack;
private ItemStack projectileItemStack;
public LivingGetProjectileEvent(LivingEntity livingEntity, ItemStack projectileWeaponItemStack, ItemStack ammo)
{
super(livingEntity);
this.projectileWeaponItemStack = projectileWeaponItemStack;
this.projectileItemStack = ammo;
}
/**
* @return The itemstack of the itrm that is looking for a projectile. With vanilla behavior, this usually returns
* an itemstack of a {@link net.minecraft.world.item.ProjectileWeaponItem}, but it's possible for that to not be the
* case if modder uses a different implementation of {@link LivingEntity#getProjectile(ItemStack)}.
*/
public ItemStack getProjectileWeaponItemStack()
{
return this.projectileWeaponItemStack;
}
/**
* @return The itemstack of the projectile found. Initially this is set to the projectile found by vanilla
* behaviour, but it's possible for thatnot to be the case if a modder uses a different implementation of
* {@link LivingEntity#getProjectile(ItemStack)}.
*/
public ItemStack getProjectileItemStack()
{
return this.projectileItemStack;
}
/**
* Sets the projectile itemstack to be used.
* <p>
* If the entity is a player: whenever the projectile is fired/consumed the stack will be shrunk by
* one. To disable this behaviour you can copy the stack before giving it to the event. For bows, you can use
* {@link net.minecraftforge.event.entity.player.ArrowLooseEvent} to remove the arrow yourself.
* <p>
* Be aware that since this event fires every time a living entity gets a projectile, whether or not its
* {@link LivingEntity#level} is client-side, you will want to make a conditional to always set the item stack to
* what you'd want it to be to avoid client-server desyncs.
*/
public void setProjectileItemStack(ItemStack projectileItemStack)
{
this.projectileItemStack = projectileItemStack;
}
}