Fix TurnBasedMinecraft/common/Battle.java

This commit is contained in:
Stephen Seo 2019-10-28 11:49:28 +09:00
parent f4b1bc9487
commit 6c49470509

View file

@ -8,19 +8,13 @@ import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleInfo;
import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage; import com.seodisparate.TurnBasedMinecraft.common.networking.PacketBattleMessage;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving; import net.minecraft.entity.LivingEntity;
import net.minecraft.entity.EntityLivingBase; import net.minecraft.entity.MobEntity;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.EntityPlayerMP; import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.Item; import net.minecraft.item.*;
import net.minecraft.item.ItemBow;
import net.minecraft.item.ItemFood;
import net.minecraft.item.ItemLingeringPotion;
import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemSplashPotion;
import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource; import net.minecraft.util.DamageSource;
import net.minecraftforge.fml.common.FMLCommonHandler; import net.minecraftforge.fml.network.PacketDistributor;
public class Battle public class Battle
{ {
@ -134,13 +128,13 @@ public class Battle
{ {
for(Entity e : sideA) for(Entity e : sideA)
{ {
EntityInfo entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(e.getCustomNameTag()); EntityInfo entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(e.getCustomName().getUnformattedComponentText());
if(entityInfo == null) if(entityInfo == null)
{ {
entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e); entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e);
} }
if(entityInfo == null && !(e instanceof EntityPlayer) && TurnBasedMinecraftMod.proxy.isServerRunning()) if(entityInfo == null && !(e instanceof PlayerEntity) && TurnBasedMinecraftMod.proxy.isServerRunning())
{ {
continue; continue;
} }
@ -148,7 +142,7 @@ public class Battle
newCombatant.isSideA = true; newCombatant.isSideA = true;
newCombatant.battleID = getId(); newCombatant.battleID = getId();
this.sideA.put(e.getEntityId(), newCombatant); this.sideA.put(e.getEntityId(), newCombatant);
if(e instanceof EntityPlayer) if(e instanceof PlayerEntity)
{ {
newCombatant.recalcSpeedOnCompare = true; newCombatant.recalcSpeedOnCompare = true;
playerCount.incrementAndGet(); playerCount.incrementAndGet();
@ -167,13 +161,13 @@ public class Battle
{ {
for(Entity e : sideB) for(Entity e : sideB)
{ {
EntityInfo entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(e.getCustomNameTag()); EntityInfo entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(e.getCustomName().getUnformattedComponentText());
if(entityInfo == null) if(entityInfo == null)
{ {
entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e); entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e);
} }
if(entityInfo == null && !(e instanceof EntityPlayer) && TurnBasedMinecraftMod.proxy.isServerRunning()) if(entityInfo == null && !(e instanceof PlayerEntity) && TurnBasedMinecraftMod.proxy.isServerRunning())
{ {
continue; continue;
} }
@ -181,7 +175,7 @@ public class Battle
newCombatant.isSideA = false; newCombatant.isSideA = false;
newCombatant.battleID = getId(); newCombatant.battleID = getId();
this.sideB.put(e.getEntityId(), newCombatant); this.sideB.put(e.getEntityId(), newCombatant);
if(e instanceof EntityPlayer) if(e instanceof PlayerEntity)
{ {
newCombatant.recalcSpeedOnCompare = true; newCombatant.recalcSpeedOnCompare = true;
playerCount.incrementAndGet(); playerCount.incrementAndGet();
@ -205,7 +199,7 @@ public class Battle
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, c.entityInfo.category); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, c.entityInfo.category);
} }
else if(c.entity instanceof EntityPlayer) else if(c.entity instanceof PlayerEntity)
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, "player"); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, "player");
} }
@ -220,7 +214,7 @@ public class Battle
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, c.entityInfo.category); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, c.entityInfo.category);
} }
else if(c.entity instanceof EntityPlayer) else if(c.entity instanceof PlayerEntity)
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, "player"); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, c.entity.getEntityId(), 0, id, "player");
} }
@ -302,13 +296,13 @@ public class Battle
public void addCombatantToSideA(Entity e) public void addCombatantToSideA(Entity e)
{ {
EntityInfo entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(e.getCustomNameTag()); EntityInfo entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(e.getCustomName().getUnformattedComponentText());
if(entityInfo == null) if(entityInfo == null)
{ {
entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e); entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e);
} }
if(entityInfo == null && !(e instanceof EntityPlayer) && TurnBasedMinecraftMod.proxy.isServerRunning()) if(entityInfo == null && !(e instanceof PlayerEntity) && TurnBasedMinecraftMod.proxy.isServerRunning())
{ {
return; return;
} }
@ -326,7 +320,7 @@ public class Battle
{ {
sideA.put(e.getEntityId(), newCombatant); sideA.put(e.getEntityId(), newCombatant);
} }
if(e instanceof EntityPlayer) if(e instanceof PlayerEntity)
{ {
newCombatant.recalcSpeedOnCompare = true; newCombatant.recalcSpeedOnCompare = true;
playerCount.incrementAndGet(); playerCount.incrementAndGet();
@ -349,7 +343,7 @@ public class Battle
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, newCombatant.entityInfo.category); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, newCombatant.entityInfo.category);
} }
else if(newCombatant.entity instanceof EntityPlayer) else if(newCombatant.entity instanceof PlayerEntity)
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, "player"); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, "player");
} }
@ -363,13 +357,13 @@ public class Battle
public void addCombatantToSideB(Entity e) public void addCombatantToSideB(Entity e)
{ {
EntityInfo entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(e.getCustomNameTag()); EntityInfo entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getCustomEntityInfoReference(e.getCustomName().getUnformattedComponentText());
if(entityInfo == null) if(entityInfo == null)
{ {
entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e); entityInfo = TurnBasedMinecraftMod.proxy.getConfig().getMatchingEntityInfo(e);
} }
if(entityInfo == null && !(e instanceof EntityPlayer) && TurnBasedMinecraftMod.proxy.isServerRunning()) if(entityInfo == null && !(e instanceof PlayerEntity) && TurnBasedMinecraftMod.proxy.isServerRunning())
{ {
return; return;
} }
@ -387,7 +381,7 @@ public class Battle
{ {
sideB.put(e.getEntityId(), newCombatant); sideB.put(e.getEntityId(), newCombatant);
} }
if(e instanceof EntityPlayer) if(e instanceof PlayerEntity)
{ {
newCombatant.recalcSpeedOnCompare = true; newCombatant.recalcSpeedOnCompare = true;
playerCount.incrementAndGet(); playerCount.incrementAndGet();
@ -410,7 +404,7 @@ public class Battle
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, newCombatant.entityInfo.category); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, newCombatant.entityInfo.category);
} }
else if(newCombatant.entity instanceof EntityPlayer) else if(newCombatant.entity instanceof PlayerEntity)
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, "player"); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, "player");
} }
@ -544,7 +538,7 @@ public class Battle
PacketBattleInfo infoPacket = new PacketBattleInfo(getSideAIDs(), getSideBIDs(), timer); PacketBattleInfo infoPacket = new PacketBattleInfo(getSideAIDs(), getSideBIDs(), timer);
for(Combatant p : players.values()) for(Combatant p : players.values())
{ {
TurnBasedMinecraftMod.NWINSTANCE.sendTo(infoPacket, (EntityPlayerMP)p.entity); TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity)p.entity), infoPacket);
} }
} }
@ -559,11 +553,12 @@ public class Battle
{ {
return; return;
} }
PacketBattleMessage packet = new PacketBattleMessage(type, from, to, amount, custom);
for(Combatant p : players.values()) for(Combatant p : players.values())
{ {
if(p.entity.isEntityAlive()) if(p.entity.isAlive())
{ {
TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(type, from, to, amount, custom), (EntityPlayerMP)p.entity); TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) p.entity), packet);
} }
} }
} }
@ -577,7 +572,7 @@ public class Battle
for(Iterator<Map.Entry<Integer, Combatant>> iter = sideA.entrySet().iterator(); iter.hasNext();) for(Iterator<Map.Entry<Integer, Combatant>> iter = sideA.entrySet().iterator(); iter.hasNext();)
{ {
Map.Entry<Integer, Combatant> entry = iter.next(); Map.Entry<Integer, Combatant> entry = iter.next();
if(!entry.getValue().entity.isEntityAlive()) if(!entry.getValue().entity.isAlive())
{ {
iter.remove(); iter.remove();
players.remove(entry.getKey()); players.remove(entry.getKey());
@ -588,7 +583,7 @@ public class Battle
{ {
category = entry.getValue().entityInfo.category; category = entry.getValue().entityInfo.category;
} }
else if(entry.getValue().entity instanceof EntityPlayer) else if(entry.getValue().entity instanceof PlayerEntity)
{ {
category = "player"; category = "player";
} }
@ -598,7 +593,7 @@ public class Battle
for(Iterator<Map.Entry<Integer, Combatant>> iter = sideB.entrySet().iterator(); iter.hasNext();) for(Iterator<Map.Entry<Integer, Combatant>> iter = sideB.entrySet().iterator(); iter.hasNext();)
{ {
Map.Entry<Integer, Combatant> entry = iter.next(); Map.Entry<Integer, Combatant> entry = iter.next();
if(!entry.getValue().entity.isEntityAlive()) if(!entry.getValue().entity.isAlive())
{ {
iter.remove(); iter.remove();
players.remove(entry.getKey()); players.remove(entry.getKey());
@ -609,7 +604,7 @@ public class Battle
{ {
category = entry.getValue().entityInfo.category; category = entry.getValue().entityInfo.category;
} }
else if(entry.getValue().entity instanceof EntityPlayer) else if(entry.getValue().entity instanceof PlayerEntity)
{ {
category = "player"; category = "player";
} }
@ -637,7 +632,7 @@ public class Battle
for(Iterator<Map.Entry<Integer, Combatant>> iter = players.entrySet().iterator(); iter.hasNext();) for(Iterator<Map.Entry<Integer, Combatant>> iter = players.entrySet().iterator(); iter.hasNext();)
{ {
Map.Entry<Integer, Combatant> entry = iter.next(); Map.Entry<Integer, Combatant> entry = iter.next();
if(entry.getValue().entity != null && ((EntityPlayer)entry.getValue().entity).isCreative()) if(entry.getValue().entity != null && ((PlayerEntity)entry.getValue().entity).isCreative())
{ {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.BECAME_CREATIVE, entry.getValue().entity.getEntityId(), 0, 0); sendMessageToAllPlayers(PacketBattleMessage.MessageType.BECAME_CREATIVE, entry.getValue().entity.getEntityId(), 0, 0);
iter.remove(); iter.remove();
@ -696,9 +691,9 @@ public class Battle
private void removeCombatantPostRemove(Combatant c) private void removeCombatantPostRemove(Combatant c)
{ {
if(c.entity instanceof EntityPlayer) if(c.entity instanceof PlayerEntity)
{ {
TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, 0, 0, 0), (EntityPlayerMP)c.entity); TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) c.entity), new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, 0, 0, 0));
} }
battleManager.addRecentlyLeftBattle(c); battleManager.addRecentlyLeftBattle(c);
} }
@ -808,7 +803,7 @@ public class Battle
for(Combatant c : sideA.values()) for(Combatant c : sideA.values())
{ {
// picking decision for sideA non-players // picking decision for sideA non-players
if(!(c.entity instanceof EntityPlayer) && c.decision == Decision.UNDECIDED && c.entityInfo != null) if(!(c.entity instanceof PlayerEntity) && c.decision == Decision.UNDECIDED && c.entityInfo != null)
{ {
int percentage = random.nextInt(100); int percentage = random.nextInt(100);
if(percentage < c.entityInfo.decisionAttack) if(percentage < c.entityInfo.decisionAttack)
@ -827,7 +822,7 @@ public class Battle
} }
for(Combatant c : sideB.values()) for(Combatant c : sideB.values())
{ {
if(!(c.entity instanceof EntityPlayer) && c.decision == Decision.UNDECIDED && c.entityInfo != null) if(!(c.entity instanceof PlayerEntity) && c.decision == Decision.UNDECIDED && c.entityInfo != null)
{ {
int percentage = random.nextInt(100); int percentage = random.nextInt(100);
if(percentage < c.entityInfo.decisionAttack) if(percentage < c.entityInfo.decisionAttack)
@ -874,12 +869,12 @@ public class Battle
{ {
for(Combatant next = turnOrderQueue.poll(); next != null; next = turnOrderQueue.poll()) for(Combatant next = turnOrderQueue.poll(); next != null; next = turnOrderQueue.poll())
{ {
if(!next.entity.isEntityAlive()) if(!next.entity.isAlive())
{ {
continue; continue;
} }
debugLog = next.entity.getName(); debugLog = next.entity.getDisplayName().getUnformattedComponentText();
next.remainingDefenses = 0; next.remainingDefenses = 0;
@ -895,7 +890,7 @@ public class Battle
case ATTACK: case ATTACK:
debugLog += " attack"; debugLog += " attack";
Combatant target = null; Combatant target = null;
if(next.entity instanceof EntityPlayer) if(next.entity instanceof PlayerEntity)
{ {
debugLog += " as player"; debugLog += " as player";
target = sideA.get(next.targetEntityID); target = sideA.get(next.targetEntityID);
@ -903,37 +898,35 @@ public class Battle
{ {
target = sideB.get(next.targetEntityID); target = sideB.get(next.targetEntityID);
} }
if(target == null || !target.entity.isEntityAlive() || target == next) if(target == null || !target.entity.isAlive() || target == next)
{ {
continue; continue;
} }
ItemStack heldItemStack = ((EntityPlayer)next.entity).getHeldItemMainhand(); ItemStack heldItemStack = ((PlayerEntity)next.entity).getHeldItemMainhand();
if(heldItemStack.getItem() instanceof ItemBow) if(heldItemStack.getItem() instanceof BowItem)
{ {
debugLog += " with bow"; debugLog += " with bow";
if(Utility.doesPlayerHaveArrows((EntityPlayer)next.entity)) if(Utility.doesPlayerHaveArrows((PlayerEntity)next.entity))
{ {
final Entity nextEntity = next.entity; final Entity nextEntity = next.entity;
final Entity targetEntity = target.entity; final Entity targetEntity = target.entity;
final float yawDirection = Utility.yawDirection(next.entity.posX, next.entity.posZ, target.entity.posX, target.entity.posZ); final float yawDirection = Utility.yawDirection(next.entity.posX, next.entity.posZ, target.entity.posX, target.entity.posZ);
final float pitchDirection = Utility.pitchDirection(next.entity.posX, next.entity.posY, next.entity.posZ, target.entity.posX, target.entity.posY, target.entity.posZ); final float pitchDirection = Utility.pitchDirection(next.entity.posX, next.entity.posY, next.entity.posZ, target.entity.posX, target.entity.posY, target.entity.posZ);
final int randomTimeLeft = random.nextInt(heldItemStack.getItem().getMaxItemUseDuration(heldItemStack) / 3); final int randomTimeLeft = random.nextInt(heldItemStack.getItem().getUseDuration(heldItemStack) / 3);
if(TurnBasedMinecraftMod.proxy.getConfig().isFreezeCombatantsEnabled()) if(TurnBasedMinecraftMod.proxy.getConfig().isFreezeCombatantsEnabled())
{ {
next.yaw = yawDirection; next.yaw = yawDirection;
next.pitch = pitchDirection; next.pitch = pitchDirection;
} }
next.entity.getServer().addScheduledTask(() -> { // have player look at attack target
// have player look at attack target ((ServerPlayerEntity)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, yawDirection, pitchDirection);
((EntityPlayerMP)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, yawDirection, pitchDirection); BowItem itemBow = (BowItem)heldItemStack.getItem();
ItemBow itemBow = (ItemBow)heldItemStack.getItem(); synchronized(TurnBasedMinecraftMod.proxy.getAttackerViaBowSet())
synchronized(TurnBasedMinecraftMod.proxy.getAttackerViaBowSet()) {
{ TurnBasedMinecraftMod.proxy.getAttackerViaBowSet().add(new AttackerViaBow(nextEntity, getId()));
TurnBasedMinecraftMod.proxy.getAttackerViaBowSet().add(new AttackerViaBow(nextEntity, getId())); }
} itemBow.onPlayerStoppedUsing(((PlayerEntity)nextEntity).getHeldItemMainhand(), nextEntity.getEntityWorld(), (LivingEntity) nextEntity, randomTimeLeft);
itemBow.onPlayerStoppedUsing(((EntityPlayer)nextEntity).getHeldItemMainhand(), nextEntity.getEntityWorld(), (EntityLivingBase)nextEntity, randomTimeLeft); sendMessageToAllPlayers(PacketBattleMessage.MessageType.FIRED_ARROW, nextEntity.getEntityId(), targetEntity.getEntityId(), 0);
sendMessageToAllPlayers(PacketBattleMessage.MessageType.FIRED_ARROW, nextEntity.getEntityId(), targetEntity.getEntityId(), 0);
});
} }
else else
{ {
@ -943,7 +936,7 @@ public class Battle
} }
debugLog += " without bow"; debugLog += " without bow";
int hitChance = TurnBasedMinecraftMod.proxy.getConfig().getPlayerAttackProbability(); int hitChance = TurnBasedMinecraftMod.proxy.getConfig().getPlayerAttackProbability();
if(target.entity instanceof EntityPlayer) if(target.entity instanceof PlayerEntity)
{ {
hitChance = hitChance * (100 - TurnBasedMinecraftMod.proxy.getConfig().getPlayerEvasion()) / 100; hitChance = hitChance * (100 - TurnBasedMinecraftMod.proxy.getConfig().getPlayerEvasion()) / 100;
} }
@ -967,7 +960,7 @@ public class Battle
final float yawDirection = Utility.yawDirection(next.entity.posX, next.entity.posZ, target.entity.posX, target.entity.posZ); final float yawDirection = Utility.yawDirection(next.entity.posX, next.entity.posZ, target.entity.posX, target.entity.posZ);
final float pitchDirection = Utility.pitchDirection(next.entity.posX, next.entity.posY, next.entity.posZ, target.entity.posX, target.entity.posY, target.entity.posZ); final float pitchDirection = Utility.pitchDirection(next.entity.posX, next.entity.posY, next.entity.posZ, target.entity.posX, target.entity.posY, target.entity.posZ);
final boolean defenseDamageTriggered; final boolean defenseDamageTriggered;
if(!(targetEntity instanceof EntityPlayer) && targetEntityInfo.defenseDamage > 0 && targetEntityInfo.defenseDamageProbability > 0) if(!(targetEntity instanceof PlayerEntity) && targetEntityInfo.defenseDamage > 0 && targetEntityInfo.defenseDamageProbability > 0)
{ {
if(random.nextInt(100) < targetEntityInfo.defenseDamageProbability) if(random.nextInt(100) < targetEntityInfo.defenseDamageProbability)
{ {
@ -987,26 +980,22 @@ public class Battle
next.yaw = yawDirection; next.yaw = yawDirection;
next.pitch = pitchDirection; next.pitch = pitchDirection;
} }
debugLog += " adding task..."; // have player look at attack target
next.entity.getServer().addScheduledTask(() -> { ((ServerPlayerEntity)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, yawDirection, pitchDirection);
// have player look at attack target TurnBasedMinecraftMod.proxy.setAttackingEntity(nextEntity);
((EntityPlayerMP)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, yawDirection, pitchDirection); TurnBasedMinecraftMod.proxy.setAttackingDamage(0);
TurnBasedMinecraftMod.proxy.setAttackingEntity(nextEntity); ((PlayerEntity)nextEntity).attackTargetEntityWithCurrentItem(targetEntity);
TurnBasedMinecraftMod.proxy.setAttackingDamage(0); TurnBasedMinecraftMod.proxy.setAttackingEntity(null);
((EntityPlayer)nextEntity).attackTargetEntityWithCurrentItem(targetEntity); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, nextEntity.getEntityId(), targetEntity.getEntityId(), TurnBasedMinecraftMod.proxy.getAttackingDamage());
if(defenseDamageTriggered)
{
// defense damage
DamageSource defenseDamageSource = DamageSource.causeMobDamage((LivingEntity) targetEntity);
TurnBasedMinecraftMod.proxy.setAttackingEntity(targetEntity);
nextEntity.attackEntityFrom(defenseDamageSource, targetEntityInfo.defenseDamage);
TurnBasedMinecraftMod.proxy.setAttackingEntity(null); TurnBasedMinecraftMod.proxy.setAttackingEntity(null);
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, nextEntity.getEntityId(), targetEntity.getEntityId(), TurnBasedMinecraftMod.proxy.getAttackingDamage()); sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, targetEntity.getEntityId(), nextEntity.getEntityId(), targetEntityInfo.defenseDamage);
if(defenseDamageTriggered) }
{
// defense damage
DamageSource defenseDamageSource = DamageSource.causeMobDamage((EntityLivingBase)targetEntity);
TurnBasedMinecraftMod.proxy.setAttackingEntity(targetEntity);
nextEntity.attackEntityFrom(defenseDamageSource, targetEntityInfo.defenseDamage);
TurnBasedMinecraftMod.proxy.setAttackingEntity(null);
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, targetEntity.getEntityId(), nextEntity.getEntityId(), targetEntityInfo.defenseDamage);
}
});
debugLog += "...task added";
} }
else else
{ {
@ -1026,7 +1015,7 @@ public class Battle
else else
{ {
debugLog += " as mob"; debugLog += " as mob";
EntityLivingBase attackTarget = ((EntityLiving)next.entity).getAttackTarget(); LivingEntity attackTarget = ((MobEntity)next.entity).getAttackTarget();
if(attackTarget != null && hasCombatant(attackTarget.getEntityId())) if(attackTarget != null && hasCombatant(attackTarget.getEntityId()))
{ {
debugLog += " to targeted"; debugLog += " to targeted";
@ -1066,12 +1055,12 @@ public class Battle
} }
} }
} }
if(target == null || !target.entity.isEntityAlive() || target == next) if(target == null || !target.entity.isAlive() || target == next)
{ {
continue; continue;
} }
int hitChance = next.entityInfo.attackProbability; int hitChance = next.entityInfo.attackProbability;
if(target.entity instanceof EntityPlayer) if(target.entity instanceof PlayerEntity)
{ {
hitChance = hitChance * (100 - TurnBasedMinecraftMod.proxy.getConfig().getPlayerEvasion()) / 100; hitChance = hitChance * (100 - TurnBasedMinecraftMod.proxy.getConfig().getPlayerEvasion()) / 100;
} }
@ -1088,7 +1077,7 @@ public class Battle
if(target.remainingDefenses <= 0) if(target.remainingDefenses <= 0)
{ {
debugLog += " hit success"; debugLog += " hit success";
DamageSource damageSource = DamageSource.causeMobDamage((EntityLivingBase)next.entity); DamageSource damageSource = DamageSource.causeMobDamage((LivingEntity)next.entity);
int damageAmount = next.entityInfo.attackPower; int damageAmount = next.entityInfo.attackPower;
if(next.entityInfo.attackVariance > 0) if(next.entityInfo.attackVariance > 0)
{ {
@ -1107,7 +1096,7 @@ public class Battle
final boolean defenseDamageTriggered; final boolean defenseDamageTriggered;
final boolean attackEffectTriggered; final boolean attackEffectTriggered;
if(!(targetEntity instanceof EntityPlayer) && targetEntityInfo.defenseDamage > 0 && targetEntityInfo.defenseDamageProbability > 0) if(!(targetEntity instanceof PlayerEntity) && targetEntityInfo.defenseDamage > 0 && targetEntityInfo.defenseDamageProbability > 0)
{ {
if(random.nextInt(100) < targetEntityInfo.defenseDamageProbability) if(random.nextInt(100) < targetEntityInfo.defenseDamageProbability)
{ {
@ -1139,29 +1128,25 @@ public class Battle
attackEffectTriggered = false; attackEffectTriggered = false;
} }
debugLog += " adding task..."; TurnBasedMinecraftMod.proxy.setAttackingEntity(nextEntity);
next.entity.getServer().addScheduledTask(() -> { targetEntity.attackEntityFrom(damageSource, finalDamageAmount);
TurnBasedMinecraftMod.proxy.setAttackingEntity(nextEntity); TurnBasedMinecraftMod.proxy.setAttackingEntity(null);
targetEntity.attackEntityFrom(damageSource, finalDamageAmount); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, nextEntity.getEntityId(), targetEntity.getEntityId(), finalDamageAmount);
if(defenseDamageTriggered)
{
// defense damage
DamageSource defenseDamageSource = DamageSource.causeMobDamage((LivingEntity)targetEntity);
TurnBasedMinecraftMod.proxy.setAttackingEntity(targetEntity);
nextEntity.attackEntityFrom(defenseDamageSource, targetEntityInfo.defenseDamage);
TurnBasedMinecraftMod.proxy.setAttackingEntity(null); TurnBasedMinecraftMod.proxy.setAttackingEntity(null);
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, nextEntity.getEntityId(), targetEntity.getEntityId(), finalDamageAmount); sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, targetEntity.getEntityId(), nextEntity.getEntityId(), targetEntityInfo.defenseDamage);
if(defenseDamageTriggered) }
{ // attack effect
// defense damage if(attackEffectTriggered)
DamageSource defenseDamageSource = DamageSource.causeMobDamage((EntityLivingBase)targetEntity); {
TurnBasedMinecraftMod.proxy.setAttackingEntity(targetEntity); nextEntityInfo.attackEffect.applyEffectToEntity((LivingEntity)targetEntity);
nextEntity.attackEntityFrom(defenseDamageSource, targetEntityInfo.defenseDamage); sendMessageToAllPlayers(PacketBattleMessage.MessageType.WAS_AFFECTED, nextEntity.getEntityId(), targetEntity.getEntityId(), 0, nextEntityInfo.attackEffect.getAffectedString());
TurnBasedMinecraftMod.proxy.setAttackingEntity(null); }
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, targetEntity.getEntityId(), nextEntity.getEntityId(), targetEntityInfo.defenseDamage);
}
// attack effect
if(attackEffectTriggered)
{
nextEntityInfo.attackEffect.applyEffectToEntity((EntityLivingBase)targetEntity);
sendMessageToAllPlayers(PacketBattleMessage.MessageType.WAS_AFFECTED, nextEntity.getEntityId(), targetEntity.getEntityId(), 0, nextEntityInfo.attackEffect.getAffectedString());
}
});
debugLog += "...task added";
} }
else else
{ {
@ -1191,7 +1176,7 @@ public class Battle
{ {
for(Combatant c : sideB.values()) for(Combatant c : sideB.values())
{ {
if(c.entity instanceof EntityPlayer) if(c.entity instanceof PlayerEntity)
{ {
if(TurnBasedMinecraftMod.proxy.getConfig().getPlayerSpeed() > fastestEnemySpeed) if(TurnBasedMinecraftMod.proxy.getConfig().getPlayerSpeed() > fastestEnemySpeed)
{ {
@ -1211,7 +1196,7 @@ public class Battle
{ {
for(Combatant c : sideA.values()) for(Combatant c : sideA.values())
{ {
if(c.entity instanceof EntityPlayer) if(c.entity instanceof PlayerEntity)
{ {
if(TurnBasedMinecraftMod.proxy.getConfig().getPlayerSpeed() > fastestEnemySpeed) if(TurnBasedMinecraftMod.proxy.getConfig().getPlayerSpeed() > fastestEnemySpeed)
{ {
@ -1228,7 +1213,7 @@ public class Battle
} }
} }
int fleeProbability = 0; int fleeProbability = 0;
if(next.entity instanceof EntityPlayer) if(next.entity instanceof PlayerEntity)
{ {
if(fastestEnemySpeed >= TurnBasedMinecraftMod.proxy.getConfig().getPlayerSpeed()) if(fastestEnemySpeed >= TurnBasedMinecraftMod.proxy.getConfig().getPlayerSpeed())
{ {
@ -1260,7 +1245,7 @@ public class Battle
{ {
fleeingCategory = next.entityInfo.category; fleeingCategory = next.entityInfo.category;
} }
else if(next.entity instanceof EntityPlayer) else if(next.entity instanceof PlayerEntity)
{ {
fleeingCategory = "player"; fleeingCategory = "player";
} }
@ -1282,7 +1267,7 @@ public class Battle
sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_INVALID.getValue()); sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_INVALID.getValue());
break; break;
} }
ItemStack targetItemStack = ((EntityPlayer)next.entity).inventory.getStackInSlot(next.itemToUse); ItemStack targetItemStack = ((PlayerEntity)next.entity).inventory.getStackInSlot(next.itemToUse);
Item targetItem = targetItemStack.getItem(); Item targetItem = targetItemStack.getItem();
if(targetItem == null) if(targetItem == null)
{ {
@ -1290,30 +1275,26 @@ public class Battle
sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_NOTHING.getValue()); sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_NOTHING.getValue());
break; break;
} }
if(targetItem instanceof ItemFood) if(targetItem.getGroup() == ItemGroup.FOOD)
{ {
debugLog += " food"; debugLog += " food";
sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_FOOD.getValue(), targetItemStack.getDisplayName()); sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_FOOD.getValue(), targetItemStack.getDisplayName().getFormattedText());
final Entity nextEntity = next.entity; final Entity nextEntity = next.entity;
final int nextItemToUse = next.itemToUse; final int nextItemToUse = next.itemToUse;
next.entity.getServer().addScheduledTask(() -> { ((PlayerEntity)nextEntity).inventory.setInventorySlotContents(nextItemToUse, targetItem.onItemUseFinish(targetItemStack, nextEntity.world, (LivingEntity)nextEntity));
((EntityPlayer)nextEntity).inventory.setInventorySlotContents(nextItemToUse, targetItem.onItemUseFinish(targetItemStack, nextEntity.world, (EntityLivingBase)nextEntity));
});
} }
else if(targetItem instanceof ItemPotion && !(targetItem instanceof ItemSplashPotion) && !(targetItem instanceof ItemLingeringPotion)) else if(targetItem instanceof PotionItem)
{ {
debugLog += " potion"; debugLog += " potion";
sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_POTION.getValue(), targetItemStack.getDisplayName()); sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_POTION.getValue(), targetItemStack.getDisplayName().getFormattedText());
final Entity nextEntity = next.entity; final Entity nextEntity = next.entity;
final int nextItemToUse = next.itemToUse; final int nextItemToUse = next.itemToUse;
next.entity.getServer().addScheduledTask(() -> { ((PlayerEntity)nextEntity).inventory.setInventorySlotContents(nextItemToUse, targetItem.onItemUseFinish(targetItemStack, nextEntity.world, (LivingEntity)nextEntity));
((EntityPlayer)nextEntity).inventory.setInventorySlotContents(nextItemToUse, targetItem.onItemUseFinish(targetItemStack, nextEntity.world, (EntityLivingBase)nextEntity));
});
} }
else else
{ {
debugLog += " non-consumable"; debugLog += " non-consumable";
sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_INVALID.getValue(), targetItemStack.getDisplayName()); sendMessageToAllPlayers(PacketBattleMessage.MessageType.USED_ITEM, next.entity.getEntityId(), 0, PacketBattleMessage.UsedItemAction.USED_INVALID.getValue(), targetItemStack.getDisplayName().getFormattedText());
} }
break; break;
case SWITCH_ITEM: case SWITCH_ITEM:
@ -1325,9 +1306,7 @@ public class Battle
} }
final Entity nextEntity = next.entity; final Entity nextEntity = next.entity;
final int nextItemToUse = next.itemToUse; final int nextItemToUse = next.itemToUse;
next.entity.getServer().addScheduledTask(() -> { ((PlayerEntity)nextEntity).inventory.currentItem = nextItemToUse;
((EntityPlayer)nextEntity).inventory.currentItem = nextItemToUse;
});
sendMessageToAllPlayers(PacketBattleMessage.MessageType.SWITCHED_ITEM, next.entity.getEntityId(), 0, 1); sendMessageToAllPlayers(PacketBattleMessage.MessageType.SWITCHED_ITEM, next.entity.getEntityId(), 0, 1);
break; break;
} }
@ -1343,9 +1322,7 @@ public class Battle
combatantsChanged = true; combatantsChanged = true;
} }
debugLog += ", adding task"; debugLog += ", adding task";
FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(() -> { sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 0);
sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 0);
});
debugLog = "Actions end"; debugLog = "Actions end";
break; break;
} // case ACTION } // case ACTION