summaryrefslogtreecommitdiffstats
path: root/EssentialsServerlist/src/net/mcserverlist/bukkit/Mcsl.java
blob: c9b234fd76ab9d5316b055ca33ccbead4782bdc6 (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package net.mcserverlist.bukkit;

import com.earth2me.essentials.Essentials;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.event.Event;
import org.bukkit.event.Event.Priority;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.java.JavaPlugin;


public class Mcsl extends JavaPlugin
{
	private static final Logger logger = Logger.getLogger("Minecraft");
	private McslPlayerListener playerListener;
	public final String author;

	public Mcsl() throws IOException
	{
		
		PluginDescriptionFile desc = this.getDescription();

		// Compile author list
		List<String> authors = new ArrayList<String>();
		authors.add("Vimae Development");
		int alen = authors.size();
		if (alen == 1)
		{
			author = " by " + authors.get(0);
		}
		else if (alen > 1)
		{
			int i = 0;
			StringBuilder bldr = new StringBuilder();
			for (String a : desc.getAuthors())
			{
				if (i + 1 == alen)
				{
					if (alen > 2) bldr.append(",");
					bldr.append(" and ");
				}
				else if (i++ > 0)
				{
					bldr.append(", ");
				}
				bldr.append(a);
			}
			bldr.insert(0, " by ");
			author = bldr.toString();
		}
		else
		{
			author = "";
		}
	}

	@Override
	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args)
	{
		McslCommands mcslCmd;
		try
		{
			switch (McslCommands.valueOf(cmd.getName().toUpperCase()))
			{
			case WHITELIST:
				whitelist(sender, WhitelistCommands.valueOf(args[0].toUpperCase()), args);
				return true;
				
			default:
				return false;
			}
		}
		catch (IllegalArgumentException ex)
		{
			return false;
		}
		catch (Exception ex)
		{
			logger.log(Level.WARNING, "MCSL encountered an unknown error.", ex);
			sender.sendMessage("MCSL encountered an unknown error.");
			return true;
		}
	}

	@SuppressWarnings("LoggerStringConcat")
	public void onEnable()
	{
		Plugin p = this.getServer().getPluginManager().getPlugin("Essentials");
            if (p != null) {
                if (!this.getServer().getPluginManager().isPluginEnabled(p)) {
                    this.getServer().getPluginManager().enablePlugin(p);
                }
		}
		playerListener = new McslPlayerListener(this);
		getServer().getPluginManager().registerEvent(Event.Type.PLAYER_JOIN, playerListener, Priority.Monitor, this);
		getServer().getPluginManager().registerEvent(Event.Type.PLAYER_QUIT, playerListener, Priority.Monitor, this);
		getServer().getPluginManager().registerEvent(Event.Type.PLAYER_LOGIN, playerListener, Priority.Lowest, this);

		if (!this.getDescription().getVersion().equals(Essentials.getStatic().getDescription().getVersion())) {
			logger.log(Level.WARNING, "Version mismatch! Please update all Essentials jars to the same version.");
		}
		logger.info(getDescription().getName() + " version " + getDescription().getVersion() + author + " enabled.");
	}

	@SuppressWarnings("LoggerStringConcat")
	public void onDisable()
	{
		logger.info(getDescription().getName() + " version " + getDescription().getVersion() + " disabled.");
	}

	private void whitelist(CommandSender sender, WhitelistCommands cmd, String[] args)
	{
		if (!playerListener.isWhitelistEnabled())
		{
			sender.sendMessage("§cThe whitelist is disabled.");
			return;
		}

		if (!sender.isOp())
		{
			sender.sendMessage("§cYou must be an operator to manage the whitelist.");
			return;
		}

		switch (cmd)
		{
		case RELOAD:
			playerListener.whitelistReload();
			sender.sendMessage("A whitelist updated has been queued.");
			break;
		}
	}

	private enum McslCommands
	{
		WHITELIST
	}

	private enum WhitelistCommands
	{
		RELOAD
	}
}