فهرست منبع

remove serialization header from data.yml

MasterCake 3 سال پیش
والد
کامیت
df457f2be2
2فایلهای تغییر یافته به همراه42 افزوده شده و 17 حذف شده
  1. 10 0
      src/main/java/de/Linus122/TelegramChat/Data.java
  2. 32 17
      src/main/java/de/Linus122/TelegramChat/Main.java

+ 10 - 0
src/main/java/de/Linus122/TelegramChat/Data.java

@@ -1,18 +1,28 @@
 package de.Linus122.TelegramChat;
 
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.ObjectInputStream;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.UUID;
 
+import com.google.gson.Gson;
+
 public class Data {
 	private String token = "";
+	
 	// Player name // ChatID
 	private HashMap<Integer, UUID> linkedChats = new HashMap<Integer, UUID>();
+	
 	// Player name // RandomInt
 	private HashMap<String, UUID> linkCodes = new HashMap<String, UUID>();
+	
 	public List<Integer> ids = new ArrayList<Integer>();
+	
 	private boolean firstUse = true;
+	
 
 	public String getToken() {
 		return token;

+ 32 - 17
src/main/java/de/Linus122/TelegramChat/Main.java

@@ -2,14 +2,15 @@ package de.Linus122.TelegramChat;
 
 import java.io.File;
 import java.io.FileInputStream;
-import java.io.FileOutputStream;
+import java.io.FileReader;
+import java.io.FileWriter;
 import java.io.IOException;
 import java.io.ObjectInputStream;
-import java.io.ObjectOutputStream;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Random;
 import java.util.UUID;
+import java.util.logging.Level;
 
 import org.bukkit.Bukkit;
 import org.bukkit.OfflinePlayer;
@@ -25,8 +26,8 @@ import org.bukkit.plugin.java.JavaPlugin;
 import com.google.gson.Gson;
 
 import de.Linus122.Metrics.Metrics;
-import de.Linus122.TelegramComponents.ChatMessageToTelegram;
 import de.Linus122.TelegramComponents.ChatMessageToMc;
+import de.Linus122.TelegramComponents.ChatMessageToTelegram;
 
 public class Main extends JavaPlugin implements Listener {
 	private static File datad = new File("plugins/TelegramChat/data.json");
@@ -49,16 +50,32 @@ public class Main extends JavaPlugin implements Listener {
 		dir.mkdir();
 		data = new Data();
 		if (datad.exists()) {
+			Gson gson = new Gson();
 			try {
-				FileInputStream fin = new FileInputStream(datad);
-				ObjectInputStream ois = new ObjectInputStream(fin);
-				Gson gson = new Gson();
-				data = (Data) gson.fromJson((String) ois.readObject(), Data.class);
-				ois.close();
-				fin.close();
+				FileReader fileReader = new FileReader(datad);
+				StringBuilder sb = new StringBuilder();
+				int c;
+			    while((c = fileReader.read()) !=-1) {
+			    	sb.append((char) c);
+			    }
+
+				data = (Data) gson.fromJson(sb.toString(), Data.class);
+				
+				fileReader.close();
 			} catch (Exception e) {
-				// TODO Auto-generated catch block
-				e.printStackTrace();
+				// old method for loading the data.yml file
+				try {
+					FileInputStream fin = new FileInputStream(datad);
+					ObjectInputStream ois = new ObjectInputStream(fin);
+					
+					data = (Data) gson.fromJson((String) ois.readObject(), Data.class);
+					ois.close();
+					fin.close();
+				} catch (Exception e2) {
+					e2.printStackTrace();
+				}
+				this.getLogger().log(Level.INFO, "Converted old data.yml");
+				save();
 			}
 		}
 
@@ -89,12 +106,10 @@ public class Main extends JavaPlugin implements Listener {
 		Gson gson = new Gson();
 
 		try {
-			FileOutputStream fout = new FileOutputStream(datad);
-			ObjectOutputStream oos = new ObjectOutputStream(fout);
-
-			oos.writeObject(gson.toJson(data));
-			fout.close();
-			oos.close();
+			FileWriter fileWriter = new FileWriter(datad);
+			fileWriter.write(gson.toJson(data));
+			
+			fileWriter.close();
 		} catch (IOException e) {
 			// TODO Auto-generated catch block
 			e.printStackTrace();