SiegeScheduleData.java 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (C) 2004-2015 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.data.xml.impl;
  20. import java.util.ArrayList;
  21. import java.util.Calendar;
  22. import java.util.List;
  23. import org.w3c.dom.Document;
  24. import org.w3c.dom.NamedNodeMap;
  25. import org.w3c.dom.Node;
  26. import com.l2jserver.gameserver.model.SiegeScheduleDate;
  27. import com.l2jserver.gameserver.model.StatsSet;
  28. import com.l2jserver.gameserver.util.Util;
  29. import com.l2jserver.util.data.xml.IXmlReader;
  30. /**
  31. * Siege Schedule data.
  32. * @author UnAfraid
  33. */
  34. public class SiegeScheduleData implements IXmlReader
  35. {
  36. private final List<SiegeScheduleDate> _scheduleData = new ArrayList<>();
  37. protected SiegeScheduleData()
  38. {
  39. load();
  40. }
  41. @Override
  42. public synchronized void load()
  43. {
  44. _scheduleData.clear();
  45. parseDatapackFile("config/SiegeSchedule.xml");
  46. LOGGER.info("{}: Loaded: {} siege schedulers.", getClass().getSimpleName(), _scheduleData.size());
  47. if (_scheduleData.isEmpty())
  48. {
  49. _scheduleData.add(new SiegeScheduleDate(new StatsSet()));
  50. LOGGER.info("{}: Emergency Loaded: {} default siege schedulers.", getClass().getSimpleName(), _scheduleData.size());
  51. }
  52. }
  53. @Override
  54. public void parseDocument(Document doc)
  55. {
  56. for (Node n = doc.getFirstChild(); n != null; n = n.getNextSibling())
  57. {
  58. if ("list".equalsIgnoreCase(n.getNodeName()))
  59. {
  60. for (Node cd = n.getFirstChild(); cd != null; cd = cd.getNextSibling())
  61. {
  62. switch (cd.getNodeName())
  63. {
  64. case "schedule":
  65. {
  66. final StatsSet set = new StatsSet();
  67. final NamedNodeMap attrs = cd.getAttributes();
  68. for (int i = 0; i < attrs.getLength(); i++)
  69. {
  70. Node node = attrs.item(i);
  71. String key = node.getNodeName();
  72. String val = node.getNodeValue();
  73. if ("day".equals(key))
  74. {
  75. if (!Util.isDigit(val))
  76. {
  77. val = Integer.toString(getValueForField(val));
  78. }
  79. }
  80. set.set(key, val);
  81. }
  82. _scheduleData.add(new SiegeScheduleDate(set));
  83. break;
  84. }
  85. }
  86. }
  87. }
  88. }
  89. }
  90. private int getValueForField(String field)
  91. {
  92. try
  93. {
  94. return Calendar.class.getField(field).getInt(Calendar.class);
  95. }
  96. catch (Exception e)
  97. {
  98. LOGGER.warn("{}: Unable to get value!", getClass().getSimpleName(), e);
  99. return -1;
  100. }
  101. }
  102. public List<SiegeScheduleDate> getScheduleDates()
  103. {
  104. return _scheduleData;
  105. }
  106. public static final SiegeScheduleData getInstance()
  107. {
  108. return SingletonHolder._instance;
  109. }
  110. private static class SingletonHolder
  111. {
  112. protected static final SiegeScheduleData _instance = new SiegeScheduleData();
  113. }
  114. }