Revamp superadmin list.

TODO: Auto-add.
This commit is contained in:
Steven Lawson 2011-10-30 15:27:06 -04:00
parent 342ece9934
commit 5f9626921d
4 changed files with 144 additions and 54 deletions

View File

@ -2,8 +2,9 @@ package me.StevenLawson.TotalFreedomMod.Commands;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.logging.Logger;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -28,6 +29,47 @@ public class Command_saconfig extends TFM_Command
return false; 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 user_ip = p.getAddress().getAddress().toString().replaceAll("/", "").trim();
//
// sender.sendMessage(ChatColor.GRAY + "Adding " + user_name + " as a superadmin, with current IP = " + user_ip);
//
// if (!plugin.superadmins.contains(user_name))
// {
// plugin.superadmins.add(user_name);
// }
//
// if (!plugin.superadmin_ips.contains(user_ip))
// {
// plugin.superadmin_ips.add(user_ip);
// }
//
// try
// {
// FileConfiguration config = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
// config.set("superadmins", plugin.superadmins);
// config.set("superadmin_ips", plugin.superadmin_ips);
// config.save(new File(plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
// }
// catch (IOException ex)
// {
// Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex);
// }
// }
if (args[0].equalsIgnoreCase("add")) if (args[0].equalsIgnoreCase("add"))
{ {
Player p; Player p;
@ -42,30 +84,51 @@ public class Command_saconfig extends TFM_Command
} }
String user_name = p.getName().toLowerCase().trim(); String user_name = p.getName().toLowerCase().trim();
String user_ip = p.getAddress().getAddress().toString().replaceAll("/", "").trim(); String new_ip = p.getAddress().getAddress().toString().replaceAll("/", "").trim();
sender.sendMessage(ChatColor.GRAY + "Adding " + user_name + " as a superadmin, with current IP = " + user_ip); boolean something_changed = false;
if (!plugin.superadmins.contains(user_name)) if (!plugin.superadmins.contains(user_name))
{ {
plugin.superadmins.add(user_name); plugin.superadmins.add(user_name);
sender.sendMessage("Adding new superadmin: " + user_name);
something_changed = true;
} }
if (!plugin.superadmin_ips.contains(user_ip)) if (!plugin.superadmin_ips.contains(new_ip))
{ {
plugin.superadmin_ips.add(user_ip); plugin.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!");
}
FileConfiguration config = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
List<String> user_ips = new ArrayList<String>();
if (config.contains(user_name))
{
user_ips = config.getStringList(user_name);
}
if (!user_ips.contains(new_ip))
{
user_ips.add(new_ip);
}
config.set(user_name, user_ips);
try try
{ {
FileConfiguration sa_config = YamlConfiguration.loadConfiguration(new File(plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE)); config.save(new File(plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
sa_config.set("superadmins", plugin.superadmins);
sa_config.set("superadmin_ips", plugin.superadmin_ips);
sa_config.save(new File(plugin.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
} }
catch (IOException ex) catch (IOException ex)
{ {
Logger.getLogger("Minecraft").log(Level.SEVERE, null, ex); log.log(Level.SEVERE, null, ex);
} }
} }

View File

@ -290,16 +290,18 @@ public class TFM_Util
{ {
tfm.superadmin_ips.add(user_ip); tfm.superadmin_ips.add(user_ip);
try log.severe("TODO: Implement add to superadmin list.");
{
FileConfiguration sa_config = YamlConfiguration.loadConfiguration(new File(tfm.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE)); // try
sa_config.set("superadmin_ips", tfm.superadmin_ips); // {
sa_config.save(new File(tfm.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE)); // FileConfiguration sa_config = YamlConfiguration.loadConfiguration(new File(tfm.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
} // sa_config.set("superadmin_ips", tfm.superadmin_ips);
catch (IOException ex) // sa_config.save(new File(tfm.getDataFolder(), TotalFreedomMod.SUPERADMIN_FILE));
{ // }
Logger.getLogger(TFM_Util.class.getName()).log(Level.SEVERE, null, ex); // catch (IOException ex)
} // {
// Logger.getLogger(TFM_Util.class.getName()).log(Level.SEVERE, null, ex);
// }
} }
return match_found; return match_found;

View File

@ -169,37 +169,61 @@ public class TotalFreedomMod extends JavaPlugin
public List<String> superadmins = new ArrayList<String>(); public List<String> superadmins = new ArrayList<String>();
public List<String> superadmin_ips = new ArrayList<String>(); public List<String> superadmin_ips = new ArrayList<String>();
// public void loadSuperadminConfig()
// {
// TFM_Util.createDefaultConfiguration(SUPERADMIN_FILE, this, getFile());
// FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), SUPERADMIN_FILE));
//
// superadmins = new ArrayList<String>();
// List<String> superadmins_temp = config.getStringList("superadmins");
// if (superadmins_temp == null || superadmins_temp.isEmpty())
// {
// superadmins.add("Madgeek1450");
// superadmins.add("markbyron");
// }
// else
// {
// for (String admin_name : superadmins_temp)
// {
// superadmins.add(admin_name.toLowerCase().trim());
// }
// }
//
// superadmin_ips = new ArrayList<String>();
// List<String> superadmin_ips_temp = config.getStringList("superadmin_ips");
// if (superadmin_ips_temp == null || superadmin_ips_temp.isEmpty())
// {
// superadmin_ips.add("127.0.0.1");
// }
// else
// {
// for (String admin_ip : superadmin_ips_temp)
// {
// superadmin_ips.add(admin_ip.toLowerCase().trim());
// }
// }
// }
public void loadSuperadminConfig() public void loadSuperadminConfig()
{ {
TFM_Util.createDefaultConfiguration(SUPERADMIN_FILE, this, getFile()); TFM_Util.createDefaultConfiguration(SUPERADMIN_FILE, this, getFile());
FileConfiguration sa_config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), SUPERADMIN_FILE)); FileConfiguration config = YamlConfiguration.loadConfiguration(new File(getDataFolder(), SUPERADMIN_FILE));
superadmins = new ArrayList<String>(); superadmins = new ArrayList<String>();
List<String> superadmins_temp = (List<String>) sa_config.getList("superadmins", null);
if (superadmins_temp == null || superadmins_temp.isEmpty())
{
superadmins.add("Madgeek1450");
superadmins.add("markbyron");
}
else
{
for (String admin_name : superadmins_temp)
{
superadmins.add(admin_name.toLowerCase().trim());
}
}
superadmin_ips = new ArrayList<String>(); superadmin_ips = new ArrayList<String>();
List<String> superadmin_ips_temp = (List<String>) sa_config.getList("superadmin_ips", null);
if (superadmin_ips_temp == null || superadmin_ips_temp.isEmpty()) for (String user : config.getKeys(false))
{ {
superadmin_ips.add("127.0.0.1"); superadmins.add(user.toLowerCase().trim());
List<String> user_ips = config.getStringList(user);
for (String ip : user_ips)
{
ip = ip.toLowerCase().trim();
if (!superadmin_ips.contains(ip))
{
superadmin_ips.add(ip);
} }
else
{
for (String admin_ip : superadmin_ips_temp)
{
superadmin_ips.add(admin_ip.toLowerCase().trim());
} }
} }
} }

View File

@ -1,5 +1,6 @@
superadmins: madgeek1450:
- Madgeek1450
- markbyron
superadmin_ips:
- 127.0.0.1 - 127.0.0.1
- 8.8.8.8
markbyron:
- 74.125.224.72
- 8.8.4.4