From 519815f3209219e4794c8cdfb4c290d9f6137d07 Mon Sep 17 00:00:00 2001 From: micle Date: Wed, 29 Sep 2021 18:49:26 +0100 Subject: [PATCH] Added OnLivingSetAttackTargetEventHandler event. --- .../OnLivingSetAttackTargetEventHandler.java | 22 +++++++++++++++++++ .../loginprotection/setup/Registration.java | 1 + 2 files changed, 23 insertions(+) create mode 100644 src/main/java/com/micle/loginprotection/events/OnLivingSetAttackTargetEventHandler.java diff --git a/src/main/java/com/micle/loginprotection/events/OnLivingSetAttackTargetEventHandler.java b/src/main/java/com/micle/loginprotection/events/OnLivingSetAttackTargetEventHandler.java new file mode 100644 index 0000000..6a3b548 --- /dev/null +++ b/src/main/java/com/micle/loginprotection/events/OnLivingSetAttackTargetEventHandler.java @@ -0,0 +1,22 @@ +package com.micle.loginprotection.events; + +import com.micle.loginprotection.LoginProtection; +import com.micle.loginprotection.data.ProtectedPlayer; +import com.micle.loginprotection.setup.Config; +import net.minecraft.entity.MobEntity; +import net.minecraft.entity.player.PlayerEntity; +import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent; +import net.minecraftforge.eventbus.api.SubscribeEvent; + +public class OnLivingSetAttackTargetEventHandler { + @SubscribeEvent + public void LivingSetAttackTargetEvent(LivingSetAttackTargetEvent event) { + if (!(event.getTarget() instanceof PlayerEntity target)) { return; } + + ProtectedPlayer player = LoginProtection.protected_players.getPlayer(target.getUUID()); + if (player == null) { return; } + if (player.isLoading() && !Config.MAIN_IGNORE_PLAYER_ENABLED.get()) { return; } + if (!player.isLoading() && !Config.POST_GRACE_IGNORE_PLAYER_ENABLED.get()) { return; } + ((MobEntity) event.getEntityLiving()).setTarget(null); + } +} diff --git a/src/main/java/com/micle/loginprotection/setup/Registration.java b/src/main/java/com/micle/loginprotection/setup/Registration.java index c0ad513..cd6af9b 100755 --- a/src/main/java/com/micle/loginprotection/setup/Registration.java +++ b/src/main/java/com/micle/loginprotection/setup/Registration.java @@ -16,6 +16,7 @@ public class Registration { MinecraftForge.EVENT_BUS.register(new OnPlayerTickEventHandler()); MinecraftForge.EVENT_BUS.register(new OnKeyPressEventHandler()); MinecraftForge.EVENT_BUS.register(new OnPlayerLeaveEventHandler()); + MinecraftForge.EVENT_BUS.register(new OnLivingSetAttackTargetEventHandler()); int id = 0; LoginProtection.INSTANCE.registerMessage(id++,