2011-03-19 22:39:51 +00:00
package com.earth2me.essentials.commands ;
2013-10-16 19:59:39 +00:00
import com.earth2me.essentials.CommandSource ;
2013-06-09 20:35:51 +00:00
import com.earth2me.essentials.utils.DescParseTickFormat ;
2013-06-08 21:31:19 +00:00
import com.earth2me.essentials.utils.NumberUtil ;
2017-06-11 00:17:43 +00:00
import com.google.common.collect.Lists ;
2011-11-18 17:42:26 +00:00
import org.bukkit.Server ;
import org.bukkit.World ;
2011-03-19 22:39:51 +00:00
2020-09-09 15:00:13 +00:00
import java.util.Arrays ;
import java.util.Collection ;
import java.util.Collections ;
import java.util.Comparator ;
import java.util.Iterator ;
import java.util.List ;
import java.util.Locale ;
import java.util.Set ;
import java.util.StringJoiner ;
import java.util.TreeSet ;
2015-04-15 04:06:16 +00:00
import static com.earth2me.essentials.I18n.tl ;
2011-03-19 22:39:51 +00:00
2015-04-15 04:06:16 +00:00
public class Commandtime extends EssentialsCommand {
2020-08-11 18:09:22 +00:00
private final List < String > subCommands = Arrays . asList ( " add " , " set " ) ;
private final List < String > timeNames = Arrays . asList ( " sunrise " , " day " , " morning " , " noon " , " afternoon " , " sunset " , " night " , " midnight " ) ;
private final List < String > timeNumbers = Arrays . asList ( " 1000 " , " 2000 " , " 3000 " , " 4000 " , " 5000 " ) ;
2015-04-15 04:06:16 +00:00
public Commandtime ( ) {
super ( " time " ) ;
}
@Override
public void run ( final Server server , final CommandSource sender , final String commandLabel , final String [ ] args ) throws Exception {
2020-08-11 18:09:22 +00:00
long timeTick ;
Set < World > worlds ;
2015-04-15 04:06:16 +00:00
boolean add = false ;
2020-08-11 18:09:22 +00:00
if ( args . length = = 0 ) {
worlds = getWorlds ( server , sender , null ) ;
if ( commandLabel . endsWith ( " day " ) | | commandLabel . endsWith ( " night " ) ) {
timeTick = DescParseTickFormat . parse ( commandLabel . toLowerCase ( Locale . ENGLISH ) . replace ( " e " , " " ) ) ; // These are 100% safe things to parse, no need for catching
2015-04-15 04:06:16 +00:00
} else {
getWorldsTime ( sender , worlds ) ;
2020-08-11 18:09:22 +00:00
return ;
}
} else if ( args . length = = 1 ) {
worlds = getWorlds ( server , sender , null ) ;
try {
timeTick = DescParseTickFormat . parse ( NumberUtil . isInt ( args [ 0 ] ) ? ( args [ 0 ] + " t " ) : args [ 0 ] ) ;
} catch ( NumberFormatException e ) {
throw new NotEnoughArgumentsException ( e ) ;
2015-04-15 04:06:16 +00:00
}
} else {
2020-08-11 18:09:22 +00:00
if ( args [ 0 ] . equalsIgnoreCase ( " set " ) | | args [ 0 ] . equalsIgnoreCase ( " add " ) ) {
try {
add = args [ 0 ] . equalsIgnoreCase ( " add " ) ;
timeTick = DescParseTickFormat . parse ( NumberUtil . isInt ( args [ 1 ] ) ? ( args [ 1 ] + " t " ) : args [ 1 ] ) ;
worlds = getWorlds ( server , sender , args . length > 2 ? args [ 2 ] : null ) ;
} catch ( NumberFormatException e ) {
throw new NotEnoughArgumentsException ( e ) ;
}
} else {
try {
timeTick = DescParseTickFormat . parse ( NumberUtil . isInt ( args [ 0 ] ) ? ( args [ 0 ] + " t " ) : args [ 0 ] ) ;
worlds = getWorlds ( server , sender , args [ 1 ] ) ;
} catch ( NumberFormatException e ) {
throw new NotEnoughArgumentsException ( e ) ;
}
}
2015-04-15 04:06:16 +00:00
}
2020-08-11 18:09:22 +00:00
// Start updating world times, we have what we need
2020-09-09 15:00:13 +00:00
if ( ! sender . isAuthorized ( " essentials.time.set " , ess ) ) {
2020-08-22 19:07:21 +00:00
throw new Exception ( tl ( " timeSetPermission " ) ) ;
}
2016-01-25 01:17:47 +00:00
for ( World world : worlds ) {
2020-09-09 15:00:13 +00:00
if ( ! canUpdateWorld ( sender , world ) ) {
//We can ensure that this is User as the console has all permissions (for essentials commands).
throw new Exception ( tl ( " timeSetWorldPermission " , sender . getUser ( ess ) . getBase ( ) . getWorld ( ) . getName ( ) ) ) ;
2016-01-25 01:17:47 +00:00
}
}
2020-08-11 18:09:22 +00:00
final StringJoiner joiner = new StringJoiner ( " , " ) ;
2015-04-15 04:06:16 +00:00
for ( World world : worlds ) {
long time = world . getTime ( ) ;
if ( ! add ) {
time - = time % 24000 ;
}
2020-08-11 18:09:22 +00:00
world . setTime ( time + ( add ? 0 : 24000 ) + timeTick ) ;
joiner . add ( world . getName ( ) ) ;
2015-04-15 04:06:16 +00:00
}
2020-08-11 18:09:22 +00:00
sender . sendMessage ( tl ( add ? " timeWorldAdd " : " timeWorldSet " , DescParseTickFormat . formatTicks ( timeTick ) , joiner . toString ( ) ) ) ;
}
2015-04-15 04:06:16 +00:00
2020-08-11 18:09:22 +00:00
private void getWorldsTime ( final CommandSource sender , final Collection < World > worlds ) {
if ( worlds . size ( ) = = 1 ) {
final Iterator < World > iter = worlds . iterator ( ) ;
sender . sendMessage ( DescParseTickFormat . format ( iter . next ( ) . getTime ( ) ) ) ;
return ;
2015-04-15 04:06:16 +00:00
}
2020-08-11 18:09:22 +00:00
for ( World world : worlds ) {
sender . sendMessage ( tl ( " timeWorldCurrent " , world . getName ( ) , DescParseTickFormat . format ( world . getTime ( ) ) ) ) ;
}
2015-04-15 04:06:16 +00:00
}
/ * *
2020-08-11 18:09:22 +00:00
* Parses worlds from command args , otherwise returns all worlds .
2015-04-15 04:06:16 +00:00
* /
private Set < World > getWorlds ( final Server server , final CommandSource sender , final String selector ) throws Exception {
2015-06-03 20:11:56 +00:00
final Set < World > worlds = new TreeSet < > ( new WorldNameComparator ( ) ) ;
2015-04-15 04:06:16 +00:00
// 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 ) {
if ( sender . isPlayer ( ) ) {
2020-08-11 18:09:22 +00:00
worlds . add ( sender . getPlayer ( ) . getWorld ( ) ) ;
2015-04-15 04:06:16 +00:00
} else {
worlds . addAll ( server . getWorlds ( ) ) ;
}
return worlds ;
}
// Try to find the world with name = selector
final World world = server . getWorld ( selector ) ;
if ( world ! = null ) {
worlds . add ( world ) ;
2020-08-11 18:09:22 +00:00
} else if ( selector . equalsIgnoreCase ( " * " ) | | selector . equalsIgnoreCase ( " all " ) ) { // If that fails, Is the argument something like "*" or "all"?
2016-01-25 01:17:47 +00:00
worlds . addAll ( server . getWorlds ( ) ) ;
2020-08-11 18:09:22 +00:00
} else { // We failed to understand the world target...
2015-04-15 04:06:16 +00:00
throw new Exception ( tl ( " invalidWorld " ) ) ;
}
return worlds ;
}
2016-01-14 18:16:36 +00:00
2020-09-09 15:00:13 +00:00
private boolean canUpdateAll ( CommandSource sender ) {
return ! ess . getSettings ( ) . isWorldTimePermissions ( ) // First check if per world permissions are enabled, if not, return true.
| | sender . isAuthorized ( " essentials.time.world.all " , ess ) ;
2016-01-14 18:16:36 +00:00
}
2020-09-09 15:00:13 +00:00
private boolean canUpdateWorld ( CommandSource sender , World world ) {
return canUpdateAll ( sender ) | | sender . isAuthorized ( " essentials.time.world. " + normalizeWorldName ( world ) , ess ) ;
2016-01-14 18:16:36 +00:00
}
2016-01-14 12:32:48 +00:00
private String normalizeWorldName ( World world ) {
2016-01-14 18:16:36 +00:00
return world . getName ( ) . toLowerCase ( ) . replaceAll ( " \\ s+ " , " _ " ) ;
2016-01-14 12:32:48 +00:00
}
2017-06-11 00:17:43 +00:00
@Override
protected List < String > getTabCompleteOptions ( Server server , CommandSource sender , String commandLabel , String [ ] args ) {
if ( args . length = = 1 ) {
2020-08-11 18:09:22 +00:00
if ( sender . isAuthorized ( " essentials.time.set " , ess ) ) {
return subCommands ;
} else {
return Collections . emptyList ( ) ;
}
} else if ( args . length = = 2 ) {
if ( args [ 0 ] . equalsIgnoreCase ( " set " ) ) {
return timeNames ;
} else if ( args [ 0 ] . equalsIgnoreCase ( " add " ) ) {
return timeNumbers ;
2017-06-11 00:17:43 +00:00
} else {
return Collections . emptyList ( ) ;
}
} else if ( args . length = = 3 & & ( args [ 0 ] . equalsIgnoreCase ( " set " ) | | args [ 0 ] . equalsIgnoreCase ( " add " ) ) ) {
List < String > worlds = Lists . newArrayList ( ) ;
for ( World world : server . getWorlds ( ) ) {
2020-08-11 18:09:22 +00:00
if ( sender . isAuthorized ( " essentials.time.world. " + normalizeWorldName ( world ) , ess ) ) {
2017-06-11 00:17:43 +00:00
worlds . add ( world . getName ( ) ) ;
}
}
2020-08-11 18:09:22 +00:00
if ( sender . isAuthorized ( " essentials.time.world.all " , ess ) ) {
2017-06-11 00:17:43 +00:00
worlds . add ( " * " ) ;
}
return worlds ;
} else {
return Collections . emptyList ( ) ;
}
}
2011-08-08 15:00:04 +00:00
}
2015-04-15 04:06:16 +00:00
class WorldNameComparator implements Comparator < World > {
@Override
public int compare ( final World a , final World b ) {
return a . getName ( ) . compareTo ( b . getName ( ) ) ;
}
2014-04-24 09:24:35 +00:00
}