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

View File

@ -14,12 +14,14 @@ public class Command_rd extends TFM_Command
{
if (!(senderIsConsole || TFM_Util.isUserSuperadmin(sender)))
{
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
return true;
sender.sendMessage(TotalFreedomMod.MSG_NO_PERMS);
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.");
return true;
}
}

View File

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

View File

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

View File

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

View File

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