package igotyou.SnitchTrack;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.apache.commons.net.ftp.FTPClient;
public class MapElementManager
{
private List<MapElement> mapElements;
public MapElementManager()
{
mapElements = new ArrayList<MapElement>();
try
{
load(new File("mapElements.txt"));
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void downloadFromFtp(String host, int port, String username, String password, String fileName)
{
FTPClient client = new FTPClient();
FileOutputStream fos = null;
try
{
client.connect(host,port);
client.login(username, password);
fos = new FileOutputStream(fileName);
client.retrieveFile("/" + fileName, fos);
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
if (fos != null)
{
fos.close();
}
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
this.load(new File(fileName));
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void uploadToFtp(String host, int port, String username, String password, String fileName)
{
FTPClient client = new FTPClient();
FileInputStream fis = null;
try
{
client.connect(host, port);
client.login(username, password);
fis = new FileInputStream(fileName);
client.storeFile(fileName, fis);
client.logout();
}
catch (IOException e)
{
e.printStackTrace();
}
try
{
if (fis != null)
{
fis.close();
}
client.disconnect();
}
catch (IOException e)
{
e.printStackTrace();
}
}
public void load(File file) throws IOException
{
FileInputStream fileInputStream = new FileInputStream(file);
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
String line;
while ((line = bufferedReader.readLine()) != null)
{
String parts[] = line.split("�");
//ORDER x1 z1 x2 z1 world name type
int x1 = Integer.parseInt(parts[0]);
int z1 = Integer.parseInt(parts[1]);
int x2 = Integer.parseInt(parts[2]);
int z2 = Integer.parseInt(parts[3]);
int world = Integer.parseInt(parts[4]);
String name = parts[5];
int type = Integer.parseInt(parts[6]);
create_element(x1,z1,x2,z2,world,name,type);
}
fileInputStream.close();
}
public void save(File file) throws IOException
{
FileOutputStream fileOutputStream = new FileOutputStream(file);
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
for (MapElement element : mapElements)
{
//ORDER x1 z1 x2 z1 world name type
bufferedWriter.append(Integer.toString(element.get_x1()));
bufferedWriter.append("�");
bufferedWriter.append(Integer.toString(element.get_z1()));
bufferedWriter.append("�");
bufferedWriter.append(Integer.toString(element.get_x2()));
bufferedWriter.append("�");
bufferedWriter.append(Integer.toString(element.get_z2()));
bufferedWriter.append("�");
bufferedWriter.append(Integer.toString(element.get_world()));
bufferedWriter.append("�");
bufferedWriter.append(element.get_name());
bufferedWriter.append("�");
bufferedWriter.append(Integer.toString(element.get_type()));
bufferedWriter.append("\n");
}
bufferedWriter.flush();
fileOutputStream.close();
}
public void create_element(int x1, int z1, int x2, int z2, int world, String name, int type)
{
MapElement element = new MapElement(x1,z1,x2,z2,world,name,type);
mapElements.add(element);
}
public void remove_element(MapElement element)
{
mapElements.remove(element);
}
public List<MapElement> get_elements()
{
return mapElements;
}
}