1 package pcc.addon.crops;
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;
11 import java.util.Iterator;
13 public class PlantDataStream {
15 private String dirPath;
16 private String filePath;
18 private BufferedReader input;
19 private BufferedWriter output;
23 private HashSet<PlantData> plantDataHash = new HashSet<PlantData>();
25 public PlantDataStream(String dirPath, String filePath) {
26 this.dirPath = dirPath;
27 this.filePath = filePath;
30 public void createDataInputStream() throws IOException {
31 this.file = new File(this.dirPath + this.filePath);
32 if (!this.file.exists()) {
33 this.file.createNewFile();
36 if (this.file.canRead()) {
37 if (this.file.canWrite()) {
38 this.input = new BufferedReader(
39 new FileReader(this.file)
42 throw new IOException("You don't have Write Permission.");
45 throw new IOException("You don't have Read Permission.");
49 public void createDataOutputStream() throws IOException {
50 this.file = new File(this.dirPath + this.filePath);
51 if (!this.file.exists()) {
52 this.file.createNewFile();
55 if (this.file.canRead()) {
56 if (this.file.canWrite()) {
57 this.output = new BufferedWriter(
58 new FileWriter(this.file)
61 throw new IOException("You don't have Write Permission.");
64 throw new IOException("You don't have Read Permission.");
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) {
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++) {
86 this.plantDataHash.add(p);
91 public void write() throws IOException {
93 for (PlantData p: this.plantDataHash) {
95 String worldName = p.getWorldName();
96 int dimID = p.getDimID();
101 int meta = p.getMeta();
102 ArrayList<String> others = p.getOthers();
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) + "#";
114 this.output.newLine();
115 this.output.write(result);
118 } catch (IOException e) {
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);
130 System.out.println("Data not found");
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)) {
141 System.out.println("ID not found");
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)) {
152 System.out.println("Meta not found");
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)) {
164 PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, 0);
165 newPlantData.add(data);
166 this.plantDataHash.add(newPlantData);
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)) {
177 PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, id, 0);
178 this.plantDataHash.add(newPlantData);
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)) {
189 PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, 0, meta);
190 this.plantDataHash.add(newPlantData);
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)) {
202 PlantData newPlantData = new PlantData(worldName, dimID, x, y, z, id, meta);
203 this.plantDataHash.add(newPlantData);
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);
216 public void clearPlantDataHash() {
217 this.plantDataHash.clear();