mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-26 23:13:15 +00:00
[1.18.x] Add the projectile search event. (#8322)
Co-authored-by: noeppi_noeppi <[email protected]>
This commit is contained in:
1 parent
02fd859d5c
commit
c3bd365baa
7 files changed
+173
No files matched your search
@@ -540,6 +540,15 @@
|
||||
}
|
||||
|
||||
public boolean m_5830_() {
|
||||
@@ -3139,7 +_,7 @@
|
||||
}
|
||||
|
||||
public ItemStack m_6298_(ItemStack p_21272_) {
|
||||
- return ItemStack.f_41583_;
|
||||
+ return net.minecraftforge.common.ForgeHooks.getProjectile(this, p_21272_, ItemStack.f_41583_);
|
||||
}
|
||||
|
||||
public ItemStack m_5584_(Level p_21067_, ItemStack p_21068_) {
|
||||
@@ -3196,6 +_,64 @@
|
||||
this.m_21166_(p_21191_ == InteractionHand.MAIN_HAND ? EquipmentSlot.MAINHAND : EquipmentSlot.OFFHAND);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,14 @@
|
||||
--- a/net/minecraft/world/entity/monster/Monster.java
|
||||
+++ b/net/minecraft/world/entity/monster/Monster.java
|
||||
@@ -116,9 +_,9 @@
|
||||
if (p_33038_.m_41720_() instanceof ProjectileWeaponItem) {
|
||||
Predicate<ItemStack> predicate = ((ProjectileWeaponItem)p_33038_.m_41720_()).m_6442_();
|
||||
ItemStack itemstack = ProjectileWeaponItem.m_43010_(this, predicate);
|
||||
- return itemstack.m_41619_() ? new ItemStack(Items.f_42412_) : itemstack;
|
||||
+ return net.minecraftforge.common.ForgeHooks.getProjectile(this, p_33038_, itemstack.m_41619_() ? new ItemStack(Items.f_42412_) : itemstack);
|
||||
} else {
|
||||
- return ItemStack.f_41583_;
|
||||
+ return net.minecraftforge.common.ForgeHooks.getProjectile(this, p_33038_, ItemStack.f_41583_);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -346,6 +346,28 @@
|
||||
return this.m_36218_(mutablecomponent);
|
||||
}
|
||||
|
||||
@@ -1895,18 +_,18 @@
|
||||
Predicate<ItemStack> predicate = ((ProjectileWeaponItem)p_36349_.m_41720_()).m_6442_();
|
||||
ItemStack itemstack = ProjectileWeaponItem.m_43010_(this, predicate);
|
||||
if (!itemstack.m_41619_()) {
|
||||
- return itemstack;
|
||||
+ return net.minecraftforge.common.ForgeHooks.getProjectile(this, p_36349_, itemstack);
|
||||
} else {
|
||||
predicate = ((ProjectileWeaponItem)p_36349_.m_41720_()).m_6437_();
|
||||
|
||||
for(int i = 0; i < this.f_36093_.m_6643_(); ++i) {
|
||||
ItemStack itemstack1 = this.f_36093_.m_8020_(i);
|
||||
if (predicate.test(itemstack1)) {
|
||||
- return itemstack1;
|
||||
+ return net.minecraftforge.common.ForgeHooks.getProjectile(this, p_36349_, itemstack1);
|
||||
}
|
||||
}
|
||||
|
||||
- return this.f_36077_.f_35937_ ? new ItemStack(Items.f_42412_) : ItemStack.f_41583_;
|
||||
+ return net.minecraftforge.common.ForgeHooks.getProjectile(this, p_36349_, this.f_36077_.f_35937_ ? new ItemStack(Items.f_42412_) : ItemStack.f_41583_);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1991,5 +_,63 @@
|
||||
public Component m_36423_() {
|
||||
return this.f_36413_;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
+45
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Minecraft Forge - Forge Development LLC
|
||||
* SPDX-License-Identifier: LGPL-2.1-only
|
||||
*/
|
||||
|
||||
package net.minecraftforge.debug.entity.living;
|
||||
|
||||
import net.minecraft.world.entity.player.Player;
|
||||
import net.minecraft.world.item.ItemStack;
|
||||
import net.minecraft.world.item.Items;
|
||||
import net.minecraftforge.common.MinecraftForge;
|
||||
import net.minecraftforge.event.entity.living.LivingGetProjectileEvent;
|
||||
import net.minecraftforge.event.entity.player.ArrowLooseEvent;
|
||||
import net.minecraftforge.fml.common.Mod;
|
||||
import org.apache.logging.log4j.LogManager;
|
||||
import org.apache.logging.log4j.Logger;
|
||||
|
||||
@Mod("living_get_projectile_event_test")
|
||||
public class LivingGetProjectileEventTest
|
||||
{
|
||||
public static final boolean ENABLED = true;
|
||||
public static final Logger LOGGER = LogManager.getLogger();
|
||||
|
||||
public LivingGetProjectileEventTest()
|
||||
{
|
||||
if (ENABLED)
|
||||
{
|
||||
MinecraftForge.EVENT_BUS.addListener(this::onLivingGetProjectile);
|
||||
}
|
||||
}
|
||||
|
||||
public void onLivingGetProjectile(LivingGetProjectileEvent event)
|
||||
{
|
||||
LOGGER.info("{} about to fire {} with a {}", event.getEntity(), event.getProjectileItemStack(), event.getProjectileWeaponItemStack());
|
||||
|
||||
// for this test, we're checking if the player has a spectral arrow itemstack in their offhand and if they're firing a normal arrow.
|
||||
// if they do, we're going to use that itemstack. if not, we will create a spectral arrow itemstack
|
||||
// this demonstrates the usage of specific itemstacks with this event. you can use specific itemstacks from the player's inventory in this similar style
|
||||
if (event.getEntityLiving() instanceof Player player && event.getProjectileItemStack().getItem() == Items.ARROW)
|
||||
{
|
||||
ItemStack offhandItem = player.getOffhandItem();
|
||||
event.setProjectileItemStack(offhandItem.getItem() == Items.SPECTRAL_ARROW ? offhandItem : new ItemStack(Items.SPECTRAL_ARROW));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -210,5 +210,7 @@ license="LGPL v2.1"
|
||||
modId="potion_size_event_test"
|
||||
[[mods]]
|
||||
modId="hide_neighbor_face_test"
|
||||
[[mods]]
|
||||
modId="living_get_projectile_event_test"
|
||||
|
||||
# ADD ABOVE THIS LINE
|
||||
Reference in new issue
Block a user