package com.seodisparate.TurnBasedMinecraft.common;
+import java.util.ArrayDeque;
+import java.util.Queue;
+
+import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
+
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.fml.common.eventhandler.SubscribeEvent;
public class AttackEventHandler
{
+ private boolean isAttackerValid(LivingAttackEvent event)
+ {
+ if(event.getSource().getTrueSource() == null)
+ {
+ return false;
+ }
+ else if(event.getSource().getTrueSource().equals(TurnBasedMinecraftMod.attackingEntity))
+ {
+ return true;
+ }
+ else
+ {
+ Queue<AttackerViaBow> removeQueue = new ArrayDeque<AttackerViaBow>();
+ final long now = System.nanoTime();
+ boolean isValid = false;
+ synchronized(TurnBasedMinecraftMod.attackerViaBow)
+ {
+ for(AttackerViaBow attacker : TurnBasedMinecraftMod.attackerViaBow)
+ {
+ if(now - attacker.attackTime >= AttackerViaBow.ATTACK_TIMEOUT)
+ {
+ removeQueue.add(attacker);
+ }
+ else if(event.getSource().getTrueSource().equals(attacker.entity) && event.getSource().isProjectile())
+ {
+ removeQueue.add(attacker);
+ isValid = true;
+ Battle b = TurnBasedMinecraftMod.battleManager.getBattleByID(attacker.battleID);
+ if(b != null)
+ {
+ b.sendMessageToAllPlayers(PacketBattleMessage.MessageType.ARROW_HIT, attacker.entity.getEntityId(), event.getEntity().getEntityId(), 0);
+ }
+ }
+ }
+ AttackerViaBow next = removeQueue.poll();
+ while(next != null)
+ {
+ TurnBasedMinecraftMod.attackerViaBow.remove(next);
+ next = removeQueue.poll();
+ }
+ }
+ return isValid;
+ }
+ }
+
@SubscribeEvent
public void entityAttacked(LivingAttackEvent event)
{
TurnBasedMinecraftMod.battleManager = new BattleManager(TurnBasedMinecraftMod.logger);
}
- if(!(event.getSource().getTrueSource() == null || event.getSource().getTrueSource().equals(TurnBasedMinecraftMod.attackingEntity)) && TurnBasedMinecraftMod.battleManager.checkAttack(event))
+ if(!isAttackerValid(event) && event.getEntity() != null && event.getSource().getTrueSource() != null && TurnBasedMinecraftMod.battleManager.checkAttack(event))
{
// TurnBasedMinecraftMod.logger.debug("Canceled LivingAttackEvent between " + TurnBasedMinecraftMod.attackingEntity + " and " + event.getEntity());
event.setCanceled(true);
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
+import net.minecraft.item.ItemBow;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemLingeringPotion;
import net.minecraft.item.ItemPotion;
}
Combatant newCombatant = new Combatant(e, entityInfo);
newCombatant.isSideA = true;
+ newCombatant.battleID = getId();
this.sideA.put(e.getEntityId(), newCombatant);
if(e instanceof EntityPlayer)
{
}
Combatant newCombatant = new Combatant(e, entityInfo);
newCombatant.isSideA = false;
+ newCombatant.battleID = getId();
this.sideB.put(e.getEntityId(), newCombatant);
if(e instanceof EntityPlayer)
{
}
Combatant newCombatant = new Combatant(e, entityInfo);
newCombatant.isSideA = true;
+ newCombatant.battleID = getId();
sideA.put(e.getEntityId(), newCombatant);
if(e instanceof EntityPlayer)
{
}
Combatant newCombatant = new Combatant(e, entityInfo);
newCombatant.isSideA = false;
+ newCombatant.battleID = getId();
sideB.put(e.getEntityId(), newCombatant);
if(e instanceof EntityPlayer)
{
}
}
- private void sendMessageToAllPlayers(PacketBattleMessage.MessageType type, int from, int to, int amount)
+ protected void sendMessageToAllPlayers(PacketBattleMessage.MessageType type, int from, int to, int amount)
{
if(!isServer)
{
}
}
- private void sendMessageToAllPlayers(PacketBattleMessage.MessageType type, int from, int to, int amount, String custom)
+ protected void sendMessageToAllPlayers(PacketBattleMessage.MessageType type, int from, int to, int amount, String custom)
{
if(!isServer)
{
{
break;
}
+ ItemStack heldItemStack = ((EntityPlayer)next.entity).getHeldItemMainhand();
+ if(heldItemStack.getItem() instanceof ItemBow)
+ {
+ if(Utility.doesPlayerHaveArrows((EntityPlayer)next.entity))
+ {
+ final Entity nextEntity = next.entity;
+ final Entity targetEntity = target.entity;
+ next.entity.getServer().addScheduledTask(() -> {
+ // have player look at attack target
+ ((EntityPlayerMP)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, Utility.yawDirection(nextEntity.posX, nextEntity.posZ, targetEntity.posX, targetEntity.posZ), Utility.pitchDirection(nextEntity.posX, nextEntity.posY, nextEntity.posZ, targetEntity.posX, targetEntity.posY, targetEntity.posZ));
+ ItemBow itemBow = (ItemBow)heldItemStack.getItem();
+ synchronized(TurnBasedMinecraftMod.attackerViaBow)
+ {
+ TurnBasedMinecraftMod.attackerViaBow.add(new AttackerViaBow(nextEntity, getId()));
+ }
+ itemBow.onPlayerStoppedUsing(((EntityPlayer)nextEntity).getHeldItemMainhand(), nextEntity.getEntityWorld(), (EntityLivingBase)nextEntity, (int)(Math.random() * (itemBow.getMaxItemUseDuration(heldItemStack)) / 3));
+ sendMessageToAllPlayers(PacketBattleMessage.MessageType.FIRED_ARROW, nextEntity.getEntityId(), targetEntity.getEntityId(), 0);
+ });
+ }
+ else
+ {
+ sendMessageToAllPlayers(PacketBattleMessage.MessageType.BOW_NO_AMMO, next.entity.getEntityId(), 0, 0);
+ }
+ next = turnOrderQueue.poll();
+ continue;
+ }
int hitChance = TurnBasedMinecraftMod.config.getPlayerAttackProbability();
if(target.entity instanceof EntityPlayer)
{
if(target.remainingDefenses <= 0)
{
// attack
- // TODO damage via bow and arrow
- // have player look at attack target
final Entity nextEntity = next.entity;
final Entity targetEntity = target.entity;
final EntityInfo targetEntityInfo = target.entityInfo;
next.entity.getServer().addScheduledTask(() -> {
+ // have player look at attack target
((EntityPlayerMP)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, Utility.yawDirection(nextEntity.posX, nextEntity.posZ, targetEntity.posX, targetEntity.posZ), Utility.pitchDirection(nextEntity.posX, nextEntity.posY, nextEntity.posZ, targetEntity.posX, targetEntity.posY, targetEntity.posZ));
TurnBasedMinecraftMod.attackingEntity = nextEntity;
TurnBasedMinecraftMod.attackingDamage = 0;