From efa65da72115db5f6e612258cc49b5b8f41bb0a3 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Tue, 18 Sep 2018 15:56:06 +0900 Subject: [PATCH] Fix bow attack, config, add freeze enemies option Fix bug where bow attack hit announcement was made multiple times. Fix config format for Entities. Add feature to freeze combatants when battle starts, option to enable in config. --- .../common/AttackEventHandler.java | 15 +- .../TurnBasedMinecraft/common/Battle.java | 73 +++++- .../TurnBasedMinecraft/common/Combatant.java | 5 + .../TurnBasedMinecraft/common/Config.java | 157 ++++++----- .../assets/TurnBasedMinecraft/TBM_Config.xml | 245 +++++++++++------- 5 files changed, 312 insertions(+), 183 deletions(-) diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/AttackEventHandler.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/AttackEventHandler.java index 01ac49e..1fc1438 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/AttackEventHandler.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/AttackEventHandler.java @@ -36,19 +36,20 @@ public class AttackEventHandler 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) + if(!isValid) { - b.sendMessageToAllPlayers(PacketBattleMessage.MessageType.ARROW_HIT, attacker.entity.getEntityId(), event.getEntity().getEntityId(), 0); + Battle b = TurnBasedMinecraftMod.battleManager.getBattleByID(attacker.battleID); + if(b != null) + { + b.sendMessageToAllPlayers(PacketBattleMessage.MessageType.ARROW_HIT, attacker.entity.getEntityId(), event.getEntity().getEntityId(), 0); + } + isValid = true; } } } - AttackerViaBow next = removeQueue.poll(); - while(next != null) + for(AttackerViaBow next = removeQueue.poll(); next != null; next = removeQueue.poll()) { TurnBasedMinecraftMod.attackerViaBow.remove(next); - next = removeQueue.poll(); } } return isValid; diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java index ee2e2b4..4e8057b 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Battle.java @@ -144,6 +144,14 @@ public class Battle playerCount.incrementAndGet(); players.put(e.getEntityId(), newCombatant); } + if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled()) + { + newCombatant.x = e.posX; + newCombatant.y = e.posY; + newCombatant.z = e.posZ; + newCombatant.yaw = e.rotationYaw; + newCombatant.pitch = e.rotationPitch; + } } } if(sideB != null) @@ -165,6 +173,14 @@ public class Battle playerCount.incrementAndGet(); players.put(e.getEntityId(), newCombatant); } + if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled()) + { + newCombatant.x = e.posX; + newCombatant.y = e.posY; + newCombatant.z = e.posZ; + newCombatant.yaw = e.rotationYaw; + newCombatant.pitch = e.rotationPitch; + } } } @@ -251,6 +267,14 @@ public class Battle undecidedCount.incrementAndGet(); } } + if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled()) + { + newCombatant.x = e.posX; + newCombatant.y = e.posY; + newCombatant.z = e.posZ; + newCombatant.yaw = e.rotationYaw; + newCombatant.pitch = e.rotationPitch; + } if(newCombatant.entityInfo != null) { sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, newCombatant.entityInfo.category); @@ -283,6 +307,14 @@ public class Battle undecidedCount.incrementAndGet(); } } + if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled()) + { + newCombatant.x = e.posX; + newCombatant.y = e.posY; + newCombatant.z = e.posZ; + newCombatant.yaw = e.rotationYaw; + newCombatant.pitch = e.rotationPitch; + } if(newCombatant.entityInfo != null) { sendMessageToAllPlayers(PacketBattleMessage.MessageType.ENTERED, newCombatant.entity.getEntityId(), 0, id, newCombatant.entityInfo.category); @@ -516,6 +548,18 @@ public class Battle } } + private void enforceFreezePositions() + { + for(Combatant c : sideA.values()) + { + c.entity.setPositionAndRotation(c.x, c.y, c.z, c.yaw, c.pitch); + } + for(Combatant c : sideB.values()) + { + c.entity.setPositionAndRotation(c.x, c.y, c.z, c.yaw, c.pitch); + } + } + /** * @return True if battle has ended */ @@ -537,6 +581,10 @@ public class Battle { return true; } + if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled()) + { + enforceFreezePositions(); + } switch(state) { case DECISION: @@ -604,12 +652,10 @@ public class Battle break; case ACTION: { - Combatant next = turnOrderQueue.poll(); - while(next != null) + for(Combatant next = turnOrderQueue.poll(); next != null; next = turnOrderQueue.poll()) { if(!next.entity.isEntityAlive()) { - next = turnOrderQueue.poll(); continue; } @@ -643,9 +689,16 @@ public class Battle { final Entity nextEntity = next.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 pitchDirection = Utility.pitchDirection(next.entity.posX, next.entity.posY, next.entity.posZ, target.entity.posX, target.entity.posY, target.entity.posZ); + if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled()) + { + next.yaw = yawDirection; + next.pitch = pitchDirection; + } 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)); + ((EntityPlayerMP)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, yawDirection, pitchDirection); ItemBow itemBow = (ItemBow)heldItemStack.getItem(); synchronized(TurnBasedMinecraftMod.attackerViaBow) { @@ -659,7 +712,6 @@ public class Battle { sendMessageToAllPlayers(PacketBattleMessage.MessageType.BOW_NO_AMMO, next.entity.getEntityId(), 0, 0); } - next = turnOrderQueue.poll(); continue; } int hitChance = TurnBasedMinecraftMod.config.getPlayerAttackProbability(); @@ -683,9 +735,16 @@ public class Battle final Entity nextEntity = next.entity; final Entity targetEntity = target.entity; final EntityInfo targetEntityInfo = target.entityInfo; + 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); + if(TurnBasedMinecraftMod.config.isFreezeCombatantsEnabled()) + { + next.yaw = yawDirection; + next.pitch = pitchDirection; + } 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)); + ((EntityPlayerMP)nextEntity).connection.setPlayerLocation(nextEntity.posX, nextEntity.posY, nextEntity.posZ, yawDirection, pitchDirection); TurnBasedMinecraftMod.attackingEntity = nextEntity; TurnBasedMinecraftMod.attackingDamage = 0; ((EntityPlayer)nextEntity).attackTargetEntityWithCurrentItem(targetEntity); @@ -746,7 +805,6 @@ public class Battle } if(target == null || !target.entity.isEntityAlive()) { - next = turnOrderQueue.poll(); continue; } int hitChance = next.entityInfo.attackProbability; @@ -965,7 +1023,6 @@ public class Battle sendMessageToAllPlayers(PacketBattleMessage.MessageType.SWITCHED_ITEM, next.entity.getEntityId(), 0, 1); break; } - next = turnOrderQueue.poll(); } for(Combatant c : sideA.values()) { diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Combatant.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Combatant.java index 80f2c1f..d2ac975 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Combatant.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Combatant.java @@ -19,6 +19,11 @@ public class Combatant public boolean isSideA; public int remainingDefenses; public int battleID; + public double x; + public double y; + public double z; + public float yaw; + public float pitch; public Combatant() { diff --git a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java index b27e43f..3a752c5 100644 --- a/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java +++ b/src/main/java/com/seodisparate/TurnBasedMinecraft/common/Config.java @@ -37,6 +37,7 @@ public class Config private int maxInBattle = 8; private Set musicBattleTypes; private Set musicSillyTypes; + private boolean freezeCombatantsInBattle = false; public Config(Logger logger) { @@ -163,6 +164,10 @@ public class Config { maxInBattle = Integer.parseInt(xmlReader.getElementText()); } + else if(xmlReader.getLocalName().equals("FreezeBattleCombatants")) + { + freezeCombatantsInBattle = !xmlReader.getElementText().toLowerCase().equals("false"); + } else if(xmlReader.getLocalName().equals("IgnoreBattleTypes")) { do @@ -263,103 +268,108 @@ public class Config xmlReader.next(); if(xmlReader.isStartElement()) { - String classType = xmlReader.getLocalName(); - EntityInfo eInfo = new EntityInfo(); - try + if(xmlReader.getLocalName().equals("Entry")) { - eInfo.classType = Class.forName(classType); - } catch (ClassNotFoundException e) - { - logger.error("Failed to get class of name " + classType); - } - do - { - xmlReader.next(); - if(xmlReader.isStartElement()) + EntityInfo eInfo = new EntityInfo(); + do { - if(xmlReader.getLocalName().equals("AttackPower")) + xmlReader.next(); + if(xmlReader.isStartElement()) { - for(int i = 0; i < xmlReader.getAttributeCount(); ++i) + if(xmlReader.getLocalName().equals("Name")) { - if(xmlReader.getAttributeLocalName(i).equals("Probability")) + try { - eInfo.attackProbability = Integer.parseInt(xmlReader.getAttributeValue(i)); - } - else if(xmlReader.getAttributeLocalName(i).equals("Variance")) + eInfo.classType = Class.forName(xmlReader.getElementText()); + } catch (ClassNotFoundException e) { - eInfo.attackVariance = Integer.parseInt(xmlReader.getAttributeValue(i)); + logger.error("Failed to get class of name " + xmlReader.getElementText()); } } - eInfo.attackPower = Integer.parseInt(xmlReader.getElementText()); - } - else if(xmlReader.getLocalName().equals("AttackEffect")) - { - for(int i = 0; i < xmlReader.getAttributeCount(); ++i) + else if(xmlReader.getLocalName().equals("AttackPower")) { - if(xmlReader.getAttributeLocalName(i).equals("Probability")) + for(int i = 0; i < xmlReader.getAttributeCount(); ++i) { - eInfo.attackEffectProbability = Integer.parseInt(xmlReader.getAttributeValue(i)); + if(xmlReader.getAttributeLocalName(i).equals("Probability")) + { + eInfo.attackProbability = Integer.parseInt(xmlReader.getAttributeValue(i)); + } + else if(xmlReader.getAttributeLocalName(i).equals("Variance")) + { + eInfo.attackVariance = Integer.parseInt(xmlReader.getAttributeValue(i)); + } } + eInfo.attackPower = Integer.parseInt(xmlReader.getElementText()); } - eInfo.attackEffect = EntityInfo.Effect.fromString(xmlReader.getElementText()); - } - else if(xmlReader.getLocalName().equals("Evasion")) - { - eInfo.evasion = Integer.parseInt(xmlReader.getElementText()); - } - else if(xmlReader.getLocalName().equals("DefenseDamage")) - { - for(int i = 0; i < xmlReader.getAttributeCount(); ++i) + else if(xmlReader.getLocalName().equals("AttackEffect")) { - if(xmlReader.getAttributeLocalName(i).equals("Probability")) + for(int i = 0; i < xmlReader.getAttributeCount(); ++i) { - eInfo.defenseDamageProbability = Integer.parseInt(xmlReader.getAttributeValue(i)); + if(xmlReader.getAttributeLocalName(i).equals("Probability")) + { + eInfo.attackEffectProbability = Integer.parseInt(xmlReader.getAttributeValue(i)); + } } + eInfo.attackEffect = EntityInfo.Effect.fromString(xmlReader.getElementText()); } - eInfo.defenseDamage = Integer.parseInt(xmlReader.getElementText()); - } - else if(xmlReader.getLocalName().equals("Category")) - { - eInfo.category = xmlReader.getElementText().toLowerCase(); - } - else if(xmlReader.getLocalName().equals("IgnoreBattle")) - { - if(xmlReader.getElementText().toLowerCase().equals("true")) + else if(xmlReader.getLocalName().equals("Evasion")) { - eInfo.ignoreBattle = true; + eInfo.evasion = Integer.parseInt(xmlReader.getElementText()); } - } - else if(xmlReader.getLocalName().equals("Speed")) - { - eInfo.speed = Integer.parseInt(xmlReader.getElementText()); - } - else if(xmlReader.getLocalName().equals("Decision")) - { - do + else if(xmlReader.getLocalName().equals("DefenseDamage")) { - xmlReader.next(); - if(xmlReader.isStartElement()) + for(int i = 0; i < xmlReader.getAttributeCount(); ++i) { - if(xmlReader.getLocalName().equals("Attack")) + if(xmlReader.getAttributeLocalName(i).equals("Probability")) { - eInfo.decisionAttack = Integer.parseInt(xmlReader.getElementText()); + eInfo.defenseDamageProbability = Integer.parseInt(xmlReader.getAttributeValue(i)); } - else if(xmlReader.getLocalName().equals("Defend")) - { - eInfo.decisionDefend = Integer.parseInt(xmlReader.getElementText()); - } - else if(xmlReader.getLocalName().equals("Flee")) + } + eInfo.defenseDamage = Integer.parseInt(xmlReader.getElementText()); + } + else if(xmlReader.getLocalName().equals("Category")) + { + eInfo.category = xmlReader.getElementText().toLowerCase(); + } + else if(xmlReader.getLocalName().equals("IgnoreBattle")) + { + if(xmlReader.getElementText().toLowerCase().equals("true")) + { + eInfo.ignoreBattle = true; + } + } + else if(xmlReader.getLocalName().equals("Speed")) + { + eInfo.speed = Integer.parseInt(xmlReader.getElementText()); + } + else if(xmlReader.getLocalName().equals("Decision")) + { + do + { + xmlReader.next(); + if(xmlReader.isStartElement()) { - eInfo.decisionFlee = Integer.parseInt(xmlReader.getElementText()); + if(xmlReader.getLocalName().equals("Attack")) + { + eInfo.decisionAttack = Integer.parseInt(xmlReader.getElementText()); + } + else if(xmlReader.getLocalName().equals("Defend")) + { + eInfo.decisionDefend = Integer.parseInt(xmlReader.getElementText()); + } + else if(xmlReader.getLocalName().equals("Flee")) + { + eInfo.decisionFlee = Integer.parseInt(xmlReader.getElementText()); + } } - } - } while(!(xmlReader.isEndElement() && xmlReader.getLocalName().equals("Decision"))); + } while(!(xmlReader.isEndElement() && xmlReader.getLocalName().equals("Decision"))); + } } + } while(!(xmlReader.isEndElement() && xmlReader.getLocalName().equals("Entry"))); + if(eInfo.classType != null) + { + entityInfoMap.put(eInfo.classType.getName(), eInfo); } - } while(!(xmlReader.isEndElement() && xmlReader.getLocalName().equals(classType))); - if(eInfo.classType != null) - { - entityInfoMap.put(eInfo.classType.getName(), eInfo); } } } while(!(xmlReader.isEndElement() && xmlReader.getLocalName().equals("EntityStats"))); @@ -500,4 +510,9 @@ public class Config { return musicSillyTypes.contains(type.toLowerCase()); } + + public boolean isFreezeCombatantsEnabled() + { + return freezeCombatantsInBattle; + } } diff --git a/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml b/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml index 952bd98..294cff4 100644 --- a/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml +++ b/src/main/resources/assets/TurnBasedMinecraft/TBM_Config.xml @@ -1,8 +1,10 @@ - 2 + 3 8 + + false @@ -37,6 +39,7 @@ 4 + @@ -46,7 +49,8 @@ - + + net.minecraft.entity.monster.EntityBlaze 5 fire 5 @@ -57,8 +61,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityCaveSpider 2 poison 35 @@ -69,8 +74,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityCreeper true 15 5 @@ -81,8 +87,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityElderGuardian 8 2 25 @@ -93,8 +100,9 @@ 20 0 - - + + + net.minecraft.entity.monster.EntityEnderman 7 40 monster @@ -104,8 +112,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityEndermite 2 40 monster @@ -115,8 +124,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityEvoker 6 35 monster @@ -126,8 +136,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityGhast true 13 35 @@ -138,8 +149,9 @@ 0 25 - - + + + net.minecraft.entity.monster.EntityGiantZombie 11 2 monster @@ -149,8 +161,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityGuardian 6 2 25 @@ -161,8 +174,9 @@ 20 0 - - + + + net.minecraft.entity.monster.EntityHusk 3 hunger 5 @@ -173,8 +187,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityIronGolem 14 5 monster @@ -184,8 +199,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityMagmaCube 3 12 monster @@ -195,8 +211,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityPigZombie 8 10 monster @@ -206,8 +223,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityPolarBear 6 5 animal @@ -217,8 +235,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityShulker 4 15 monster @@ -228,8 +247,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntitySilverfish 1 37 monster @@ -239,8 +259,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntitySkeleton 3 13 monster @@ -250,8 +271,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntitySlime 2 10 monster @@ -261,8 +283,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntitySnowman 0 5 passive @@ -272,8 +295,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntitySpider 2 25 monster @@ -283,8 +307,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityStray 3 slow 13 @@ -295,8 +320,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityVex 9 30 monster @@ -306,8 +332,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityVindicator 13 10 monster @@ -317,8 +344,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityWitch 5 8 monster @@ -328,8 +356,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityWitherSkeleton 8 wither 7 @@ -340,8 +369,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityZombie 3 5 monster @@ -351,8 +381,9 @@ 0 0 - - + + + net.minecraft.entity.monster.EntityZombieVillager 3 5 monster @@ -362,8 +393,9 @@ 0 0 - - + + + net.minecraft.entity.passive.EntityBat 0 35 passive @@ -373,8 +405,9 @@ 0 90 - - + + + net.minecraft.entity.passive.EntityChicken 0 10 passive @@ -384,8 +417,9 @@ 0 90 - - + + + net.minecraft.entity.passive.EntityCow 0 1 passive @@ -395,8 +429,9 @@ 10 80 - - + + + net.minecraft.entity.passive.EntityDonkey 0 10 passive @@ -406,8 +441,9 @@ 0 90 - - + + + net.minecraft.entity.passive.EntityHorse 0 10 passive @@ -417,8 +453,9 @@ 0 90 - - + + + net.minecraft.entity.passive.EntityLlama 1 10 passive @@ -428,8 +465,9 @@ 0 25 - - + + + net.minecraft.entity.passive.EntityMooshroom 0 1 passive @@ -439,8 +477,9 @@ 10 80 - - + + + net.minecraft.entity.passive.EntityMule 0 10 passive @@ -450,8 +489,9 @@ 0 90 - - + + + net.minecraft.entity.passive.EntityOcelot 1 10 passive @@ -461,8 +501,9 @@ 0 90 - - + + + net.minecraft.entity.passive.EntityParrot 0 35 passive @@ -472,8 +513,9 @@ 0 90 - - + + + net.minecraft.entity.passive.EntityPig 0 10 passive @@ -483,8 +525,9 @@ 5 85 - - + + + net.minecraft.entity.passive.EntityRabbit 0 40 passive @@ -494,8 +537,9 @@ 0 100 - - + + + net.minecraft.entity.passive.EntitySheep 0 5 passive @@ -505,8 +549,9 @@ 0 90 - - + + + net.minecraft.entity.passive.EntitySkeletonHorse 0 5 passive @@ -516,8 +561,9 @@ 0 90 - - + + + net.minecraft.entity.passive.EntitySquid 0 15 passive @@ -527,8 +573,9 @@ 0 90 - - + + + net.minecraft.entity.passive.EntityVillager 0 5 passive @@ -538,8 +585,9 @@ 10 80 - - + + + net.minecraft.entity.passive.EntityWolf 4 20 animal @@ -549,8 +597,9 @@ 15 5 - - + + + net.minecraft.entity.passive.EntityZombieHorse 0 8 passive @@ -560,8 +609,9 @@ 0 90 - - + + + net.minecraft.entity.boss.EntityDragon 10 27 boss @@ -571,8 +621,9 @@ 0 0 - - + + + net.minecraft.entity.boss.EntityWither 8 20 wither @@ -583,6 +634,6 @@ 0 0 - + \ No newline at end of file -- 2.49.0