TF-EssentialsX/Essentials/src/com/earth2me/essentials/commands/Commandptime.java

248 lines
5.7 KiB
Java
Raw Normal View History

package com.earth2me.essentials.commands;
import com.earth2me.essentials.DescParseTickFormat;
import org.bukkit.Server;
import org.bukkit.command.CommandSender;
import com.earth2me.essentials.User;
2011-08-08 15:49:32 +00:00
import java.util.Collection;
import java.util.Comparator;
2011-08-10 04:25:45 +00:00
import java.util.HashSet;
2011-08-08 15:49:32 +00:00
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import org.bukkit.ChatColor;
2011-08-08 15:49:32 +00:00
import org.bukkit.World;
import org.bukkit.entity.Player;
public class Commandptime extends EssentialsCommand
{
// TODO: I suggest that the chat colors be centralized in the config file.
public static final ChatColor colorDefault = ChatColor.YELLOW;
public static final ChatColor colorChrome = ChatColor.GOLD;
public static final ChatColor colorLogo = ChatColor.GREEN;
public static final ChatColor colorHighlight1 = ChatColor.AQUA;
public static final ChatColor colorBad = ChatColor.RED;
2011-08-10 04:25:45 +00:00
public static final Set<String> getAliases = new HashSet<String>();
static {
getAliases.add("get");
getAliases.add("list");
getAliases.add("show");
getAliases.add("display");
}
2011-08-08 15:49:32 +00:00
public Commandptime()
{
super("ptime");
}
@Override
public void run(final Server server, final CommandSender sender, final String commandLabel, final String[] args) throws Exception
{
// Which Players(s) / Users(s) are we interested in?
String userSelector = null;
if (args.length == 2)
{
userSelector = args[1];
}
Set<User> users = getUsers(server, sender, userSelector);
2011-08-08 15:49:32 +00:00
// If no arguments we are reading the time
if (args.length == 0)
{
getUsersTime(sender, users);
return;
}
User user = ess.getUser(sender);
2011-08-08 15:49:32 +00:00
if (user != null && !user.isAuthorized("essentials.ptime.others"))
{
// TODO should not be hardcoded !!
2011-08-08 15:49:32 +00:00
throw new Exception(colorBad + "You are not authorized to set others PlayerTime");
}
2011-08-08 15:49:32 +00:00
Long ticks;
// Parse the target time int ticks from args[0]
String timeParam = args[0];
2011-08-10 04:25:45 +00:00
if (getAliases.contains(timeParam))
{
getUsersTime(sender, users);
return;
}
else if (DescParseTickFormat.meansReset(timeParam))
{
ticks = null;
}
else
{
try
{
2011-08-08 15:49:32 +00:00
ticks = DescParseTickFormat.parse(timeParam);
}
catch (NumberFormatException e)
{
2011-08-08 15:49:32 +00:00
throw new NotEnoughArgumentsException();
}
}
2011-08-08 15:49:32 +00:00
setUsersTime(sender, users, ticks);
}
2011-08-08 15:49:32 +00:00
/**
* Used to get the time and inform
*/
2011-08-08 15:49:32 +00:00
private void getUsersTime(final CommandSender sender, final Collection<User> users)
{
if (users.size() == 1)
{
2011-08-08 15:49:32 +00:00
final User user = users.iterator().next();
if (user.isPlayerTimeRelative())
{
sender.sendMessage(colorDefault + user.getName() + "'s time is normal. Time is the same as on the server.");
}
else
{
2011-08-08 15:49:32 +00:00
sender.sendMessage(colorDefault + user.getName() + "'s time is fixed to: " + DescParseTickFormat.format(user.getPlayerTime()));
}
return;
}
2011-08-08 15:49:32 +00:00
sender.sendMessage(colorDefault + "These players have fixed time:");
2011-08-08 15:49:32 +00:00
for (User user : users)
{
2011-08-08 15:49:32 +00:00
if (!user.isPlayerTimeRelative())
{
2011-08-08 15:49:32 +00:00
sender.sendMessage(colorDefault + user.getName() + ": " + DescParseTickFormat.format(user.getPlayerTime()));
}
}
return;
}
2011-08-08 15:49:32 +00:00
/**
* Used to set the time and inform of the change
*/
2011-08-08 15:49:32 +00:00
private void setUsersTime(final CommandSender sender, final Collection<User> users, final Long ticks)
{
// Update the time
if (ticks == null)
{
// Reset
for (User user : users)
{
user.resetPlayerTime();
}
}
else
{
// Set
for (User user : users)
{
2011-08-08 15:49:32 +00:00
long time = user.getPlayerTime();
time -= time % 24000;
final World world = user.getWorld();
user.setPlayerTime(time + 24000 + ticks - world.getTime(), true);
}
}
2011-08-08 15:49:32 +00:00
// Inform the sender of the change
sender.sendMessage("");
2011-08-08 15:49:32 +00:00
final StringBuilder msg = new StringBuilder();
if (ticks == null)
{
2011-08-08 15:49:32 +00:00
sender.sendMessage(colorDefault + "The players time was reset for:");
}
else
{
2011-08-08 15:49:32 +00:00
sender.sendMessage(colorDefault + "The players time was fixed to:");
sender.sendMessage(DescParseTickFormat.format(ticks));
msg.append(colorDefault);
msg.append("For: ");
}
2011-08-08 15:49:32 +00:00
boolean first = true;
for (User user : users)
{
2011-08-08 15:49:32 +00:00
if (!first)
{
msg.append(colorDefault);
msg.append(", ");
}
else
{
first = false;
}
2011-08-08 15:49:32 +00:00
msg.append(colorHighlight1);
msg.append(user.getName());
}
2011-08-08 15:49:32 +00:00
sender.sendMessage(msg.toString());
}
2011-08-08 15:49:32 +00:00
/**
* Used to parse an argument of the type "users(s) selector"
2011-08-08 15:49:32 +00:00
*/
private Set<User> getUsers(final Server server, final CommandSender sender, final String selector) throws Exception
{
2011-08-08 15:49:32 +00:00
final Set<User> users = new TreeSet<User>(new UserNameComparator());
// If there is no selector we want the sender itself. Or all users if sender isn't a user.
if (selector == null)
{
2011-08-08 15:49:32 +00:00
final User user = ess.getUser(sender);
if (user == null)
{
2011-08-08 15:49:32 +00:00
for (Player player : server.getOnlinePlayers())
{
users.add(ess.getUser(player));
}
}
else
{
users.add(user);
}
return users;
}
2011-08-08 15:49:32 +00:00
// Try to find the user with name = selector
User user = null;
2011-08-08 15:49:32 +00:00
final List<Player> matchedPlayers = server.matchPlayer(selector);
if (!matchedPlayers.isEmpty())
{
user = ess.getUser(matchedPlayers.get(0));
}
2011-08-08 15:49:32 +00:00
if (user != null)
{
users.add(user);
}
// If that fails, Is the argument something like "*" or "all"?
else if (selector.equalsIgnoreCase("*") || selector.equalsIgnoreCase("all"))
{
2011-08-08 15:49:32 +00:00
for (Player player : server.getOnlinePlayers())
{
users.add(ess.getUser(player));
}
}
// We failed to understand the world target...
else
{
2011-08-08 15:49:32 +00:00
throw new Exception("Could not find the player(s) \"" + selector + "\"");
}
2011-08-08 15:49:32 +00:00
return users;
}
}
2011-08-08 15:49:32 +00:00
class UserNameComparator implements Comparator<User>
{
public int compare(User a, User b)
{
return a.getName().compareTo(b.getName());
}
}