Skip to content

Commit

Permalink
Added /vmodules and /vgimport command usages to plugin.yml
Browse files Browse the repository at this point in the history
Migrated /vmodules and /vgimport command handlers to args4j framework
Addresses #92
  • Loading branch information
nristock committed Jun 9, 2013
1 parent 88dd545 commit b29b8ef
Show file tree
Hide file tree
Showing 5 changed files with 249 additions and 182 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package com.thevoxelbox.voxelguest.commands;

import com.google.common.base.Preconditions;
import com.thevoxelbox.voxelguest.A4JUtils;
import com.thevoxelbox.voxelguest.commands.arguments.ImportCommandArguments;
import com.thevoxelbox.voxelguest.modules.asshat.ban.Banlist;
import com.thevoxelbox.voxelguest.modules.general.AfkMessage;
import com.thevoxelbox.voxelguest.modules.greylist.GreylistHelper;
import com.thevoxelbox.voxelguest.modules.greylist.GreylistDAO;
import com.thevoxelbox.voxelguest.persistence.Persistence;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
Expand All @@ -15,7 +17,6 @@
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
Expand All @@ -29,67 +30,53 @@
*/
public final class ImportCommandExecutor implements TabExecutor
{
private static final String[] OPTIONS = {"bans", "greylist", "afkmessages"};

@Override
public boolean onCommand(final CommandSender sender, final Command command, final String alias, final String[] args)
{
if (args.length != 1)
// we assume the player wants to see the usage if he does not enter any argument
if (args.length == 0)
{
sender.sendMessage("Wrong number of parameters.");
return false;
}

switch (args[0].toLowerCase())
final ImportCommandArguments arguments = A4JUtils.parse(ImportCommandArguments.class, args, sender);
if (arguments == null)
{
case "bans":
// Import bans from VG3 and VG4
boolean fail = false;
if (!this.importVG3Bans(sender))
{
fail = true;
}
if (!this.importVG4Bans())
{
fail = true;
}

if (fail)
{
sender.sendMessage(ChatColor.RED + "Banlist import failed!");
}
else
{
sender.sendMessage(ChatColor.GRAY + "Banlist import completed!");
}
return true;
return false;
}

case "afkmessages":
// Import Afk messages from VG3
if (this.importAfkMessages(sender))
{
sender.sendMessage(ChatColor.GRAY + "Afk message import completed!");
}
else
{
sender.sendMessage(ChatColor.RED + "Afk message import failed!");
}
return true;
if (arguments.isImportBans())
{
importVG3Bans(sender);
importVG4Bans(sender);
sender.sendMessage(ChatColor.GRAY + "Banlist import done.");
}

case "greylist":
if (this.importGreylist(sender))
{
sender.sendMessage(ChatColor.GRAY + "greylist import completed!");
}
else
{
sender.sendMessage(ChatColor.RED + "greylist import failed!");
}
if (arguments.isImportAfmMessages())
{
if (this.importAfkMessages(sender))
{
sender.sendMessage(ChatColor.GRAY + "Afk message import completed.");
}
else
{
sender.sendMessage(ChatColor.RED + "Afk message import failed.");
}
}

default:
sender.sendMessage(ChatColor.RED + "Unknown import statement.");
return false;
if (arguments.isImportGreylist())
{
if (this.importGreylist(sender))
{
sender.sendMessage(ChatColor.GRAY + "greylist import completed.");
}
else
{
sender.sendMessage(ChatColor.RED + "greylist import failed.");
}
}

return true;
}

@Override
Expand All @@ -98,54 +85,27 @@ public List<String> onTabComplete(final CommandSender sender, final Command comm
List<String> matches = new ArrayList<>();
if (args.length >= 1)
{
for (String opt : OPTIONS)
if (args[0].toLowerCase().startsWith("-b"))
{
if (opt.startsWith(args[0].toLowerCase()))
{
matches.add(opt);
}
matches.add("-bans");
}
else if (args[0].toLowerCase().startsWith("-g"))

This comment has been minimized.

Copy link
@TheCryoknight

TheCryoknight Oct 6, 2013

This doesn't work as -guehfiwhfu returns true for -greylist, witch it isn't.

This comment has been minimized.

Copy link
@nristock

nristock Oct 6, 2013

Author

True but since we don't have other imports starting with g this is a nice way to have short handles and typos do not influence the results. Anyway, this handler will change quite a bit soon since I'm using a4j argument parsing now.

{
matches.add("-greylist");
}
else if (args[0].toLowerCase().startsWith("-a"))
{
matches.add("-afkmessages");
}
}
else
{
matches.addAll(Arrays.asList(OPTIONS));
}
return matches;
}

/**
* Imports all bans from VoxelGuest version 4.
* If a player is already banned it will do nothing.
* These are the bans stored at "plugins/VoxelGuest/asshatmitigation/banned.properties".
*
* @return Returns true if the import was successful, false otherwise.
*/
private boolean importVG4Bans()
{
final Properties properties = new Properties();
final File banfileVG4 = new File("plugins" + File.separator + "VoxelGuest" + File.separator + "asshatmitigation" + File.separator + "banned.properties");
try
{
properties.load(new FileInputStream(banfileVG4));
}
catch (IOException e)
{
e.printStackTrace();
return false;
}
for (Entry<Object, Object> ban : properties.entrySet())
{
Preconditions.checkState(ban.getKey() instanceof String);
Preconditions.checkState(ban.getValue() instanceof String);
matches.add("-bans");
matches.add("-greylist");
matches.add("-afkmessages");

final String bannedName = (String) ban.getKey();
final String banReason = (String) ban.getValue();
if (!Banlist.isPlayerBanned(bannedName))
{
Banlist.ban(bannedName, banReason);
}
}
return true;
return matches;
}

/**
Expand All @@ -154,30 +114,28 @@ private boolean importVG4Bans()
* These are the bans stored at "plugins\VoxelGuest\banned.txt".
*
* @param sender User running the command
*
* @return Returns true if the import was successful, false otherwise.
*/
private boolean importVG3Bans(final CommandSender sender)
private void importVG3Bans(final CommandSender sender)
{
final File banfileVG3 = new File("plugins" + File.separator + "VoxelGuest" + File.separator + "banned.txt");
final Scanner scanner;

try
{
scanner = new Scanner(new FileInputStream(banfileVG3));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
sender.sendMessage("Could not find banned.txt file.");
return false;
sender.sendMessage(ChatColor.RED + "VG3 Ban import: Could not find banned.txt file.");
return;
}

try
{
while (scanner.hasNextLine())
{
final String line = scanner.nextLine();
final String[] data = line.split(">");
final String[] data = scanner.nextLine().split(">");
if (!Banlist.isPlayerBanned(data[0]))
{
Banlist.ban(data[0], data[1]);
Expand All @@ -188,13 +146,62 @@ private boolean importVG3Bans(final CommandSender sender)
catch (Exception e)
{
e.printStackTrace();
return false;
sender.sendMessage(ChatColor.RED + "An internal error occurred: " + e.getMessage());
}
finally
{
scanner.close();
}
return true;
}

/**
* Imports all bans from VoxelGuest version 4.
* If a player is already banned it will do nothing.
* These are the bans stored at "plugins/VoxelGuest/asshatmitigation/banned.properties".
*/
private void importVG4Bans(final CommandSender sender)
{
final Properties properties = new Properties();
final File banfileVG4 = new File("plugins" + File.separator + "VoxelGuest" + File.separator + "asshatmitigation" + File.separator + "banned.properties");

try
{
properties.load(new FileInputStream(banfileVG4));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
sender.sendMessage(ChatColor.RED + "VG4 Ban import: Could not find banned.properties file.");
return;
}
catch (IOException e)
{
e.printStackTrace();
sender.sendMessage(ChatColor.RED + "VG4 Ban import: Could not load banned.properties file.");
return;
}

for (final Entry<Object, Object> ban : properties.entrySet())
{
try
{
Preconditions.checkState(ban.getKey() instanceof String);
Preconditions.checkState(ban.getValue() instanceof String);
}
catch (IllegalStateException e)
{
e.printStackTrace();
continue;
}

final String bannedName = (String) ban.getKey();
final String banReason = (String) ban.getValue();
if (!Banlist.isPlayerBanned(bannedName))
{
Banlist.ban(bannedName, banReason);
}
sender.sendMessage(ChatColor.DARK_AQUA + "Imported ban: " + ChatColor.GOLD + bannedName);
}
}

/**
Expand All @@ -215,7 +222,7 @@ private boolean importAfkMessages(final CommandSender sender)
catch (FileNotFoundException e)
{
e.printStackTrace();
sender.sendMessage("Could not find afkmsg.txt file.");
sender.sendMessage(ChatColor.RED + "Could not find afkmsg.txt file.");
return false;
}

Expand All @@ -231,6 +238,7 @@ private boolean importAfkMessages(final CommandSender sender)
catch (Exception e)
{
e.printStackTrace();
sender.sendMessage(ChatColor.RED + "An internal error occurred: " + e.getMessage());
return false;
}
finally
Expand All @@ -249,41 +257,43 @@ private boolean importAfkMessages(final CommandSender sender)
*/
private boolean importGreylist(final CommandSender sender)
{
final GreylistHelper greylistHelper = new GreylistHelper();
final File afkMsgFile = new File("plugins" + File.separator + "VoxelGuest" + File.separator + "greylist.txt");
final Scanner scan;
final Scanner scanner;

try
{
scan = new Scanner(new FileInputStream(afkMsgFile));
scanner = new Scanner(new FileInputStream(afkMsgFile));
}
catch (FileNotFoundException e)
{
e.printStackTrace();
sender.sendMessage("Could not find greylist.txt file.");
sender.sendMessage(ChatColor.RED + "Could not find greylist.txt file.");
return false;
}

try
{
while (scan.hasNextLine())
while (scanner.hasNextLine())
{
final String name = scan.nextLine();
if (!greylistHelper.isOnPersistentGreylist(name))
final String name = scanner.nextLine();
if (!GreylistDAO.isOnPersistentGreylist(name))
{
greylistHelper.greylist(name);
GreylistDAO.greylist(name);
sender.sendMessage(ChatColor.DARK_AQUA + "Imported greylistee: " + ChatColor.GRAY + name);
}
}
}
catch (Exception e)
{
e.printStackTrace();
sender.sendMessage(ChatColor.RED + "An internal error occurred: " + e.getMessage());
return false;
}
finally
{
scan.close();
scanner.close();
}

return true;
}
}
Loading

0 comments on commit b29b8ef

Please sign in to comment.