PageRenderTime 62ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/com/sk89q/commandbook/locations/FlatFileLocationsManager.java

https://github.com/rcwidower/commandbook
Java | 242 lines | 176 code | 40 blank | 26 comment | 25 complexity | c534f0221ece56def04cb179e7ff9ae2 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.locations;
  19. import java.io.BufferedReader;
  20. import java.io.BufferedWriter;
  21. import java.io.File;
  22. import java.io.FileInputStream;
  23. import java.io.FileOutputStream;
  24. import java.io.IOException;
  25. import java.io.InputStreamReader;
  26. import java.io.OutputStreamWriter;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. import java.util.logging.Logger;
  30. import org.bukkit.Location;
  31. import org.bukkit.World;
  32. import org.bukkit.entity.Player;
  33. import au.com.bytecode.opencsv.CSVReader;
  34. import au.com.bytecode.opencsv.CSVWriter;
  35. import com.sk89q.commandbook.CommandBookPlugin;
  36. public class FlatFileLocationsManager implements WarpsManager, HomesManager {
  37. private static final Logger logger = Logger.getLogger("Minecraft.CommandBook");
  38. private CommandBookPlugin plugin;
  39. private World castWorld;
  40. private File file;
  41. private Map<String, NamedLocation> locs = new HashMap<String, NamedLocation>();
  42. private boolean forHomes = false;
  43. /**
  44. * Construct the manager.
  45. *
  46. * @param plugin
  47. * @param file
  48. * @param forHomes
  49. */
  50. public FlatFileLocationsManager(File file, CommandBookPlugin plugin, boolean forHomes) {
  51. this.plugin = plugin;
  52. this.file = file;
  53. this.forHomes = forHomes;
  54. }
  55. public void castWorld(World world) {
  56. castWorld = world;
  57. }
  58. public void load() throws IOException {
  59. FileInputStream input = null;
  60. Map<String, NamedLocation> locs = new HashMap<String, NamedLocation>();
  61. file.getParentFile().mkdirs();
  62. try {
  63. input = new FileInputStream(file);
  64. InputStreamReader streamReader = new InputStreamReader(input, "utf-8");
  65. BufferedReader reader = new BufferedReader(streamReader);
  66. CSVReader csv = new CSVReader(reader);
  67. String[] line;
  68. while ((line = csv.readNext()) != null) {
  69. if (line.length < 7) {
  70. logger.warning("CommandBook: " + (forHomes ? "Homes" : "Warps") + " data file has an invalid line with < 7 fields");
  71. } else {
  72. try {
  73. String name = line[0].trim().replace(" ", "");
  74. String worldName = line[1]; // Set to null if the world exists
  75. String creator = line[2];
  76. double x = Double.parseDouble(line[3]);
  77. double y = Double.parseDouble(line[4]);
  78. double z = Double.parseDouble(line[5]);
  79. float pitch = Float.parseFloat(line[6]);
  80. float yaw = Float.parseFloat(line[7]);
  81. World world = plugin.getServer().getWorld(worldName);
  82. if (world == null) {
  83. // We shouldn't have this warp
  84. if (castWorld != null) {
  85. continue;
  86. }
  87. world = plugin.getServer().getWorlds().get(0);
  88. } else {
  89. // We shouldn't have this warp
  90. if (castWorld != null && !castWorld.equals(world)) {
  91. continue;
  92. }
  93. worldName = null;
  94. }
  95. Location loc = new Location(world, x, y, z, yaw, pitch);
  96. NamedLocation warp = new NamedLocation(name, loc);
  97. warp.setWorldName(worldName);
  98. warp.setCreatorName(creator);
  99. locs.put(name.toLowerCase(), warp);
  100. } catch (NumberFormatException e) {
  101. logger.warning("CommandBook: " + (forHomes ? "Homes" : "Warps") + " data file has an invalid line with non-numeric numeric fields");
  102. }
  103. }
  104. }
  105. this.locs = locs;
  106. if (castWorld != null) {
  107. logger.warning("CommandBook: " + locs.size() + " " + (forHomes ? "homes" : "warps") + "(s) loaded for "
  108. + castWorld.getName());
  109. } else {
  110. logger.warning("CommandBook: " + locs.size() + " " + (forHomes ? "homes" : "warps") + "(s) loaded");
  111. }
  112. } finally {
  113. if (input != null) {
  114. try {
  115. input.close();
  116. } catch (IOException e) {
  117. }
  118. }
  119. }
  120. }
  121. public void save() throws IOException {
  122. FileOutputStream output = null;
  123. try {
  124. output = new FileOutputStream(file);
  125. OutputStreamWriter streamWriter = new OutputStreamWriter(output, "utf-8");
  126. BufferedWriter writer = new BufferedWriter(streamWriter);
  127. CSVWriter csv = new CSVWriter(writer);
  128. synchronized (this) {
  129. for (Map.Entry<String, NamedLocation> entry : locs.entrySet()) {
  130. NamedLocation warp = entry.getValue();
  131. csv.writeNext(new String[] {
  132. warp.getName(),
  133. warp.getWorldName() != null ? warp.getWorldName()
  134. : warp.getLocation().getWorld().getName(),
  135. warp.getCreatorName(),
  136. String.valueOf(warp.getLocation().getX()),
  137. String.valueOf(warp.getLocation().getY()),
  138. String.valueOf(warp.getLocation().getZ()),
  139. String.valueOf(warp.getLocation().getPitch()),
  140. String.valueOf(warp.getLocation().getYaw()),
  141. });
  142. }
  143. }
  144. csv.flush();
  145. csv.close();
  146. } finally {
  147. if (output != null) {
  148. try {
  149. output.close();
  150. } catch (IOException e) {
  151. }
  152. }
  153. }
  154. }
  155. public NamedLocation get(String id) {
  156. return locs.get(id.toLowerCase());
  157. }
  158. public boolean remove(String id) {
  159. return locs.remove(id.toLowerCase()) != null;
  160. }
  161. public NamedLocation create(String id, Location loc, Player player) {
  162. id = id.trim();
  163. NamedLocation warp = new NamedLocation(id, loc);
  164. locs.put(id.toLowerCase(), warp);
  165. if (player != null) {
  166. warp.setCreatorName(player.getName());
  167. } else {
  168. warp.setCreatorName("");
  169. }
  170. return warp;
  171. }
  172. public static class WarpsFactory implements WarpsManagerFactory {
  173. private CommandBookPlugin plugin;
  174. public File rootDir;
  175. public WarpsFactory(File rootDir, CommandBookPlugin plugin) {
  176. this.rootDir = rootDir;
  177. this.plugin = plugin;
  178. }
  179. public WarpsManager createManager() {
  180. return new FlatFileLocationsManager(new File(rootDir, "warps.csv"), plugin, false);
  181. }
  182. public WarpsManager createManager(World castWorld) {
  183. return new FlatFileLocationsManager(
  184. new File(rootDir, "warps/" + castWorld.getName() + ".csv"), plugin, false);
  185. }
  186. }
  187. public static class HomesFactory implements HomesManagerFactory {
  188. private CommandBookPlugin plugin;
  189. public File rootDir;
  190. public HomesFactory(File rootDir, CommandBookPlugin plugin) {
  191. this.rootDir = rootDir;
  192. this.plugin = plugin;
  193. }
  194. public HomesManager createManager() {
  195. return new FlatFileLocationsManager(new File(rootDir, "homes.csv"), plugin, true);
  196. }
  197. public HomesManager createManager(World castWorld) {
  198. return new FlatFileLocationsManager(
  199. new File(rootDir, "homes/" + castWorld.getName() + ".csv"), plugin, true);
  200. }
  201. }
  202. }