-3

我刚遇到简单的问题,但似乎找不到解决方案。好吧,以下代码是开源项目的一部分,但这部分是我从头开始编写的。

好吧,这个“脚本”中的所有内容都运行良好,没有问题,除了一件事,调用方法后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();
        }
    }
}

那么有什么建议可以解决这个问题吗?

4

3 回答 3

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 以查看这样的值会产生什么影响。

于 2013-07-04T11:52:37.420 回答
0

StartParticipation() 在线程内被调用。甚至在实际变化发生之前,检查您是否试图找出它的值。

[不知道你是如何在代码的后面部分计算出“CastleBattle.CB_State”的值]

于 2013-07-04T11:59:50.207 回答
0

底部的类被称为CastleBattleLoop,但它不包含任何循环,因此StartParticipation()只被调用一次(如果CB_State在那一刻为 1)。

你应该添加类似的东西

while(running){
    if (CastleBattle.CB_State == 1)
    {
        CastleBattle.StartParticipation();
    }
    Thread.sleep(100);
}
于 2013-07-04T11:52:33.093 回答