TurnBasedMinecraftMod/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/AttackEventHandler.java

171 lines
8.9 KiB
Java
Raw Normal View History

2019-11-25 06:29:25 +00:00
package com.burnedkirby.TurnBasedMinecraft.common;
import java.util.Iterator;
2019-11-25 06:29:25 +00:00
import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketBattleMessage;
2019-11-25 06:29:25 +00:00
import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketEditingMessage;
import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketGeneralMessage;
import net.minecraft.entity.monster.CreeperEntity;
2019-10-21 07:13:11 +00:00
import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
2019-10-21 07:13:11 +00:00
import net.minecraftforge.eventbus.api.SubscribeEvent;
import net.minecraftforge.fml.network.PacketDistributor;
public class AttackEventHandler
{
private boolean isAttackerValid(LivingAttackEvent event)
{
2021-05-21 05:44:31 +00:00
if(event.getSource().getEntity() == null)
{
return false;
}
2021-05-21 05:44:31 +00:00
else if(event.getSource().getEntity().equals(TurnBasedMinecraftMod.proxy.getAttackingEntity()))
{
return true;
}
else
{
final long now = System.nanoTime();
boolean isValid = false;
synchronized(TurnBasedMinecraftMod.proxy.getAttackerViaBowSet())
{
for(Iterator<AttackerViaBow> iter = TurnBasedMinecraftMod.proxy.getAttackerViaBowSet().iterator(); iter.hasNext();)
{
AttackerViaBow attacker = iter.next();
if(now - attacker.attackTime >= AttackerViaBow.ATTACK_TIMEOUT)
{
iter.remove();
}
2021-05-21 05:44:31 +00:00
else if(event.getSource().getEntity().equals(attacker.entity) && event.getSource().isProjectile())
{
iter.remove();
if(!isValid)
{
Battle b = TurnBasedMinecraftMod.proxy.getBattleManager().getBattleByID(attacker.battleID);
if(b != null)
{
2021-05-21 05:44:31 +00:00
b.sendMessageToAllPlayers(PacketBattleMessage.MessageType.ARROW_HIT, attacker.entity.getId(), event.getEntity().getId(), 0);
}
isValid = true;
}
}
}
}
return isValid;
}
}
@SubscribeEvent
public void entityAttacked(LivingAttackEvent event)
{
2021-05-21 05:44:31 +00:00
if(event.getEntity().level.isClientSide)
{
return;
}
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)
{
2021-05-21 05:44:31 +00:00
final EditingInfo editingInfo = proxy.getEditingInfo(event.getSource().getEntity().getId());
if(editingInfo != null && editingInfo.isPendingEntitySelection)
{
editingInfo.isPendingEntitySelection = false;
event.setCanceled(true);
if(editingInfo.isEditingCustomName)
{
2019-10-21 07:13:11 +00:00
if(!event.getEntity().hasCustomName())
{
TurnBasedMinecraftMod.logger.error("Cannot edit custom name from entity without custom name");
TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity)editingInfo.editor), new PacketGeneralMessage("Cannot edit custom name from entity without custom name"));
return;
}
2021-05-21 05:44:31 +00:00
editingInfo.entityInfo = config.getCustomEntityInfo(event.getEntity().getCustomName().getString());
if(editingInfo.entityInfo == null)
{
editingInfo.entityInfo = new EntityInfo();
2019-10-21 07:13:11 +00:00
editingInfo.entityInfo.customName = event.getEntity().getCustomName().getString();
}
2021-05-21 05:44:31 +00:00
TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity)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() + "\"");
TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity)editingInfo.editor), new PacketEditingMessage(PacketEditingMessage.Type.PICK_EDIT, editingInfo.entityInfo));
}
else
{
2018-10-26 05:18:48 +00:00
editingInfo.entityInfo = config.getMatchingEntityInfo(event.getEntity());
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();
}
TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity)editingInfo.editor), new PacketGeneralMessage("Editing entity \"" + editingInfo.entityInfo.classType.getName() + "\""));
TurnBasedMinecraftMod.logger.info("Begin editing \"" + editingInfo.entityInfo.classType.getName() + "\"");
TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity)editingInfo.editor), new PacketEditingMessage(PacketEditingMessage.Type.PICK_EDIT, editingInfo.entityInfo));
}
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())))
{
2021-05-21 05:44:31 +00:00
if(event.getSource().getEntity().getEntity() instanceof CreeperEntity && TurnBasedMinecraftMod.proxy.getConfig().getCreeperAlwaysAllowDamage()) {
event.setCanceled(false);
} else {
// TurnBasedMinecraftMod.logger.debug("Canceled attack");
event.setCanceled(true);
}
return;
}
else if(!isAttackerValid(event)
&& 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())
&& event.getEntity().level.dimension().equals(event.getSource().getEntity().level.dimension())
&& battleManager.checkAttack(event))
{
// TurnBasedMinecraftMod.logger.debug("Canceled LivingAttackEvent between " + TurnBasedMinecraftMod.proxy.getAttackingEntity() + " and " + event.getEntity());
event.setCanceled(true);
} else {
2018-09-13 05:12:04 +00:00
// TurnBasedMinecraftMod.logger.debug("Did not cancel attack");
}
if(TurnBasedMinecraftMod.proxy.getAttackingDamage() < (int) event.getAmount())
{
TurnBasedMinecraftMod.proxy.setAttackingDamage((int) event.getAmount());
}
}
@SubscribeEvent
public void entityTargeted(LivingSetAttackTargetEvent event)
{
Config config = TurnBasedMinecraftMod.proxy.getConfig();
BattleManager battleManager = TurnBasedMinecraftMod.proxy.getBattleManager();
2021-05-21 05:44:31 +00:00
if(event.getEntity().level.isClientSide
|| config.isOldBattleBehaviorEnabled()
2021-05-21 05:44:31 +00:00
|| (event.getEntity() != null && battleManager.isRecentlyLeftBattle(event.getEntity().getId()))
|| (event.getTarget() != null && battleManager.isRecentlyLeftBattle(event.getTarget().getId()))
|| (event.getEntity() != null && event.getTarget() != null && Utility.distanceBetweenEntities(event.getEntity(), event.getTarget()) > (double)config.getAggroStartBattleDistance()))
{
return;
}
else if(event.getEntity() != null
&& event.getTarget() != null
2021-05-21 05:44:31 +00:00
&& !config.getBattleIgnoringPlayers().contains(event.getEntity().getId())
&& !config.getBattleIgnoringPlayers().contains(event.getTarget().getId())
&& event.getEntity().level.dimension().equals(event.getTarget().level.dimension()))
{
TurnBasedMinecraftMod.proxy.getBattleManager().checkTargeted(event);
}
}
}