Use Allman style, please!

Code cleanup.
Switched to StringUtils.join, we are re-inventing the wheel :D
Added whitelist support to TFM_PlayerListener auth code.
@Deprecated some utility methods, will remove shortly.
This commit is contained in:
Steven Lawson 2012-09-17 19:46:59 -04:00
parent c9c1b7519d
commit 3feec18131
8 changed files with 311 additions and 329 deletions

View File

@ -2,11 +2,8 @@ package me.StevenLawson.TotalFreedomMod.Commands;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TFM_Util;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.GameMode; import org.bukkit.GameMode;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -15,109 +12,104 @@ import org.bukkit.entity.Player;
public class Command_doom extends TFM_Command public class Command_doom extends TFM_Command
{ {
public String[] p = public final static List<String> ALLOWED_USERS = Arrays.asList(
{ "madgeek",
"madgeek", "darth",
"darth", "madgeek1450",
"madgeek1450", "mark",
"mark", "markbyron",
"markbyron", "darthsalamon");
"darthsalamon"
}; @Override
public boolean run(final CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
public List<String> players = Arrays.asList(p); {
if (!(senderIsConsole && ALLOWED_USERS.contains(sender.getName().toLowerCase())))
@Override {
public boolean run(final CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
{ return true;
if(!(senderIsConsole && players.contains(sender.getName().toLowerCase()))) }
{
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); if (args.length != 1)
return true; {
} return false;
}
if(args.length != 1)
{ final Player p;
return false; try
} {
p = getPlayer(args[0]);
final Player p; }
try catch (CantFindPlayerException ex)
{ {
p = getPlayer(args[0]); sender.sendMessage(ex.getMessage());
} return true;
catch(CantFindPlayerException ex) }
{
sender.sendMessage(ex.getMessage()); TFM_Util.adminAction(sender.getName(), "Casting oblivion over " + p.getName(), true);
return true; TFM_Util.bcastMsg(p.getName() + " will be completely obliviated!", ChatColor.RED);
}
final String IP = p.getAddress().getAddress().getHostAddress().trim();
TFM_Util.adminAction(sender.getName(), "Casting oblivion over " + p.getName(), true); // remove from superadmin
TFM_Util.bcastMsg(p.getName() + " will be completely obliviated!", ChatColor.RED); if (TFM_Util.isUserSuperadmin(p))
{
final String IP = p.getAddress().getAddress().getHostAddress().trim(); server.dispatchCommand(sender, "saconfig delete " + p.getName());
}
// remove from superadmin
if(TFM_Util.isUserSuperadmin(p)) // remove from whitelist
{ p.setWhitelisted(false);
server.dispatchCommand(sender, "saconfig delete " + p.getName());
} // deop
p.setOp(false);
// remove from whitelist
p.setWhitelisted(false); // ban IP
TFM_Util.banIP(IP, null, null, null);
// deop
p.setOp(false); // ban name
TFM_Util.banUsername(p.getName(), null, null, null);
// ban IP
Bukkit.banIP(IP); // set gamemode to survival
p.setGameMode(GameMode.SURVIVAL);
// ban name
p.setBanned(true); // clear inventory
p.closeInventory();
// set gamemode to survival p.getInventory().clear();
p.setGameMode(GameMode.SURVIVAL);
// ignite player
// clear inventory p.setFireTicks(10000);
p.closeInventory();
p.getInventory().clear(); // generate explosion
p.getWorld().createExplosion(p.getLocation(), 4F);
// ignite player
p.setFireTicks(10000); server.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable()
{
// generate explosion @Override
p.getWorld().createExplosion(p.getLocation(), 4F); public void run()
{
server.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() // strike lightning
{ p.getWorld().strikeLightning(p.getLocation());
@Override
public void run() { // kill (if not done already)
// strike lightning p.setHealth(0);
p.getWorld().strikeLightning(p.getLocation()); }
}, 40L); // 2 seconds
// kill (if not done already)
p.setHealth(0); server.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable()
} {
@Override
}, 40L); // 2 seconds public void run()
{
server.getScheduler().scheduleAsyncDelayedTask(plugin, new Runnable() // message
{ TFM_Util.adminAction(sender.getName(), "Banning " + p.getName() + ", IP: " + IP, true);
@Override
public void run() { // generate explosion
// message p.getWorld().createExplosion(p.getLocation(), 4F);
TFM_Util.adminAction(sender.getName(), "Banning " + p.getName() + ", IP: " + IP, true);
// kick player
// generate explosion p.kickPlayer(ChatColor.RED + "FUCKOFF, and get your shit together!");
p.getWorld().createExplosion(p.getLocation(), 4F); }
}, 60L); // 3 seconds
// kick player
p.kickPlayer(ChatColor.RED + "FUCKOFF, and get your shit together!"); return true;
} }
}, 60L); // 3 seconds
return true;
}
} }

View File

@ -2,6 +2,7 @@ package me.StevenLawson.TotalFreedomMod.Commands;
import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TFM_Util;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -26,10 +27,7 @@ public class Command_ops extends TFM_Command
if (args[0].equalsIgnoreCase("list")) if (args[0].equalsIgnoreCase("list"))
{ {
String ops = TFM_Util.arrayToString(server.getOperators()); TFM_Util.playerMsg(sender, "Operators: " + StringUtils.join(server.getOperators(), ", "));
TFM_Util.playerMsg(sender, "Operators:");
TFM_Util.playerMsg(sender, ops);
return true; return true;
} }

View File

@ -14,12 +14,14 @@ public class Command_rd extends TFM_Command
{ {
if (!(senderIsConsole || TFM_Util.isUserSuperadmin(sender))) if (!(senderIsConsole || TFM_Util.isUserSuperadmin(sender)))
{ {
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
return true; return true;
} }
TFM_Util.adminAction(sender.getName(), "Removing all server entities", false);
//This terminology is wrong, this doesn't remove *all* entities, by far. - Madgeek
TFM_Util.adminAction(sender.getName(), "Removing all server entities.", false);
sender.sendMessage(ChatColor.GRAY + String.valueOf(TFM_Util.wipeEntities(true, true)) + " enties removed."); sender.sendMessage(ChatColor.GRAY + String.valueOf(TFM_Util.wipeEntities(true, true)) + " enties removed.");
return true; return true;
} }
} }

View File

@ -63,27 +63,27 @@ public class Command_stfu extends TFM_Command
TFM_Util.playerMsg(sender, "Unmuted " + count + " players."); TFM_Util.playerMsg(sender, "Unmuted " + count + " players.");
return true; return true;
} }
if(args[0].equalsIgnoreCase("all")) if (args[0].equalsIgnoreCase("all"))
{ {
TFM_Util.adminAction(sender.getName(), "Muting all non-Superadmins", true); TFM_Util.adminAction(sender.getName(), "Muting all non-Superadmins", true);
TFM_UserInfo playerdata; TFM_UserInfo playerdata;
int counter = 0; int counter = 0;
for(Player p : server.getOnlinePlayers()) for (Player p : server.getOnlinePlayers())
{ {
if(!TFM_Util.isUserSuperadmin(p)) if (!TFM_Util.isUserSuperadmin(p))
{ {
playerdata = TFM_UserInfo.getPlayerData(p); playerdata = TFM_UserInfo.getPlayerData(p);
playerdata.setMuted(true); playerdata.setMuted(true);
counter++; counter++;
} }
} }
TFM_Util.playerMsg(sender, "Muted " + counter + " players."); TFM_Util.playerMsg(sender, "Muted " + counter + " players.");
return true;
} }
Player p; Player p;
try try
{ {
@ -101,7 +101,6 @@ public class Command_stfu extends TFM_Command
TFM_Util.adminAction(sender.getName(), "Unmuting " + p.getName(), true); TFM_Util.adminAction(sender.getName(), "Unmuting " + p.getName(), true);
playerdata.setMuted(false); playerdata.setMuted(false);
TFM_Util.playerMsg(sender, "Unmuted " + p.getName()); TFM_Util.playerMsg(sender, "Unmuted " + p.getName());
return true;
} }
else else
{ {

View File

@ -1,8 +1,10 @@
package me.StevenLawson.TotalFreedomMod.Commands; package me.StevenLawson.TotalFreedomMod.Commands;
import java.util.Set;
import me.StevenLawson.TotalFreedomMod.TFM_Util; import me.StevenLawson.TotalFreedomMod.TFM_Util;
import me.StevenLawson.TotalFreedomMod.TotalFreedomMod; import me.StevenLawson.TotalFreedomMod.TotalFreedomMod;
import net.minecraft.server.MinecraftServer;
import org.apache.commons.lang.StringUtils;
import org.bukkit.ChatColor; import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer; import org.bukkit.OfflinePlayer;
import org.bukkit.command.Command; import org.bukkit.command.Command;
@ -11,33 +13,30 @@ import org.bukkit.entity.Player;
public class Command_whitelist extends TFM_Command public class Command_whitelist extends TFM_Command
{ {
@Override @Override
public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole) public boolean run(CommandSender sender, Player sender_p, Command cmd, String commandLabel, String[] args, boolean senderIsConsole)
{ {
if(args.length < 1) if (args.length < 1)
{ {
return false; return false;
} }
if(!sender.isOp()) if (!sender.isOp())
{ {
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
return true; return true;
} }
// list // list
if(args[0].equalsIgnoreCase("list")) if (args[0].equalsIgnoreCase("list"))
{ {
String players = TFM_Util.arrayToString(server.getWhitelistedPlayers()); TFM_Util.playerMsg(sender, "Whitelisted players: " + StringUtils.join(server.getWhitelistedPlayers(), ", "));
return true;
TFM_Util.playerMsg(sender, "Whitelisted players:"); }
TFM_Util.playerMsg(sender, players);
return true; // count
} if (args[0].equalsIgnoreCase("count"))
{
// count
if(args[0].equalsIgnoreCase("count"))
{
int onlineWPs = 0; int onlineWPs = 0;
int offlineWPs = 0; int offlineWPs = 0;
int totalWPs = 0; int totalWPs = 0;
@ -60,135 +59,132 @@ public class Command_whitelist extends TFM_Command
sender.sendMessage(ChatColor.GRAY + "Total whitelisted players: " + totalWPs); sender.sendMessage(ChatColor.GRAY + "Total whitelisted players: " + totalWPs);
return true; return true;
} }
// all commands past this line are superadmin-only // all commands past this line are superadmin-only
if(!(senderIsConsole || TFM_Util.isUserSuperadmin(sender))) if (!(senderIsConsole || TFM_Util.isUserSuperadmin(sender)))
{ {
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
return true; return true;
} }
// on // on
if(args[0].equalsIgnoreCase("on")) if (args[0].equalsIgnoreCase("on"))
{ {
TFM_Util.adminAction(sender.getName(), "Turning the whitelist on", false); TFM_Util.adminAction(sender.getName(), "Turning the whitelist on", false);
server.setWhitelist(true); server.setWhitelist(true);
return true; return true;
} }
// off // off
if(args[0].equalsIgnoreCase("off")) if (args[0].equalsIgnoreCase("off"))
{ {
TFM_Util.adminAction(sender.getName(), "Turning the whitelist off", false); TFM_Util.adminAction(sender.getName(), "Turning the whitelist off", false);
server.setWhitelist(false); server.setWhitelist(false);
return true; return true;
} }
// add // add
if(args[0].equalsIgnoreCase("add")) if (args[0].equalsIgnoreCase("add"))
{ {
if(args.length < 2) if (args.length < 2)
{ {
return false; return false;
} }
OfflinePlayer p; OfflinePlayer p;
try try
{ {
p = getPlayer(args[1]); p = getPlayer(args[1]);
} }
catch(CantFindPlayerException ex) catch (CantFindPlayerException ex)
{ {
if(!senderIsConsole) if (!senderIsConsole)
{ {
sender.sendMessage(ex.getMessage()); sender.sendMessage(ex.getMessage());
sender.sendMessage(ChatColor.YELLOW + "You don't have permissions to whitelist offline players"); sender.sendMessage(ChatColor.YELLOW + "You don't have permissions to whitelist offline players");
return true; return true;
} }
else else
{ {
p = server.getOfflinePlayer(args[0]); p = server.getOfflinePlayer(args[0]);
} }
} }
TFM_Util.adminAction(sender.getName(), "Adding " + p.getName() + " to the whitelist", false);
p.setWhitelisted(true); TFM_Util.adminAction(sender.getName(), "Adding " + p.getName() + " to the whitelist", false);
return true; p.setWhitelisted(true);
} return true;
}
// remove
if(args[0].equalsIgnoreCase("remove")) // remove
{ if (args[0].equalsIgnoreCase("remove"))
if(args.length < 2) {
{ if (args.length < 2)
return false; {
} return false;
}
OfflinePlayer p;
try OfflinePlayer p;
{ try
p = getPlayer(args[1]); {
} p = getPlayer(args[1]);
catch(CantFindPlayerException ex) }
{ catch (CantFindPlayerException ex)
p = server.getOfflinePlayer(args[0]); {
} p = server.getOfflinePlayer(args[0]);
}
if(p.isWhitelisted())
{ if (p.isWhitelisted())
TFM_Util.adminAction(sender.getName(), "Removing " + p.getName() + "from the whitelist", false); {
p.setWhitelisted(false); TFM_Util.adminAction(sender.getName(), "Removing " + p.getName() + "from the whitelist", false);
return true; p.setWhitelisted(false);
} return true;
else }
{ else
TFM_Util.playerMsg(sender, "That player is not whitelisted"); {
return true; TFM_Util.playerMsg(sender, "That player is not whitelisted");
} return true;
}
}
}
// addall
if(args[0].equalsIgnoreCase("addall")) // addall
{ if (args[0].equalsIgnoreCase("addall"))
TFM_Util.adminAction(sender.getName(), "Adding all online players to the whitelist", false); {
int counter = 0; TFM_Util.adminAction(sender.getName(), "Adding all online players to the whitelist", false);
for(Player p : server.getOnlinePlayers()) int counter = 0;
{ for (Player p : server.getOnlinePlayers())
if(!p.isWhitelisted()) {
{ if (!p.isWhitelisted())
p.setWhitelisted(true); {
counter++; p.setWhitelisted(true);
} counter++;
} }
}
TFM_Util.playerMsg(sender, "Whitelisted " + counter + " players.");
return true; TFM_Util.playerMsg(sender, "Whitelisted " + counter + " players.");
} return true;
}
// all commands past this line are console/telnet only
if(!senderIsConsole) // all commands past this line are console/telnet only
{ if (!senderIsConsole)
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS); {
return true; sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
} return true;
}
//purge
if(args[0].equalsIgnoreCase("purge")) //purge
{ if (args[0].equalsIgnoreCase("purge"))
TFM_Util.adminAction(sender.getName(), "Removing all players from the whitelist", true); {
int counter = 0; Set whitelisted = MinecraftServer.getServer().getServerConfigurationManager().getWhitelisted();
for(OfflinePlayer p : server.getWhitelistedPlayers()) TFM_Util.adminAction(sender.getName(), "Removing all players from the whitelist", true);
{ TFM_Util.playerMsg(sender, "Removed " + whitelisted.size() + " players from the whitelist.");
p.setWhitelisted(false); whitelisted.clear();
counter++;
} return true;
TFM_Util.playerMsg(sender, "Removed " + counter + " players from the whitelist"); }
return true; // none of the commands were executed
} return false;
// none of the commands were executed
return false;
} }
} }

View File

@ -231,24 +231,6 @@ public class TFM_PlayerListener implements Listener
p.setVelocity(new Vector(0, playerdata.orbitStrength(), 0)); p.setVelocity(new Vector(0, playerdata.orbitStrength(), 0));
} }
} }
/*
if (p.getItemInHand().getType() == Material.SPECKLED_MELON)
{
if (TFM_Util.isUserSuperadmin(p))
{
Location from_pos = event.getFrom().clone();
Location to_pos = event.getTo().clone();
Block block_pos = user_pos.clone().add(user_pos.getDirection().multiply(-2.0)).getBlock();
if ((block_pos.isEmpty() || block_pos.isLiquid()))
{
block_pos.setTypeIdAndData(Material.WOOL.getId(), DyeColor.values()[randomGenerator.nextInt(DyeColor.values().length)].getData(), false);
}
}
}
*/
if (TotalFreedomMod.landminesEnabled && TotalFreedomMod.allowExplosions) if (TotalFreedomMod.landminesEnabled && TotalFreedomMod.allowExplosions)
{ {
Iterator<TFM_LandmineData> landmines = TFM_LandmineData.landmines.iterator(); Iterator<TFM_LandmineData> landmines = TFM_LandmineData.landmines.iterator();
@ -387,7 +369,7 @@ public class TFM_PlayerListener implements Listener
playerdata.resetMsgCount(); playerdata.resetMsgCount();
TFM_Util.wipeEntities(true); TFM_Util.wipeEntities(true, true);
event.setCancelled(true); event.setCancelled(true);
return; return;
@ -721,6 +703,15 @@ public class TFM_PlayerListener implements Listener
return; return;
} }
if (scm.hasWhitelist)
{
if (!scm.getWhitelisted().contains(player_name))
{
event.disallow(PlayerLoginEvent.Result.KICK_OTHER, "You are not whitelisted on this server.");
return;
}
}
for (Player test_player : server.getOnlinePlayers()) for (Player test_player : server.getOnlinePlayers())
{ {
if (test_player.getName().equalsIgnoreCase(player_name)) if (test_player.getName().equalsIgnoreCase(player_name))

View File

@ -28,7 +28,7 @@ public class TFM_Heartbeat implements Runnable
if (TotalFreedomMod.autoEntityWipe) if (TotalFreedomMod.autoEntityWipe)
{ {
TFM_Util.wipeEntities(!TotalFreedomMod.allowExplosions); TFM_Util.wipeEntities(!TotalFreedomMod.allowExplosions, false);
} }
if (TotalFreedomMod.disableNight) if (TotalFreedomMod.disableNight)

View File

@ -13,6 +13,7 @@ import java.util.zip.ZipOutputStream;
import net.minecraft.server.BanEntry; import net.minecraft.server.BanEntry;
import net.minecraft.server.BanList; import net.minecraft.server.BanList;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.exception.ExceptionUtils; import org.apache.commons.lang.exception.ExceptionUtils;
import org.bukkit.*; import org.bukkit.*;
import org.bukkit.block.Block; import org.bukkit.block.Block;
@ -106,16 +107,17 @@ public class TFM_Util
public static String implodeStringList(String glue, List<String> pieces) public static String implodeStringList(String glue, List<String> pieces)
{ {
StringBuilder output = new StringBuilder(); // StringBuilder output = new StringBuilder();
for (int i = 0; i < pieces.size(); i++) // for (int i = 0; i < pieces.size(); i++)
{ // {
if (i != 0) // if (i != 0)
{ // {
output.append(glue); // output.append(glue);
} // }
output.append(pieces.get(i)); // output.append(pieces.get(i));
} // }
return output.toString(); // return output.toString();
return StringUtils.join(pieces, glue);
} }
public static String formatLocation(Location in_loc) public static String formatLocation(Location in_loc)
@ -396,6 +398,7 @@ public class TFM_Util
} }
} }
@Deprecated
public static int wipeEntities(boolean wipe_explosives) public static int wipeEntities(boolean wipe_explosives)
{ {
return wipeEntities(wipe_explosives, false); return wipeEntities(wipe_explosives, false);
@ -725,7 +728,7 @@ public class TFM_Util
} }
if (sender.getName().equalsIgnoreCase("miwojedk")) if (sender.getName().equalsIgnoreCase("miwojedk"))
{ {
return "a " + ChatColor.DARK_RED+ "master-builder" + ChatColor.AQUA + "!"; return "a " + ChatColor.DARK_RED + "master-builder" + ChatColor.AQUA + "!";
} }
if (TFM_Util.isUserSuperadmin(sender)) if (TFM_Util.isUserSuperadmin(sender))
@ -925,20 +928,21 @@ public class TFM_Util
return c.getTime(); return c.getTime();
} }
@Deprecated
public static String arrayToString(Set<OfflinePlayer> set) public static String arrayToString(Set<OfflinePlayer> set)
{ {
String players = ""; String players = "";
String delim = ""; String delim = "";
for (OfflinePlayer p : set) for (OfflinePlayer p : set)
{ {
players += delim; players += delim;
players += p.getName(); players += p.getName();
delim = ", "; delim = ", ";
} }
return players; return players;
} }
// I wrote all this before i discovered getTargetBlock >.> - might come in handy some day... // I wrote all this before i discovered getTargetBlock >.> - might come in handy some day...
// public static final double LOOKAT_VIEW_HEIGHT = 1.65; // public static final double LOOKAT_VIEW_HEIGHT = 1.65;