summaryrefslogtreecommitdiffstats
path: root/Essentials/src/com/earth2me/essentials/storage/AbstractDelayedYamlFileReader.java
blob: 1f2887d88caf8067cbe9b24568b6097ae911762d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package com.earth2me.essentials.storage;

import com.earth2me.essentials.IEssentials;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.logging.Level;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.yaml.snakeyaml.error.YAMLException;


public abstract class AbstractDelayedYamlFileReader<T extends StorageObject> implements Runnable
{
	private final transient File file;
	private final transient Class<T> clazz;
	private final transient Plugin plugin;

	public AbstractDelayedYamlFileReader(final IEssentials ess, final File file, final Class<T> clazz)
	{
		this.file = file;
		this.clazz = clazz;
		this.plugin = ess;
		ess.scheduleAsyncDelayedTask(this);
	}

	public abstract void onStart();

	@Override
	public void run()
	{
		FileReader reader = null;
		try
		{
			onStart();
			try
			{
				reader = new FileReader(file);
				T object = new YamlStorageReader(reader, plugin).load(clazz);
				onSuccess(object);
			}
			catch (ObjectLoadException ex)
			{
				onException();
				Bukkit.getLogger().log(Level.SEVERE, "File broken: " + file.toString(), ex.getCause());
			}
		}
		catch (FileNotFoundException ex)
		{
			Bukkit.getLogger().log(Level.SEVERE, file.toString(), ex);
		}
		finally
		{
			try
			{
				if (reader != null)
				{
					reader.close();
				}
			}
			catch (IOException ex)
			{
				Bukkit.getLogger().log(Level.SEVERE, ex.getMessage(), ex);
			}
		}
	}

	public abstract void onSuccess(T object);

	public abstract void onException();
}