TFM-4.3-Reloaded/src/me/StevenLawson/TotalFreedomMod/Commands/Command_saconfig.java

171 lines
5.6 KiB
Java
Raw Normal View History

package me.StevenLawson.TotalFreedomMod.Commands;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import me.StevenLawson.TotalFreedomMod.TFM_Log;
import me.StevenLawson.TotalFreedomMod.TFM_Util;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.entity.Player;
public class Command_saconfig extends TFM_Command
{
@Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{
if (args.length == 1)
{
if (args[0].equals("list"))
{
sender.sendMessage(ChatColor.GOLD + "Superadmins: " + TFM_Util.implodeStringList(", ", TotalFreedomMod.superadmins));
return true;
}
return false;
}
if (!senderIsConsole || sender.getName().equalsIgnoreCase("remotebukkit"))
{
sender.sendMessage(ChatColor.GRAY + "This command may only be used from the Telnet or BukkitHttpd console.");
return true;
}
if (args.length < 2)
{
return false;
}
if (args[0].equalsIgnoreCase("add"))
{
Player p;
try
{
p = getPlayer(args[1]);
}
catch (CantFindPlayerException ex)
{
sender.sendMessage(ex.getMessage());
return true;
}
String user_name = p.getName().toLowerCase().trim();
String new_ip = p.getAddress().getAddress().getHostAddress();
boolean something_changed = false;
2011-11-21 06:47:19 +00:00
if (!TotalFreedomMod.superadmins.contains(user_name))
{
2011-11-21 06:47:19 +00:00
TotalFreedomMod.superadmins.add(user_name);
sender.sendMessage("Adding new superadmin: " + user_name);
something_changed = true;
}
2011-11-21 06:47:19 +00:00
if (!TotalFreedomMod.superadmin_ips.contains(new_ip))
{
2011-11-21 06:47:19 +00:00
TotalFreedomMod.superadmin_ips.add(new_ip);
sender.sendMessage("Adding new superadmin IP: " + new_ip);
something_changed = true;
}
if (!something_changed)
{
sender.sendMessage("That superadmin/superadmin ip pair already exists. Nothing to change!");
}
else
{
TFM_Util.adminAction(sender.getName(), "Adding " + user_name + " to the superadmin list.", true);
}
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
List<String> user_ips = new ArrayList<String>();
if (config.contains(user_name))
{
2011-11-28 22:44:51 +00:00
user_ips = (List<String>) config.getStringList(user_name);
}
if (!user_ips.contains(new_ip))
{
user_ips.add(new_ip);
}
config.set(user_name, user_ips);
try
{
config.save(new File(plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
}
catch (IOException ex)
{
TFM_Log.severe(ex);
}
}
else if (args[0].equalsIgnoreCase("delete") || args[0].equalsIgnoreCase("del") || args[0].equalsIgnoreCase("remove"))
2011-10-31 12:00:42 +00:00
{
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
String user_name = null;
try
{
Player p = getPlayer(args[1]);
user_name = p.getName().toLowerCase().trim();
}
catch (CantFindPlayerException ex)
{
for (String user : config.getKeys(false))
{
if (user.equalsIgnoreCase(args[1]))
{
user_name = user;
break;
}
}
}
if (user_name == null)
{
sender.sendMessage("Superadmin not found: " + user_name);
return true;
}
2012-07-22 18:06:01 +00:00
TFM_Util.adminAction(sender.getName(), "Removing " + user_name + " from the superadmin list.", true);
2012-07-22 18:06:01 +00:00
sender.sendMessage("Removing superadmin: " + user_name);
2011-11-21 06:47:19 +00:00
TotalFreedomMod.superadmins.remove(user_name);
2011-10-31 12:00:42 +00:00
if (config.contains(user_name))
{
2011-11-28 22:44:51 +00:00
List<String> user_ips = (List<String>) config.getStringList(user_name);
2011-10-31 12:00:42 +00:00
for (String ip : user_ips)
{
2012-07-22 18:06:01 +00:00
sender.sendMessage("Removing superadmin IP: " + ip);
2011-11-21 06:47:19 +00:00
TotalFreedomMod.superadmin_ips.remove(ip);
2011-10-31 12:00:42 +00:00
}
}
config.set(user_name, null);
try
{
config.save(new File(plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
}
catch (IOException ex)
{
TFM_Log.severe(ex);
2011-10-31 12:00:42 +00:00
}
}
else
{
return false;
}
return true;
}
}