SiegeScheduleData.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (C) 2004-2014 L2J Server
  3. *
  4. * This file is part of L2J Server.
  5. *
  6. * L2J Server is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * L2J Server is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. package com.l2jserver.gameserver.datatables;
  20. import java.util.ArrayList;
  21. import java.util.Calendar;
  22. import java.util.List;
  23. import java.util.logging.Level;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.gameserver.engines.DocumentParser;
  27. import com.l2jserver.gameserver.model.SiegeScheduleDate;
  28. import com.l2jserver.gameserver.model.StatsSet;
  29. import com.l2jserver.gameserver.util.Util;
  30. /**
  31. * @author UnAfraid
  32. */
  33. public class SiegeScheduleData extends DocumentParser
  34. {
  35. private final List<SiegeScheduleDate> _scheduleData = new ArrayList<>();
  36. protected SiegeScheduleData()
  37. {
  38. load();
  39. }
  40. @Override
  41. public synchronized void load()
  42. {
  43. _scheduleData.clear();
  44. parseDatapackFile("config/SiegeSchedule.xml");
  45. _log.log(Level.INFO, getClass().getSimpleName() + ": Loaded: " + _scheduleData.size() + " siege schedulers.");
  46. if (_scheduleData.isEmpty())
  47. {
  48. _scheduleData.add(new SiegeScheduleDate(new StatsSet()));
  49. _log.log(Level.INFO, getClass().getSimpleName() + ": Emergency Loaded: " + _scheduleData.size() + " default siege schedulers.");
  50. }
  51. }
  52. @Override
  53. protected void parseDocument()
  54. {
  55. for (Node n = getCurrentDocument().getFirstChild(); n != null; n = n.getNextSibling())
  56. {
  57. if ("list".equalsIgnoreCase(n.getNodeName()))
  58. {
  59. for (Node cd = n.getFirstChild(); cd != null; cd = cd.getNextSibling())
  60. {
  61. switch (cd.getNodeName())
  62. {
  63. case "schedule":
  64. {
  65. final StatsSet set = new StatsSet();
  66. final NamedNodeMap attrs = cd.getAttributes();
  67. for (int i = 0; i < attrs.getLength(); i++)
  68. {
  69. Node node = attrs.item(i);
  70. String key = node.getNodeName();
  71. String val = node.getNodeValue();
  72. if ("day".equals(key))
  73. {
  74. if (!Util.isDigit(val))
  75. {
  76. val = Integer.toString(getValueForField(val));
  77. }
  78. }
  79. set.set(key, val);
  80. }
  81. _scheduleData.add(new SiegeScheduleDate(set));
  82. break;
  83. }
  84. }
  85. }
  86. }
  87. }
  88. }
  89. private int getValueForField(String field)
  90. {
  91. try
  92. {
  93. return Calendar.class.getField(field).getInt(Calendar.class);
  94. }
  95. catch (Exception e)
  96. {
  97. _log.log(Level.WARNING, "", e);
  98. return -1;
  99. }
  100. }
  101. public List<SiegeScheduleDate> getScheduleDates()
  102. {
  103. return _scheduleData;
  104. }
  105. public static final SiegeScheduleData getInstance()
  106. {
  107. return SingletonHolder._instance;
  108. }
  109. private static class SingletonHolder
  110. {
  111. protected static final SiegeScheduleData _instance = new SiegeScheduleData();
  112. }
  113. }