2018-09-12 05:43:59 +00:00
|
|
|
package com.seodisparate.TurnBasedMinecraft.common;
|
|
|
|
|
2018-09-17 06:49:10 +00:00
|
|
|
import java.util.ArrayDeque;
|
|
|
|
import java.util.Queue;
|
|
|
|
|
|
|
|
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
|
|
|
|
|
2018-09-12 05:43:59 +00:00
|
|
|
import net.minecraftforge.event.entity.living.LivingAttackEvent;
|
|
|
|
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
|
|
|
|
{
|
|
|
|
Queue<AttackerViaBow> removeQueue = new ArrayDeque<AttackerViaBow>();
|
|
|
|
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-09-28 08:45:32 +00:00
|
|
|
for(AttackerViaBow attacker : TurnBasedMinecraftMod.proxy.getAttackerViaBowSet())
|
2018-09-17 06:49:10 +00:00
|
|
|
{
|
|
|
|
if(now - attacker.attackTime >= AttackerViaBow.ATTACK_TIMEOUT)
|
|
|
|
{
|
|
|
|
removeQueue.add(attacker);
|
|
|
|
}
|
|
|
|
else if(event.getSource().getTrueSource().equals(attacker.entity) && event.getSource().isProjectile())
|
|
|
|
{
|
|
|
|
removeQueue.add(attacker);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-18 06:56:06 +00:00
|
|
|
for(AttackerViaBow next = removeQueue.poll(); next != null; next = removeQueue.poll())
|
2018-09-17 06:49:10 +00:00
|
|
|
{
|
2018-09-28 08:45:32 +00:00
|
|
|
TurnBasedMinecraftMod.proxy.getAttackerViaBowSet().remove(next);
|
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-16 05:02:44 +00:00
|
|
|
if(!isAttackerValid(event)
|
|
|
|
&& event.getEntity() != null
|
|
|
|
&& event.getSource().getTrueSource() != null
|
|
|
|
&& !TurnBasedMinecraftMod.proxy.getConfig().getBattleIgnoringPlayers().contains(event.getSource().getTrueSource().getEntityId())
|
|
|
|
&& !TurnBasedMinecraftMod.proxy.getConfig().getBattleIgnoringPlayers().contains(event.getEntity().getEntityId())
|
|
|
|
&& TurnBasedMinecraftMod.proxy.getBattleManager().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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|