Fixes, change how BattleManager is initialized

This commit is contained in:
Stephen Seo 2018-09-20 15:15:34 +09:00
parent 74abaa76c0
commit bdb090f74b
6 changed files with 66 additions and 18 deletions

View file

@ -17,13 +17,13 @@ public class ClientProxy extends CommonProxy
private Logger logger; private Logger logger;
private Config config; private Config config;
public ClientProxy() @Override
public void initialize()
{ {
super();
battleGui = new BattleGui(); battleGui = new BattleGui();
battleMusic = null; // will be initialized in postInit() battleMusic = null; // will be initialized in postInit()
} }
@Override @Override
public void setBattleGuiTime(int timeRemaining) public void setBattleGuiTime(int timeRemaining)
{ {

View file

@ -63,10 +63,6 @@ public class AttackEventHandler
{ {
return; return;
} }
else if(TurnBasedMinecraftMod.battleManager == null)
{
TurnBasedMinecraftMod.battleManager = new BattleManager(TurnBasedMinecraftMod.logger);
}
if(!isAttackerValid(event) && event.getEntity() != null && event.getSource().getTrueSource() != null && TurnBasedMinecraftMod.battleManager.checkAttack(event)) if(!isAttackerValid(event) && event.getEntity() != null && event.getSource().getTrueSource() != null && TurnBasedMinecraftMod.battleManager.checkAttack(event))
{ {

View file

@ -16,13 +16,15 @@ public class BattleManager
private int IDCounter = 0; private int IDCounter = 0;
protected Map<Integer, Battle> battleMap; protected Map<Integer, Battle> battleMap;
private Thread updaterThread; private Thread updaterThread;
private BattleUpdater battleUpdater;
private Logger logger; private Logger logger;
public BattleManager(Logger logger) public BattleManager(Logger logger)
{ {
this.logger = logger; this.logger = logger;
battleMap = new Hashtable<Integer, Battle>(); battleMap = new Hashtable<Integer, Battle>();
updaterThread = new Thread(new BattleUpdater(this)); battleUpdater = new BattleUpdater(this);
updaterThread = new Thread(battleUpdater);
updaterThread.start(); updaterThread.start();
} }
@ -169,4 +171,12 @@ public class BattleManager
{ {
return battleMap.get(id); return battleMap.get(id);
} }
public void cleanup()
{
battleUpdater.setIsRunning(false);
battleUpdater = null;
updaterThread = null;
battleMap.clear();
}
} }

View file

@ -2,16 +2,17 @@ package com.seodisparate.TurnBasedMinecraft.common;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Queue; import java.util.Queue;
import java.util.concurrent.atomic.AtomicBoolean;
public class BattleUpdater implements Runnable public class BattleUpdater implements Runnable
{ {
private BattleManager manager; private BattleManager manager;
private boolean isRunning; private AtomicBoolean isRunning;
public BattleUpdater(BattleManager manager) public BattleUpdater(BattleManager manager)
{ {
this.manager = manager; this.manager = manager;
isRunning = true; isRunning = new AtomicBoolean(true);
} }
@Override @Override
@ -19,7 +20,7 @@ public class BattleUpdater implements Runnable
{ {
Queue<Integer> endedQueue = new ArrayDeque<Integer>(); Queue<Integer> endedQueue = new ArrayDeque<Integer>();
Integer ended; Integer ended;
while(isRunning) while(isRunning.get())
{ {
for(Battle e : manager.battleMap.values()) for(Battle e : manager.battleMap.values())
{ {
@ -28,11 +29,9 @@ public class BattleUpdater implements Runnable
endedQueue.add(e.getId()); endedQueue.add(e.getId());
} }
} }
ended = endedQueue.poll(); for(ended = endedQueue.poll(); ended != null; ended = endedQueue.poll())
while(ended != null)
{ {
manager.battleMap.remove(ended); manager.battleMap.remove(ended);
ended = endedQueue.poll();
} }
try { Thread.sleep(250); } catch (Exception e) { /* ignored */ } try { Thread.sleep(250); } catch (Exception e) { /* ignored */ }
} }
@ -40,6 +39,6 @@ public class BattleUpdater implements Runnable
public void setIsRunning(boolean isRunning) public void setIsRunning(boolean isRunning)
{ {
this.isRunning = isRunning; this.isRunning.set(isRunning);
} }
} }

View file

@ -7,6 +7,29 @@ import net.minecraftforge.fml.common.FMLCommonHandler;
public class CommonProxy public class CommonProxy
{ {
public void initialize() {}
public boolean initializeBattleManager()
{
if(TurnBasedMinecraftMod.battleManager == null)
{
TurnBasedMinecraftMod.battleManager = new BattleManager(TurnBasedMinecraftMod.logger);
return true;
}
return false;
}
public boolean cleanupBattleManager ()
{
if(TurnBasedMinecraftMod.battleManager != null)
{
TurnBasedMinecraftMod.battleManager.cleanup();
TurnBasedMinecraftMod.battleManager = null;
return true;
}
return false;
}
public void setBattleGuiTime(int timeRemaining) {} public void setBattleGuiTime(int timeRemaining) {}
public void setBattleGuiBattleChanged() {} public void setBattleGuiBattleChanged() {}

View file

@ -19,6 +19,8 @@ import net.minecraftforge.fml.common.SidedProxy;
import net.minecraftforge.fml.common.event.FMLInitializationEvent; import net.minecraftforge.fml.common.event.FMLInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPostInitializationEvent; import net.minecraftforge.fml.common.event.FMLPostInitializationEvent;
import net.minecraftforge.fml.common.event.FMLPreInitializationEvent; import net.minecraftforge.fml.common.event.FMLPreInitializationEvent;
import net.minecraftforge.fml.common.event.FMLServerStartingEvent;
import net.minecraftforge.fml.common.event.FMLServerStoppingEvent;
import net.minecraftforge.fml.common.network.NetworkRegistry; import net.minecraftforge.fml.common.network.NetworkRegistry;
import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper; import net.minecraftforge.fml.common.network.simpleimpl.SimpleNetworkWrapper;
import net.minecraftforge.fml.relauncher.Side; import net.minecraftforge.fml.relauncher.Side;
@ -59,12 +61,12 @@ public class TurnBasedMinecraftMod
public void preInit(FMLPreInitializationEvent event) public void preInit(FMLPreInitializationEvent event)
{ {
logger = event.getModLog(); logger = event.getModLog();
logger.debug("PREINIT");
} }
@EventHandler @EventHandler
public void init(FMLInitializationEvent event) public void init(FMLInitializationEvent event)
{ {
commonProxy.initialize();
currentBattle = null; currentBattle = null;
battleManager = null; battleManager = null;
attackerViaBow = new HashSet<AttackerViaBow>(); attackerViaBow = new HashSet<AttackerViaBow>();
@ -91,7 +93,6 @@ public class TurnBasedMinecraftMod
PacketBattleMessage.class, PacketBattleMessage.class,
packetHandlerID++, packetHandlerID++,
Side.CLIENT); Side.CLIENT);
logger.debug("INIT");
// register event handler(s) // register event handler(s)
MinecraftForge.EVENT_BUS.register(new AttackEventHandler()); MinecraftForge.EVENT_BUS.register(new AttackEventHandler());
@ -103,7 +104,26 @@ public class TurnBasedMinecraftMod
config = new Config(logger); config = new Config(logger);
commonProxy.setConfig(config); commonProxy.setConfig(config);
commonProxy.postInit(); commonProxy.postInit();
logger.debug("POSTINIT"); }
@EventHandler
public void serverStarting(FMLServerStartingEvent event)
{
logger.debug("About to initialize BattleManager");
if(commonProxy.initializeBattleManager())
{
logger.debug("Initialized BattleManager");
}
}
@EventHandler
public void serverStopping(FMLServerStoppingEvent event)
{
logger.debug("About to cleanup BattleManager");
if(commonProxy.cleanupBattleManager())
{
logger.debug("Cleaned up BattleManager");
}
} }
public static BattleManager getBattleManager() public static BattleManager getBattleManager()