package com.mcbans.firestar.mcbans.callBacks;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;

import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;

import com.mcbans.firestar.mcbans.MCBans;
import com.mcbans.firestar.mcbans.org.json.JSONException;
import com.mcbans.firestar.mcbans.org.json.JSONObject;
import com.mcbans.firestar.mcbans.request.JsonHandler;
import com.mcbans.firestar.mcbans.util.Util;

public class ManualResync implements Runnable {
    private final MCBans plugin;
    private final String commandSend;
    
    public ManualResync(final MCBans plugin, final String sender){
        this.plugin = plugin;
        this.commandSend = sender;
    }
    
    @Override
    public void run() {
        if(plugin.syncRunning){
            Util.message(commandSend, ChatColor.GREEN + " Sync already in progress!" );
            return;
        }
        plugin.syncRunning = true;
        
        try{
            boolean goNext = true;
            int f = 1;
            plugin.last_req=0;
            plugin.lastID = 0;
            plugin.timeRecieved = 0;
            
            while(goNext){
                long startID = plugin.lastID;
                JsonHandler webHandle = new JsonHandler( plugin );
                HashMap<String, String> url_items = new HashMap<String, String>();
                url_items.put( "latestSync", String.valueOf(plugin.lastID) );
                url_items.put( "timeRecieved", String.valueOf(plugin.timeRecieved) );
                url_items.put( "exec", "banSyncInitialNew" );
                JSONObject response = webHandle.hdl_jobj(url_items);
                try {
                    if(response.has("banned")){
                        if (response.getJSONArray("banned").length() > 0) {
                            for (int v = 0; v < response.getJSONArray("banned").length(); v++) {
                                String[] plyer = response.getJSONArray("banned").getString(v).split(";");
                                OfflinePlayer d = plugin.getServer().getOfflinePlayer(plyer[0]);
                                if(d.isBanned()){
                                    if(plyer[1].equals("u")){
                                        d.setBanned(false);
                                    }
                                }else{
                                    if(plyer[1].equals("b")){
                                        d.setBanned(true);
                                    }
                                }
                            }
                        }
                    }
                    if(plugin.lastID == 0){
                        if(response.has("timerecieved")){
                            plugin.timeRecieved = response.getLong("timerecieved");
                        }
                    }
                    if(response.has("lastid")){
                        plugin.lastID = response.getLong("lastid");
                    }
                    goNext = response.has("more");
                } catch (JSONException e) {
                    if(plugin.getConfigs().isDebug()){
                        e.printStackTrace();
                    }
                } catch (NullPointerException e) {
                    if(plugin.getConfigs().isDebug()){
                        e.printStackTrace();
                    }
                }
                if(plugin.lastID == startID){
                    f++;
                }else{
                    f = 1;
                }
                if(f > 5){
                    goNext = false;
                }
                try {
                    Thread.sleep(500);
                } catch (InterruptedException ignore) {}
            }
        } finally {
            plugin.syncRunning = false;
        }
        
        Util.message(commandSend, ChatColor.GREEN + " Sync finished");
        this.save();
    }

    public void save(){
        try {
            Writer writer = new OutputStreamWriter(new FileOutputStream(new File(plugin.getDataFolder(), "sync.last")), "UTF-8");
            BufferedWriter fout = new BufferedWriter(writer);
            fout.write(String.valueOf(plugin.lastID));
            fout.close();
            writer.close();
        } catch (Exception e) {
            if(plugin.getConfigs().isDebug()){
                e.printStackTrace();
            }
        }
    }
}

@@ -130,16 +130,45 @@
 tream;%0D%0A
+import java.io.IOException;%0D%0A
 import j
@@ -4165,32 +4165,100 @@
 ave()%7B%0D%0A        
+Writer writer = null;%0D%0A        BufferedWriter fout = null;%0D%0A        
 try %7B%0D%0A         
@@ -4255,31 +4255,24 @@
 %0A           
- Writer
  writer = ne
@@ -4380,39 +4380,24 @@
             
-BufferedWriter 
 fout = new B
@@ -4488,64 +4488,240 @@
     
-    fout.close();%0D%0A            writer.close();%0D%0A
+%7D catch (Exception e) %7B%0D%0A            if(plugin.getConfigs().isDebug())%7B%0D%0A                e.printStackTrace();%0D%0A            %7D%0D%0A        %7D finally %7B%0D%0A            if (fout != null)%7B%0D%0A                try %7B fout.close(); %7D %0D%0A     
         
 %7D ca
@@ -4712,33 +4712,34 @@
             
-%7D
+  
  catch (
 Exception e)
@@ -4718,32 +4718,34 @@
          catch (
+IO
 Exception e) %7B%0D%0A
@@ -4742,98 +4742,156 @@
 ion 
-e) %7B%0D%0A            if(plugin.getConfigs().isDebug())%7B%0D%0A                e.printStackTrace();
+ignore) %7B%7D%0D%0A            %7D%0D%0A            if (writer != null)%7B%0D%0A                try %7B writer.close(); %7D %0D%0A                catch (IOException ignore) %7B%7D
 %0D%0A  

Refactor ManualResync.java again