OSDN Git Service

656265a1804bce404f4c160332a4580c9d2bac76
[chemicraft/chemicraft.git] / common / pcc / addon / crops / PlantDataStream.java
1 package pcc.addon.crops;
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 import java.util.Iterator;
12
13 public class PlantDataStream {
14
15         private String dirPath;
16         private String filePath;
17
18         private BufferedReader input;
19         private BufferedWriter output;
20
21         private File file;
22
23         private HashSet<PlantData> plantDataHash = new HashSet<PlantData>();
24
25         public PlantDataStream(String dirPath, String filePath) {
26                 this.dirPath = dirPath;
27                 this.filePath = filePath;
28         }
29
30         public void createDataInputStream() throws IOException {
31                 this.file = new File(this.dirPath + this.filePath);
32                 if (!this.file.exists()) {
33                         this.file.createNewFile();
34                 }
35
36                 if (this.file.canRead()) {
37                         if (this.file.canWrite()) {
38                                 this.input = new BufferedReader(
39                                                 new FileReader(this.file)
40                                                 );
41                         } else {
42                                 throw new IOException("You don't have Write Permission.");
43                         }
44                 } else {
45                         throw new IOException("You don't have Read Permission.");
46                 }
47         }
48
49         public void createDataOutputStream() throws IOException {
50                 this.file = new File(this.dirPath + this.filePath);
51                 if (!this.file.exists()) {
52                         this.file.createNewFile();
53                 }
54
55                 if (this.file.canRead()) {
56                         if (this.file.canWrite()) {
57                                 this.output = new BufferedWriter(
58                                                 new FileWriter(this.file)
59                                                 );
60                         } else {
61                                 throw new IOException("You don't have Write Permission.");
62                         }
63                 } else {
64                         throw new IOException("You don't have Read Permission.");
65                 }
66         }
67
68         public void read() throws IOException {
69                 String readData = null;
70                 while ((readData = this.input.readLine()) != null) {
71                         String[] datas = readData.split("#");
72                         if (datas.length < 6) {
73                                 continue;
74                         }
75                         String worldName = datas[0];
76                         int dimID = Integer.parseInt(datas[1]);
77                         int x = Integer.parseInt(datas[2]);
78                         int y = Integer.parseInt(datas[3]);
79                         int z = Integer.parseInt(datas[4]);
80                         int id = Integer.parseInt(datas[5]);
81                         int meta = Integer.parseInt(datas[6]);
82                         PlantData p = new PlantData(worldName, dimID, x, y, z, id, meta);
83                         for (int i = 0; i < datas.length - 7; i++) {
84                                 p.add(datas[7 + i]);
85                         }
86                         this.plantDataHash.add(p);
87                 }
88                 this.input.close();
89         }
90
91         public void write() throws IOException {
92                 try {
93                         for (PlantData p: this.plantDataHash) {
94                                 String result = "";
95                                 String worldName = p.getWorldName();
96                                 int dimID = p.getDimID();
97                                 int x = p.getX();
98                                 int y = p.getY();
99                                 int z = p.getZ();
100                                 int id = p.getId();
101                                 int meta = p.getMeta();
102                                 ArrayList<String> others = p.getOthers();
103
104                                 result = result + worldName + "#";
105                                 result = result + dimID + "#";
106                                 result = result + x + "#";
107                                 result = result + y + "#";
108                                 result = result + z + "#";
109                                 result = result + id + "#";
110                                 result = result + meta + "#";
111                                 for (int i = 0; i < others.size(); i++) {
112                                         result = result + others.get(i) + "#";
113                                 }
114                                 this.output.newLine();
115                                 this.output.write(result);
116                                 this.output.flush();
117                         }
118                 } catch (IOException e) {
119                 }
120                 this.output.close();
121         }
122
123         public String get(String worldName, int dimID, int x, int y, int z, int dataID) {
124                 for (PlantData p: this.plantDataHash) {
125                         PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, 0);
126                         if (p.equals(newPlantData)) {
127                                 return p.getOthers().get(dataID);
128                         }
129                 }
130                 System.out.println("Data not found");
131                 return null;
132         }
133
134         public int getID(String worldName, int dimID, int x, int y, int z) {
135                 for (PlantData p: this.plantDataHash) {
136                         PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, 0);
137                         if (p.equals(newPlantData)) {
138                                 return p.getId();
139                         }
140                 }
141                 System.out.println("ID not found");
142                 return -1;
143         }
144
145         public int getMeta(String worldName, int dimID, int x, int y, int z) {
146                 for (PlantData p: this.plantDataHash) {
147                         PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, 0);
148                         if (p.equals(newPlantData)) {
149                                 return p.getMeta();
150                         }
151                 }
152                 System.out.println("Meta not found");
153                 return -1;
154         }
155
156         public void set(String worldName, int dimID, int x, int y, int z, String data) {
157                 for (PlantData p: this.plantDataHash) {
158                         PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, 0);
159                         if (p.equals(newPlantData)) {
160                                 p.add(data);
161                                 return;
162                         }
163                 }
164                 PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, 0);
165                 newPlantData.add(data);
166                 this.plantDataHash.add(newPlantData);
167         }
168
169         public void setID(String worldName, int dimID, int x, int y, int z, int id) {
170                 for (PlantData p: this.plantDataHash) {
171                         PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, 0);
172                         if (p.equals(newPlantData)) {
173                                 p.setId(id);
174                                 return;
175                         }
176                 }
177                 PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, id, 0);
178                 this.plantDataHash.add(newPlantData);
179         }
180
181         public void setMeta(String worldName, int dimID, int x, int y, int z, int meta) {
182                 for (PlantData p: this.plantDataHash) {
183                         PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, 0);
184                         if (p.equals(newPlantData)) {
185                                 p.setMeta(meta);
186                                 return;
187                         }
188                 }
189                 PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, meta);
190                 this.plantDataHash.add(newPlantData);
191         }
192
193         public void setData(String worldName, int dimID, int x, int y, int z, int id, int meta) {
194                 for (PlantData p: this.plantDataHash) {
195                         PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, 0);
196                         if (p.equals(newPlantData)) {
197                                 p.setId(id);
198                                 p.setMeta(meta);
199                                 return;
200                         }
201                 }
202                 PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, id, meta);
203                 this.plantDataHash.add(newPlantData);
204         }
205
206         public void remove(String worldName, int dimID, int x, int y, int z) {
207                 for (PlantData p: this.plantDataHash) {
208                         PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, 0);
209                         if (p.equals(newPlantData)) {
210                                 this.plantDataHash.remove(p);
211                                 return;
212                         }
213                 }
214         }
215
216         public void clearPlantDataHash() {
217                 this.plantDataHash.clear();
218         }
219
220 }