Move battle attacks to main thread, minor fixes

Battle attacks moved to main thread for stability (fixes possible
concurrent modification exception).
This commit is contained in:
Stephen Seo 2018-09-17 13:27:13 +09:00
parent c237a434f5
commit b684310be8
4 changed files with 113 additions and 91 deletions

View file

@ -7,6 +7,8 @@ import com.seodisparate.TurnBasedMinecraft.common.Config;
import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod; import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
import net.minecraft.client.Minecraft; import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity;
import net.minecraft.util.text.TextComponentString;
public class ClientProxy extends CommonProxy public class ClientProxy extends CommonProxy
{ {
@ -141,4 +143,16 @@ public class ClientProxy extends CommonProxy
{ {
this.config = config; this.config = config;
} }
@Override
public void displayString(String message)
{
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString(message));
}
@Override
public Entity getEntityByID(int id)
{
return Minecraft.getMinecraft().world.getEntityByID(id);
}
} }

View file

@ -26,6 +26,7 @@ import net.minecraft.item.ItemPotion;
import net.minecraft.item.ItemSplashPotion; import net.minecraft.item.ItemSplashPotion;
import net.minecraft.item.ItemStack; import net.minecraft.item.ItemStack;
import net.minecraft.util.DamageSource; import net.minecraft.util.DamageSource;
import net.minecraftforge.fml.common.FMLCommonHandler;
public class Battle public class Battle
{ {
@ -652,26 +653,27 @@ public class Battle
// have player look at attack target // have player look at attack target
final Entity nextEntity = next.entity; final Entity nextEntity = next.entity;
final Entity targetEntity = target.entity; final Entity targetEntity = target.entity;
final EntityInfo targetEntityInfo = target.entityInfo;
next.entity.getServer().addScheduledTask(() -> { next.entity.getServer().addScheduledTask(() -> {
((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, 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.attackingEntity = next.entity;
TurnBasedMinecraftMod.attackingDamage = 0; TurnBasedMinecraftMod.attackingDamage = 0;
((EntityPlayer)next.entity).attackTargetEntityWithCurrentItem(target.entity); ((EntityPlayer)nextEntity).attackTargetEntityWithCurrentItem(targetEntity);
TurnBasedMinecraftMod.attackingEntity = null; TurnBasedMinecraftMod.attackingEntity = null;
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, next.entity.getEntityId(), target.entity.getEntityId(), TurnBasedMinecraftMod.attackingDamage); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, nextEntity.getEntityId(), targetEntity.getEntityId(), TurnBasedMinecraftMod.attackingDamage);
if(!(target.entity instanceof EntityPlayer) && target.entityInfo.defenseDamage > 0) if(!(targetEntity instanceof EntityPlayer) && targetEntityInfo.defenseDamage > 0)
{ {
if((int)(Math.random() * 100) < target.entityInfo.defenseDamageProbability) if((int)(Math.random() * 100) < targetEntityInfo.defenseDamageProbability)
{ {
// defense damage // defense damage
DamageSource defenseDamageSource = DamageSource.causeMobDamage((EntityLivingBase)target.entity); DamageSource defenseDamageSource = DamageSource.causeMobDamage((EntityLivingBase)targetEntity);
TurnBasedMinecraftMod.attackingEntity = target.entity; TurnBasedMinecraftMod.attackingEntity = targetEntity;
next.entity.attackEntityFrom(defenseDamageSource, target.entityInfo.defenseDamage); nextEntity.attackEntityFrom(defenseDamageSource, targetEntityInfo.defenseDamage);
TurnBasedMinecraftMod.attackingEntity = null; TurnBasedMinecraftMod.attackingEntity = null;
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, target.entity.getEntityId(), next.entity.getEntityId(), target.entityInfo.defenseDamage); sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, targetEntity.getEntityId(), nextEntity.getEntityId(), targetEntityInfo.defenseDamage);
} }
} }
});
} }
else else
{ {
@ -741,32 +743,39 @@ public class Battle
damageAmount += (int)(Math.random() * (next.entityInfo.attackVariance * 2 + 1)) - next.entityInfo.attackVariance; damageAmount += (int)(Math.random() * (next.entityInfo.attackVariance * 2 + 1)) - next.entityInfo.attackVariance;
} }
// attack // attack
TurnBasedMinecraftMod.attackingEntity = next.entity; final Entity nextEntity = next.entity;
target.entity.attackEntityFrom(damageSource, damageAmount); final EntityInfo nextEntityInfo = next.entityInfo;
final Entity targetEntity = target.entity;
final EntityInfo targetEntityInfo = target.entityInfo;
final int finalDamageAmount = damageAmount;
next.entity.getServer().addScheduledTask(() -> {
TurnBasedMinecraftMod.attackingEntity = nextEntity;
targetEntity.attackEntityFrom(damageSource, finalDamageAmount);
TurnBasedMinecraftMod.attackingEntity = null; TurnBasedMinecraftMod.attackingEntity = null;
sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, next.entity.getEntityId(), target.entity.getEntityId(), damageAmount); sendMessageToAllPlayers(PacketBattleMessage.MessageType.ATTACK, nextEntity.getEntityId(), targetEntity.getEntityId(), finalDamageAmount);
if(!(target.entity instanceof EntityPlayer) && target.entityInfo.defenseDamage > 0) if(!(targetEntity instanceof EntityPlayer) && targetEntityInfo.defenseDamage > 0)
{ {
if((int)(Math.random() * 100) < target.entityInfo.defenseDamageProbability) if((int)(Math.random() * 100) < targetEntityInfo.defenseDamageProbability)
{ {
// defense damage // defense damage
DamageSource defenseDamageSource = DamageSource.causeMobDamage((EntityLivingBase)target.entity); DamageSource defenseDamageSource = DamageSource.causeMobDamage((EntityLivingBase)targetEntity);
TurnBasedMinecraftMod.attackingEntity = target.entity; TurnBasedMinecraftMod.attackingEntity = targetEntity;
next.entity.attackEntityFrom(defenseDamageSource, target.entityInfo.defenseDamage); nextEntity.attackEntityFrom(defenseDamageSource, targetEntityInfo.defenseDamage);
TurnBasedMinecraftMod.attackingEntity = null; TurnBasedMinecraftMod.attackingEntity = null;
sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, target.entity.getEntityId(), next.entity.getEntityId(), target.entityInfo.defenseDamage); sendMessageToAllPlayers(PacketBattleMessage.MessageType.DEFENSE_DAMAGE, targetEntity.getEntityId(), nextEntity.getEntityId(), targetEntityInfo.defenseDamage);
} }
} }
// attack effect // attack effect
if(next.entityInfo.attackEffect != EntityInfo.Effect.UNKNOWN && next.entityInfo.attackEffectProbability > 0) if(nextEntityInfo.attackEffect != EntityInfo.Effect.UNKNOWN && nextEntityInfo.attackEffectProbability > 0)
{ {
int effectChance = (int)(Math.random() * 100); int effectChance = (int)(Math.random() * 100);
if(effectChance < next.entityInfo.attackEffectProbability) if(effectChance < nextEntityInfo.attackEffectProbability)
{ {
next.entityInfo.attackEffect.applyEffectToEntity((EntityLivingBase)target.entity); nextEntityInfo.attackEffect.applyEffectToEntity((EntityLivingBase)targetEntity);
sendMessageToAllPlayers(PacketBattleMessage.MessageType.WAS_AFFECTED, next.entity.getEntityId(), target.entity.getEntityId(), 0, next.entityInfo.attackEffect.getAffectedString()); sendMessageToAllPlayers(PacketBattleMessage.MessageType.WAS_AFFECTED, nextEntity.getEntityId(), targetEntity.getEntityId(), 0, nextEntityInfo.attackEffect.getAffectedString());
} }
} }
});
} }
else else
{ {
@ -892,13 +901,20 @@ public class Battle
if(targetItem instanceof ItemFood) if(targetItem instanceof ItemFood)
{ {
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());
((ItemFood)targetItem).onItemUseFinish(targetItemStack, next.entity.world, (EntityLivingBase)next.entity); final Entity nextEntity = next.entity;
next.entity.getServer().addScheduledTask(() -> {
((ItemFood)targetItem).onItemUseFinish(targetItemStack, nextEntity.world, (EntityLivingBase)nextEntity);
});
} }
else if(targetItem instanceof ItemPotion && !(targetItem instanceof ItemSplashPotion) && !(targetItem instanceof ItemLingeringPotion)) else if(targetItem instanceof ItemPotion && !(targetItem instanceof ItemSplashPotion) && !(targetItem instanceof ItemLingeringPotion))
{ {
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());
((ItemPotion)targetItem).onItemUseFinish(targetItemStack, next.entity.world, (EntityLivingBase)next.entity); final Entity nextEntity = next.entity;
((EntityPlayer)next.entity).inventory.setInventorySlotContents(next.itemToUse, new ItemStack(Items.GLASS_BOTTLE)); final int nextItemToUse = next.itemToUse;
next.entity.getServer().addScheduledTask(() -> {
((ItemPotion)targetItem).onItemUseFinish(targetItemStack, nextEntity.world, (EntityLivingBase)nextEntity);
((EntityPlayer)nextEntity).inventory.setInventorySlotContents(nextItemToUse, new ItemStack(Items.GLASS_BOTTLE));
});
} }
else else
{ {
@ -911,7 +927,11 @@ public class Battle
sendMessageToAllPlayers(PacketBattleMessage.MessageType.SWITCHED_ITEM, next.entity.getEntityId(), 0, 0); sendMessageToAllPlayers(PacketBattleMessage.MessageType.SWITCHED_ITEM, next.entity.getEntityId(), 0, 0);
break; break;
} }
((EntityPlayer)next.entity).inventory.currentItem = next.itemToUse; final Entity nextEntity = next.entity;
final int nextItemToUse = next.itemToUse;
next.entity.getServer().addScheduledTask(() -> {
((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;
} }
@ -929,7 +949,9 @@ public class Battle
undecidedCount.set(players.size()); undecidedCount.set(players.size());
healthCheck(); healthCheck();
isCreativeCheck(); isCreativeCheck();
FMLCommonHandler.instance().getMinecraftServerInstance().addScheduledTask(() -> {
sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 0); sendMessageToAllPlayers(PacketBattleMessage.MessageType.TURN_END, 0, 0, 0);
});
break; break;
} // case ACTION } // case ACTION
default: default:

View file

@ -2,6 +2,9 @@ package com.seodisparate.TurnBasedMinecraft.common;
import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.Logger;
import net.minecraft.entity.Entity;
import net.minecraftforge.fml.common.FMLCommonHandler;
public class CommonProxy public class CommonProxy
{ {
public void setBattleGuiTime(int timeRemaining) {} public void setBattleGuiTime(int timeRemaining) {}
@ -29,4 +32,11 @@ public class CommonProxy
public void typeEnteredBattle(String type) {} public void typeEnteredBattle(String type) {}
public void setConfig(Config config) {} public void setConfig(Config config) {}
public void displayString(String message) {}
public Entity getEntityByID(int id)
{
return FMLCommonHandler.instance().getMinecraftServerInstance().getEntityWorld().getEntityByID(id);
}
} }

View file

@ -7,11 +7,9 @@ import com.seodisparate.TurnBasedMinecraft.common.Battle;
import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod; import com.seodisparate.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import net.minecraft.client.Minecraft;
import net.minecraft.entity.Entity; import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer; import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.scoreboard.ScorePlayerTeam; import net.minecraft.scoreboard.ScorePlayerTeam;
import net.minecraft.util.text.TextComponentString;
import net.minecraftforge.fml.common.network.ByteBufUtils; import net.minecraftforge.fml.common.network.ByteBufUtils;
import net.minecraftforge.fml.common.network.simpleimpl.IMessage; import net.minecraftforge.fml.common.network.simpleimpl.IMessage;
import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler; import net.minecraftforge.fml.common.network.simpleimpl.IMessageHandler;
@ -150,7 +148,7 @@ public class PacketBattleMessage implements IMessage
@Override @Override
public IMessage onMessage(PacketBattleMessage message, MessageContext ctx) public IMessage onMessage(PacketBattleMessage message, MessageContext ctx)
{ {
Entity fromEntity = Minecraft.getMinecraft().world.getEntityByID(message.entityIDFrom); Entity fromEntity = TurnBasedMinecraftMod.commonProxy.getEntityByID(message.entityIDFrom);
String from = "Unknown"; String from = "Unknown";
if(fromEntity != null) if(fromEntity != null)
{ {
@ -186,7 +184,7 @@ public class PacketBattleMessage implements IMessage
} }
} }
} }
Entity toEntity = Minecraft.getMinecraft().world.getEntityByID(message.entityIDTo); Entity toEntity = TurnBasedMinecraftMod.commonProxy.getEntityByID(message.entityIDTo);
String to = "Unknown"; String to = "Unknown";
if(toEntity != null) if(toEntity != null)
{ {
@ -226,8 +224,7 @@ public class PacketBattleMessage implements IMessage
switch(message.messageType) switch(message.messageType)
{ {
case ENTERED: case ENTERED:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " entered battle!");
from + " entered battle!"));
if(TurnBasedMinecraftMod.currentBattle == null || TurnBasedMinecraftMod.currentBattle.getId() != message.amount) if(TurnBasedMinecraftMod.currentBattle == null || TurnBasedMinecraftMod.currentBattle.getId() != message.amount)
{ {
TurnBasedMinecraftMod.currentBattle = new Battle(message.amount, null, null, false); TurnBasedMinecraftMod.currentBattle = new Battle(message.amount, null, null, false);
@ -245,110 +242,89 @@ public class PacketBattleMessage implements IMessage
case FLEE: case FLEE:
if(message.amount != 0) if(message.amount != 0)
{ {
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " fled battle!");
from + " fled battle!"));
} }
else else
{ {
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " tried to flee battle but failed!");
from + " tried to flee battle but failed!"));
} }
break; break;
case DIED: case DIED:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " died in battle!");
from + " died in battle!"));
break; break;
case ENDED: case ENDED:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString("Battle has ended!");
"Battle has ended!"));
TurnBasedMinecraftMod.commonProxy.battleEnded(); TurnBasedMinecraftMod.commonProxy.battleEnded();
TurnBasedMinecraftMod.commonProxy.stopMusic(); TurnBasedMinecraftMod.commonProxy.stopMusic();
break; break;
case ATTACK: case ATTACK:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " attacked " + to + " and dealt " + message.amount + " damage!");
from + " attacked " + to + " and dealt " + message.amount + " damage!"));
break; break;
case DEFEND: case DEFEND:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " blocked " + to + "'s attack!");
from + " blocked " + to + "'s attack!"));
break; break;
case DEFENSE_DAMAGE: case DEFENSE_DAMAGE:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " retaliated from " + to + "'s attack and dealt " + message.amount + " damage!");
from + " retaliated from " + to + "'s attack and dealt " + message.amount + " damage!"));
break; break;
case MISS: case MISS:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " attacked " + to + " but missed!");
from + " attacked " + to + " but missed!"));
break; break;
case DEFENDING: case DEFENDING:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " is defending!");
from + " is defending!"));
break; break;
case DID_NOTHING: case DID_NOTHING:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " did nothing!");
from + " did nothing!"));
break; break;
case USED_ITEM: case USED_ITEM:
switch(UsedItemAction.valueOf(message.amount)) switch(UsedItemAction.valueOf(message.amount))
{ {
case USED_NOTHING: case USED_NOTHING:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " tried to use nothing!");
from + " tried to use nothing!"));
break; break;
case USED_INVALID: case USED_INVALID:
if(message.custom.length() > 0) if(message.custom.length() > 0)
{ {
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " tried to consume " + message.custom + " and failed!");
from + " tried to consume " + message.custom + " and failed!"));
} }
else else
{ {
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " tried to consume an invalid item and failed!");
from + " tried to consume an invalid item and failed!"));
} }
break; break;
case USED_FOOD: case USED_FOOD:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " ate a " + message.custom + "!");
from + " ate a " + message.custom + "!"));
break; break;
case USED_POTION: case USED_POTION:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " drank a " + message.custom + "!");
from + " drank a " + message.custom + "!"));
break; break;
} }
break; break;
case TURN_BEGIN: case TURN_BEGIN:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString("The turn begins!");
"The turn begins!"));
TurnBasedMinecraftMod.commonProxy.battleGuiTurnBegin(); TurnBasedMinecraftMod.commonProxy.battleGuiTurnBegin();
break; break;
case TURN_END: case TURN_END:
if(TurnBasedMinecraftMod.currentBattle != null) if(TurnBasedMinecraftMod.currentBattle != null)
{ {
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString("The turn ended!");
"The turn ended!"));
} }
TurnBasedMinecraftMod.commonProxy.battleGuiTurnEnd(); TurnBasedMinecraftMod.commonProxy.battleGuiTurnEnd();
break; break;
case SWITCHED_ITEM: case SWITCHED_ITEM:
if(message.amount != 0) if(message.amount != 0)
{ {
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " switched to a different item!");
from + " switched to a different item!"));
} }
else else
{ {
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " switched to a different item but failed because it was invalid!");
from + " switched to a different item but failed because it was invalid!"));
} }
break; break;
case WAS_AFFECTED: case WAS_AFFECTED:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(to + " was " + message.custom + " by " + from + "!");
to + " was " + message.custom + " by " + from + "!"));
break; break;
case BECAME_CREATIVE: case BECAME_CREATIVE:
Minecraft.getMinecraft().ingameGUI.getChatGUI().printChatMessage(new TextComponentString( TurnBasedMinecraftMod.commonProxy.displayString(from + " entered creative mode and left battle!");
from + " entered creative mode and left battle!"));
break; break;
} }
return null; return null;