2011-08-08 15:00:04 +00:00
|
|
|
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;
|
2011-08-08 15:00:04 +00:00
|
|
|
import org.bukkit.ChatColor;
|
2011-08-08 15:49:32 +00:00
|
|
|
import org.bukkit.World;
|
2011-08-08 15:00:04 +00:00
|
|
|
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>();
|
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-08 15:49:32 +00:00
|
|
|
if (user != null && !user.isAuthorized("essentials.ptime.others"))
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
|
|
|
// 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: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
|
|
|
{
|
|
|
|
if (users.size() == 1)
|
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
final User user = users.iterator().next();
|
|
|
|
|
2011-08-10 05:36:09 +00:00
|
|
|
if (user.getPlayerTimeOffset() == 0)
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
|
|
|
sender.sendMessage(colorDefault + user.getName() + "'s time is normal. Time is the same as on the server.");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-10 14:06:42 +00:00
|
|
|
String time = DescParseTickFormat.format(user.getPlayerTime());
|
|
|
|
if (!user.isPlayerTimeRelative())
|
|
|
|
{
|
|
|
|
time = "fixed to " + time;
|
|
|
|
}
|
|
|
|
sender.sendMessage(colorDefault + user.getName() + "'s time is " + time);
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-10 14:06:42 +00:00
|
|
|
sender.sendMessage(colorDefault + "These players have their own time:");
|
2011-08-08 15:49:32 +00:00
|
|
|
|
|
|
|
for (User user : users)
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
2011-08-10 06:05:05 +00:00
|
|
|
//if (!user.isPlayerTimeRelative())
|
|
|
|
if (user.getPlayerTimeOffset() != 0)
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
2011-08-10 14:06:42 +00:00
|
|
|
String time = DescParseTickFormat.format(user.getPlayerTime());
|
|
|
|
if (!user.isPlayerTimeRelative())
|
|
|
|
{
|
|
|
|
time = "fixed to " + time;
|
|
|
|
}
|
|
|
|
sender.sendMessage(colorDefault + user.getName() + "'s time is " + time);
|
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-08 15:00:04 +00:00
|
|
|
// Inform the sender of the change
|
|
|
|
sender.sendMessage("");
|
2011-08-08 15:49:32 +00:00
|
|
|
final StringBuilder msg = new StringBuilder();
|
2011-08-08 15:00:04 +00:00
|
|
|
if (ticks == null)
|
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
sender.sendMessage(colorDefault + "The players time was reset for:");
|
2011-08-08 15:00:04 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
sender.sendMessage(colorDefault + "The players time was fixed to:");
|
2011-08-08 15:00:04 +00:00
|
|
|
sender.sendMessage(DescParseTickFormat.format(ticks));
|
|
|
|
msg.append(colorDefault);
|
|
|
|
msg.append("For: ");
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
boolean first = true;
|
|
|
|
for (User user : users)
|
|
|
|
{
|
2011-08-08 15:49:32 +00:00
|
|
|
if (!first)
|
2011-08-08 15:00:04 +00:00
|
|
|
{
|
|
|
|
msg.append(colorDefault);
|
|
|
|
msg.append(", ");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
first = false;
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
msg.append(colorHighlight1);
|
|
|
|
msg.append(user.getName());
|
|
|
|
}
|
2011-08-08 15:49:32 +00:00
|
|
|
|
2011-08-08 15:00:04 +00:00
|
|
|
sender.sendMessage(msg.toString());
|
|
|
|
}
|
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-08 15:49:32 +00:00
|
|
|
throw new Exception("Could not find the player(s) \"" + selector + "\"");
|
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());
|
|
|
|
}
|
|
|
|
}
|