Skip to content

Commit

Permalink
remove serialization header from data.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
mastercake10 committed Mar 14, 2021
1 parent b4e326b commit df457f2
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 17 deletions.
10 changes: 10 additions & 0 deletions src/main/java/de/Linus122/TelegramChat/Data.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
49 changes: 32 additions & 17 deletions src/main/java/de/Linus122/TelegramChat/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

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;
Expand All @@ -25,8 +26,8 @@
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");
Expand All @@ -49,16 +50,32 @@ public void onEnable() {
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();
}
}

Expand Down Expand Up @@ -89,12 +106,10 @@ public static void save() {
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();
Expand Down

0 comments on commit df457f2

Please sign in to comment.