package mineplex.mapparser; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.DataInputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileWriter; import java.io.InputStreamReader; import java.util.HashSet; import org.bukkit.entity.Player; public class MapData { public String MapFolder; public GameType GameType = null; public String MapName = "null"; public String MapCreator = "null"; public HashSet BuildList; public MapData(String mapFolder) { MapFolder = mapFolder; BuildList = new HashSet(); if ((new File(MapFolder + File.separator + "Map.dat")).exists()) Read(); else Write(); } public void Read() { String line = null; try { FileInputStream fstream = new FileInputStream(MapFolder + File.separator + "Map.dat"); DataInputStream in = new DataInputStream(fstream); BufferedReader br = new BufferedReader(new InputStreamReader(in)); while ((line = br.readLine()) != null) { String[] tokens = line.split(":"); if (tokens.length < 2) continue; if (tokens[0].length() == 0) continue; //Name & Author if (tokens[0].equalsIgnoreCase("MAP_NAME")) { MapName = tokens[1]; } else if (tokens[0].equalsIgnoreCase("MAP_AUTHOR")) { MapCreator = tokens[1]; } else if (tokens[0].equalsIgnoreCase("GAME_TYPE")) { GameType = GameType.valueOf(tokens[1] == null ? "Null" : tokens[1]); } else if (tokens[0].equalsIgnoreCase("BUILD_LIST")) { for (String cur : tokens[1].split(",")) BuildList.add(cur); } } in.close(); } catch (Exception e) { e.printStackTrace(); System.err.println("Line: " + line); } } public void Write() { //Save try { FileWriter fstream = new FileWriter(MapFolder + File.separator + "Map.dat"); BufferedWriter out = new BufferedWriter(fstream); out.write("MAP_NAME:"+MapName); out.write("\n"); out.write("MAP_AUTHOR:"+MapCreator); out.write("\n"); out.write("GAME_TYPE:"+GameType); String buildList = ""; for (String cur : BuildList) buildList += cur + ","; out.write("\n"); out.write("BUILD_LIST:"+buildList); out.close(); } catch (Exception e) { e.printStackTrace(); } } public boolean CanBuild(Player player) { return BuildList.contains(player.getName()) || player.isOp(); } public boolean CanJoin(Player player) { return true; } public boolean CanRename(Player player) { return true; } }