source-code/Data.java

package nl.rpsonline.remcodemah.alarmmessage;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;

import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World;

public class Data {
	static File file = new File("plugins/AlarmMessage/data");
	static File folder = new File("plugins/AlarmMessage/");
	public static AlarmMessage plugin;
	
	@SuppressWarnings("unused")
	public static boolean Load() {
		// check if file exists;
		if (!file.exists()) {
			try {
				folder.mkdirs();
				file.createNewFile();
			} catch (IOException e) {
				return false;
			}
			return true;
		}

		// Read data!;
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			String string;
			while ((string = in.readLine()) != null) {
				if (string != null) {
					AlarmMessage log = new AlarmMessage();
					log.logger.info("[AlarmMessage] Reading file");
					stringToHashMap(string);
				}
			}
			in.close();
		} catch (IOException e) {
			AlarmMessage core = new AlarmMessage();
			core.logger.warning("Can not Load the File.");
			return false;
		}
		BufferedReader in;
		return true;
	}

	// Save data!;
	@SuppressWarnings("unused")
	public static boolean Save() {
		if (!file.exists()) {
			try {
				folder.mkdirs();
				file.createNewFile();
			} catch (IOException e) {
				return false;
			}
		}
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(file));
			BufferedReader in = new BufferedReader(new FileReader(file));
			Iterator<Location> iterator = AlarmMessage.hm.keySet().iterator();
			while(iterator.hasNext()){
				Location location = iterator.next();
				String line234 = AlarmMessage.hm.get(location);
			out.write(LocationToString(location));
			out.write(" - " + line234);
			out.newLine();
		}
			out.close();
		} catch (IOException e) {
			return false;
		}
		BufferedWriter out;
		return true;

	}

	private static void stringToHashMap(String string) {		
    String[] split = string.split(" - ");
		
		try {
			World world = Bukkit.getWorld(split[0]);
			double x = Double.parseDouble(split[1]);
			double y = Double.parseDouble(split[2]);
			double z = Double.parseDouble(split[3]);
			Location location = new Location (world, x, y, z);
			
			String tekst = split[4];
			
			AlarmMessage.hm.put(location, tekst);
		} catch (Exception e) {
			AlarmMessage lugin = new AlarmMessage();
			lugin.logger.warning("Error Converting File");
			lugin.logger.warning("Make shure that it look like (World - Double x - Double y - Double z - String)");
			lugin.logger.info("World is just a name, Doubles are cordinates, String is tekst");
			lugin.logger.info("If the file looks correct and you get the error please make a ticket, and attach your file");
		}
	}

	private static String LocationToString(Location location) {
		String world = location.getWorld().getName().trim();
		double x = location.getX();
		double y = location.getY();
		double z = location.getZ();
		String sx = Double.toString(x);
		String sy = Double.toString(y);
		String sz = Double.toString(z);
		String tekst = world + " - " + sx + " - " + sy + " - " + sz;

		return tekst;
	}
}

Comments

Posts Quoted:
Reply
Clear All Quotes