2011-08-08 15:00:04 +00:00
|
|
|
package com.earth2me.essentials.commands;
|
|
|
|
|
|
|
|
import com.earth2me.essentials.DescParseTickFormat;
|
|
|
|
import com.earth2me.essentials.User;
|
2011-08-20 17:09:55 +00:00
|
|
|
import com.earth2me.essentials.Util;
|
2011-11-18 17:42:26 +00:00
|
|
|
import java.util.*;
|
|
|
|
import org.bukkit.Server;
|
2011-08-08 15:49:32 +00:00
|
|
|
import org.bukkit.World;
|
2011-11-18 17:42:26 +00:00
|
|
|
import org.bukkit.command.CommandSender;
|
2011-08-08 15:00:04 +00:00
|
|
|
import org.bukkit.entity.Player;
|
|
|
|
|
|
|
|
|
|
|
|
public class Commandptime extends EssentialsCommand
|
|
|
|
{
|
2011-08-10 04:25:45 +00:00
|
|
|
public static final Set<String> getAliases = new HashSet<String>();
|
2011-08-10 14:06:42 +00:00
|
|
|
|
|
|
|
static
|
|
|
|
{
|
2011-08-10 04:25:45 +00:00
|
|
|
getAliases.add("get");
|
|
|
|
getAliases.add("list");
|
|
|
|
getAliases.add("show");
|
|
|
|
getAliases.add("display");
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +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
|
|
|
|
2011-08-08 15:00:04 +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-20 17:09:55 +00:00
|
|
|
if ((!users.contains(user) || users.size() > 1) && user != null && !user.isAuthorized("essentials.ptime.others"))
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
2011-08-20 17:09:55 +00:00
|
|
|
user.sendMessage(Util.i18n("pTimeOthersPermission"));
|
|
|
|
return;
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
Long ticks;
|
|
|
|
// Parse the target time int ticks from args[0]
|
2011-08-08 16:21:38 +00:00
|
|
|
String timeParam = args[0];
|
2011-08-10 14:06:42 +00:00
|
|
|
Boolean relative = true;
|
|
|
|
if (timeParam.startsWith("@"))
|
|
|
|
{
|
|
|
|
relative = false;
|
|
|
|
timeParam = timeParam.substring(1);
|
|
|
|
}
|
|
|
|
|
2011-08-10 04:25:45 +00:00
|
|
|
if (getAliases.contains(timeParam))
|
|
|
|
{
|
|
|
|
getUsersTime(sender, users);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if (DescParseTickFormat.meansReset(timeParam))
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
|
|
|
ticks = null;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
ticks = DescParseTickFormat.parse(timeParam);
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
catch (NumberFormatException e)
|
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
throw new NotEnoughArgumentsException();
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-10 14:06:42 +00:00
|
|
|
setUsersTime(sender, users, ticks, relative);
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +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)
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
2011-09-26 02:20:56 +00:00
|
|
|
if (users.size() > 1)
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
2011-08-20 17:09:55 +00:00
|
|
|
sender.sendMessage(Util.format("pTimePlayers"));
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
2011-09-26 02:20:56 +00:00
|
|
|
|
|
|
|
for (User user : users)
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
2011-09-26 02:20:56 +00:00
|
|
|
if (user.getPlayerTimeOffset() == 0)
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
2011-08-20 17:09:55 +00:00
|
|
|
sender.sendMessage(Util.format("pTimeNormal", user.getName()));
|
|
|
|
}
|
2011-09-26 02:20:56 +00:00
|
|
|
else
|
|
|
|
{
|
2011-08-10 14:06:42 +00:00
|
|
|
String time = DescParseTickFormat.format(user.getPlayerTime());
|
2011-09-26 02:20:56 +00:00
|
|
|
if (!user.isPlayerTimeRelative())
|
2011-08-10 14:06:42 +00:00
|
|
|
{
|
2011-08-20 17:09:55 +00:00
|
|
|
sender.sendMessage(Util.format("pTimeCurrentFixed", user.getName(), time));
|
|
|
|
}
|
2011-09-26 02:20:56 +00:00
|
|
|
else
|
|
|
|
{
|
2011-08-20 17:09:55 +00:00
|
|
|
sender.sendMessage(Util.format("pTimeCurrent", user.getName(), time));
|
2011-08-10 14:06:42 +00:00
|
|
|
}
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
}
|
2011-09-26 02:20:56 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
/**
|
|
|
|
* Used to set the time and inform of the change
|
|
|
|
*/
|
2011-08-10 14:06:42 +00:00
|
|
|
private void setUsersTime(final CommandSender sender, final Collection<User> users, final Long ticks, Boolean relative)
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
|
|
|
// Update the time
|
|
|
|
if (ticks == null)
|
|
|
|
{
|
|
|
|
// Reset
|
|
|
|
for (User user : users)
|
|
|
|
{
|
|
|
|
user.resetPlayerTime();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Set
|
|
|
|
for (User user : users)
|
|
|
|
{
|
2011-08-10 14:06:42 +00:00
|
|
|
final World world = user.getWorld();
|
2011-08-08 15:49:32 +00:00
|
|
|
long time = user.getPlayerTime();
|
|
|
|
time -= time % 24000;
|
2011-08-10 14:06:42 +00:00
|
|
|
time += 24000 + ticks;
|
|
|
|
if (relative)
|
|
|
|
{
|
|
|
|
time -= world.getTime();
|
|
|
|
}
|
|
|
|
user.setPlayerTime(time, relative);
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-20 17:09:55 +00:00
|
|
|
final StringBuilder msg = new StringBuilder();
|
|
|
|
for (User user : users)
|
|
|
|
{
|
|
|
|
if (msg.length() > 0)
|
|
|
|
{
|
|
|
|
msg.append(", ");
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.append(user.getName());
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
// Inform the sender of the change
|
|
|
|
if (ticks == null)
|
|
|
|
{
|
2011-08-20 17:09:55 +00:00
|
|
|
sender.sendMessage(Util.format("pTimeReset", msg.toString()));
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-11 02:04:57 +00:00
|
|
|
String time = DescParseTickFormat.format(ticks);
|
|
|
|
if (!relative)
|
|
|
|
{
|
2011-08-20 17:09:55 +00:00
|
|
|
sender.sendMessage(Util.format("pTimeSetFixed", time, msg.toString()));
|
2011-08-11 02:04:57 +00:00
|
|
|
}
|
2011-09-26 02:20:56 +00:00
|
|
|
else
|
|
|
|
{
|
2011-08-20 17:09:55 +00:00
|
|
|
sender.sendMessage(Util.format("pTimeSet", time, msg.toString()));
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +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:00:04 +00:00
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
final Set<User> users = new TreeSet<User>(new UserNameComparator());
|
2011-08-08 15:00:04 +00:00
|
|
|
// 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);
|
2011-08-08 15:00:04 +00:00
|
|
|
if (user == null)
|
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
for (Player player : server.getOnlinePlayers())
|
|
|
|
{
|
|
|
|
users.add(ess.getUser(player));
|
|
|
|
}
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
users.add(user);
|
|
|
|
}
|
|
|
|
return users;
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +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())
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
|
|
|
user = ess.getUser(matchedPlayers.get(0));
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +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));
|
|
|
|
}
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
// We failed to understand the world target...
|
|
|
|
else
|
|
|
|
{
|
2011-08-20 17:09:55 +00:00
|
|
|
throw new Exception(Util.i18n("playerNotFound"));
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
return users;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-08 15:49:32 +00:00
|
|
|
|
|
|
|
class UserNameComparator implements Comparator<User>
|
|
|
|
{
|
2011-08-08 15:00:04 +00:00
|
|
|
public int compare(User a, User b)
|
|
|
|
{
|
|
|
|
return a.getName().compareTo(b.getName());
|
|
|
|
}
|
|
|
|
}
|