Mineplex2018-withcommit/Plugins/Mineplex.MapParser/src/mineplex/mapparser/MapData.java

138 lines
2.8 KiB
Java
Raw Normal View History

2014-07-09 03:08:37 +02:00
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 String GameType = "null";
public String MapName = "null";
public String MapCreator = "null";
public HashSet<String> WhiteList;
public HashSet<String> BuildList;
public MapData(String mapFolder)
{
MapFolder = mapFolder;
WhiteList = new HashSet<String>();
BuildList = new HashSet<String>();
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 = tokens[1];
}
else if (tokens[0].equalsIgnoreCase("WHITE_LIST"))
{
for (String cur : tokens[1].split(","))
WhiteList.add(cur);
}
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 whiteList = "";
for (String cur : WhiteList)
whiteList += cur + ",";
out.write("\n");
out.write("WHITE_LIST:"+whiteList);
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 BuildList.contains(player.getName()) || WhiteList.contains(player.getName()) || player.isOp();
}
}