66import java .io .InputStreamReader ;
77import java .io .PrintWriter ;
88import java .util .HashMap ;
9+ import java .util .HashSet ;
10+ import java .util .Set ;
911import java .util .TreeMap ;
1012import java .util .UUID ;
1113
1921import com .google .gson .Gson ;
2022import com .google .gson .GsonBuilder ;
2123
24+ import net .tfminecraft .advancedcrafting .AdvancedCrafting ;
2225import net .tfminecraft .advancedcrafting .loaders .HitLoader ;
2326import net .tfminecraft .advancedcrafting .loaders .RecipeLoader ;
2427import net .tfminecraft .advancedcrafting .objects .crafting .CraftingRecipe ;
2730
2831public class Database {
2932 private JSONObject json ; // org.json.simple
33+ private final Set <File > rejectedStationFiles = new HashSet <>();
3034 JSONParser parser = new JSONParser ();
3135 public HashMap <Location , CraftingStation > loadStations () {
3236 HashMap <Location , CraftingStation > map = new HashMap <>();
33- File folder = new File ("plugins/AdvancedCrafting/data/stations" );
34- for (final File file : folder .listFiles ()) {
37+ File folder = new File (AdvancedCrafting .plugin .getDataFolder (), "data/stations" );
38+ File [] files = folder .listFiles ();
39+ if (files == null ) return map ;
40+ for (final File file : files ) {
3541 if (!file .isDirectory ()) {
3642 try {
3743 json = (JSONObject ) parser .parse (new InputStreamReader (new FileInputStream (file ), "UTF-8" ));
38- Location loc = new Location (Bukkit .getServer ().getWorld ((String ) json .get ("world" )), (Double ) json .get ("xPos" ),(Double ) json .get ("yPos" ),(Double ) json .get ("zPos" ));
44+ org .bukkit .World world = Bukkit .getServer ().getWorld ((String ) json .get ("world" ));
45+ if (world == null ) throw new IllegalStateException ("Station world is not loaded: " + json .get ("world" ));
46+ Location loc = new Location (world , (Double ) json .get ("xPos" ),(Double ) json .get ("yPos" ),(Double ) json .get ("zPos" ));
3947 CraftingRecipe r = RecipeLoader .getByString ((String ) json .get ("recipe" ));
4048 HashMap <String , Integer > materials = new HashMap <>();
4149 int i = 0 ;
@@ -58,18 +66,22 @@ public HashMap<Location, CraftingStation> loadStations() {
5866 i ++;
5967 }
6068 map .put (loc , new CraftingStation (loc , r , materials , hits ));
69+ rejectedStationFiles .remove (file );
6170
6271 } catch (Exception ex ) {
72+ rejectedStationFiles .add (file );
6373 ex .printStackTrace ();
6474 }
6575 }
6676 }
6777 return map ;
6878 }
6979 public void clear () {
70- File folder = new File ("plugins/AdvancedCrafting/data/stations" );
71- for (final File file : folder .listFiles ()) {
72- if (!file .isDirectory ()) {
80+ File folder = new File (AdvancedCrafting .plugin .getDataFolder (), "data/stations" );
81+ File [] files = folder .listFiles ();
82+ if (files == null ) return ;
83+ for (final File file : files ) {
84+ if (!file .isDirectory () && !rejectedStationFiles .contains (file )) {
7385 file .delete ();
7486 }
7587 }
@@ -78,7 +90,12 @@ public void clear() {
7890 public void saveStation (CraftingStation s ) {
7991 if (!s .hasRecipe ()) return ;
8092 try {
81- File file = new File ("plugins/AdvancedCrafting/data/stations" ,UUID .randomUUID ().toString ()+".json" );
93+ File folder = new File (AdvancedCrafting .plugin .getDataFolder (), "data/stations" );
94+ folder .mkdirs ();
95+ File file = new File (folder , UUID .randomUUID ().toString ()+".json" );
96+ while (rejectedStationFiles .contains (file )) {
97+ file = new File (folder , UUID .randomUUID ().toString ()+".json" );
98+ }
8299 if (file .exists () == true ) {
83100 file .delete ();
84101 }
@@ -90,7 +107,7 @@ public void saveStation(CraftingStation s) {
90107 pw .close ();
91108 HashMap <String , Object > defaults = new HashMap <String , Object >();
92109 json = (JSONObject ) parser .parse (new InputStreamReader (new FileInputStream (file ), "UTF-8" ));
93- defaults .put ("world" , s .getLoc ().getWorld ().toString (). replace ( "CraftWorld{name=" , "" ). replace ( "}" , "" ));
110+ defaults .put ("world" , s .getLoc ().getWorld ().getName ( ));
94111 defaults .put ("xPos" , s .getLoc ().getX ());
95112 defaults .put ("yPos" , s .getLoc ().getY ());
96113 defaults .put ("zPos" , s .getLoc ().getZ ());
0 commit comments