summaryrefslogtreecommitdiffstats
path: root/EssentialsUpdate/src/com/earth2me/essentials/update/chat/AbstractFileCommand.java
blob: 824383285a506e5315f9817c56fa6413dbe72cf2 (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
73
74
75
76
77
package com.earth2me.essentials.update.chat;

import com.earth2me.essentials.update.PastieUpload;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import org.bukkit.plugin.Plugin;


public abstract class AbstractFileCommand implements Command
{
	private final transient Plugin plugin;
	private final static Charset UTF8 = Charset.forName("utf-8");

	public AbstractFileCommand(final Plugin plugin)
	{
		this.plugin = plugin;
	}

	protected BufferedReader getServerLogReader() throws IOException
	{
		final File bukkitFolder = plugin.getDataFolder().getAbsoluteFile().getParentFile().getParentFile();
		if (bukkitFolder == null || !bukkitFolder.exists())
		{
			throw new IOException("Bukkit folder not found.");
		}
		final File logFile = new File(bukkitFolder, "server.log");
		if (!logFile.exists())
		{
			throw new IOException("Server log not found.");
		}
		final FileInputStream fis = new FileInputStream(logFile);
		try
		{
			if (logFile.length() > 1000000)
			{
				fis.skip(logFile.length() - 1000000);
			}
			return new BufferedReader(new InputStreamReader(fis));
		}
		catch (IOException ex)
		{
			fis.close();
			throw ex;
		}
	}

	protected BufferedReader getPluginConfig(final String pluginName, final String fileName) throws IOException
	{
		final File configFolder = new File(plugin.getDataFolder().getAbsoluteFile().getParentFile(), pluginName);
		if (!configFolder.exists())
		{
			throw new IOException(pluginName + " plugin folder not found.");
		}
		final File configFile = new File(configFolder, fileName);
		if (!configFile.exists())
		{
			throw new IOException(pluginName + " plugin file " + fileName + " not found.");
		}
		return new BufferedReader(new InputStreamReader(new FileInputStream(configFile), UTF8));

	}

	protected String uploadToPastie(final StringBuilder input) throws IOException
	{
		if (input.length() > 15000)
		{
			input.delete(0, input.length() - 15000);
			input.append("## Cropped after 15000 bytes");
		}
		final PastieUpload pastie = new PastieUpload();
		return pastie.send(input.toString());
	}
}