Skip to content
This repository has been archived by the owner on Jul 27, 2019. It is now read-only.

Commit

Permalink
Add load and unload commands.
Browse files Browse the repository at this point in the history
  • Loading branch information
cnaude committed Oct 18, 2014
1 parent 849c7a6 commit 06972b9
Show file tree
Hide file tree
Showing 7 changed files with 172 additions and 14 deletions.
6 changes: 4 additions & 2 deletions src/main/java/com/cnaude/purpleirc/CommandHandlers.java
Expand Up @@ -16,8 +16,8 @@
*/
public class CommandHandlers implements CommandExecutor {

public HashMap<String, IRCCommandInterface> commands = new HashMap<String, IRCCommandInterface>();
public ArrayList<String> sortedCommands = new ArrayList<String>();
public HashMap<String, IRCCommandInterface> commands = new HashMap<>();
public ArrayList<String> sortedCommands = new ArrayList<>();
private final PurpleIRC plugin;

/**
Expand All @@ -44,6 +44,7 @@ public CommandHandlers(PurpleIRC plugin) {
commands.put("listops", new ListOps(plugin));
commands.put("listvoices", new ListVoices(plugin));
commands.put("login", new Login(plugin));
commands.put("load", new Load(plugin));
commands.put("messagedelay", new MessageDelay(plugin));
commands.put("msg", new Msg(plugin));
commands.put("motd", new Motd(plugin));
Expand All @@ -67,6 +68,7 @@ public CommandHandlers(PurpleIRC plugin) {
commands.put("server", new Server(plugin));
commands.put("topic", new Topic(plugin));
commands.put("unmute", new UnMute(plugin));
commands.put("unload", new Unload(plugin));
commands.put("voice", new Voice(plugin));
commands.put("whois", new Whois(plugin));
commands.put("help", new Help(plugin));
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/cnaude/purpleirc/Commands/ListBots.java
Expand Up @@ -39,8 +39,8 @@ public void dispatch(CommandSender sender, String[] args) {
sender.sendMessage(ChatColor.DARK_PURPLE + "-----[ " + ChatColor.WHITE + "IRC Bots"
+ ChatColor.DARK_PURPLE + " ]-----");
for (PurpleBot ircBot : plugin.ircBots.values()) {
sender.sendMessage(ChatColor.DARK_PURPLE + "* " + ChatColor.WHITE + ircBot.botNick
+ ChatColor.DARK_PURPLE + " [" + ChatColor.GRAY + ircBot.getFileName() + ChatColor.DARK_PURPLE + "]");
sender.sendMessage(ChatColor.DARK_PURPLE + "* " + ChatColor.WHITE + ircBot.getFileName()
+ ChatColor.DARK_PURPLE + " [" + ChatColor.WHITE + ircBot.botNick + ChatColor.DARK_PURPLE + "]");
if (ircBot.isConnected()) {
for (Channel channel : ircBot.getChannels()) {
sender.sendMessage(ChatColor.DARK_PURPLE + " - " + ChatColor.WHITE + channel.getName());
Expand Down
69 changes: 69 additions & 0 deletions src/main/java/com/cnaude/purpleirc/Commands/Load.java
@@ -0,0 +1,69 @@
package com.cnaude.purpleirc.Commands;

import com.cnaude.purpleirc.PurpleBot;
import com.cnaude.purpleirc.PurpleIRC;
import java.io.File;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

/**
*
* @author cnaude
*/
public class Load implements IRCCommandInterface {

private final PurpleIRC plugin;
private final String usage = "[bot]";
private final String desc = "Load a bot file.";
private final String name = "load";
private final String fullUsage = ChatColor.WHITE + "Usage: " + ChatColor.GOLD + "/irc " + name + " " + usage;

/**
*
* @param plugin
*/
public Load(PurpleIRC plugin) {
this.plugin = plugin;
}

/**
*
* @param sender
* @param args
*/
@Override
public void dispatch(CommandSender sender, String[] args) {
if (args.length >= 2) {
String bot = plugin.botify(args[1]);
if (plugin.ircBots.containsKey(bot)) {
sender.sendMessage(ChatColor.RED + "Sorry that bot is already loaded. Try to unload it first.");
return;
}
File file = new File(plugin.botsFolder, bot);
if (file.exists()) {
sender.sendMessage(ChatColor.WHITE + "Loading " + bot + "...");
plugin.ircBots.put(file.getName(), new PurpleBot(file, plugin));
sender.sendMessage("Loaded bot: " + file.getName() + "[" + plugin.ircBots.get(file.getName()).botNick + "]");
} else {
sender.sendMessage(ChatColor.RED + "No such bot file: " + ChatColor.WHITE + bot);
}
} else {
sender.sendMessage(fullUsage);
}
}

@Override
public String name() {
return name;
}

@Override
public String desc() {
return desc;
}

@Override
public String usage() {
return usage;
}
}
71 changes: 71 additions & 0 deletions src/main/java/com/cnaude/purpleirc/Commands/Unload.java
@@ -0,0 +1,71 @@
package com.cnaude.purpleirc.Commands;

import com.cnaude.purpleirc.PurpleIRC;
import java.io.File;
import org.bukkit.ChatColor;
import org.bukkit.command.CommandSender;

/**
*
* @author cnaude
*/
public class Unload implements IRCCommandInterface {

private final PurpleIRC plugin;
private final String usage = "[bot] (disable)";
private final String desc = "Unload the bot and optionally disable it.";
private final String name = "unload";
private final String fullUsage = ChatColor.WHITE + "Usage: " + ChatColor.GOLD + "/irc " + name + " " + usage;

/**
*
* @param plugin
*/
public Unload(PurpleIRC plugin) {
this.plugin = plugin;
}

/**
*
* @param sender
* @param args
*/
@Override
public void dispatch(CommandSender sender, String[] args) {
if (args.length >= 2) {
String bot = plugin.botify(args[1]);
if (plugin.ircBots.containsKey(bot)) {
sender.sendMessage(ChatColor.WHITE + "Unloading " + bot + "...");
plugin.ircBots.get(bot).quit();
plugin.ircBots.get(bot).saveConfig(plugin.getServer().getConsoleSender());
plugin.ircBots.remove(bot);
if (args.length >= 3) {
if (args[2].equalsIgnoreCase("disable")) {
sender.sendMessage(ChatColor.WHITE + "Renaming " + bot + " to " + bot + ".disabled");
File file = new File(plugin.botsFolder, bot);
file.renameTo(new File(plugin.botsFolder, bot + ".disabled"));
}
}
} else {
sender.sendMessage(plugin.invalidBotName.replace("%BOT%", bot));
}
} else {
sender.sendMessage(fullUsage);
}
}

@Override
public String name() {
return name;
}

@Override
public String desc() {
return desc;
}

@Override
public String usage() {
return usage;
}
}
10 changes: 6 additions & 4 deletions src/main/java/com/cnaude/purpleirc/PurpleBot.java
Expand Up @@ -281,10 +281,12 @@ public void run() {
for (String channelName : botChannels) {
if (channelAutoJoin.containsKey(channelName)) {
if (channelAutoJoin.get(channelName)) {
if (channelPassword.get(channelName).isEmpty()) {
bot.sendIRC().joinChannel(channelName);
} else {
bot.sendIRC().joinChannel(channelName, channelPassword.get(channelName));
if (bot.isConnected()) {
if (channelPassword.get(channelName).isEmpty()) {
bot.sendIRC().joinChannel(channelName);
} else {
bot.sendIRC().joinChannel(channelName, channelPassword.get(channelName));
}
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/main/java/com/cnaude/purpleirc/PurpleIRC.java
Expand Up @@ -80,7 +80,7 @@ public class PurpleIRC extends JavaPlugin {
private final String sampleFileName;
private final String MAINCONFIG;
private File pluginFolder;
private File botsFolder;
public File botsFolder;
private File configFile;
public static long startTime;
public boolean identServerEnabled;
Expand Down Expand Up @@ -599,11 +599,10 @@ private void loadBots() {
if (botsFolder.exists()) {
logInfo("Checking for bot files in " + botsFolder);
for (final File file : botsFolder.listFiles()) {
if (file.getName().endsWith(".yml")) {
if (file.getName().toLowerCase().endsWith(".yml")) {
logInfo("Loading bot file: " + file.getName());
PurpleBot pircBot = new PurpleBot(file, this);
ircBots.put(file.getName(), pircBot);
logInfo("Loaded bot: " + pircBot.botNick);
ircBots.put(file.getName(), new PurpleBot(file, this));
logInfo("Loaded bot: " + file.getName() + "[" + ircBots.get(file.getName()).botNick + "]");
}
}
}
Expand Down Expand Up @@ -1209,4 +1208,13 @@ public void clearHostCache(Player player) {
hostCache.remove(playerIP);
}
}

public String botify(String bot) {
if (bot.toLowerCase().endsWith("yml")) {
return bot;
} else {
return bot + ".yml";
}
}

}
8 changes: 7 additions & 1 deletion src/main/resources/plugin.yml
Expand Up @@ -98,4 +98,10 @@ permissions:
default: op
'irc.motd':
description: Gives player access to the /irc motd command.
default: op
default: op
'irc.load':
description: Gives player access to the /irc load command.
default: op
'irc.unload':
description: Gives player access to the /irc unload command.
default: op

0 comments on commit 06972b9

Please sign in to comment.