Fixed issue with token saving

This commit is contained in:
Linus 2016-11-08 17:28:13 +01:00
parent cb191b2a28
commit e43630fa0a
3 changed files with 18 additions and 14 deletions

View File

@ -12,24 +12,24 @@ public class LinkTelegramCmd implements CommandExecutor {
@Override
public boolean onCommand(CommandSender cs, Command arg1, String arg2, String[] args) {
if(!(cs instanceof Player)){
cs.sendMessage("<EFBFBD>cSorry, but you can't link the console currently.");
cs.sendMessage("§cSorry, but you can't link the console currently.");
}
if(!cs.hasPermission("telegram.linktelegram")){
cs.sendMessage("<EFBFBD>cYou don't have permissions to use this!");
cs.sendMessage("§cYou don't have permissions to use this!");
return true;
}
if(Main.data == null){
Main.data = new Data();
}
if(Telegram.authJson == null){
cs.sendMessage("<EFBFBD>cPlease add a bot to your server first! /telegram");
cs.sendMessage("§cPlease add a bot to your server first! /telegram");
return true;
}
String token = Main.generateLinkToken();
Main.data.linkCodes.put(token, ((Player) cs).getUniqueId());
cs.sendMessage("<EFBFBD>aAdd " + Telegram.authJson.getAsJsonObject("result").get("username").getAsString() + " to Telegram and send this message to " + Telegram.authJson.getAsJsonObject("result").get("username").getAsString() + ":");
cs.sendMessage("<EFBFBD>c" + token);
cs.sendMessage("§aAdd " + Telegram.authJson.getAsJsonObject("result").get("username").getAsString() + " to Telegram and send this message to " + Telegram.authJson.getAsJsonObject("result").get("username").getAsString() + ":");
cs.sendMessage("§c" + token);
return true;
}

View File

@ -31,7 +31,7 @@ import de.Linus122.TelegramComponents.Chat;
public class Main extends JavaPlugin implements Listener{
File datad = new File("plugins/TelegramChat/data.json");
public static File datad = new File("plugins/TelegramChat/data.json");
public static FileConfiguration cfg;
public static Data data = new Data();
@ -69,8 +69,7 @@ public class Main extends JavaPlugin implements Listener{
}
}, 20L, 20L);
}
@Override
public void onDisable(){
public static void save(){
Gson gson = new Gson();
try {
@ -85,12 +84,16 @@ public class Main extends JavaPlugin implements Listener{
e.printStackTrace();
}
}
@Override
public void onDisable(){
save();
}
public static void sendToMC(UUID uuid, String msg, int sender){
OfflinePlayer op = Bukkit.getOfflinePlayer(uuid);
List<Integer> recievers = new ArrayList<Integer>();
recievers.addAll(Main.data.ids);
recievers.remove((Object) sender);
String msgF = Main.cfg.getString("chat-format").replace('&', '<27>').replace("%player%", op.getName()).replace("%message%", msg);
String msgF = Main.cfg.getString("chat-format").replace('&', '<27>').replace("%player%", op.getName()).replace("%message%", msg);
for(int id : recievers){
Telegram.sendMsg(id, msgF);
}

View File

@ -9,25 +9,26 @@ public class TelegramCmd implements CommandExecutor {
@Override
public boolean onCommand(CommandSender cs, Command arg1, String arg2, String[] args) {
if(!cs.hasPermission("telegram.settoken")){
cs.sendMessage("<EFBFBD>cYou don't have permissions to use this!");
cs.sendMessage("§cYou don't have permissions to use this!");
return true;
}
if(args.length == 0){
cs.sendMessage("<EFBFBD>c/telegram [token]");
cs.sendMessage("§c/telegram [token]");
return true;
}
if(Main.data == null){
Main.data = new Data();
}
Main.data.token = args[0];
Main.save();
boolean success = false;
success = Telegram.auth();
if(success){
cs.sendMessage("<EFBFBD>cSuccessfully connected to Telegram!");
cs.sendMessage("<EFBFBD>aAdd " + Telegram.authJson.getAsJsonObject("result").get("username").getAsString() + " to Telegram!");
cs.sendMessage("§cSuccessfully connected to Telegram!");
cs.sendMessage("§aAdd " + Telegram.authJson.getAsJsonObject("result").get("username").getAsString() + " to Telegram!");
}else{
cs.sendMessage("<EFBFBD>cWrong token. Paste in the whole token!");
cs.sendMessage("§cWrong token. Paste in the whole token!");
}
return true;
}