import net.minecraft.entity.player.ServerPlayerEntity;
import net.minecraft.item.*;
import net.minecraft.util.DamageSource;
+import net.minecraft.world.dimension.DimensionType;
import net.minecraftforge.fml.network.PacketDistributor;
public class Battle
private Random random;
public String debugLog; // TODO remove after freeze bug has been found
+
+ private DimensionType dimension;
public enum State
{
}
}
- public Battle(BattleManager battleManager, int id, Collection<Entity> sideA, Collection<Entity> sideB, boolean isServer)
+ public Battle(BattleManager battleManager, int id, Collection<Entity> sideA, Collection<Entity> sideB, boolean isServer, DimensionType dimension)
{
this.battleManager = battleManager;
this.isServer = isServer;
playerCount = new AtomicInteger(0);
undecidedCount = new AtomicInteger(0);
random = new Random();
+ this.dimension = dimension;
if(sideA != null)
{
for(Entity e : sideA)
{
return;
}
- PacketBattleMessage packet = new PacketBattleMessage(type, from, to, amount, custom);
+ PacketBattleMessage packet = new PacketBattleMessage(type, from, to, dimension, amount, custom);
for(Combatant p : players.values())
{
if(p.entity.isAlive())
{
if(c.entity instanceof PlayerEntity)
{
- TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) c.entity), new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, 0, 0, 0));
+ TurnBasedMinecraftMod.getHandler().send(PacketDistributor.PLAYER.with(() -> (ServerPlayerEntity) c.entity), new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, 0, 0, dimension, 0));
}
battleManager.addRecentlyLeftBattle(c);
}
+ public void forceRemoveCombatant(EntityIDDimPair e) {
+ sideA.remove(e.id);
+ sideB.remove(e.id);
+ if(players.remove(e.id) != null) {
+ playerCount.decrementAndGet();
+ }
+ for(Iterator<Combatant> iter = sideAEntryQueue.iterator(); iter.hasNext();) {
+ Combatant c = iter.next();
+ if(c.entity.getEntityId() == e.id) {
+ iter.remove();
+ break;
+ }
+ }
+ for(Iterator<Combatant> iter = sideBEntryQueue.iterator(); iter.hasNext();) {
+ Combatant c = iter.next();
+ if(c.entity.getEntityId() == e.id) {
+ iter.remove();
+ break;
+ }
+ }
+ }
+
private void setDecisionState()
{
for(Combatant c : sideA.values())
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.ServerPlayerEntity;
+import net.minecraft.world.dimension.DimensionType;
import net.minecraftforge.common.MinecraftForge;
import net.minecraftforge.fml.network.PacketDistributor;
import org.apache.logging.log4j.Logger;
private Logger logger;
private Map<Integer, Combatant> recentlyLeftBattle;
private BattleUpdater battleUpdater;
+ private Map<EntityIDDimPair, Integer> entityToBattleMap;
public BattleManager(Logger logger)
{
battleMap = new HashMap<Integer, Battle>();
recentlyLeftBattle = new HashMap<Integer, Combatant>();
battleUpdater = new BattleUpdater(this);
+ entityToBattleMap = new HashMap<EntityIDDimPair, Integer>();
MinecraftForge.EVENT_BUS.register(battleUpdater);
}
|| !((event.getSource().getTrueSource() instanceof PlayerEntity && !((PlayerEntity)event.getSource().getTrueSource()).isCreative())
|| (config.getEntityInfoReference(attackerClassName) != null || config.getCustomEntityInfoReference(attackerCustomName) != null)))
{
- logger.debug("BattleManager: Failed first check, attacker is \"" + attackerClassName + "\", defender is \"" + receiverClassName + "\"");
+// logger.debug("BattleManager: Failed first check, attacker is \"" + attackerClassName + "\", defender is \"" + receiverClassName + "\"");
return false;
}
if(entityInfo != null && (config.isIgnoreBattleType(entityInfo.category) || entityInfo.ignoreBattle))
{
// attacked entity ignores battle
- synchronized(battleMap)
- {
- for(Battle b : battleMap.values())
- {
- if(b.hasCombatant(event.getSource().getTrueSource().getEntityId()))
- {
- logger.debug("Attack Canceled: attacked ignores battle but attacker in battle");
- return true;
- }
- }
+ Battle battle = battleMap.get(entityToBattleMap.get(new EntityIDDimPair(event.getSource().getTrueSource())));
+ if(battle != null && battle.hasCombatant(event.getSource().getTrueSource().getEntityId())) {
+ logger.debug("Attack Canceled: attacked ignores battle but attacker in battle");
+ return true;
+ } else {
+ logger.debug("Attack Not Canceled: attacked ignores battle");
+ return false;
}
- logger.debug("Attack Not Canceled: attacked ignores battle");
- return false;
}
entityInfo = config.getCustomEntityInfoReference(attackerCustomName);
if(entityInfo != null && (config.isIgnoreBattleType(entityInfo.category) || entityInfo.ignoreBattle))
{
// attacker entity ignores battle
- synchronized(battleMap)
- {
- for(Battle b : battleMap.values())
- {
- if(b.hasCombatant(event.getEntity().getEntityId()))
- {
- logger.debug("Attack Canceled: attacker ignores battle but attacked in battle");
- return true;
- }
- }
+ Battle battle = battleMap.get(entityToBattleMap.get(new EntityIDDimPair(event.getEntity())));
+ if(battle != null && battle.hasCombatant(event.getEntity().getEntityId())) {
+ logger.debug("Attack Canceled: attacker ignores battle but attacked in battle");
+ return true;
+ } else {
+ logger.debug("Attack Not Canceled: attacker ignores battle");
+ return false;
}
- logger.debug("Attack Not Canceled: attacker ignores battle");
- return false;
}
// check if one is in battle
- Entity inBattle = null;
- Entity notInBattle = null;
- Battle battle = null;
-
- synchronized(battleMap)
- {
- for(Battle b : battleMap.values())
- {
- if(b.hasCombatant(event.getSource().getTrueSource().getEntityId()))
- {
- if(inBattle != null)
- {
- // both combatants are in battle
- logger.debug("Attack Canceled: both are in battle");
- return true;
- }
- else
- {
- inBattle = event.getSource().getTrueSource();
- notInBattle = event.getEntity();
- battle = b;
- }
- }
- if(b.hasCombatant(event.getEntity().getEntityId()))
- {
- if(inBattle != null)
- {
- // both combatants are in battle
- logger.debug("Attack Canceled: both are in battle");
- return true;
- }
- else
- {
- inBattle = event.getEntity();
- notInBattle = event.getSource().getTrueSource();
- battle = b;
- }
- }
- }
+ Battle attackerBattle = battleMap.get(entityToBattleMap.get(new EntityIDDimPair(event.getSource().getTrueSource())));
+ if(attackerBattle != null && !attackerBattle.hasCombatant(event.getSource().getTrueSource().getEntityId())) {
+ attackerBattle = null;
}
-
- if(inBattle == null)
- {
+ Battle defenderBattle = battleMap.get(entityToBattleMap.get(new EntityIDDimPair(event.getEntity())));
+ if(defenderBattle != null && !defenderBattle.hasCombatant(event.getEntity().getEntityId())) {
+ defenderBattle = null;
+ }
+
+ if(attackerBattle != null && defenderBattle != null) {
+ // both in battle, attack canceled
+ return true;
+ } else if(attackerBattle == null && defenderBattle == null) {
// neither entity is in battle
if(event.getEntity() instanceof PlayerEntity || event.getSource().getTrueSource() instanceof PlayerEntity)
{
Collection<Entity> sideB = new ArrayList<Entity>(1);
sideA.add(event.getEntity());
sideB.add(event.getSource().getTrueSource());
- createBattle(sideA, sideB);
+ createBattle(sideA, sideB, event.getEntity().dimension);
logger.debug("Attack Not Canceled: new battle created");
}
else
logger.debug("Attack Not Canceled: neither are in battle or players");
}
return false;
- }
-
- // at this point only one entity is in battle, so add entity to other side
- if(battle.getSize() >= config.getMaxInBattle())
- {
- // battle limit reached, cannot add to battle
- return true;
- }
- else if(battle.hasCombatantInSideA(inBattle.getEntityId()))
- {
- battle.addCombatantToSideB(notInBattle);
- }
- else
- {
- battle.addCombatantToSideA(notInBattle);
+ } else {
+ // at this point only one entity is in battle, so add entity to other side
+ if(attackerBattle != null) {
+ if (attackerBattle.getSize() >= config.getMaxInBattle()) {
+ // battle limit reached, cannot add to battle
+ return true;
+ } else if (attackerBattle.hasCombatantInSideA(event.getSource().getTrueSource().getEntityId())) {
+ attackerBattle.addCombatantToSideB(event.getEntity());
+ } else {
+ attackerBattle.addCombatantToSideA(event.getEntity());
+ }
+ entityToBattleMap.put(new EntityIDDimPair(event.getEntity()), attackerBattle.getId());
+ } else {
+ if (defenderBattle.getSize() >= config.getMaxInBattle()) {
+ // battle limit reached, cannot add to battle
+ return true;
+ } else if (defenderBattle.hasCombatantInSideA(event.getEntity().getEntityId())) {
+ defenderBattle.addCombatantToSideB(event.getSource().getTrueSource());
+ } else {
+ defenderBattle.addCombatantToSideA(event.getSource().getTrueSource());
+ }
+ entityToBattleMap.put(new EntityIDDimPair(event.getSource().getTrueSource()), defenderBattle.getId());
+ }
}
logger.debug("Attack Canceled: one is in battle");
{
return;
}
-
- Entity inBattle = null;
- Entity notInBattle = null;
- Battle battle = null;
-
- synchronized(battleMap)
- {
- for(Battle b : battleMap.values())
- {
- if(b.hasCombatant(event.getEntity().getEntityId()))
- {
- if(inBattle != null)
- {
- // both entities already in battle
- return;
- }
- else
- {
- inBattle = event.getEntity();
- notInBattle = event.getTarget();
- battle = b;
- }
- }
- if(b.hasCombatant(event.getTarget().getEntityId()))
- {
- if(inBattle != null)
- {
- // both entities already in battle
- return;
- }
- else
- {
- inBattle = event.getTarget();
- notInBattle = event.getEntity();
- battle = b;
- }
- }
- }
+
+ // check if one is in battle
+ Battle attackerBattle = battleMap.get(entityToBattleMap.get(new EntityIDDimPair(event.getEntity())));
+ if(attackerBattle != null && !attackerBattle.hasCombatant(event.getEntity().getEntityId())) {
+ attackerBattle = null;
}
-
- if(battle == null)
- {
+ Battle defenderBattle = battleMap.get(entityToBattleMap.get(new EntityIDDimPair(event.getTarget())));
+ if(defenderBattle != null && !defenderBattle.hasCombatant(event.getTarget().getEntityId())) {
+ defenderBattle = null;
+ }
+
+ if(attackerBattle != null && defenderBattle != null) {
+ return;
+ } else if(attackerBattle == null && defenderBattle == null) {
// neither in battle
if(event.getEntity() instanceof PlayerEntity || event.getTarget() instanceof PlayerEntity)
{
Collection<Entity> sideB = new ArrayList<Entity>(1);
sideA.add(event.getEntity());
sideB.add(event.getTarget());
- createBattle(sideA, sideB);
+ createBattle(sideA, sideB, event.getEntity().dimension);
+ logger.debug("neither in battle, at least one is player, creating new battle");
}
- }
- else
- {
+ } else {
// add entity to battle
- if(battle.getSize() >= TurnBasedMinecraftMod.proxy.getConfig().getMaxInBattle())
- {
- // battle max reached, cannot add to battle
- return;
- }
- else if(battle.hasCombatantInSideA(inBattle.getEntityId()))
- {
- battle.addCombatantToSideB(notInBattle);
- }
- else
- {
- battle.addCombatantToSideA(notInBattle);
+ if(attackerBattle != null) {
+ if (attackerBattle.getSize() >= TurnBasedMinecraftMod.proxy.getConfig().getMaxInBattle()) {
+ // battle max reached, cannot add to battle
+ return;
+ } else if (attackerBattle.hasCombatantInSideA(event.getEntity().getEntityId())) {
+ attackerBattle.addCombatantToSideB(event.getTarget());
+ } else {
+ attackerBattle.addCombatantToSideA(event.getTarget());
+ }
+ entityToBattleMap.put(new EntityIDDimPair(event.getTarget()), attackerBattle.getId());
+ } else {
+ if (defenderBattle.getSize() >= TurnBasedMinecraftMod.proxy.getConfig().getMaxInBattle()) {
+ // battle max reached, cannot add to battle
+ return;
+ } else if (defenderBattle.hasCombatantInSideA(event.getTarget().getEntityId())) {
+ defenderBattle.addCombatantToSideB(event.getEntity());
+ } else {
+ defenderBattle.addCombatantToSideA(event.getEntity());
+ }
+ entityToBattleMap.put(new EntityIDDimPair(event.getEntity()), defenderBattle.getId());
}
}
}
- private Battle createBattle(Collection<Entity> sideA, Collection<Entity> sideB)
+ private Battle createBattle(Collection<Entity> sideA, Collection<Entity> sideB, DimensionType dimension)
{
Battle newBattle = null;
synchronized(battleMap)
{
++IDCounter;
}
- newBattle = new Battle(this, IDCounter, sideA, sideB, true);
+ newBattle = new Battle(this, IDCounter, sideA, sideB, true, dimension);
battleMap.put(IDCounter, newBattle);
}
+ for(Entity e : sideA) {
+ entityToBattleMap.put(new EntityIDDimPair(e), newBattle.getId());
+ }
+ for(Entity e : sideB) {
+ entityToBattleMap.put(new EntityIDDimPair(e), newBattle.getId());
+ }
newBattle.notifyPlayersBattleInfo();
return newBattle;
}
synchronized(recentlyLeftBattle) {
recentlyLeftBattle.put(c.entity.getEntityId(), c);
}
+ entityToBattleMap.remove(new EntityIDDimPair(c.entity));
}
protected void updateRecentlyLeftBattle()
return recentlyLeftBattle.containsKey(entityID);
}
}
+
+ public boolean forceLeaveBattle(EntityIDDimPair entityInfo) {
+ boolean result = false;
+ Integer battleID = entityToBattleMap.get(entityInfo);
+ if(battleID != null) {
+ Battle battle = battleMap.get(battleID);
+ if (battle != null && battle.hasCombatant(entityInfo.id)) {
+ battle.forceRemoveCombatant(entityInfo);
+ result = true;
+ }
+ entityToBattleMap.remove(entityInfo);
+ }
+ return result;
+ }
}
\ No newline at end of file
import com.burnedkirby.TurnBasedMinecraft.common.TurnBasedMinecraftMod;
+import com.burnedkirby.TurnBasedMinecraft.common.Utility;
import net.minecraft.entity.Entity;
import net.minecraft.network.PacketBuffer;
+import net.minecraft.util.ResourceLocation;
+import net.minecraft.world.dimension.DimensionType;
import net.minecraftforge.fml.network.NetworkEvent;
public class PacketBattleMessage
int entityIDTo;
int amount;
String custom;
+ DimensionType dimension;
public PacketBattleMessage() { custom = new String(); }
- public PacketBattleMessage(MessageType messageType, int entityIDFrom, int entityIDTo, int amount)
+ public PacketBattleMessage(MessageType messageType, int entityIDFrom, int entityIDTo, DimensionType dimension, int amount)
{
this.messageType = messageType;
this.entityIDFrom = entityIDFrom;
this.entityIDTo = entityIDTo;
+ this.dimension = dimension;
this.amount = amount;
custom = new String();
}
- public PacketBattleMessage(MessageType messageType, int entityIDFrom, int entityIDTo, int amount, String custom)
+ public PacketBattleMessage(MessageType messageType, int entityIDFrom, int entityIDTo, DimensionType dimension, int amount, String custom)
{
this.messageType = messageType;
this.entityIDFrom = entityIDFrom;
this.entityIDTo = entityIDTo;
+ this.dimension = dimension;
this.amount = amount;
this.custom = custom;
}
buf.writeInt(pkt.messageType.getValue());
buf.writeInt(pkt.entityIDFrom);
buf.writeInt(pkt.entityIDTo);
+ buf.writeString(DimensionType.getKey(pkt.dimension).toString());
buf.writeInt(pkt.amount);
buf.writeString(pkt.custom);
}
buf.readInt()),
buf.readInt(),
buf.readInt(),
+ DimensionType.byName(new ResourceLocation(buf.readString())),
buf.readInt(),
buf.readString());
}
public static class Handler {
public static void handle(final PacketBattleMessage pkt, Supplier<NetworkEvent.Context> ctx) {
ctx.get().enqueueWork(() -> {
- Entity fromEntity = TurnBasedMinecraftMod.proxy.getEntityByID(pkt.entityIDFrom);
+ Entity fromEntity = Utility.getEntity(pkt.entityIDFrom, pkt.dimension);
String from = "Unknown";
if(fromEntity != null)
{
from = fromEntity.getDisplayName().getFormattedText();
}
}
- Entity toEntity = TurnBasedMinecraftMod.proxy.getEntityByID(pkt.entityIDTo);
+ Entity toEntity = Utility.getEntity(pkt.entityIDTo, pkt.dimension);
String to = "Unknown";
if(toEntity != null)
{