Skip to content

Commit

Permalink
File data is now stored directly in a JSONObject rather than in a Map…
Browse files Browse the repository at this point in the history
… and then being stored in a JSONObject
  • Loading branch information
TheGamingGrunts committed Apr 13, 2015
1 parent 7b98609 commit e76a40a
Showing 1 changed file with 20 additions and 35 deletions.
55 changes: 20 additions & 35 deletions src/me/projectx/thunderbolt2/models/ThunderFile.java
Expand Up @@ -5,24 +5,22 @@
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import me.projectx.thunderbolt2.interfaces.FileLayout;
import me.projectx.thunderbolt2.org.json.JSONArray;
import me.projectx.thunderbolt2.org.json.JSONObject;

/**
* File wrapper class containing numerous useful methods for setting, saving, and retrieving information
* from JSON files
* File class containing numerous useful methods for
* setting, saving, and retrieving information from JSON files
*
* @author Daniel S. (The Gaming Grunts)
*/
public class ThunderFile implements FileLayout{

private String name, path;
public Map<String, Object> map = new HashMap<String, Object>();
private JSONObject jo = new JSONObject();

/**
* @see ThunderFile
Expand All @@ -36,7 +34,7 @@ public ThunderFile(String name, String path){
if (!f.exists()){
try {
f.createNewFile();
System.out.println("[ThunderBolt 2] Created new file " + name + ".json at " + path);
System.out.println("[Thunderbolt 2] Created new file " + name + ".json at " + path);
} catch(IOException e) {
e.printStackTrace();
}
Expand All @@ -52,51 +50,51 @@ public String getPath(){
}

public void set(String key, Object value){
map.put(key, value);
jo.put(key, value);
}

public Object get(String key){
return map.get(key);
return jo.get(key);
}

public String getString(String key){
return (String)map.get(key);
return (String)jo.get(key);
}

public byte getByte(String key){
return (byte)map.get(key);
return (byte)jo.get(key);
}

public short getShort(String key){
return (short)map.get(key);
return (short)jo.get(key);
}

public int getInt(String key){
return (int)map.get(key);
return (int)jo.get(key);
}

public double getDouble(String key){
return (double)map.get(key);
return (double)jo.get(key);
}

public long getLong(String key){
return (long)map.get(key);
return (long)jo.get(key);
}

public float getFloat(String key){
return (float)map.get(key);
return (float)jo.get(key);
}

public boolean getBoolean(String key){
return (boolean)map.get(key);
return (boolean)jo.get(key);
}

/**
* Courtesy of the Bukkit Project
* {@link https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/configuration/MemorySection.java#L355}
*/
private List<?> getList(String key, List<?> l){
Object o = map.get(key);
Object o = jo.get(key);
return (List<?>) ((o instanceof List) ? o : l);

}
Expand All @@ -107,7 +105,7 @@ private List<?> getList(String key, List<?> l){
* {@link https://github.com/Bukkit/Bukkit/blob/master/src/main/java/org/bukkit/configuration/MemorySection.java#L360}
*/
private List<?> getList(String key) {
Object o = map.get(key);
Object o = jo.get(key);
List<Object> l = new ArrayList<Object>();
JSONArray ja = new JSONArray(o.toString());
for (int i = 0; i < ja.length(); i++){
Expand Down Expand Up @@ -204,30 +202,17 @@ public List<Float> getFloatList(String key){
}
return list;
}

/*public Map<?, ?> getMap(String key){
Iterator<?> i = map.entrySet().iterator();
Map<String, Object> newMap = new HashMap<String, Object>();
while(i.hasNext()){
String key1 = (String)i.next();
Object value = map.get(key1);
newMap.put(key1, value);
}
return newMap;
}*/

public void save() throws IOException{
new Thread(){
public void run(){
JSONObject obj = new JSONObject();
for (Map.Entry<String, Object> entry : map.entrySet()){
obj.put(entry.getKey(), entry.getValue());
}
try {
PrintWriter writer = new PrintWriter(new FileWriter(path + File.separator + name + ".json", false));
writer.write(obj.toString(2));
writer.write(jo.toString(2));
writer.close();
} catch(IOException e) {}
} catch(IOException e) {
e.printStackTrace();
}
}
}.start();
}
Expand Down

0 comments on commit e76a40a

Please sign in to comment.