Added ability to load old pre-1.1 data files (board, factions, followers) if they exist and new files don't.
Also made 1.1.2 release with this update.
This commit is contained in:
		@@ -1,11 +1,11 @@
 | 
			
		||||
package com.bukkit.mcteam.factions;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.*;
 | 
			
		||||
import java.lang.reflect.Type;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
import java.util.Iterator;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.Map;
 | 
			
		||||
import java.util.Map.Entry;
 | 
			
		||||
import java.util.TreeMap;
 | 
			
		||||
@@ -14,6 +14,9 @@ import org.bukkit.ChatColor;
 | 
			
		||||
 | 
			
		||||
import com.bukkit.mcteam.factions.util.TextUtil;
 | 
			
		||||
import com.bukkit.mcteam.gson.reflect.TypeToken;
 | 
			
		||||
import com.bukkit.mcteam.gson.JsonArray;
 | 
			
		||||
import com.bukkit.mcteam.gson.JsonObject;
 | 
			
		||||
import com.bukkit.mcteam.gson.JsonParser;
 | 
			
		||||
import com.bukkit.mcteam.util.AsciiCompass;
 | 
			
		||||
import com.bukkit.mcteam.util.DiscUtil;
 | 
			
		||||
 | 
			
		||||
@@ -204,7 +207,8 @@ public class Board {
 | 
			
		||||
		Factions.log("Loading board from disk");
 | 
			
		||||
		
 | 
			
		||||
		if ( ! file.exists()) {
 | 
			
		||||
			Factions.log("No board to load from disk. Creating new file.");
 | 
			
		||||
			if ( ! loadOld())
 | 
			
		||||
				Factions.log("No board to load from disk. Creating new file.");
 | 
			
		||||
			save();
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
@@ -221,6 +225,50 @@ public class Board {
 | 
			
		||||
			
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static boolean loadOld() {
 | 
			
		||||
		File folderBoard = new File(Factions.instance.getDataFolder(), "board");
 | 
			
		||||
 | 
			
		||||
		if ( ! folderBoard.isDirectory())
 | 
			
		||||
			return false;
 | 
			
		||||
 | 
			
		||||
		Factions.log("Board file doesn't exist, attempting to load old pre-1.1 data.");
 | 
			
		||||
 | 
			
		||||
		String ext = ".json";
 | 
			
		||||
 | 
			
		||||
		class jsonFileFilter implements FileFilter {
 | 
			
		||||
			@Override
 | 
			
		||||
			public boolean accept(File file) {
 | 
			
		||||
				return (file.getName().toLowerCase().endsWith(".json") && file.isFile());
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		File[] jsonFiles = folderBoard.listFiles(new jsonFileFilter());
 | 
			
		||||
		for (File jsonFile : jsonFiles) {
 | 
			
		||||
			// Extract the name from the filename. The name is filename minus ".json"
 | 
			
		||||
			String name = jsonFile.getName();
 | 
			
		||||
			name = name.substring(0, name.length() - ext.length());
 | 
			
		||||
			try {
 | 
			
		||||
				JsonParser parser = new JsonParser();
 | 
			
		||||
				JsonObject json = (JsonObject) parser.parse(DiscUtil.read(jsonFile));
 | 
			
		||||
				JsonArray coords = json.getAsJsonArray("coordFactionIds");
 | 
			
		||||
				Iterator coordSet = coords.iterator();
 | 
			
		||||
				while(coordSet.hasNext()) {
 | 
			
		||||
					JsonArray coordDat = (JsonArray) coordSet.next();
 | 
			
		||||
					JsonObject coord = coordDat.get(0).getAsJsonObject();
 | 
			
		||||
					int coordX = coord.get("x").getAsInt();
 | 
			
		||||
					int coordZ = coord.get("z").getAsInt();
 | 
			
		||||
					int factionId = coordDat.get(1).getAsInt();
 | 
			
		||||
					flocationIds.put(new FLocation(name, coordX, coordZ), factionId);
 | 
			
		||||
				}
 | 
			
		||||
				Factions.log("loaded pre-1.1 board "+name);
 | 
			
		||||
			} catch (Exception e) {
 | 
			
		||||
				e.printStackTrace();
 | 
			
		||||
				Factions.log(Level.WARNING, "failed to load board "+name);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,9 +1,9 @@
 | 
			
		||||
package com.bukkit.mcteam.factions;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.*;
 | 
			
		||||
import java.lang.reflect.Type;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.logging.Level;
 | 
			
		||||
import java.util.Map.Entry;
 | 
			
		||||
 | 
			
		||||
import org.bukkit.ChatColor;
 | 
			
		||||
@@ -470,7 +470,8 @@ public class FPlayer {
 | 
			
		||||
	public static boolean load() {
 | 
			
		||||
		Factions.log("Loading players from disk");
 | 
			
		||||
		if ( ! file.exists()) {
 | 
			
		||||
			Factions.log("No players to load from disk. Creating new file.");
 | 
			
		||||
			if ( ! loadOld())
 | 
			
		||||
				Factions.log("No players to load from disk. Creating new file.");
 | 
			
		||||
			save();
 | 
			
		||||
			return true;
 | 
			
		||||
		}
 | 
			
		||||
@@ -515,5 +516,41 @@ public class FPlayer {
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	private static boolean loadOld() {
 | 
			
		||||
		File folderFollower = new File(Factions.instance.getDataFolder(), "follower");
 | 
			
		||||
 | 
			
		||||
		if ( ! folderFollower.isDirectory())
 | 
			
		||||
			return false;
 | 
			
		||||
 | 
			
		||||
		Factions.log("Players file doesn't exist, attempting to load old pre-1.1 data.");
 | 
			
		||||
 | 
			
		||||
		String ext = ".json";
 | 
			
		||||
 | 
			
		||||
		class jsonFileFilter implements FileFilter {
 | 
			
		||||
			@Override
 | 
			
		||||
			public boolean accept(File file) {
 | 
			
		||||
				return (file.getName().toLowerCase().endsWith(".json") && file.isFile());
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		File[] jsonFiles = folderFollower.listFiles(new jsonFileFilter());
 | 
			
		||||
 | 
			
		||||
		for (File jsonFile : jsonFiles) {
 | 
			
		||||
			// Extract the name from the filename. The name is filename minus ".json"
 | 
			
		||||
			String name = jsonFile.getName();
 | 
			
		||||
			name = name.substring(0, name.length() - ext.length());
 | 
			
		||||
			try {
 | 
			
		||||
				FPlayer follower = Factions.gson.fromJson(DiscUtil.read(jsonFile), FPlayer.class);
 | 
			
		||||
				follower.playerName = name;
 | 
			
		||||
				follower.lastLoginTime = System.currentTimeMillis();
 | 
			
		||||
				instances.put(follower.playerName, follower);
 | 
			
		||||
				Factions.log("loaded pre-1.1 follower "+name);
 | 
			
		||||
			} catch (Exception e) {
 | 
			
		||||
				e.printStackTrace();
 | 
			
		||||
				Factions.log(Level.WARNING, "failed to load follower "+name);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
@@ -1,7 +1,6 @@
 | 
			
		||||
package com.bukkit.mcteam.factions;
 | 
			
		||||
 | 
			
		||||
import java.io.File;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.io.*;
 | 
			
		||||
import java.lang.reflect.Type;
 | 
			
		||||
import java.util.*;
 | 
			
		||||
import java.util.Map.Entry;
 | 
			
		||||
@@ -345,7 +344,8 @@ public class Faction {
 | 
			
		||||
		Factions.log("Loading factions from disk");
 | 
			
		||||
		
 | 
			
		||||
		if ( ! file.exists()) {
 | 
			
		||||
			Factions.log("No factions to load from disk. Creating new file.");
 | 
			
		||||
			if ( ! loadOld())
 | 
			
		||||
				Factions.log("No factions to load from disk. Creating new file.");
 | 
			
		||||
			save();
 | 
			
		||||
		}
 | 
			
		||||
		
 | 
			
		||||
@@ -392,7 +392,7 @@ public class Faction {
 | 
			
		||||
		}
 | 
			
		||||
		nextId += 1; // make it the next id and not the current highest.
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
 | 
			
		||||
	public static Faction get(Integer factionId) {
 | 
			
		||||
		if ( ! instances.containsKey(factionId)) {
 | 
			
		||||
			Factions.log(Level.WARNING, "Non existing factionId "+factionId+" requested! Issuing cleaning!");
 | 
			
		||||
@@ -439,4 +439,41 @@ public class Faction {
 | 
			
		||||
		// Clean the fplayers
 | 
			
		||||
		FPlayer.clean();
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	private static boolean loadOld() {
 | 
			
		||||
		File folderFaction = new File(Factions.instance.getDataFolder(), "faction");
 | 
			
		||||
 | 
			
		||||
		if ( ! folderFaction.isDirectory())
 | 
			
		||||
			return false;
 | 
			
		||||
 | 
			
		||||
		Factions.log("Factions file doesn't exist, attempting to load old pre-1.1 data.");
 | 
			
		||||
 | 
			
		||||
		String ext = ".json";
 | 
			
		||||
 | 
			
		||||
		class jsonFileFilter implements FileFilter {
 | 
			
		||||
			@Override
 | 
			
		||||
			public boolean accept(File file) {
 | 
			
		||||
				return (file.getName().toLowerCase().endsWith(".json") && file.isFile());
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		File[] jsonFiles = folderFaction.listFiles(new jsonFileFilter());
 | 
			
		||||
		for (File jsonFile : jsonFiles) {
 | 
			
		||||
			// Extract the name from the filename. The name is filename minus ".json"
 | 
			
		||||
			String name = jsonFile.getName();
 | 
			
		||||
			name = name.substring(0, name.length() - ext.length());
 | 
			
		||||
			int id = Integer.parseInt(name);
 | 
			
		||||
 | 
			
		||||
			try {
 | 
			
		||||
				Faction faction = Factions.gson.fromJson(DiscUtil.read(jsonFile), Faction.class);
 | 
			
		||||
				faction.id = id;
 | 
			
		||||
				instances.put(faction.id, faction);
 | 
			
		||||
				Factions.log("loaded pre-1.1 faction "+id);
 | 
			
		||||
			} catch (Exception e) {
 | 
			
		||||
				e.printStackTrace();
 | 
			
		||||
				Factions.log(Level.WARNING, "Failed to load faction "+id);
 | 
			
		||||
			}
 | 
			
		||||
		}
 | 
			
		||||
		return true;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user