OSDN Git Service

エラー大半修正
[chemicraft/chemicraft.git] / common / pcc / addon / crops / util / MultiBlockDataStream.java
1 package pcc.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 import java.util.Iterator;
12
13 public class MultiBlockDataStream {
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<MultiBlockData> plantDataHash = new HashSet<MultiBlockData>();
24
25         public MultiBlockDataStream(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                         MultiBlockData p = new MultiBlockData(worldName, dimID, x, y, z);
81                         for (int i = 0; i < datas.length - 5; i++) {
82                                 p.add(datas[7 + i]);
83                         }
84                         this.plantDataHash.add(p);
85                 }
86                 this.input.close();
87         }
88
89         public void write() throws IOException {
90                 try {
91                         for (MultiBlockData p: this.plantDataHash) {
92                                 String result = "";
93                                 String worldName = p.getWorldName();
94                                 int dimID = p.getDimID();
95                                 int x = p.getX();
96                                 int y = p.getY();
97                                 int z = p.getZ();
98                                 ArrayList<String> others = p.getOthers();
99
100                                 result = result + worldName + "#";
101                                 result = result + dimID + "#";
102                                 result = result + x + "#";
103                                 result = result + y + "#";
104                                 result = result + z + "#";
105                                 for (int i = 0; i < others.size(); i++) {
106                                         result = result + others.get(i) + "#";
107                                 }
108                                 this.output.newLine();
109                                 this.output.write(result);
110                                 this.output.flush();
111                         }
112                 } catch (IOException e) {
113                 }
114                 this.output.close();
115         }
116
117         public String get(String worldName, int dimID, int x, int y, int z, int dataID) {
118                 for (MultiBlockData p: this.plantDataHash) {
119                         MultiBlockData newPlantData = new MultiBlockData(worldName, dimID, x, y, z);
120                         if (p.equals(newPlantData)) {
121                                 return p.getOthers().get(dataID);
122                         }
123                 }
124                 System.out.println("Data not found");
125                 return null;
126         }
127
128         public void set(String worldName, int dimID, int x, int y, int z, String data) {
129                 for (MultiBlockData p: this.plantDataHash) {
130                         MultiBlockData newPlantData = new MultiBlockData(worldName, dimID, x, y, z);
131                         if (p.equals(newPlantData)) {
132                                 p.add(data);
133                                 return;
134                         }
135                 }
136                 MultiBlockData newPlantData = new MultiBlockData(worldName, dimID, x, y, z);
137                 newPlantData.add(data);
138                 this.plantDataHash.add(newPlantData);
139         }
140
141         public void remove(String worldName, int dimID, int x, int y, int z) {
142                 for (MultiBlockData p: this.plantDataHash) {
143                         MultiBlockData newPlantData = new MultiBlockData(worldName, dimID, x, y, z);
144                         if (p.equals(newPlantData)) {
145                                 this.plantDataHash.remove(p);
146                                 return;
147                         }
148                 }
149         }
150
151         public void clearPlantDataHash() {
152                 this.plantDataHash.clear();
153         }
154
155 }