From df457f2be295803cd129ff960b2d28efa8d5c821 Mon Sep 17 00:00:00 2001 From: MasterCake Date: Sun, 14 Mar 2021 18:30:44 +0100 Subject: [PATCH] remove serialization header from data.yml --- .../java/de/Linus122/TelegramChat/Data.java | 10 ++++ .../java/de/Linus122/TelegramChat/Main.java | 49 ++++++++++++------- 2 files changed, 42 insertions(+), 17 deletions(-) diff --git a/src/main/java/de/Linus122/TelegramChat/Data.java b/src/main/java/de/Linus122/TelegramChat/Data.java index 738b90c..388e38e 100755 --- a/src/main/java/de/Linus122/TelegramChat/Data.java +++ b/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 linkedChats = new HashMap(); + // Player name // RandomInt private HashMap linkCodes = new HashMap(); + public List ids = new ArrayList(); + private boolean firstUse = true; + public String getToken() { return token; diff --git a/src/main/java/de/Linus122/TelegramChat/Main.java b/src/main/java/de/Linus122/TelegramChat/Main.java index ab336e1..a94fda0 100755 --- a/src/main/java/de/Linus122/TelegramChat/Main.java +++ b/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();