我只是遇到了一个简单的问题,但似乎无法找到解决方案。以下代码是开源项目的一部分,但这部分是我从头开始编写的。
嗯,这个“脚本”里面的所有内容都可以正常运行,除了一件事,
调用int
方法后,CB_State
变量StartParticipation()
不会更改:
import java.util.Calendar;
import java.util.logging.Logger;
import com.l2jserver.gameserver.Announcements;
import com.l2jserver.gameserver.ThreadPoolManager;
import com.l2jserver.gameserver.model.actor.instance.L2PcInstance;
import com.l2jserver.gameserver.network.serverpackets.NpcHtmlMessage;
public final class CastleBattle
{
private static Logger _log = Logger.getLogger("CastleBattle");
private static String htm_path = "data/scripts/l2dc/CastleBattle/";
public static int CB_State = 1; // 0 - Disabled, 1 - Not running, 2 - Participation start, 3 - Participation end, 4 - Running, 5 - Event ended
public CastleBattle()
{
CB_Init();
}
// Initialize Engine
private static void CB_Init()
{
if (CB_State == 1)
{
SetStartTime();
}
}
// Event Loop
public static void SetStartTime()
{
Calendar _nextTime = Calendar.getInstance();
int _m = _nextTime.get(Calendar.MINUTE);
int x = 1;
while (_m > 5)
{
_m -= 5;
x++;
}
_nextTime.set(Calendar.MINUTE, x * 5);
ThreadPoolManager.getInstance().scheduleGeneral(new CastleBattleLoop(), _nextTime.getTimeInMillis() - System.currentTimeMillis());
}
// Allow players to participate in the event
public static void StartParticipation()
{
CB_State = 2;
Announcements.getInstance().announceToAll("Castle Battle participation has started.");
_log.info("Castle Battle participation has started.");
}
// Player requests to join event via NPC
public static void CB_bypass(String _cmd, L2PcInstance _player)
{
if (_cmd.startsWith("InitHtmlRequest"))
{
if (CB_State == 0)
{
NpcHtmlMessage _html = new NpcHtmlMessage(0);
_html.setFile("", htm_path + "CB_Disabled.htm");
_player.sendPacket(_html);
}
if (CB_State == 1)
{
NpcHtmlMessage _html = new NpcHtmlMessage(0);
_html.setFile("", htm_path + "CB_NotRunning.htm");
_player.sendPacket(_html);
}
if (CB_State == 2)
{
NpcHtmlMessage _html = new NpcHtmlMessage(0);
_html.setFile("", htm_path + "CB_Participate.htm");
_player.sendPacket(_html);
}
}
}
public static void main(String[] args)
{
_log.info("# Castle Battle Engine #");
_log.info("Author : HyperByter");
_log.info("Version : Beta");
_log.info("Version : 3.7.2013");
new CastleBattle();
}
}
class CastleBattleLoop implements Runnable
{
@Override
public void run()
{
if (CastleBattle.CB_State == 1)
{
CastleBattle.StartParticipation();
}
}
}
那么有任何建议如何解决这个问题?
答案 0 :(得分:3)
方法StartParticipation()
可能永远不会被调用:
main()
调用CastleBattle
CastleBattle
构造函数调用CB_Init()
CB_Init()
来电SetStartTime()
SetStartTime()
调用此行:
ThreadPoolManager.getInstance().scheduleGeneral(new CastleBattleLoop(), _nextTime.getTimeInMillis() - System.currentTimeMillis());
在对_nextTime
进行了一些严厉且难以理解的算术之后,调度间隔可能非常大或者可能是负数,这可能导致Runnable CastleBattleLoop永远不会启动,在这种情况下StartParticipation()永远不会被召唤。
我不知道ThreadPoolManager对奇怪的输入做了什么,但我首先要调试传递给scheduleGeneral()方法的值,然后读取javadoc以查看这样的值会产生什么影响。
答案 1 :(得分:0)
底部的类称为CastleBattleLoop
,但它不包含任何循环,因此StartParticipation()
只被调用一次(如果此时CB_State
为1)。
您应该添加类似
的内容while(running){
if (CastleBattle.CB_State == 1)
{
CastleBattle.StartParticipation();
}
Thread.sleep(100);
}
答案 2 :(得分:0)
在线程内调用StartParticipation()。 检查您是否在实际更改发生之前试图找出其值。
[不知道你是如何在代码的后半部分找出“CastleBattle.CB_State”的值]