1 package asia.tcrs.ccnp.addon.crops.util;
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
6 import java.io.FileReader;
7 import java.io.FileWriter;
8 import java.io.IOException;
9 import java.util.ArrayList;
10 import java.util.HashSet;
12 public class MultiBlockDataStream {
14 private String dirPath;
15 private String filePath;
17 private BufferedReader input;
18 private BufferedWriter output;
22 private HashSet<MultiBlockData> plantDataHash = new HashSet<MultiBlockData>();
24 public MultiBlockDataStream(String dirPath, String filePath) {
25 this.dirPath = dirPath;
26 this.filePath = filePath;
29 public void createDataInputStream() throws IOException {
30 this.file = new File(this.dirPath + this.filePath);
31 if (!this.file.exists()) {
32 this.file.createNewFile();
35 if (this.file.canRead()) {
36 if (this.file.canWrite()) {
37 this.input = new BufferedReader(
38 new FileReader(this.file)
41 throw new IOException("You don't have Write Permission.");
44 throw new IOException("You don't have Read Permission.");
48 public void createDataOutputStream() throws IOException {
49 this.file = new File(this.dirPath + this.filePath);
50 if (!this.file.exists()) {
51 this.file.createNewFile();
54 if (this.file.canRead()) {
55 if (this.file.canWrite()) {
56 this.output = new BufferedWriter(
57 new FileWriter(this.file)
60 throw new IOException("You don't have Write Permission.");
63 throw new IOException("You don't have Read Permission.");
67 public void read() throws IOException {
68 String readData = null;
69 while ((readData = this.input.readLine()) != null) {
70 String[] datas = readData.split("#");
71 if (datas.length < 6) {
74 String worldName = datas[0];
75 int dimID = Integer.parseInt(datas[1]);
76 int x = Integer.parseInt(datas[2]);
77 int y = Integer.parseInt(datas[3]);
78 int z = Integer.parseInt(datas[4]);
79 MultiBlockData p = new MultiBlockData(worldName, dimID, x, y, z);
80 for (int i = 0; i < datas.length - 5; i++) {
83 this.plantDataHash.add(p);
88 public void write() throws IOException {
90 for (MultiBlockData p: this.plantDataHash) {
92 String worldName = p.getWorldName();
93 int dimID = p.getDimID();
97 ArrayList<String> others = p.getOthers();
99 result = result + worldName + "#";
100 result = result + dimID + "#";
101 result = result + x + "#";
102 result = result + y + "#";
103 result = result + z + "#";
104 for (int i = 0; i < others.size(); i++) {
105 result = result + others.get(i) + "#";
107 this.output.newLine();
108 this.output.write(result);
111 } catch (IOException e) {
116 public String get(String worldName, int dimID, int x, int y, int z, int dataID) {
117 for (MultiBlockData p: this.plantDataHash) {
118 MultiBlockData newPlantData = new MultiBlockData(worldName, dimID, x, y, z);
119 if (p.equals(newPlantData)) {
120 return p.getOthers().get(dataID);
123 System.out.println("Data not found");
127 public void set(String worldName, int dimID, int x, int y, int z, String data) {
128 for (MultiBlockData p: this.plantDataHash) {
129 MultiBlockData newPlantData = new MultiBlockData(worldName, dimID, x, y, z);
130 if (p.equals(newPlantData)) {
135 MultiBlockData newPlantData = new MultiBlockData(worldName, dimID, x, y, z);
136 newPlantData.add(data);
137 this.plantDataHash.add(newPlantData);
140 public void remove(String worldName, int dimID, int x, int y, int z) {
141 for (MultiBlockData p: this.plantDataHash) {
142 MultiBlockData newPlantData = new MultiBlockData(worldName, dimID, x, y, z);
143 if (p.equals(newPlantData)) {
144 this.plantDataHash.remove(p);
150 public void clearPlantDataHash() {
151 this.plantDataHash.clear();