/src/main/java/com/sk89q/commandbook/kits/FlatFileKitsManager.java

https://github.com/bandless55/commandbook · Java · 163 lines · 107 code · 25 blank · 31 comment · 19 complexity · baf16a0f00e1d1e04a18f86233e19aac MD5 · raw file

  1. // $Id$
  2. /*
  3. * Copyright (C) 2010, 2011 sk89q <http://www.sk89q.com>
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. package com.sk89q.commandbook.kits;
  19. import java.io.BufferedReader;
  20. import java.io.File;
  21. import java.io.FileInputStream;
  22. import java.io.FileNotFoundException;
  23. import java.io.IOException;
  24. import java.io.InputStreamReader;
  25. import java.io.UnsupportedEncodingException;
  26. import java.util.HashMap;
  27. import java.util.Map;
  28. import java.util.logging.Logger;
  29. import java.util.regex.Matcher;
  30. import java.util.regex.Pattern;
  31. import org.bukkit.inventory.ItemStack;
  32. import com.sk89q.commandbook.CommandBookPlugin;
  33. /**
  34. * Manages kits.
  35. *
  36. * @author sk89q
  37. */
  38. public class FlatFileKitsManager implements KitManager {
  39. private static final Logger logger = Logger.getLogger("Minecraft.CommandBook");
  40. private static final Pattern kitPattern =
  41. Pattern.compile("^\\[([^\\]=]+)(?:= *([0-9]+) *)?\\]$");
  42. private CommandBookPlugin plugin;
  43. private File file;
  44. private Map<String, Kit> kits = new HashMap<String, Kit>();
  45. /**
  46. * Construct the manager.
  47. *
  48. * @param plugin
  49. * @param file
  50. */
  51. public FlatFileKitsManager(File file, CommandBookPlugin plugin) {
  52. this.plugin = plugin;
  53. this.file = file;
  54. }
  55. public synchronized void load() {
  56. FileInputStream input = null;
  57. Map<String, Kit> kits = new HashMap<String, Kit>();
  58. try {
  59. input = new FileInputStream(file);
  60. InputStreamReader streamReader = new InputStreamReader(input, "utf-8");
  61. BufferedReader reader = new BufferedReader(streamReader);
  62. Kit kit = null;
  63. String line;
  64. while ((line = reader.readLine()) != null) {
  65. line = line.trim();
  66. if (line.length() == 0
  67. || line.charAt(0) == '#'
  68. || line.charAt(0) == ';') {
  69. continue;
  70. }
  71. // Match a kit's name
  72. Matcher m = kitPattern.matcher(line);
  73. if (m.matches()) {
  74. String id = m.group(1).replace(" ", "").trim().toLowerCase();
  75. kit = new Kit();
  76. kits.put(id, kit);
  77. String coolDownTime = m.group(2);
  78. if (coolDownTime != null) {
  79. try {
  80. kit.setCoolDown((long) (Double.parseDouble(coolDownTime) * 1000));
  81. } catch (NumberFormatException e) {
  82. logger.warning("CommandBook: Invalid cool down for "
  83. + line);
  84. continue;
  85. }
  86. }
  87. continue;
  88. }
  89. // No kit defined yet!
  90. if (kit == null) {
  91. logger.warning("CommandBook: Missing \"[kitname]\" section for "
  92. + line);
  93. continue;
  94. }
  95. String[] parts = line.split(",");
  96. ItemStack item = plugin.getItem(parts[0].replace(" ", ""));
  97. if (item == null) {
  98. logger.warning("CommandBook: Unknown kit item '" + item + "'");
  99. continue;
  100. }
  101. // Attempt to parse an amount
  102. if (parts.length >= 2) {
  103. try {
  104. item.setAmount(Integer.parseInt(parts[1]));
  105. } catch (NumberFormatException e) {
  106. logger.warning("CommandBook: Invalid amount: '" + parts[1] + "'");
  107. }
  108. }
  109. kit.addItem(item);
  110. }
  111. logger.info("CommandBook: " + kits.size() + " kit(s) loaded.");
  112. } catch (FileNotFoundException e) {
  113. } catch (UnsupportedEncodingException e) {
  114. } catch (IOException e) {
  115. logger.warning("CommandBook: Failed to load kits.txt: "
  116. + e.getMessage());
  117. } finally {
  118. if (input != null) {
  119. try {
  120. input.close();
  121. } catch (IOException e) {
  122. }
  123. }
  124. }
  125. this.kits = kits;
  126. }
  127. public synchronized Kit getKit(String id) {
  128. return kits.get(id.toLowerCase());
  129. }
  130. public synchronized Map<String, Kit> getKits() {
  131. return kits;
  132. }
  133. public synchronized void flush() {
  134. for (Kit kit : kits.values()) {
  135. kit.flush();
  136. }
  137. }
  138. }