La classe ServerPropertyFile profite maintenant de la librairie Gson

This commit is contained in:
Marc Baloup 2016-11-01 22:23:17 +01:00
parent dd47b2b392
commit 774fac4c64

View File

@ -6,28 +6,23 @@ import java.io.File;
import java.io.FileReader; import java.io.FileReader;
import java.io.FileWriter; import java.io.FileWriter;
import java.io.IOException; import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import com.google.gson.Gson; import com.google.gson.Gson;
public class ServerPropertyFile { public class ServerPropertyFile {
private File file; private transient File file;
private Map<String, Object> data; private String name = "default_name";
private String memory = "512M";
private String javaArgs = "";
private String MinecraftArgs = "";
private String jarFile = "";
public ServerPropertyFile(File f) { public ServerPropertyFile(File f) {
if (f == null) throw new IllegalArgumentException("f ne doit pas être null"); if (f == null) throw new IllegalArgumentException("f ne doit pas être null");
file = f; file = f;
data = new HashMap<>();
data.put("name", "default_name");
data.put("memory", "512M");
data.put("javaArgs", "");
data.put("MinecraftArgs", "");
data.put("jarFile", "");
data.put("isLobby", false);
} }
/** /**
@ -38,22 +33,13 @@ public class ServerPropertyFile {
public boolean loadFromFile() { public boolean loadFromFile() {
try (BufferedReader in = new BufferedReader(new FileReader(file))) { try (BufferedReader in = new BufferedReader(new FileReader(file))) {
Map<String, Object> dataFile = new Gson().fromJson(in, Map.class); ServerPropertyFile dataFile = new Gson().fromJson(in, getClass());
if (!dataFile.containsKey("name") || !(dataFile.get("name") instanceof String)) return false; name = dataFile.name;
memory = dataFile.memory;
if (!dataFile.containsKey("memory") || !(dataFile.get("memory") instanceof String)) return false; javaArgs = dataFile.javaArgs;
MinecraftArgs = dataFile.MinecraftArgs;
if (!dataFile.containsKey("javaArgs") || !(dataFile.get("javaArgs") instanceof String)) return false; jarFile = dataFile.jarFile;
if (!dataFile.containsKey("MinecraftArgs") || !(dataFile.get("MinecraftArgs") instanceof String))
return false;
if (!dataFile.containsKey("jarFile") || !(dataFile.get("jarFile") instanceof String)) return false;
if (!dataFile.containsKey("isLobby") || !(dataFile.get("isLobby") instanceof Boolean)) return false;
data = dataFile;
return true; return true;
} catch (IOException e) { } catch (IOException e) {
@ -65,10 +51,7 @@ public class ServerPropertyFile {
public boolean save() { public boolean save() {
try (BufferedWriter out = new BufferedWriter(new FileWriter(file, false))) { try (BufferedWriter out = new BufferedWriter(new FileWriter(file, false))) {
String jsonStr = new Gson().toJson(data); new Gson().toJson(this, out);
out.append(jsonStr);
out.flush(); out.flush();
return true; return true;
@ -80,56 +63,48 @@ public class ServerPropertyFile {
} }
public String getName() { public String getName() {
return (String) data.get("name"); return name;
} }
public String getMemory() { public String getMemory() {
return (String) data.get("memory"); return memory;
} }
public String getJavaArgs() { public String getJavaArgs() {
return (String) data.get("javaArgs"); return javaArgs;
} }
public String getMinecraftArgs() { public String getMinecraftArgs() {
return (String) data.get("MinecraftArgs"); return MinecraftArgs;
} }
public String getJarFile() { public String getJarFile() {
return (String) data.get("jarFile"); return jarFile;
}
public boolean getIsLobby() {
return (boolean) data.get("isLobby");
} }
public void setName(String n) { public void setName(String n) {
if (n == null || !n.matches("^[a-zA-Z]$")) throw new IllegalArgumentException(); if (n == null || !n.matches("^[a-zA-Z]$")) throw new IllegalArgumentException();
data.put("name", n); name = n;
} }
public void setMemory(String m) { public void setMemory(String m) {
if (m == null || !m.matches("^[0-9]+[mgMG]$")) throw new IllegalArgumentException(); if (m == null || !m.matches("^[0-9]+[mgMG]$")) throw new IllegalArgumentException();
data.put("memory", m); memory = m;
} }
public void setJavaArgs(String ja) { public void setJavaArgs(String ja) {
if (ja == null) throw new IllegalArgumentException(); if (ja == null) throw new IllegalArgumentException();
data.put("javaArgs", ja); javaArgs = ja;
} }
public void setMinecraftArgs(String ma) { public void setMinecraftArgs(String ma) {
if (ma == null) throw new IllegalArgumentException(); if (ma == null) throw new IllegalArgumentException();
data.put("MinecraftArgs", ma); MinecraftArgs = ma;
} }
public void setJarFile(String j) { public void setJarFile(String j) {
if (j == null) throw new IllegalArgumentException(); if (j == null) throw new IllegalArgumentException();
data.put("jarFile", j); jarFile = j;
}
public void setIsLobby(boolean l) {
data.put("isLobby", l);
} }
} }