2019-11-25 06:29:25 +00:00
package com.burnedkirby.TurnBasedMinecraft.common ;
2018-09-12 05:43:59 +00:00
2018-10-18 04:34:57 +00:00
import java.util.Iterator ;
2018-09-17 06:49:10 +00:00
2019-11-25 06:29:25 +00:00
import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketBattleMessage ;
2018-09-17 06:49:10 +00:00
2019-11-25 06:29:25 +00:00
import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketEditingMessage ;
import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketGeneralMessage ;
2023-09-19 09:49:49 +00:00
import net.minecraft.world.damagesource.DamageTypes ;
2022-05-17 05:47:42 +00:00
import net.minecraft.world.entity.monster.Creeper ;
import net.minecraft.server.level.ServerPlayer ;
2018-09-12 05:43:59 +00:00
import net.minecraftforge.event.entity.living.LivingAttackEvent ;
2023-03-31 05:22:58 +00:00
import net.minecraftforge.event.entity.living.LivingChangeTargetEvent ;
2019-10-21 07:13:11 +00:00
import net.minecraftforge.eventbus.api.SubscribeEvent ;
2022-05-17 05:47:42 +00:00
import net.minecraftforge.network.PacketDistributor ;
2018-09-12 05:43:59 +00:00
public class AttackEventHandler
{
2018-09-17 06:49:10 +00:00
private boolean isAttackerValid ( LivingAttackEvent event )
{
2021-05-21 05:44:31 +00:00
if ( event . getSource ( ) . getEntity ( ) = = null )
2018-09-17 06:49:10 +00:00
{
return false ;
}
2021-05-21 05:44:31 +00:00
else if ( event . getSource ( ) . getEntity ( ) . equals ( TurnBasedMinecraftMod . proxy . getAttackingEntity ( ) ) )
2018-09-17 06:49:10 +00:00
{
return true ;
}
else
{
final long now = System . nanoTime ( ) ;
boolean isValid = false ;
2018-09-28 08:45:32 +00:00
synchronized ( TurnBasedMinecraftMod . proxy . getAttackerViaBowSet ( ) )
2018-09-17 06:49:10 +00:00
{
2018-10-18 04:34:57 +00:00
for ( Iterator < AttackerViaBow > iter = TurnBasedMinecraftMod . proxy . getAttackerViaBowSet ( ) . iterator ( ) ; iter . hasNext ( ) ; )
2018-09-17 06:49:10 +00:00
{
2018-10-18 04:34:57 +00:00
AttackerViaBow attacker = iter . next ( ) ;
2018-09-17 06:49:10 +00:00
if ( now - attacker . attackTime > = AttackerViaBow . ATTACK_TIMEOUT )
{
2018-10-18 04:34:57 +00:00
iter . remove ( ) ;
2018-09-17 06:49:10 +00:00
}
2023-09-19 09:49:49 +00:00
else if ( event . getSource ( ) . getEntity ( ) . equals ( attacker . entity ) & & event . getSource ( ) . is ( DamageTypes . ARROW ) )
2018-09-17 06:49:10 +00:00
{
2018-10-18 04:34:57 +00:00
iter . remove ( ) ;
2018-09-18 06:56:06 +00:00
if ( ! isValid )
2018-09-17 06:49:10 +00:00
{
2018-09-28 08:45:32 +00:00
Battle b = TurnBasedMinecraftMod . proxy . getBattleManager ( ) . getBattleByID ( attacker . battleID ) ;
2018-09-18 06:56:06 +00:00
if ( b ! = null )
{
2021-05-21 05:44:31 +00:00
b . sendMessageToAllPlayers ( PacketBattleMessage . MessageType . ARROW_HIT , attacker . entity . getId ( ) , event . getEntity ( ) . getId ( ) , 0 ) ;
2018-09-18 06:56:06 +00:00
}
isValid = true ;
2018-09-17 06:49:10 +00:00
}
}
}
}
return isValid ;
}
}
2018-09-12 05:43:59 +00:00
@SubscribeEvent
public void entityAttacked ( LivingAttackEvent event )
{
2023-09-19 09:49:49 +00:00
if ( event . getEntity ( ) . level ( ) . isClientSide )
2018-09-12 05:43:59 +00:00
{
return ;
}
2018-10-24 08:20:04 +00:00
CommonProxy proxy = TurnBasedMinecraftMod . proxy ;
Config config = proxy . getConfig ( ) ;
BattleManager battleManager = proxy . getBattleManager ( ) ;
// handle edit entity, pick entity via attack
{
2021-05-21 05:44:31 +00:00
if ( event . getSource ( ) . getEntity ( ) ! = null & & event . getEntity ( ) ! = null )
2018-10-24 08:20:04 +00:00
{
2021-05-21 05:44:31 +00:00
final EditingInfo editingInfo = proxy . getEditingInfo ( event . getSource ( ) . getEntity ( ) . getId ( ) ) ;
2018-10-24 08:20:04 +00:00
if ( editingInfo ! = null & & editingInfo . isPendingEntitySelection )
{
editingInfo . isPendingEntitySelection = false ;
event . setCanceled ( true ) ;
2018-10-26 04:35:20 +00:00
if ( editingInfo . isEditingCustomName )
2018-10-24 08:20:04 +00:00
{
2019-10-21 07:13:11 +00:00
if ( ! event . getEntity ( ) . hasCustomName ( ) )
2018-10-26 04:35:20 +00:00
{
TurnBasedMinecraftMod . logger . error ( " Cannot edit custom name from entity without custom name " ) ;
2022-05-17 05:47:42 +00:00
TurnBasedMinecraftMod . getHandler ( ) . send ( PacketDistributor . PLAYER . with ( ( ) - > ( ServerPlayer ) editingInfo . editor ) , new PacketGeneralMessage ( " Cannot edit custom name from entity without custom name " ) ) ;
2018-10-26 04:35:20 +00:00
return ;
}
2021-05-21 05:44:31 +00:00
editingInfo . entityInfo = config . getCustomEntityInfo ( event . getEntity ( ) . getCustomName ( ) . getString ( ) ) ;
2018-10-26 04:35:20 +00:00
if ( editingInfo . entityInfo = = null )
{
editingInfo . entityInfo = new EntityInfo ( ) ;
2019-10-21 07:13:11 +00:00
editingInfo . entityInfo . customName = event . getEntity ( ) . getCustomName ( ) . getString ( ) ;
2018-10-26 04:35:20 +00:00
}
2022-05-17 05:47:42 +00:00
TurnBasedMinecraftMod . getHandler ( ) . send ( PacketDistributor . PLAYER . with ( ( ) - > ( ServerPlayer ) editingInfo . editor ) , new PacketGeneralMessage ( " Editing custom name \" " + event . getEntity ( ) . getCustomName ( ) . getString ( ) + " \" " ) ) ;
2019-10-21 07:13:11 +00:00
TurnBasedMinecraftMod . logger . info ( " Begin editing custom \" " + event . getEntity ( ) . getCustomName ( ) . getString ( ) + " \" " ) ;
2022-05-17 05:47:42 +00:00
TurnBasedMinecraftMod . getHandler ( ) . send ( PacketDistributor . PLAYER . with ( ( ) - > ( ServerPlayer ) editingInfo . editor ) , new PacketEditingMessage ( PacketEditingMessage . Type . PICK_EDIT , editingInfo . entityInfo ) ) ;
2018-10-26 04:35:20 +00:00
}
else
{
2018-10-26 05:18:48 +00:00
editingInfo . entityInfo = config . getMatchingEntityInfo ( event . getEntity ( ) ) ;
2018-10-26 04:35:20 +00:00
if ( editingInfo . entityInfo = = null )
{
editingInfo . entityInfo = new EntityInfo ( ) ;
editingInfo . entityInfo . classType = event . getEntity ( ) . getClass ( ) ;
}
2018-10-26 05:18:48 +00:00
else
{
editingInfo . entityInfo = editingInfo . entityInfo . clone ( ) ;
}
2022-05-17 05:47:42 +00:00
TurnBasedMinecraftMod . getHandler ( ) . send ( PacketDistributor . PLAYER . with ( ( ) - > ( ServerPlayer ) editingInfo . editor ) , new PacketGeneralMessage ( " Editing entity \" " + editingInfo . entityInfo . classType . getName ( ) + " \" " ) ) ;
2018-10-26 04:35:20 +00:00
TurnBasedMinecraftMod . logger . info ( " Begin editing \" " + editingInfo . entityInfo . classType . getName ( ) + " \" " ) ;
2022-05-17 05:47:42 +00:00
TurnBasedMinecraftMod . getHandler ( ) . send ( PacketDistributor . PLAYER . with ( ( ) - > ( ServerPlayer ) editingInfo . editor ) , new PacketEditingMessage ( PacketEditingMessage . Type . PICK_EDIT , editingInfo . entityInfo ) ) ;
2018-10-24 08:20:04 +00:00
}
return ;
}
}
}
2021-05-21 05:44:31 +00:00
if ( event . getEntity ( ) ! = null & & event . getSource ( ) . getEntity ( ) ! = null & & ( battleManager . isRecentlyLeftBattle ( event . getEntity ( ) . getId ( ) ) | | battleManager . isRecentlyLeftBattle ( event . getSource ( ) . getEntity ( ) . getId ( ) ) ) )
2018-10-17 09:28:47 +00:00
{
2022-05-17 05:47:42 +00:00
if ( event . getSource ( ) . getEntity ( ) instanceof Creeper & & TurnBasedMinecraftMod . proxy . getConfig ( ) . getCreeperAlwaysAllowDamage ( ) ) {
2020-11-18 08:24:18 +00:00
event . setCanceled ( false ) ;
} else {
2019-11-28 07:32:34 +00:00
// TurnBasedMinecraftMod.logger.debug("Canceled attack");
2020-11-18 08:24:18 +00:00
event . setCanceled ( true ) ;
}
2018-10-17 09:28:47 +00:00
return ;
}
else if ( ! isAttackerValid ( event )
2018-10-16 05:02:44 +00:00
& & event . getEntity ( ) ! = null
2021-05-21 05:44:31 +00:00
& & event . getSource ( ) . getEntity ( ) ! = null
& & event . getEntity ( ) ! = event . getSource ( ) . getEntity ( )
& & ! config . getBattleIgnoringPlayers ( ) . contains ( event . getSource ( ) . getEntity ( ) . getId ( ) )
& & ! config . getBattleIgnoringPlayers ( ) . contains ( event . getEntity ( ) . getId ( ) )
2023-09-19 09:49:49 +00:00
& & event . getEntity ( ) . level ( ) . dimension ( ) . equals ( event . getSource ( ) . getEntity ( ) . level ( ) . dimension ( ) )
2018-10-17 09:28:47 +00:00
& & battleManager . checkAttack ( event ) )
2018-09-12 05:43:59 +00:00
{
2019-11-28 07:32:34 +00:00
// TurnBasedMinecraftMod.logger.debug("Canceled LivingAttackEvent between " + TurnBasedMinecraftMod.proxy.getAttackingEntity() + " and " + event.getEntity());
2018-09-12 05:43:59 +00:00
event . setCanceled ( true ) ;
2019-11-28 07:32:34 +00:00
} else {
2018-09-13 05:12:04 +00:00
// TurnBasedMinecraftMod.logger.debug("Did not cancel attack");
2018-09-12 05:43:59 +00:00
}
2019-11-28 07:32:34 +00:00
2018-09-28 08:45:32 +00:00
if ( TurnBasedMinecraftMod . proxy . getAttackingDamage ( ) < ( int ) event . getAmount ( ) )
2018-09-12 05:43:59 +00:00
{
2018-09-28 08:45:32 +00:00
TurnBasedMinecraftMod . proxy . setAttackingDamage ( ( int ) event . getAmount ( ) ) ;
2018-09-12 05:43:59 +00:00
}
}
2018-10-17 08:15:23 +00:00
@SubscribeEvent
2023-03-31 05:22:58 +00:00
public void entityTargeted ( LivingChangeTargetEvent event )
2018-10-17 08:15:23 +00:00
{
2018-10-17 09:28:47 +00:00
Config config = TurnBasedMinecraftMod . proxy . getConfig ( ) ;
BattleManager battleManager = TurnBasedMinecraftMod . proxy . getBattleManager ( ) ;
2023-09-19 09:49:49 +00:00
if ( event . getEntity ( ) . level ( ) . isClientSide
2018-10-17 09:28:47 +00:00
| | config . isOldBattleBehaviorEnabled ( )
2021-05-21 05:44:31 +00:00
| | ( event . getEntity ( ) ! = null & & battleManager . isRecentlyLeftBattle ( event . getEntity ( ) . getId ( ) ) )
2023-03-31 05:22:58 +00:00
| | ( event . getNewTarget ( ) ! = null & & battleManager . isRecentlyLeftBattle ( event . getNewTarget ( ) . getId ( ) ) )
| | ( event . getEntity ( ) ! = null & & event . getNewTarget ( ) ! = null & & Utility . distanceBetweenEntities ( event . getEntity ( ) , event . getNewTarget ( ) ) > ( double ) config . getAggroStartBattleDistance ( ) ) )
2018-10-17 08:15:23 +00:00
{
return ;
}
else if ( event . getEntity ( ) ! = null
2023-03-31 05:22:58 +00:00
& & event . getNewTarget ( ) ! = null
2021-05-21 05:44:31 +00:00
& & ! config . getBattleIgnoringPlayers ( ) . contains ( event . getEntity ( ) . getId ( ) )
2023-03-31 05:22:58 +00:00
& & ! config . getBattleIgnoringPlayers ( ) . contains ( event . getNewTarget ( ) . getId ( ) )
2023-09-19 09:49:49 +00:00
& & event . getEntity ( ) . level ( ) . dimension ( ) . equals ( event . getNewTarget ( ) . level ( ) . dimension ( ) ) )
2018-10-17 08:15:23 +00:00
{
TurnBasedMinecraftMod . proxy . getBattleManager ( ) . checkTargeted ( event ) ;
}
}
2018-09-12 05:43:59 +00:00
}