Created
August 18, 2012 03:00
-
-
Save zack6849/3384073 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Messaging manager | |
================== | |
package me.zack6849.maintenancemode.utils; | |
import me.zack6849.maintenancemode.MaintenanceMode; | |
import org.bukkit.ChatColor; | |
import org.bukkit.command.CommandSender; | |
public class MessagingManager | |
{ | |
private final MaintenanceMode plugin; | |
public MessagingManager(final MaintenanceMode instance) | |
{ | |
plugin = instance; | |
} | |
public void broadcast(final String message, final ChatColor messageColor) | |
{ | |
final String template = "%s[MM] %s%s"; | |
final String string = String.format(template, ChatColor.GOLD, messageColor, message); | |
plugin.getServer().broadcastMessage(string); | |
} | |
public void error(final CommandSender sender, final String message) | |
{ | |
final String template = "%sError: %s"; | |
final String string = String.format(template, ChatColor.RED, message); | |
sender.sendMessage(string); | |
} | |
public void notice(final CommandSender sender, final String message) | |
{ | |
final String template = "%s[MM] %s%s"; | |
final String string = String.format(template, ChatColor.GOLD, ChatColor.YELLOW, message); | |
sender.sendMessage(string); | |
} | |
public void warning(final String message, final ChatColor messageColor){ | |
final String template = "%s[%s]"; | |
final String string = String.format(template, messageColor, message); | |
plugin.getServer().broadcastMessage(string); | |
} | |
} | |
========================= | |
mmCommands | |
======================== | |
package me.zack6849.maintenancemode.commands; | |
import me.zack6849.maintenancemode.MaintenanceMode; | |
import me.zack6849.maintenancemode.config.ConfigPath; | |
import me.zack6849.maintenancemode.utils.KickPlayersTask; | |
import org.bukkit.Bukkit; | |
import org.bukkit.ChatColor; | |
import org.bukkit.command.Command; | |
import org.bukkit.command.CommandExecutor; | |
import org.bukkit.command.CommandSender; | |
import org.bukkit.command.ConsoleCommandSender; | |
public class MMCommand implements CommandExecutor | |
{ | |
private final MaintenanceMode plugin; | |
public MMCommand(final MaintenanceMode instance) | |
{ | |
plugin = instance; | |
} | |
private CommandArg getCommandArg(String argument) | |
{ | |
argument = argument.toUpperCase(); | |
CommandArg arg; | |
try | |
{ | |
arg = CommandArg.valueOf(argument); | |
} | |
catch (final Exception e) | |
{ | |
arg = null; | |
} | |
return arg; | |
} | |
private boolean enableMaintenanceMode(final CommandSender sender) | |
{ | |
if (sender instanceof ConsoleCommandSender || sender.hasPermission("mm.toggle")) | |
{ | |
final String warningMessage = plugin.config.getString(ConfigPath.WARNING_MESSAGE); | |
final String warningMessagepre = "[" + plugin.config.getString(ConfigPath.WARNING_PREFIX) + "]"; | |
final long time = plugin.config.getLong(ConfigPath.TIME); | |
if (!plugin.maintenanceMode) | |
{ | |
plugin.maintenanceMode = true; | |
if (plugin.config.getBoolean(ConfigPath.DELAY)) | |
{ | |
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new KickPlayersTask(plugin), time * 20); | |
plugin.messaging.broadcast("MaintenanceMode enabled by " + sender.getName() + ".", ChatColor.YELLOW); | |
plugin.messaging.warning(warningMessagepre + warningMessage, ChatColor.RED); | |
return true; | |
} | |
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, new KickPlayersTask(plugin)); | |
plugin.messaging.broadcast("MaintenanceMode enabled by " + sender.getName() + ".", ChatColor.YELLOW); | |
return true; | |
} | |
plugin.messaging.error(sender, "MaintenanceMode is already enabled."); | |
return true; | |
} | |
plugin.messaging.error(sender, "You don't have permission to do that."); | |
return true; | |
} | |
private boolean disableMaintenanceMode(final CommandSender sender) | |
{ | |
if (sender instanceof ConsoleCommandSender || sender.hasPermission("mm.toggle")) | |
{ | |
if (plugin.maintenanceMode) | |
{ | |
plugin.maintenanceMode = false; | |
plugin.messaging.broadcast("MaintenanceMode disabled by " + sender.getName() + ".", ChatColor.YELLOW); | |
return true; | |
} | |
plugin.messaging.error(sender, "MaintenanceMode is already disabled."); | |
return true; | |
} | |
plugin.messaging.error(sender, "You don't have permission to do that."); | |
return true; | |
} | |
private boolean statusMaintenanceMode(final CommandSender sender) | |
{ | |
if (sender instanceof ConsoleCommandSender || sender.hasPermission("mm.status")) | |
{ | |
if (plugin.maintenanceMode) | |
{ | |
plugin.messaging.notice(sender, "MaintenanceMode is currently enabled."); | |
return true; | |
} | |
plugin.messaging.notice(sender, "MaintenanceMode is currently disabled."); | |
return true; | |
} | |
plugin.messaging.error(sender, "You don't have permission to do that."); | |
return true; | |
} | |
private boolean reloadMaintenanceMode(final CommandSender sender) | |
{ | |
if (sender instanceof ConsoleCommandSender || sender.hasPermission("mm.reload")) | |
{ | |
plugin.config.reloadConfig(); | |
plugin.messaging.notice(sender, "The config has been reloaded."); | |
return true; | |
} | |
plugin.messaging.error(sender, "You don't have permission to do that."); | |
return true; | |
} | |
@Override | |
public boolean onCommand(final CommandSender sender, final Command cmd, final String label, final String[] args) | |
{ | |
if (args.length == 1) | |
{ | |
switch (getCommandArg(args[0])) | |
{ | |
case ENABLE: | |
return enableMaintenanceMode(sender); | |
case DISABLE: | |
return disableMaintenanceMode(sender); | |
case STATUS: | |
return statusMaintenanceMode(sender); | |
case RELOAD: | |
return reloadMaintenanceMode(sender); | |
default: | |
return false; | |
} | |
} | |
return false; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment