2013-12-16 21:21:32 +01:00
package com.graywolf336.jail.command.commands ;
2013-12-19 18:13:07 +01:00
import java.util.concurrent.TimeUnit ;
import org.bukkit.ChatColor ;
2013-12-16 21:21:32 +01:00
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
2013-12-19 00:26:39 +01:00
import com.beust.jcommander.JCommander ;
import com.beust.jcommander.ParameterException ;
2013-12-16 21:21:32 +01:00
import com.graywolf336.jail.JailManager ;
2013-12-24 19:28:40 +01:00
import com.graywolf336.jail.Util ;
2013-12-19 23:13:41 +01:00
import com.graywolf336.jail.beans.Jail ;
2013-12-19 18:13:07 +01:00
import com.graywolf336.jail.beans.Prisoner ;
2013-12-16 21:21:32 +01:00
import com.graywolf336.jail.command.Command ;
import com.graywolf336.jail.command.CommandInfo ;
2013-12-19 00:26:39 +01:00
import com.graywolf336.jail.command.parameters.JailParameters ;
2013-12-24 17:23:39 +01:00
import com.graywolf336.jail.enums.LangString ;
2013-12-24 19:28:40 +01:00
import com.graywolf336.jail.enums.Settings ;
2013-12-19 23:13:41 +01:00
import com.graywolf336.jail.events.PrisonerJailedEvent ;
2013-12-16 21:21:32 +01:00
@CommandInfo (
maxArgs = - 1 ,
minimumArgs = 1 ,
needsPlayer = false ,
pattern = " jail|j " ,
permission = " jail.command.jail " ,
2013-12-19 00:26:39 +01:00
usage = " /jail [-p name] (-t time) (-j JailName) (-c CellName) (-m Muted) (-r A reason for jailing) "
2013-12-16 21:21:32 +01:00
)
public class JailCommand implements Command {
2013-12-19 23:13:41 +01:00
/ *
* Executes the command . Checks the following :
*
* - If there are any jails .
* - If the command can be parsed correctly .
* - If the specified jail is null ( TODO : if no jail given then find one close to them )
2013-12-25 00:51:41 +01:00
* - If the given time can be parsed correctly , defaults to what is defined in the config
2013-12-19 23:13:41 +01:00
* - If the prisoner is online or not .
* /
2013-12-16 21:21:32 +01:00
public boolean execute ( JailManager jm , CommandSender sender , String . . . args ) {
2013-12-19 18:13:07 +01:00
if ( jm . getJails ( ) . size ( ) = = 0 ) {
sender . sendMessage ( ChatColor . RED + " No jails found. " ) ;
return true ;
}
2013-12-19 00:26:39 +01:00
JailParameters params = new JailParameters ( ) ;
2013-12-16 21:32:06 +01:00
2013-12-19 00:26:39 +01:00
try {
new JCommander ( params , args ) ;
} catch ( ParameterException e ) {
2013-12-19 18:13:07 +01:00
sender . sendMessage ( ChatColor . RED + e . getMessage ( ) ) ;
2013-12-19 16:19:55 +01:00
return true ;
2013-12-16 21:32:06 +01:00
}
2013-12-25 00:51:41 +01:00
//Check the jail params. If it is empty, let's get the default jail
//from the config. If that is nearest, let's make a call to getting the nearest jail to
//the sender but otherwise if it isn't nearest then let's set it to the default jail
2013-12-25 05:25:14 +01:00
//which is defined in the config. After that is done, we set the name of it in the params
//so that we can keep consistency.
2013-12-25 00:51:41 +01:00
if ( params . jail ( ) . isEmpty ( ) ) {
String dJail = jm . getPlugin ( ) . getConfig ( ) . getString ( Settings . DEFAULTJAIL . getPath ( ) ) ;
if ( dJail . equalsIgnoreCase ( " nearest " ) ) {
params . setJail ( jm . getNearestJail ( sender ) . getName ( ) ) ;
} else {
params . setJail ( jm . getPlugin ( ) . getConfig ( ) . getString ( Settings . DEFAULTJAIL . getPath ( ) ) ) ;
}
} else if ( jm . getJail ( params . jail ( ) ) = = null ) {
2013-12-19 23:13:41 +01:00
sender . sendMessage ( ChatColor . RED + " No jail found by the name of ' " + params . jail ( ) + " '. " ) ;
return true ;
}
2013-12-25 05:25:14 +01:00
//Try to parse the time, if they give us nothing in the time parameter then we get the default time
//from the config and if that isn't there then we default to thirty minutes.
2013-12-19 18:32:04 +01:00
Long time = 10L ;
try {
2013-12-25 02:42:12 +01:00
if ( params . time ( ) . isEmpty ( ) ) {
time = Util . getTime ( jm . getPlugin ( ) . getConfig ( ) . getString ( Settings . JAILDEFAULTTIME . getPath ( ) , " 30m " ) ) ;
2013-12-25 05:25:14 +01:00
} else if ( Integer . valueOf ( params . time ( ) ) = = - 1 ) {
time = - 1L ;
2013-12-25 02:42:12 +01:00
} else {
time = Util . getTime ( params . time ( ) ) ;
2013-12-24 19:28:40 +01:00
}
2013-12-25 02:42:12 +01:00
} catch ( Exception e ) {
sender . sendMessage ( ChatColor . RED + " Number format is incorrect. " ) ;
return true ;
2013-12-19 18:32:04 +01:00
}
2013-12-25 05:25:14 +01:00
//Get the jail instance from the name of jail in the params.
2013-12-19 23:13:41 +01:00
Jail j = jm . getJail ( params . jail ( ) ) ;
2013-12-25 00:51:41 +01:00
Prisoner pris = new Prisoner ( params . player ( ) , params . muted ( ) , time ) ;
2013-12-19 23:13:41 +01:00
Player p = jm . getPlugin ( ) . getServer ( ) . getPlayer ( params . player ( ) ) ;
//call the event
PrisonerJailedEvent event = new PrisonerJailedEvent ( j , pris , p , p = = null , sender . getName ( ) ) ;
jm . getPlugin ( ) . getServer ( ) . getPluginManager ( ) . callEvent ( event ) ;
//check if the event is cancelled
if ( event . isCancelled ( ) ) {
if ( event . getCancelledMessage ( ) . isEmpty ( ) )
sender . sendMessage ( ChatColor . RED + " Jailing " + params . player ( ) + " was cancelled by another plugin and they didn't leave you a message. " ) ;
else
sender . sendMessage ( event . getCancelledMessage ( ) ) ;
return true ;
}
//recall data from the event
j = event . getJail ( ) ;
pris = event . getPrisoner ( ) ;
p = event . getPlayer ( ) ;
String jailer = event . getJailer ( ) ;
2013-12-16 21:21:32 +01:00
//Player is not online
if ( p = = null ) {
2013-12-19 23:13:41 +01:00
sender . sendMessage ( pris . getName ( ) + " is offline and will be jailed for " + pris . getRemainingTime ( ) + " milliseconds in the jail " + params . jail ( ) + " in the cell " + params . cell ( ) + " and will be muted: " + pris . isMuted ( ) + " . " ) ;
sender . sendMessage ( pris . getName ( ) + " will be jailed for " + TimeUnit . MINUTES . convert ( pris . getRemainingTime ( ) , TimeUnit . MILLISECONDS ) + " minutes by " + jailer + " . " ) ;
2013-12-16 21:21:32 +01:00
} else {
//Player *is* online
2013-12-19 23:13:41 +01:00
sender . sendMessage ( pris . getName ( ) + " is online and will be jailed for " + pris . getRemainingTime ( ) + " milliseconds in the jail " + params . jail ( ) + " in the cell " + params . cell ( ) + " and will be muted: " + pris . isMuted ( ) + " . " ) ;
sender . sendMessage ( pris . getName ( ) + " will be jailed for " + TimeUnit . MINUTES . convert ( pris . getRemainingTime ( ) , TimeUnit . MILLISECONDS ) + " minutes by " + jailer + " . " ) ;
2013-12-24 17:23:39 +01:00
if ( params . reason ( ) . isEmpty ( ) ) {
p . sendMessage ( jm . getPlugin ( ) . getJailIO ( ) . getLanguageString ( LangString . JAILED ) ) ;
} else {
p . sendMessage ( jm . getPlugin ( ) . getJailIO ( ) . getLanguageString ( LangString . JAILEDWITHREASON , new String [ ] { params . reason ( ) } ) ) ;
}
2013-12-16 21:21:32 +01:00
}
return true ;
}
}