2014-07-09 04:55:51 +02:00
package mineplex.hub.commands ;
2014-07-10 02:38:11 +02:00
import java.util.HashMap ;
import java.util.Iterator ;
2014-07-09 04:55:51 +02:00
import org.bukkit.entity.Player ;
import mineplex.core.command.CommandBase ;
import mineplex.core.common.Rank ;
import mineplex.core.common.util.C ;
2014-07-10 02:38:11 +02:00
import mineplex.core.common.util.Callback ;
2014-07-09 04:55:51 +02:00
import mineplex.core.common.util.F ;
import mineplex.core.common.util.UtilPlayer ;
2014-07-16 04:31:53 +02:00
import mineplex.core.common.util.UtilServer ;
2014-07-09 04:55:51 +02:00
import mineplex.hub.HubManager ;
import mineplex.hub.modules.NewsManager ;
public class NewsCommand extends CommandBase < HubManager >
2014-07-11 03:10:35 +02:00
{
2014-07-09 04:55:51 +02:00
public NewsCommand ( HubManager plugin )
{
super ( plugin , Rank . ADMIN , " news " ) ;
}
@Override
2014-07-10 02:38:11 +02:00
public void Execute ( final Player caller , final String [ ] args )
2014-07-09 04:55:51 +02:00
{
2014-07-11 03:10:35 +02:00
final NewsManager newsMang = Plugin . GetNewsManager ( ) ;
2014-07-09 04:55:51 +02:00
2014-07-10 02:38:11 +02:00
if ( args = = null | | args . length = = 0 )
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " Your arguments are inappropriate for this command! " ) ) ;
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cGray + " Available news arguments for this command: " ) ) ;
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cWhite + " news list " + C . cGray + " - Lists (numbered) stored news messages from database. " ) ) ;
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cWhite + " news add <newsEntry> " + C . cGray + " - Adds specified news entry string to database at end of table. " ) ) ;
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cWhite + " news delete # " + C . cGray + " - Removes specified (numbered) news entry string from database. " ) ) ;
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cWhite + " news set # <newsEntry> " + C . cGray + " - Updates specified (numbered) news entry string in database. " ) ) ;
2014-07-16 04:31:53 +02:00
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cWhite + " *Please Note: " + C . cGray + " Updates to server news entries from the database are on a 4 minute refresh cycle! " ) ) ;
return ;
2014-07-10 02:38:11 +02:00
}
else if ( args . length = = 1 & & args [ 0 ] . equalsIgnoreCase ( " list " ) )
2014-07-09 04:55:51 +02:00
{
2014-07-11 03:10:35 +02:00
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cGray + " Current server news messages: " ) ) ;
2014-07-10 02:38:11 +02:00
newsMang . RetriveNewsEntries ( new Callback < HashMap < String , String > > ( )
{
2014-07-11 03:10:35 +02:00
public void run ( final HashMap < String , String > newsEntries )
2014-07-10 02:38:11 +02:00
{
2014-07-11 03:10:35 +02:00
// Order newsEntries set or its output by newsPosition, not hash order...
newsMang . RetrieveMaxNewsPosition ( new Callback < Integer > ( )
2014-07-10 02:38:11 +02:00
{
2014-07-11 03:10:35 +02:00
public void run ( Integer maxPosition )
{
String [ ] newsStrings = new String [ maxPosition ] ;
for ( Iterator < String > iterator = newsEntries . keySet ( ) . iterator ( ) ; iterator . hasNext ( ) ; )
{
String newsPosition = iterator . next ( ) ;
newsStrings [ Integer . parseInt ( newsPosition ) - 1 ] = newsEntries . get ( newsPosition ) ;
}
2014-07-10 02:38:11 +02:00
2014-07-11 03:10:35 +02:00
for ( int i = 0 ; i < newsStrings . length ; i + + )
{
2014-07-16 04:31:53 +02:00
UtilServer . getServer ( ) . dispatchCommand ( UtilServer . getServer ( ) . getConsoleSender ( ) , " tellraw " + caller . getName ( ) + " { \" text \" : \" " + Plugin . GetName ( ) + " > \" , color:blue, \" extra \" :[{ \" text \" : \" [DELETE] \" , color:red, \" clickEvent \" :{ \" action \" : \" run_command \" , \" value \" : \" /news ¢¤₦₣¡₨₥ " + ( i + 1 ) + " \" }}, { \" text \" : \" News " + ( i + 1 ) + " \" , color:gold}, { \" text \" : \" : \" , color:gray}, { \" text \" : \" " + newsStrings [ i ] + " \" , color:white}]} " ) ;
2014-07-11 03:10:35 +02:00
}
}
} ) ;
2014-07-10 02:38:11 +02:00
}
} ) ;
return ;
2014-07-09 04:55:51 +02:00
}
2014-07-11 03:10:35 +02:00
else if ( args . length > = 2 & & args . length < = 128 )
2014-07-09 04:55:51 +02:00
{
2014-07-16 04:31:53 +02:00
if ( args [ 0 ] . equals ( " ¢¤₦₣¡₨₥ " ) & & args . length = = 2 )
{
int newsPosition ;
try
{
newsPosition = Integer . parseInt ( args [ 1 ] ) ;
}
catch ( Exception exception )
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " The specified news position is invalid! " ) ) ;
return ;
}
UtilServer . getServer ( ) . dispatchCommand ( UtilServer . getServer ( ) . getConsoleSender ( ) , " tellraw " + caller . getName ( ) + " { \" text \" : \" " + Plugin . GetName ( ) + " > \" , color:blue, \" extra \" :[{ \" text \" : \" [CONFIRM] \" , color:green, \" clickEvent \" :{ \" action \" : \" run_command \" , \" value \" : \" /news delete " + newsPosition + " \" }}, { \" text \" : \" News Entry " + newsPosition + " \" , color:gold}, { \" text \" : \" deletion? \" , color:gray}]} " ) ;
return ;
}
else if ( args [ 0 ] . equalsIgnoreCase ( " delete " ) & & args . length = = 2 )
2014-07-09 04:55:51 +02:00
{
2014-07-11 03:10:35 +02:00
final int newsPosition ;
try
{
newsPosition = Integer . parseInt ( args [ 1 ] ) ;
}
catch ( Exception exception )
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " The specified news position is invalid! " ) ) ;
return ;
}
newsMang . DeleteNewsEntry ( newsPosition , new Callback < Boolean > ( )
{
public void run ( Boolean success )
{
if ( success )
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cGray + " The news entry at position " + C . cGold + newsPosition + C . cGray + " has been deleted! " ) ) ;
}
else
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " There was an error deleting the news entry; likely the specified news position was invalid! " ) ) ;
}
}
} ) ;
2014-07-10 02:38:11 +02:00
return ;
2014-07-09 04:55:51 +02:00
}
2014-07-11 03:10:35 +02:00
else if ( args [ 0 ] . equalsIgnoreCase ( " add " ) )
2014-07-09 04:55:51 +02:00
{
2014-07-11 03:10:35 +02:00
String newsEntry = " " ;
for ( int i = 1 ; i < args . length ; i + + )
{
newsEntry + = args [ i ] + " " ;
}
newsEntry = newsEntry . substring ( 0 , newsEntry . length ( ) - 1 ) ;
// Check for 256 character length for MySQL!
if ( newsEntry . length ( ) > 256 )
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " The specified news entry is too long [> 256 characters]! " ) ) ;
return ;
}
newsMang . AddNewsEntry ( newsEntry , new Callback < Boolean > ( )
{
public void run ( Boolean success )
{
if ( success )
{
String newsEntry = " " ;
for ( int i = 1 ; i < args . length ; i + + )
{
newsEntry + = args [ i ] + " " ;
}
newsEntry = newsEntry . substring ( 0 , newsEntry . length ( ) - 1 ) ;
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cGray + " The news entry: " + C . cGold + newsEntry + C . cGray + " has been added to the database! " ) ) ;
}
else
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " There was an error adding the news entry to the database! " ) ) ;
}
}
} ) ;
2014-07-10 02:38:11 +02:00
return ;
2014-07-11 03:10:35 +02:00
}
else if ( args [ 0 ] . equalsIgnoreCase ( " set " ) )
2014-07-10 02:38:11 +02:00
{
final int newsPosition ;
String newsEntry = " " ;
for ( int i = 2 ; i < args . length ; i + + )
{
newsEntry + = args [ i ] + " " ;
}
newsEntry = newsEntry . substring ( 0 , newsEntry . length ( ) - 1 ) ;
2014-07-11 03:10:35 +02:00
// Check for 256 character length for MySQL!
if ( newsEntry . length ( ) > 256 )
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " The specified news entry is too long [> 256 characters]! " ) ) ;
return ;
}
2014-07-10 02:38:11 +02:00
try
{
newsPosition = Integer . parseInt ( args [ 1 ] ) ;
}
catch ( Exception exception )
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " The specified news position is invalid! " ) ) ;
return ;
}
newsMang . SetNewsEntry ( newsEntry , newsPosition , new Callback < Boolean > ( )
{
public void run ( Boolean success )
{
if ( success )
{
String newsEntry = " " ;
for ( int i = 2 ; i < args . length ; i + + )
{
newsEntry + = args [ i ] + " " ;
}
newsEntry = newsEntry . substring ( 0 , newsEntry . length ( ) - 1 ) ;
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cGray + " The news entry at position " + C . cGold + newsPosition + C . cGray + " has been updated to: " + C . cGold + newsEntry + C . cGray + " ! " ) ) ;
}
else
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " There was an error updating the news entry; likely the specified news position was invalid! " ) ) ;
}
}
} ) ;
return ;
2014-07-09 04:55:51 +02:00
}
2014-07-11 03:10:35 +02:00
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " Your arguments are inappropriate for this command! " ) ) ;
return ;
2014-07-09 04:55:51 +02:00
}
else
{
UtilPlayer . message ( caller , F . main ( Plugin . GetName ( ) , C . cRed + " Your arguments are inappropriate for this command! " ) ) ;
return ;
}
}
}