我目前正致力于将所有House对象数据保存到文件中。当前我通过.txt加载它并使用BufferedWriter中的write函数保存它。我相信我可以使用JSON来解析我的所有HouseObject数据,但我不知道我该怎么做。我对这一切都很陌生。
这是我的加载/保存文件:
package com.darkasylum.rs2.content.house;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import com.darkasylum.rs2.entity.object.GameObject;
import com.darkasylum.rs2.entity.player.Player;
public class HouseUtils {
// Map of all houses.
public static HashMap<Integer, HouseObject> ALL_HOUSES = new HashMap<Integer, HouseObject>();
public static int currentlyLoaded = 0;
// load all hosues
public static void load(Player player) {
FileReader fileReader = null;
BufferedReader reader = null;
try {
if (!ALL_HOUSES.containsValue(player.getHouse())) {
fileReader = new FileReader(new File("./data/houses/" + player.getUsername() + ".txt"));
reader = new BufferedReader(fileReader);
ALL_HOUSES.put(currentlyLoaded, player.getHouse());
player.player_house_key = currentlyLoaded;
currentlyLoaded++;
try {
reader.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} else {
System.out.println("Already in map");
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// save/input new house for new player
public static void save(Player player) {
BufferedWriter writeHouse = null;
File playerfile = new File("./data/houses/" + player.getUsername() + ".txt");
if (!playerfile.exists()) {
try {
playerfile.createNewFile();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
try {
writeHouse = new BufferedWriter(new FileWriter(playerfile));
writeHouse.write("Owner Name:" + player.getHouse().getOwner_name() + "\n");
writeHouse.write("House Name:" + player.getHouse().getHouse_name() + "\n");
writeHouse.write("Personal Objects:\n");
player.getHouse();
for (int i = 0; i < player.getHouse().myPersonalObjects.size(); i++) {
writeHouse.write("\t" + player.getHouse().myPersonalObjects.get(i).getId() + ", " + player.getHouse().myPersonalObjects.get(i).getLocation().getX() + ", " + player.getHouse().myPersonalObjects.get(i).getLocation().getY() + ", " + player.getHouse().myPersonalObjects.get(i).getType() + ", " + player.getHouse().myPersonalObjects.get(i).getType() + "\n");
}
writeHouse.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
这是我的HouseObject
package com.darkasylum.rs2.content.house;
import java.util.HashMap;
import com.darkasylum.rs2.entity.object.GameObject;
public class HouseObject {
private String house_name;
private String owner_name;
public HashMap<Integer, GameObject> myPersonalObjects = new HashMap<Integer, GameObject>();
public String getOwner_name() {
return owner_name;
}
public void setOwner_name(String owner_name) {
this.owner_name = owner_name;
}
public String getHouse_name() {
return house_name;
}
public void setHouse_name(String house_name) {
this.house_name = house_name;
}
public HouseObject() {
this.house_name = "N/A";
this.owner_name = "N/A";
}
public HouseObject(String house_name, String owner_name, GameObject object) {
this.house_name = house_name;
this.owner_name = owner_name;
add_object(object);
}
public void add_object(GameObject objectToAdd) {
myPersonalObjects.put(myPersonalObjects.size(), objectToAdd);
}
}
我真的想要一些有关改进的提示。我知道我有一些格式错误,比如我的一些命名惯例,但那些是我的一些习惯,我真的不想改善个人喜好。