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-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-17 09:28:47 +00:00
|
|
|
Config config = TurnBasedMinecraftMod.proxy.getConfig();
|
|
|
|
BattleManager battleManager = TurnBasedMinecraftMod.proxy.getBattleManager();
|
|
|
|
if((event.getEntity() != null && battleManager.isRecentlyLeftBattle(event.getEntity().getEntityId()))
|
|
|
|
|| (event.getSource().getTrueSource() != null && battleManager.isRecentlyLeftBattle(event.getSource().getTrueSource().getEntityId())))
|
|
|
|
{
|
|
|
|
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-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()))
|
|
|
|
|| (event.getTarget() != null && battleManager.isRecentlyLeftBattle(event.getTarget().getEntityId())))
|
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
|
|
|
}
|