OSDN Git Service

パッケージ変更
[chemicraft/ChemiCraftNext.git] / src / asia / tcrs / ccnp / addon / crops / util / MultiBlockDataStream.java
1 package asia.tcrs.ccnp.addon.crops.util;
2
3 import java.io.BufferedReader;
4 import java.io.BufferedWriter;
5 import java.io.File;
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;
11
12 public class MultiBlockDataStream {
13
14         private String dirPath;
15         private String filePath;
16
17         private BufferedReader input;
18         private BufferedWriter output;
19
20         private File file;
21
22         private HashSet<MultiBlockData> plantDataHash = new HashSet<MultiBlockData>();
23
24         public MultiBlockDataStream(String dirPath, String filePath) {
25                 this.dirPath = dirPath;
26                 this.filePath = filePath;
27         }
28
29         public void createDataInputStream() throws IOException {
30                 this.file = new File(this.dirPath + this.filePath);
31                 if (!this.file.exists()) {
32                         this.file.createNewFile();
33                 }
34
35                 if (this.file.canRead()) {
36                         if (this.file.canWrite()) {
37                                 this.input = new BufferedReader(
38                                                 new FileReader(this.file)
39                                                 );
40                         } else {
41                                 throw new IOException("You don't have Write Permission.");
42                         }
43                 } else {
44                         throw new IOException("You don't have Read Permission.");
45                 }
46         }
47
48         public void createDataOutputStream() throws IOException {
49                 this.file = new File(this.dirPath + this.filePath);
50                 if (!this.file.exists()) {
51                         this.file.createNewFile();
52                 }
53
54                 if (this.file.canRead()) {
55                         if (this.file.canWrite()) {
56                                 this.output = new BufferedWriter(
57                                                 new FileWriter(this.file)
58                                                 );
59                         } else {
60                                 throw new IOException("You don't have Write Permission.");
61                         }
62                 } else {
63                         throw new IOException("You don't have Read Permission.");
64                 }
65         }
66
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) {
72                                 continue;
73                         }
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++) {
81                                 p.add(datas[7 + i]);
82                         }
83                         this.plantDataHash.add(p);
84                 }
85                 this.input.close();
86         }
87
88         public void write() throws IOException {
89                 try {
90                         for (MultiBlockData p: this.plantDataHash) {
91                                 String result = "";
92                                 String worldName = p.getWorldName();
93                                 int dimID = p.getDimID();
94                                 int x = p.getX();
95                                 int y = p.getY();
96                                 int z = p.getZ();
97                                 ArrayList<String> others = p.getOthers();
98
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) + "#";
106                                 }
107                                 this.output.newLine();
108                                 this.output.write(result);
109                                 this.output.flush();
110                         }
111                 } catch (IOException e) {
112                 }
113                 this.output.close();
114         }
115
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);
121                         }
122                 }
123                 System.out.println("Data not found");
124                 return null;
125         }
126
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)) {
131                                 p.add(data);
132                                 return;
133                         }
134                 }
135                 MultiBlockData newPlantData = new MultiBlockData(worldName, dimID, x, y, z);
136                 newPlantData.add(data);
137                 this.plantDataHash.add(newPlantData);
138         }
139
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);
145                                 return;
146                         }
147                 }
148         }
149
150         public void clearPlantDataHash() {
151                 this.plantDataHash.clear();
152         }
153
154 }