mirror of
https://github.com/EssentialsX/Essentials.git
synced 2024-12-24 10:09:50 +01:00
Some fixes to the time code by me
This commit is contained in:
parent
f75390bd3f
commit
a117080b32
@ -3,8 +3,6 @@ package com.earth2me.essentials;
|
||||
import com.earth2me.essentials.commands.Commandtime;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.*;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
|
||||
/**
|
||||
@ -16,7 +14,7 @@ import java.util.logging.Logger;
|
||||
*
|
||||
* @author Olof Larsson
|
||||
*/
|
||||
public class DescParseTickFormat
|
||||
public final class DescParseTickFormat
|
||||
{
|
||||
// ============================================
|
||||
// First some information vars. TODO: Should this be in a config file?
|
||||
@ -64,6 +62,10 @@ public class DescParseTickFormat
|
||||
resetAliases.add("default");
|
||||
}
|
||||
|
||||
private DescParseTickFormat()
|
||||
{
|
||||
}
|
||||
|
||||
// ============================================
|
||||
// PARSE. From describing String to int
|
||||
// --------------------------------------------
|
||||
@ -71,8 +73,8 @@ public class DescParseTickFormat
|
||||
{
|
||||
Long ret;
|
||||
|
||||
// Only look at alphanumeric and lowercase
|
||||
desc = desc.toLowerCase().replaceAll("[^A-Za-z0-9]", "");
|
||||
// Only look at alphanumeric and lowercase and : for 24:00
|
||||
desc = desc.toLowerCase().replaceAll("[^A-Za-z0-9:]", "");
|
||||
|
||||
// Detect ticks format
|
||||
try
|
||||
@ -140,8 +142,8 @@ public class DescParseTickFormat
|
||||
throw new NumberFormatException();
|
||||
}
|
||||
|
||||
int hours = Integer.parseInt(desc.substring(0, 2));
|
||||
int minutes = Integer.parseInt(desc.substring(2, 4));
|
||||
final int hours = Integer.parseInt(desc.substring(0, 2));
|
||||
final int minutes = Integer.parseInt(desc.substring(2, 4));
|
||||
|
||||
return hoursMinutesToTicks(hours, minutes);
|
||||
}
|
||||
@ -193,7 +195,7 @@ public class DescParseTickFormat
|
||||
return hoursMinutesToTicks(hours, minutes);
|
||||
}
|
||||
|
||||
public static long hoursMinutesToTicks(int hours, int minutes)
|
||||
public static long hoursMinutesToTicks(final int hours, final int minutes)
|
||||
{
|
||||
long ret = ticksAtMidnight;
|
||||
ret += (hours - 1) * ticksPerHour;
|
||||
@ -204,9 +206,9 @@ public class DescParseTickFormat
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static long parseAlias(String desc) throws NumberFormatException
|
||||
public static long parseAlias(final String desc) throws NumberFormatException
|
||||
{
|
||||
Integer ret = nameToTicks.get(desc);
|
||||
final Integer ret = nameToTicks.get(desc);
|
||||
if (ret == null)
|
||||
{
|
||||
throw new NumberFormatException();
|
||||
@ -215,7 +217,7 @@ public class DescParseTickFormat
|
||||
return ret;
|
||||
}
|
||||
|
||||
public static boolean meansReset(String desc)
|
||||
public static boolean meansReset(final String desc)
|
||||
{
|
||||
return resetAliases.contains(desc);
|
||||
}
|
||||
@ -223,9 +225,9 @@ public class DescParseTickFormat
|
||||
// ============================================
|
||||
// FORMAT. From int to describing String
|
||||
// --------------------------------------------
|
||||
public static String format(long ticks)
|
||||
public static String format(final long ticks)
|
||||
{
|
||||
StringBuilder msg = new StringBuilder();
|
||||
final StringBuilder msg = new StringBuilder();
|
||||
msg.append(Commandtime.colorHighlight1);
|
||||
msg.append(format24(ticks));
|
||||
msg.append(Commandtime.colorDefault);
|
||||
@ -239,24 +241,30 @@ public class DescParseTickFormat
|
||||
return msg.toString();
|
||||
}
|
||||
|
||||
public static String formatTicks(long ticks)
|
||||
public static String formatTicks(final long ticks)
|
||||
{
|
||||
return "" + ticks % ticksPerDay + "ticks";
|
||||
return (ticks % ticksPerDay) + "ticks";
|
||||
}
|
||||
|
||||
public static String format24(long ticks)
|
||||
public static String format24(final long ticks)
|
||||
{
|
||||
return formatDateFormat(ticks, SDFTwentyFour);
|
||||
synchronized (SDFTwentyFour)
|
||||
{
|
||||
return formatDateFormat(ticks, SDFTwentyFour);
|
||||
}
|
||||
}
|
||||
|
||||
public static String format12(long ticks)
|
||||
public static String format12(final long ticks)
|
||||
{
|
||||
return formatDateFormat(ticks, SDFTwelve);
|
||||
synchronized (SDFTwelve)
|
||||
{
|
||||
return formatDateFormat(ticks, SDFTwelve);
|
||||
}
|
||||
}
|
||||
|
||||
public static String formatDateFormat(long ticks, SimpleDateFormat format)
|
||||
public static String formatDateFormat(final long ticks, final SimpleDateFormat format)
|
||||
{
|
||||
Date date = ticksToDate(ticks);
|
||||
final Date date = ticksToDate(ticks);
|
||||
return format.format(date);
|
||||
}
|
||||
|
||||
@ -271,18 +279,18 @@ public class DescParseTickFormat
|
||||
ticks = ticks - days * ticksPerDay;
|
||||
|
||||
// How many hours on the last day?
|
||||
long hours = ticks / ticksPerHour;
|
||||
final long hours = ticks / ticksPerHour;
|
||||
ticks = ticks - hours * ticksPerHour;
|
||||
|
||||
// How many minutes on the last day?
|
||||
long minutes = (long)Math.floor(ticks / ticksPerMinute);
|
||||
double dticks = ticks - minutes * ticksPerMinute;
|
||||
final long minutes = (long)Math.floor(ticks / ticksPerMinute);
|
||||
final double dticks = ticks - minutes * ticksPerMinute;
|
||||
|
||||
// How many seconds on the last day?
|
||||
long seconds = (long)Math.floor(dticks / ticksPerSecond);
|
||||
final long seconds = (long)Math.floor(dticks / ticksPerSecond);
|
||||
|
||||
// Now we create an english GMT calendar (We wan't no daylight savings)
|
||||
Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);
|
||||
final Calendar cal = Calendar.getInstance(TimeZone.getTimeZone("GMT"), Locale.ENGLISH);
|
||||
cal.setLenient(true);
|
||||
|
||||
// And we set the time to 0! And append the time that passed!
|
||||
|
@ -4,8 +4,13 @@ import com.earth2me.essentials.DescParseTickFormat;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import com.earth2me.essentials.User;
|
||||
import java.util.*;
|
||||
import java.util.Collection;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
|
||||
@ -17,7 +22,7 @@ public class Commandptime extends EssentialsCommand
|
||||
public static final ChatColor colorLogo = ChatColor.GREEN;
|
||||
public static final ChatColor colorHighlight1 = ChatColor.AQUA;
|
||||
public static final ChatColor colorBad = ChatColor.RED;
|
||||
|
||||
|
||||
public Commandptime()
|
||||
{
|
||||
super("ptime");
|
||||
@ -32,8 +37,12 @@ public class Commandptime extends EssentialsCommand
|
||||
{
|
||||
userSelector = args[1];
|
||||
}
|
||||
if (args.length == 3)
|
||||
{
|
||||
userSelector = args[2];
|
||||
}
|
||||
Set<User> users = getUsers(server, sender, userSelector);
|
||||
|
||||
|
||||
// If no arguments we are reading the time
|
||||
if (args.length == 0)
|
||||
{
|
||||
@ -42,16 +51,16 @@ public class Commandptime extends EssentialsCommand
|
||||
}
|
||||
|
||||
User user = ess.getUser(sender);
|
||||
if ( user != null && ! user.isAuthorized("essentials.ptime.others"))
|
||||
if (user != null && !user.isAuthorized("essentials.ptime.others"))
|
||||
{
|
||||
// TODO should not be hardcoded !!
|
||||
sender.sendMessage(colorBad + "You are not authorized to set others PlayerTime");
|
||||
return; // TODO: How to not just die silently? in a good way??
|
||||
throw new Exception(colorBad + "You are not authorized to set others PlayerTime");
|
||||
}
|
||||
|
||||
|
||||
Long ticks;
|
||||
// Parse the target time int ticks from args[0]
|
||||
if (DescParseTickFormat.meansReset(args[0]))
|
||||
String timeParam = args.length == 2 ? args[0] : args[0] + args[1];
|
||||
if (DescParseTickFormat.meansReset(timeParam))
|
||||
{
|
||||
ticks = null;
|
||||
}
|
||||
@ -59,57 +68,53 @@ public class Commandptime extends EssentialsCommand
|
||||
{
|
||||
try
|
||||
{
|
||||
ticks = DescParseTickFormat.parse(args[0]);
|
||||
ticks = DescParseTickFormat.parse(timeParam);
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
// TODO: Display an error with help included... on how to specify the time
|
||||
sender.sendMessage(colorBad + "Unknown time descriptor... brlalidididiablidadadibibibiiba!! TODO");
|
||||
return;
|
||||
throw new NotEnoughArgumentsException();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
setUsersTime(sender, users, ticks);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Used to get the time and inform
|
||||
*/
|
||||
private void getUsersTime(CommandSender sender, Collection<User> users)
|
||||
private void getUsersTime(final CommandSender sender, final Collection<User> users)
|
||||
{
|
||||
if (users.size() == 1)
|
||||
{
|
||||
Iterator<User> iter = users.iterator();
|
||||
User user = iter.next();
|
||||
|
||||
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
|
||||
{
|
||||
sender.sendMessage(colorDefault + user.getName() + "'s time is fixed to: "+DescParseTickFormat.format(user.getPlayerTime()));
|
||||
sender.sendMessage(colorDefault + user.getName() + "'s time is fixed to: " + DescParseTickFormat.format(user.getPlayerTime()));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
sender.sendMessage(colorDefault + "These players have fixed time:");
|
||||
|
||||
for (User user : users)
|
||||
|
||||
for (User user : users)
|
||||
{
|
||||
if ( ! user.isPlayerTimeRelative())
|
||||
if (!user.isPlayerTimeRelative())
|
||||
{
|
||||
sender.sendMessage(colorDefault + user.getName() + ": "+DescParseTickFormat.format(user.getPlayerTime()));
|
||||
sender.sendMessage(colorDefault + user.getName() + ": " + DescParseTickFormat.format(user.getPlayerTime()));
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Used to set the time and inform of the change
|
||||
*/
|
||||
private void setUsersTime(CommandSender sender, Collection<User> users, Long ticks)
|
||||
private void setUsersTime(final CommandSender sender, final Collection<User> users, final Long ticks)
|
||||
{
|
||||
// Update the time
|
||||
if (ticks == null)
|
||||
@ -125,30 +130,33 @@ public class Commandptime extends EssentialsCommand
|
||||
// Set
|
||||
for (User user : users)
|
||||
{
|
||||
user.setPlayerTime(ticks, false);
|
||||
long time = user.getPlayerTime();
|
||||
time -= time % 24000;
|
||||
final World world = user.getWorld();
|
||||
user.setPlayerTime(time + 24000 + ticks - world.getTime(), true);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
// Inform the sender of the change
|
||||
sender.sendMessage("");
|
||||
StringBuilder msg = new StringBuilder();
|
||||
final StringBuilder msg = new StringBuilder();
|
||||
if (ticks == null)
|
||||
{
|
||||
sender.sendMessage(colorDefault + "The PlayerTime was reset for:");
|
||||
sender.sendMessage(colorDefault + "The players time was reset for:");
|
||||
}
|
||||
else
|
||||
{
|
||||
sender.sendMessage(colorDefault + "The PlayerTime was fixed to:");
|
||||
sender.sendMessage(colorDefault + "The players time was fixed to:");
|
||||
sender.sendMessage(DescParseTickFormat.format(ticks));
|
||||
msg.append(colorDefault);
|
||||
msg.append("For: ");
|
||||
}
|
||||
|
||||
|
||||
boolean first = true;
|
||||
for (User user : users)
|
||||
{
|
||||
if ( ! first)
|
||||
if (!first)
|
||||
{
|
||||
msg.append(colorDefault);
|
||||
msg.append(", ");
|
||||
@ -157,28 +165,30 @@ public class Commandptime extends EssentialsCommand
|
||||
{
|
||||
first = false;
|
||||
}
|
||||
|
||||
|
||||
msg.append(colorHighlight1);
|
||||
msg.append(user.getName());
|
||||
}
|
||||
|
||||
|
||||
sender.sendMessage(msg.toString());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Used to parse an argument of the type "users(s) selector"
|
||||
*/
|
||||
private Set<User> getUsers(Server server, CommandSender sender, String selector) throws Exception
|
||||
*/
|
||||
private Set<User> getUsers(final Server server, final CommandSender sender, final String selector) throws Exception
|
||||
{
|
||||
Set<User> users = new TreeSet<User>(new UserNameComparator());
|
||||
Player[] players;
|
||||
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)
|
||||
{
|
||||
User user = ess.getUser(sender);
|
||||
final User user = ess.getUser(sender);
|
||||
if (user == null)
|
||||
{
|
||||
users.addAll(ess.getAllOnlineUsers().values());
|
||||
for (Player player : server.getOnlinePlayers())
|
||||
{
|
||||
users.add(ess.getUser(player));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -186,15 +196,15 @@ public class Commandptime extends EssentialsCommand
|
||||
}
|
||||
return users;
|
||||
}
|
||||
|
||||
|
||||
// Try to find the user with name = selector
|
||||
User user = null;
|
||||
List<Player> matchedPlayers = server.matchPlayer(selector);
|
||||
if (matchedPlayers.size() > 0)
|
||||
final List<Player> matchedPlayers = server.matchPlayer(selector);
|
||||
if (!matchedPlayers.isEmpty())
|
||||
{
|
||||
user = ess.getUser(matchedPlayers.get(0));
|
||||
}
|
||||
|
||||
|
||||
if (user != null)
|
||||
{
|
||||
users.add(user);
|
||||
@ -202,19 +212,24 @@ public class Commandptime extends EssentialsCommand
|
||||
// If that fails, Is the argument something like "*" or "all"?
|
||||
else if (selector.equalsIgnoreCase("*") || selector.equalsIgnoreCase("all"))
|
||||
{
|
||||
users.addAll(ess.getAllOnlineUsers().values());
|
||||
for (Player player : server.getOnlinePlayers())
|
||||
{
|
||||
users.add(ess.getUser(player));
|
||||
}
|
||||
}
|
||||
// We failed to understand the world target...
|
||||
else
|
||||
{
|
||||
throw new Exception("Could not find the player(s) \""+selector+"\"");
|
||||
throw new Exception("Could not find the player(s) \"" + selector + "\"");
|
||||
}
|
||||
|
||||
|
||||
return users;
|
||||
}
|
||||
}
|
||||
|
||||
class UserNameComparator implements Comparator<User> {
|
||||
|
||||
class UserNameComparator implements Comparator<User>
|
||||
{
|
||||
public int compare(User a, User b)
|
||||
{
|
||||
return a.getName().compareTo(b.getName());
|
||||
|
@ -17,7 +17,7 @@ public class Commandtime extends EssentialsCommand
|
||||
public static final ChatColor colorLogo = ChatColor.GREEN;
|
||||
public static final ChatColor colorHighlight1 = ChatColor.AQUA;
|
||||
public static final ChatColor colorBad = ChatColor.RED;
|
||||
|
||||
|
||||
public Commandtime()
|
||||
{
|
||||
super("time");
|
||||
@ -32,8 +32,12 @@ public class Commandtime extends EssentialsCommand
|
||||
{
|
||||
worldSelector = args[1];
|
||||
}
|
||||
if (args.length == 3)
|
||||
{
|
||||
worldSelector = args[2];
|
||||
}
|
||||
Set<World> worlds = getWorlds(server, sender, worldSelector);
|
||||
|
||||
|
||||
// If no arguments we are reading the time
|
||||
if (args.length == 0)
|
||||
{
|
||||
@ -42,29 +46,26 @@ public class Commandtime extends EssentialsCommand
|
||||
}
|
||||
|
||||
User user = ess.getUser(sender);
|
||||
if ( user != null && ! user.isAuthorized("essentials.time.set"))
|
||||
if (user != null && !user.isAuthorized("essentials.time.set"))
|
||||
{
|
||||
// TODO should not be hardcoded !!
|
||||
sender.sendMessage(colorBad + "You are not authorized to set the time");
|
||||
return; // TODO: How to not just die silently? in a good way??
|
||||
throw new Exception(colorBad + "You are not authorized to set the time");
|
||||
}
|
||||
|
||||
|
||||
// Parse the target time int ticks from args[0]
|
||||
long ticks;
|
||||
try
|
||||
{
|
||||
ticks = DescParseTickFormat.parse(args[0]);
|
||||
ticks = DescParseTickFormat.parse(args.length == 2 ? args[0] : args[0] + args[1]);
|
||||
}
|
||||
catch (NumberFormatException e)
|
||||
{
|
||||
// TODO: Display an error with help included... on how to specify the time
|
||||
sender.sendMessage(colorBad + "Unknown time descriptor... brlalidididiablidadadibibibiiba!! TODO");
|
||||
return;
|
||||
throw new NotEnoughArgumentsException();
|
||||
}
|
||||
|
||||
setWorldsTime(sender, worlds, ticks);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Used to get the time and inform
|
||||
*/
|
||||
@ -77,14 +78,14 @@ public class Commandtime extends EssentialsCommand
|
||||
sender.sendMessage(DescParseTickFormat.format(iter.next().getTime()));
|
||||
return;
|
||||
}
|
||||
|
||||
for (World world : worlds)
|
||||
|
||||
for (World world : worlds)
|
||||
{
|
||||
sender.sendMessage(colorDefault + world.getName()+": " + DescParseTickFormat.format(world.getTime()));
|
||||
sender.sendMessage(colorDefault + world.getName() + ": " + DescParseTickFormat.format(world.getTime()));
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Used to set the time and inform of the change
|
||||
*/
|
||||
@ -95,19 +96,19 @@ public class Commandtime extends EssentialsCommand
|
||||
{
|
||||
world.setTime(ticks);
|
||||
}
|
||||
|
||||
|
||||
// Inform the sender of the change
|
||||
sender.sendMessage("");
|
||||
sender.sendMessage(colorDefault + "The time was set to");
|
||||
sender.sendMessage(DescParseTickFormat.format(ticks));
|
||||
|
||||
|
||||
StringBuilder msg = new StringBuilder();
|
||||
msg.append(colorDefault);
|
||||
msg.append("In ");
|
||||
boolean first = true;
|
||||
for (World world : worlds)
|
||||
{
|
||||
if ( ! first)
|
||||
if (!first)
|
||||
{
|
||||
msg.append(colorDefault);
|
||||
msg.append(", ");
|
||||
@ -116,21 +117,21 @@ public class Commandtime extends EssentialsCommand
|
||||
{
|
||||
first = false;
|
||||
}
|
||||
|
||||
|
||||
msg.append(colorHighlight1);
|
||||
msg.append(world.getName());
|
||||
}
|
||||
|
||||
|
||||
sender.sendMessage(msg.toString());
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Used to parse an argument of the type "world(s) selector"
|
||||
*/
|
||||
*/
|
||||
private Set<World> getWorlds(Server server, CommandSender sender, String selector) throws Exception
|
||||
{
|
||||
Set<World> worlds = new TreeSet<World>(new WorldNameComparator());
|
||||
|
||||
|
||||
// If there is no selector we want the world the user is currently in. Or all worlds if it isn't a user.
|
||||
if (selector == null)
|
||||
{
|
||||
@ -145,7 +146,7 @@ public class Commandtime extends EssentialsCommand
|
||||
}
|
||||
return worlds;
|
||||
}
|
||||
|
||||
|
||||
// Try to find the world with name = selector
|
||||
World world = server.getWorld(selector);
|
||||
if (world != null)
|
||||
@ -160,16 +161,16 @@ public class Commandtime extends EssentialsCommand
|
||||
// We failed to understand the world target...
|
||||
else
|
||||
{
|
||||
throw new Exception("Could not find the world(s) \""+selector+"\"");
|
||||
throw new Exception("Could not find the world(s) \"" + selector + "\"");
|
||||
}
|
||||
|
||||
|
||||
return worlds;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
class WorldNameComparator implements Comparator<World> {
|
||||
class WorldNameComparator implements Comparator<World>
|
||||
{
|
||||
public int compare(World a, World b)
|
||||
{
|
||||
return a.getName().compareTo(b.getName());
|
||||
|
@ -206,6 +206,10 @@ commands:
|
||||
description: Assigns a command to the item in hand, {player} will be replaced by the name of the player that you click.
|
||||
usage: /<command> [command] <arguments>
|
||||
aliases: [pt,epowertool,ept]
|
||||
ptime:
|
||||
description: Set or reset the playertime for yourself or the player(s) specified.
|
||||
usage: /<command> [reset|day|night|dawn|17:30|4pm|4000ticks] <playername|all>
|
||||
aliases: [eptime]
|
||||
r:
|
||||
description: Quickly reply to the last player to message you.
|
||||
usage: /<command> [message]
|
||||
@ -271,8 +275,8 @@ commands:
|
||||
usage: /<command> <true/false> [duration]
|
||||
aliases: [ethunder]
|
||||
time:
|
||||
description: Change the time to day or night of the player (default) or world (essentials.time.world permission).
|
||||
usage: /<command> [day|night|reset] <playername>
|
||||
description: Change the time in the world where you are (default) or in the world(s) specified
|
||||
usage: /<command> [day|night|dawn|17:30|4pm|4000ticks] <worldname|all>
|
||||
aliases: [etime, day, night, playertime]
|
||||
togglejail:
|
||||
description: Prevents a player from interacting with the world and teleports him/her to the the jail specified
|
||||
|
Loading…
Reference in New Issue
Block a user