@Override
public void createLocalBattle(int id)
{
- localBattle = new Battle(id, null, null, false);
+ localBattle = new Battle(null, id, null, null, false);
}
}
{
return;
}
-
- if(!isAttackerValid(event)
+ Config config = TurnBasedMinecraftMod.proxy.getConfig();
+ BattleManager battleManager = TurnBasedMinecraftMod.proxy.getBattleManager();
+ if((event.getEntity() != null && battleManager.isRecentlyLeftBattle(event.getEntity().getEntityId()))
+ || (event.getSource().getTrueSource() != null && battleManager.isRecentlyLeftBattle(event.getSource().getTrueSource().getEntityId())))
+ {
+ event.setCanceled(true);
+ return;
+ }
+ else if(!isAttackerValid(event)
&& event.getEntity() != null
&& event.getSource().getTrueSource() != null
- && !TurnBasedMinecraftMod.proxy.getConfig().getBattleIgnoringPlayers().contains(event.getSource().getTrueSource().getEntityId())
- && !TurnBasedMinecraftMod.proxy.getConfig().getBattleIgnoringPlayers().contains(event.getEntity().getEntityId())
- && TurnBasedMinecraftMod.proxy.getBattleManager().checkAttack(event))
+ && !config.getBattleIgnoringPlayers().contains(event.getSource().getTrueSource().getEntityId())
+ && !config.getBattleIgnoringPlayers().contains(event.getEntity().getEntityId())
+ && battleManager.checkAttack(event))
{
// TurnBasedMinecraftMod.logger.debug("Canceled LivingAttackEvent between " + TurnBasedMinecraftMod.commonProxy.getAttackingEntity() + " and " + event.getEntity());
event.setCanceled(true);
@SubscribeEvent
public void entityTargeted(LivingSetAttackTargetEvent event)
{
- if(event.getEntity().world.isRemote || TurnBasedMinecraftMod.proxy.getConfig().isOldBattleBehaviorEnabled())
+ Config config = TurnBasedMinecraftMod.proxy.getConfig();
+ BattleManager battleManager = TurnBasedMinecraftMod.proxy.getBattleManager();
+ if(event.getEntity().world.isRemote
+ || config.isOldBattleBehaviorEnabled()
+ || (event.getEntity() != null && battleManager.isRecentlyLeftBattle(event.getEntity().getEntityId()))
+ || (event.getTarget() != null && battleManager.isRecentlyLeftBattle(event.getTarget().getEntityId())))
{
return;
}
else if(event.getEntity() != null
&& event.getTarget() != null
- && !TurnBasedMinecraftMod.proxy.getConfig().getBattleIgnoringPlayers().contains(event.getEntity().getEntityId())
- && !TurnBasedMinecraftMod.proxy.getConfig().getBattleIgnoringPlayers().contains(event.getTarget().getEntityId()))
+ && !config.getBattleIgnoringPlayers().contains(event.getEntity().getEntityId())
+ && !config.getBattleIgnoringPlayers().contains(event.getTarget().getEntityId()))
{
TurnBasedMinecraftMod.proxy.getBattleManager().checkTargeted(event);
}
private boolean isServer;
private boolean battleEnded;
+ private BattleManager battleManager;
+
public enum State
{
DECISION(0),
}
}
- public Battle(int id, Collection<Entity> sideA, Collection<Entity> sideB, boolean isServer)
+ public Battle(BattleManager battleManager, int id, Collection<Entity> sideA, Collection<Entity> sideB, boolean isServer)
{
+ this.battleManager = battleManager;
this.isServer = isServer;
this.id = id;
this.sideA = new Hashtable<Integer, Combatant>();
*/
private boolean healthCheck()
{
- Queue<Integer> removeQueue = new ArrayDeque<Integer>();
+ Queue<Combatant> removeQueue = new ArrayDeque<Combatant>();
for(Combatant c : sideA.values())
{
if(!c.entity.isEntityAlive())
{
- removeQueue.add(c.entity.getEntityId());
- if(c.entity instanceof EntityPlayer)
- {
- TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, c.entity.getEntityId(), 0, 0), (EntityPlayerMP)c.entity);
- }
+ removeQueue.add(c);
String category = new String();
if(c.entityInfo != null)
{
{
if(!c.entity.isEntityAlive())
{
- removeQueue.add(c.entity.getEntityId());
- if(c.entity instanceof EntityPlayer)
- {
- TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, c.entity.getEntityId(), 0, 0), (EntityPlayerMP)c.entity);
- }
+ removeQueue.add(c);
String category = new String();
if(c.entityInfo != null)
{
}
}
boolean didRemove = !removeQueue.isEmpty();
- for(Integer toRemove = removeQueue.poll(); toRemove != null; toRemove = removeQueue.poll())
+ for(Combatant toRemove = removeQueue.poll(); toRemove != null; toRemove = removeQueue.poll())
{
- sideA.remove(toRemove);
- sideB.remove(toRemove);
- if(players.remove(toRemove) != null)
- {
- playerCount.decrementAndGet();
- }
+ removeCombatant(toRemove);
}
if(players.isEmpty() || sideA.isEmpty() || sideB.isEmpty())
{
*/
private boolean isCreativeCheck()
{
- Queue<Integer> removeQueue = new ArrayDeque<Integer>();
+ Queue<Combatant> removeQueue = new ArrayDeque<Combatant>();
for(Combatant c : players.values())
{
if(c.entity != null && ((EntityPlayer)c.entity).isCreative())
{
- TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, c.entity.getEntityId(), 0, 0), (EntityPlayerMP)c.entity);
- removeQueue.add(c.entity.getEntityId());
+ removeQueue.add(c);
}
}
boolean didRemove = false;
- for(Integer toRemove = removeQueue.poll(); toRemove != null; toRemove = removeQueue.poll())
+ for(Combatant toRemove = removeQueue.poll(); toRemove != null; toRemove = removeQueue.poll())
{
didRemove = true;
- sideA.remove(toRemove);
- sideB.remove(toRemove);
- players.remove(toRemove);
- playerCount.decrementAndGet();
- sendMessageToAllPlayers(PacketBattleMessage.MessageType.BECAME_CREATIVE, toRemove, 0, 0);
+ removeCombatant(toRemove);
+ sendMessageToAllPlayers(PacketBattleMessage.MessageType.BECAME_CREATIVE, toRemove.entity.getEntityId(), 0, 0);
}
if(didRemove)
{
}
}
+ private void removeCombatant(Combatant c)
+ {
+ sideA.remove(c.entity.getEntityId());
+ sideB.remove(c.entity.getEntityId());
+ if(players.remove(c.entity.getEntityId()) != null)
+ {
+ playerCount.decrementAndGet();
+ TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, 0, 0, 0), (EntityPlayerMP)c.entity);
+ }
+ battleManager.addRecentlyLeftBattle(c);
+ }
+
/**
* @return True if battle has ended
*/
}
else if(battleEnded)
{
+ Collection<Combatant> combatants = new ArrayList<Combatant>();
+ combatants.addAll(sideA.values());
+ combatants.addAll(sideB.values());
+ for(Combatant c : combatants)
+ {
+ removeCombatant(c);
+ }
return true;
}
long nextInstant = System.nanoTime();
{
if(battleEnded)
{
+ Collection<Combatant> combatants = new ArrayList<Combatant>();
+ combatants.addAll(sideA.values());
+ combatants.addAll(sideB.values());
+ for(Combatant c : combatants)
+ {
+ removeCombatant(c);
+ }
return true;
}
boolean combatantsChanged = false;
if((int)(Math.random() * 100) < fleeProbability)
{
// flee success
- if(next.isSideA)
- {
- sideA.remove(next.entity.getEntityId());
- }
- else
- {
- sideB.remove(next.entity.getEntityId());
- }
combatantsChanged = true;
String fleeingCategory = new String();
if(next.entityInfo != null)
fleeingCategory = "player";
}
sendMessageToAllPlayers(PacketBattleMessage.MessageType.FLEE, next.entity.getEntityId(), 0, 1, fleeingCategory);
- if(next.entity instanceof EntityPlayer)
- {
- players.remove(next.entity.getEntityId());
- playerCount.decrementAndGet();
- TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketBattleMessage(PacketBattleMessage.MessageType.ENDED, 0, 0, 0), (EntityPlayerMP)next.entity);
- }
+ removeCombatant(next);
}
else
{
{
notifyPlayersBattleInfo();
}
+ if(battleEnded)
+ {
+ Collection<Combatant> combatants = new ArrayList<Combatant>();
+ combatants.addAll(sideA.values());
+ combatants.addAll(sideB.values());
+ for(Combatant c : combatants)
+ {
+ removeCombatant(c);
+ }
+ }
return battleEnded;
} // update(final long dt)
}
package com.seodisparate.TurnBasedMinecraft.common;
+import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Collection;
+import java.util.HashMap;
+import java.util.HashSet;
import java.util.Hashtable;
import java.util.Map;
+import java.util.Queue;
+import java.util.Set;
import org.apache.logging.log4j.Logger;
+import com.seodisparate.TurnBasedMinecraft.common.networking.PacketGeneralMessage;
+
import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayer;
+import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraftforge.event.entity.living.LivingAttackEvent;
import net.minecraftforge.event.entity.living.LivingSetAttackTargetEvent;
private Thread updaterThread;
private BattleUpdater battleUpdater;
private Logger logger;
+ private Map<Integer, Combatant> recentlyLeftBattle;
public BattleManager(Logger logger)
{
battleUpdater = new BattleUpdater(this);
updaterThread = new Thread(battleUpdater);
updaterThread.start();
+ recentlyLeftBattle = new HashMap<Integer, Combatant>();
}
/**
{
++IDCounter;
}
- Battle newBattle = new Battle(IDCounter, sideA, sideB, true);
+ Battle newBattle = new Battle(this, IDCounter, sideA, sideB, true);
battleMap.put(IDCounter, newBattle);
newBattle.notifyPlayersBattleInfo();
return newBattle;
updaterThread = null;
battleMap.clear();
}
+
+ protected void addRecentlyLeftBattle(Combatant c)
+ {
+ c.time = System.nanoTime();
+ Config config = TurnBasedMinecraftMod.proxy.getConfig();
+ if(c.entity instanceof EntityPlayerMP)
+ {
+ TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("You just left battle! " + config.getLeaveBattleCooldownSeconds() + " seconds until you can attack/be-attacked again!"), (EntityPlayerMP)c.entity);
+ }
+ synchronized(recentlyLeftBattle)
+ {
+ recentlyLeftBattle.put(c.entity.getEntityId(), c);
+ }
+ }
+
+ protected void updateRecentlyLeftBattle()
+ {
+ long current = System.nanoTime();
+ Queue<Combatant> removeQueue = new ArrayDeque<Combatant>();
+ synchronized(recentlyLeftBattle)
+ {
+ for(Combatant c : recentlyLeftBattle.values())
+ {
+ if(current - c.time > TurnBasedMinecraftMod.proxy.getConfig().getLeaveBattleCooldownNanos())
+ {
+ removeQueue.add(c);
+ }
+ }
+ for(Combatant c = removeQueue.poll(); c != null; c = removeQueue.poll())
+ {
+ if(c.entity instanceof EntityPlayerMP)
+ {
+ TurnBasedMinecraftMod.NWINSTANCE.sendTo(new PacketGeneralMessage("Timer ended, you can now attack/be-attacked again."), (EntityPlayerMP)c.entity);
+ }
+ recentlyLeftBattle.remove(c.entity.getEntityId());
+ }
+ }
+ }
+
+ public boolean isRecentlyLeftBattle(int entityID)
+ {
+ synchronized(recentlyLeftBattle)
+ {
+ return recentlyLeftBattle.containsKey(entityID);
+ }
+ }
}
\ No newline at end of file
{
manager.battleMap.remove(ended);
}
+ manager.updateRecentlyLeftBattle();
try { Thread.sleep(250); } catch (Throwable t) { /* ignored */ }
}
}
public double z;
public float yaw;
public float pitch;
+ public long time;
public Combatant()
{
import net.minecraft.command.CommandBase;
import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
-import net.minecraft.entity.Entity;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.server.MinecraftServer;
import java.io.InputStream;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
-import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
private boolean onlyOPsSelfDisableTB = true;
private boolean battleDisabledForAll = false;
private boolean oldBattleBehaviorEnabled = false;
+ private int leaveBattleCooldownSeconds = 5;
public Config(Logger logger)
{
{
continue;
}
+ else if(xmlReader.getLocalName().equals("LeaveBattleCooldown"))
+ {
+ leaveBattleCooldownSeconds = Integer.parseInt(xmlReader.getElementText());
+ if(leaveBattleCooldownSeconds <= 0)
+ {
+ leaveBattleCooldownSeconds = 1;
+ }
+ else if(leaveBattleCooldownSeconds > 10)
+ {
+ leaveBattleCooldownSeconds = 10;
+ }
+ }
else if(xmlReader.getLocalName().equals("OldBattleBehavior"))
{
if(xmlReader.getElementText().toLowerCase().equals("false"))
{
return oldBattleBehaviorEnabled;
}
+
+ public int getLeaveBattleCooldownSeconds()
+ {
+ return leaveBattleCooldownSeconds;
+ }
+
+ public long getLeaveBattleCooldownNanos()
+ {
+ return (long)leaveBattleCooldownSeconds * 1000000000L;
+ }
}
<TurnBasedMinecraftConfig>
<!-- If the mod has a newer version config, it will rename the existing config and place the new config -->
<Version>5</Version>
+ <!-- Number of seconds that an entity cannot enter battle after having just left one. Minimum 1, maximum 10-->
+ <LeaveBattleCooldown>5</LeaveBattleCooldown>
<!-- If not "false", uses old battle behavior where battles only start on attack/hit. Otherwise, battles can
start when a hostile mob targets a player or another entity in battle. -->
<OldBattleBehavior>false</OldBattleBehavior>