[1.19.x] Make LivingSetAttackTargetEvent compatible with the Brain/Behavior system. (Port of PR #8918) (#8954)

This commit is contained in:
Budschie authored and GitHub committed 2022-09-03 20:11:10 +01:00
1 parent 4e2d656e9d
commit ed198f062c
7 files changed
+228 -10

No files matched your search

@@ -0,0 +1,44 @@
package net.minecraftforge.debug.entity.living;
import net.minecraft.world.entity.monster.piglin.AbstractPiglin;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Items;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.event.entity.living.LivingChangeTargetEvent;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.common.Mod;
@Mod("living_set_attack_target_event_test")
public class LivingSetAttackTargetEventTest
{
public static final boolean ENABLE = true;
public LivingSetAttackTargetEventTest()
{
if(ENABLE)
{
MinecraftForge.EVENT_BUS.register(this);
}
}
@SubscribeEvent
public void onLivingSetAttackTarget(LivingSetAttackTargetEvent event)
{
// Make piglins die when they start attacking a player who is not holding a stick in their main hand.
if (event.getTarget() instanceof Player player && event.getEntity() instanceof AbstractPiglin piglin && player.getMainHandItem().getItem() != Items.STICK)
{
event.getEntity().kill();
}
}
@SubscribeEvent
public void onLivingChangeTargetEvent(LivingChangeTargetEvent event)
{
// Prevents the piglin from attacking the player if they hold a stick in their hands.
if(event.getNewTarget() instanceof Player player && event.getEntity() instanceof AbstractPiglin piglin && player.getMainHandItem().getItem() == Items.STICK)
{
event.setCanceled(true);
}
}
}