2018-09-12 05:43:59 +00:00
package com.seodisparate.TurnBasedMinecraft.common ;
2018-10-18 04:34:57 +00:00
import java.util.Iterator ;
2018-09-17 06:49:10 +00:00
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage ;
2018-10-24 08:20:04 +00:00
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketEditingMessage ;
2018-10-26 04:35:20 +00:00
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketGeneralMessage ;
2018-10-24 08:20:04 +00:00
import net.minecraft.entity.player.EntityPlayerMP ;
2018-09-12 05:43:59 +00:00
import net.minecraftforge.event.entity.living.LivingAttackEvent ;
2018-10-17 08:15:23 +00:00
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent ;
2018-09-12 05:43:59 +00:00
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent ;
public class AttackEventHandler
{
2018-09-17 06:49:10 +00:00
private boolean isAttackerValid ( LivingAttackEvent event )
{
if ( event . getSource ( ) . getTrueSource ( ) = = null )
{
return false ;
}
2018-09-28 08:45:32 +00:00
else if ( event . getSource ( ) . getTrueSource ( ) . 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
}
else if ( event . getSource ( ) . getTrueSource ( ) . equals ( attacker . entity ) & & event . getSource ( ) . isProjectile ( ) )
{
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 )
{
b . sendMessageToAllPlayers ( PacketBattleMessage . MessageType . ARROW_HIT , attacker . entity . getEntityId ( ) , event . getEntity ( ) . getEntityId ( ) , 0 ) ;
}
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 )
{
if ( event . getEntity ( ) . world . isRemote )
{
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
{
EditingInfo editingInfo = null ;
if ( event . getSource ( ) . getTrueSource ( ) ! = null & & event . getEntity ( ) ! = null )
{
editingInfo = proxy . getEditingInfo ( event . getSource ( ) . getTrueSource ( ) . getEntityId ( ) ) ;
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
{
2018-10-26 04:35:20 +00:00
if ( event . getEntity ( ) . getCustomNameTag ( ) . isEmpty ( ) )
{
TurnBasedMinecraftMod . logger . error ( " Cannot edit custom name from entity without custom name " ) ;
TurnBasedMinecraftMod . NWINSTANCE . sendTo ( new PacketGeneralMessage ( " Cannot edit custom name from entity without custom name " ) , ( EntityPlayerMP ) editingInfo . editor ) ;
return ;
}
editingInfo . entityInfo = config . getCustomEntityInfo ( event . getEntity ( ) . getCustomNameTag ( ) ) ;
if ( editingInfo . entityInfo = = null )
{
editingInfo . entityInfo = new EntityInfo ( ) ;
editingInfo . entityInfo . customName = event . getEntity ( ) . getCustomNameTag ( ) ;
}
TurnBasedMinecraftMod . NWINSTANCE . sendTo ( new PacketGeneralMessage ( " Editing custom name \" " + event . getEntity ( ) . getCustomNameTag ( ) + " \" " ) , ( EntityPlayerMP ) editingInfo . editor ) ;
TurnBasedMinecraftMod . logger . info ( " Begin editing custom \" " + event . getEntity ( ) . getCustomNameTag ( ) + " \" " ) ;
TurnBasedMinecraftMod . NWINSTANCE . sendTo ( new PacketEditingMessage ( PacketEditingMessage . Type . PICK_EDIT , editingInfo . entityInfo ) , ( EntityPlayerMP ) editingInfo . editor ) ;
}
else
{
editingInfo . entityInfo = config . getMatchingEntityInfo ( event . getEntity ( ) ) . clone ( ) ;
if ( editingInfo . entityInfo = = null )
{
editingInfo . entityInfo = new EntityInfo ( ) ;
editingInfo . entityInfo . classType = event . getEntity ( ) . getClass ( ) ;
}
TurnBasedMinecraftMod . NWINSTANCE . sendTo ( new PacketGeneralMessage ( " Editing entity \" " + editingInfo . entityInfo . classType . getName ( ) + " \" " ) , ( EntityPlayerMP ) editingInfo . editor ) ;
TurnBasedMinecraftMod . logger . info ( " Begin editing \" " + editingInfo . entityInfo . classType . getName ( ) + " \" " ) ;
TurnBasedMinecraftMod . NWINSTANCE . sendTo ( new PacketEditingMessage ( PacketEditingMessage . Type . PICK_EDIT , editingInfo . entityInfo ) , ( EntityPlayerMP ) editingInfo . editor ) ;
2018-10-24 08:20:04 +00:00
}
return ;
}
}
}
2018-10-26 04:35:20 +00:00
if ( event . getEntity ( ) ! = null & & event . getSource ( ) . getTrueSource ( ) ! = null & & ( battleManager . isRecentlyLeftBattle ( event . getEntity ( ) . getEntityId ( ) ) | | battleManager . isRecentlyLeftBattle ( event . getSource ( ) . getTrueSource ( ) . getEntityId ( ) ) ) )
2018-10-17 09:28:47 +00:00
{
event . setCanceled ( true ) ;
return ;
}
else if ( ! isAttackerValid ( event )
2018-10-16 05:02:44 +00:00
& & event . getEntity ( ) ! = null
& & event . getSource ( ) . getTrueSource ( ) ! = null
2018-10-20 11:06:18 +00:00
& & event . getEntity ( ) ! = event . getSource ( ) . getTrueSource ( )
2018-10-17 09:28:47 +00:00
& & ! config . getBattleIgnoringPlayers ( ) . contains ( event . getSource ( ) . getTrueSource ( ) . getEntityId ( ) )
& & ! config . getBattleIgnoringPlayers ( ) . contains ( event . getEntity ( ) . getEntityId ( ) )
& & battleManager . checkAttack ( event ) )
2018-09-12 05:43:59 +00:00
{
2018-09-28 08:45:32 +00:00
// TurnBasedMinecraftMod.logger.debug("Canceled LivingAttackEvent between " + TurnBasedMinecraftMod.commonProxy.getAttackingEntity() + " and " + event.getEntity());
2018-09-12 05:43:59 +00:00
event . setCanceled ( true ) ;
}
else
{
2018-09-13 05:12:04 +00:00
// TurnBasedMinecraftMod.logger.debug("Did not cancel attack");
2018-09-12 05:43:59 +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
public void entityTargeted ( LivingSetAttackTargetEvent event )
{
2018-10-17 09:28:47 +00:00
Config config = TurnBasedMinecraftMod . proxy . getConfig ( ) ;
BattleManager battleManager = TurnBasedMinecraftMod . proxy . getBattleManager ( ) ;
if ( event . getEntity ( ) . world . isRemote
| | config . isOldBattleBehaviorEnabled ( )
| | ( event . getEntity ( ) ! = null & & battleManager . isRecentlyLeftBattle ( event . getEntity ( ) . getEntityId ( ) ) )
2018-10-18 07:26:09 +00:00
| | ( event . getTarget ( ) ! = null & & battleManager . isRecentlyLeftBattle ( event . getTarget ( ) . getEntityId ( ) ) )
| | ( event . getEntity ( ) ! = null & & event . getTarget ( ) ! = null & & Utility . distanceBetweenEntities ( event . getEntity ( ) , event . getTarget ( ) ) > ( double ) config . getAggroStartBattleDistance ( ) ) )
2018-10-17 08:15:23 +00:00
{
return ;
}
else if ( event . getEntity ( ) ! = null
& & event . getTarget ( ) ! = null
2018-10-17 09:28:47 +00:00
& & ! config . getBattleIgnoringPlayers ( ) . contains ( event . getEntity ( ) . getEntityId ( ) )
& & ! config . getBattleIgnoringPlayers ( ) . contains ( event . getTarget ( ) . getEntityId ( ) ) )
2018-10-17 08:15:23 +00:00
{
TurnBasedMinecraftMod . proxy . getBattleManager ( ) . checkTargeted ( event ) ;
}
}
2018-09-12 05:43:59 +00:00
}