2014-09-26 22:53:42 +02:00
package com.intellectualcrafters.plot.commands ;
2014-10-03 12:36:30 +10:00
import java.io.IOException ;
import java.util.Arrays ;
import java.util.HashMap ;
import java.util.Map ;
2014-10-02 14:35:20 +10:00
2014-10-02 18:21:34 +10:00
import org.apache.commons.lang.StringUtils ;
import org.bukkit.block.Biome ;
2014-09-26 22:53:42 +02:00
import org.bukkit.entity.Player ;
import org.bukkit.event.Listener ;
2014-10-02 18:21:34 +10:00
2014-10-03 12:36:30 +10:00
import com.intellectualcrafters.plot.C ;
import com.intellectualcrafters.plot.PlayerFunctions ;
import com.intellectualcrafters.plot.PlotMain ;
import com.intellectualcrafters.plot.PlotWorld ;
2014-09-26 22:53:42 +02:00
/ * *
* Created by Citymonstret on 2014 - 09 - 26 .
* /
public class Setup extends SubCommand implements Listener {
public static Map < String , SetupObject > setupMap = new HashMap < > ( ) ;
private static class SetupStep {
private String constant ;
private Object default_value ;
private String description ;
private Object value = 0 ;
2014-10-02 18:21:34 +10:00
private String type ;
2014-10-03 12:36:30 +10:00
2014-10-02 18:21:34 +10:00
public SetupStep ( String constant , Object default_value , String description , String type , boolean require_previous ) {
2014-09-26 22:53:42 +02:00
this . constant = constant ;
this . default_value = default_value ;
this . description = description ;
2014-09-28 10:21:18 +02:00
this . type = type ;
2014-10-02 18:21:34 +10:00
}
2014-10-03 12:36:30 +10:00
2014-10-02 18:21:34 +10:00
public String getType ( ) {
return this . type ;
2014-09-28 10:21:18 +02:00
}
2014-10-02 18:21:34 +10:00
public boolean setValue ( String string ) {
if ( ! validValue ( string ) ) {
return false ;
2014-10-02 14:35:20 +10:00
}
2014-10-02 18:21:34 +10:00
switch ( this . type ) {
2014-10-03 12:36:30 +10:00
case " integer " :
this . value = Integer . parseInt ( string ) ;
break ;
case " boolean " :
this . value = Boolean . parseBoolean ( string ) ;
break ;
case " double " :
this . value = Double . parseDouble ( string ) ;
break ;
case " float " :
this . value = Float . parseFloat ( string ) ;
break ;
case " biome " :
this . value = ( string . toUpperCase ( ) ) ;
break ;
case " block " :
this . value = string ;
break ;
case " blocklist " :
this . value = string . split ( " , " ) ;
break ;
case " string " :
this . value = string ;
break ;
2014-10-02 14:35:20 +10:00
}
2014-09-28 10:21:18 +02:00
return true ;
}
2014-10-03 12:36:30 +10:00
2014-09-28 10:21:18 +02:00
public boolean validValue ( String string ) {
2014-10-02 14:35:20 +10:00
try {
2014-10-02 18:21:34 +10:00
if ( this . type . equals ( " integer " ) ) {
2014-10-02 14:35:20 +10:00
Integer . parseInt ( string ) ;
return true ;
}
2014-10-02 18:21:34 +10:00
if ( this . type . equals ( " boolean " ) ) {
2014-10-02 14:35:20 +10:00
Boolean . parseBoolean ( string ) ;
return true ;
}
2014-10-02 18:21:34 +10:00
if ( this . type . equals ( " double " ) ) {
2014-10-02 14:35:20 +10:00
Double . parseDouble ( string ) ;
return true ;
}
2014-10-02 18:21:34 +10:00
if ( this . type . equals ( " float " ) ) {
2014-10-02 14:35:20 +10:00
Float . parseFloat ( string ) ;
return true ;
}
2014-10-02 18:21:34 +10:00
if ( this . type . equals ( " biome " ) ) {
Biome . valueOf ( string . toUpperCase ( ) ) ;
return true ;
}
if ( this . type . equals ( " block " ) ) {
if ( string . contains ( " : " ) ) {
String [ ] split = string . split ( " : " ) ;
Short . parseShort ( split [ 0 ] ) ;
Short . parseShort ( split [ 1 ] ) ;
2014-10-03 12:36:30 +10:00
} else {
2014-10-02 18:21:34 +10:00
Short . parseShort ( string ) ;
}
return true ;
}
if ( this . type . equals ( " blocklist " ) ) {
2014-10-03 12:36:30 +10:00
for ( String block : string . split ( " , " ) ) {
2014-10-02 18:21:34 +10:00
if ( block . contains ( " : " ) ) {
String [ ] split = block . split ( " : " ) ;
Short . parseShort ( split [ 0 ] ) ;
Short . parseShort ( split [ 1 ] ) ;
2014-10-03 12:36:30 +10:00
} else {
2014-10-02 18:21:34 +10:00
Short . parseShort ( block ) ;
}
}
return true ;
}
if ( this . type . equals ( " string " ) ) {
2014-10-02 14:35:20 +10:00
return true ;
}
2014-10-03 12:36:30 +10:00
} catch ( Exception e ) {
2014-10-02 14:35:20 +10:00
}
return false ;
2014-09-26 22:53:42 +02:00
}
public Object getValue ( ) {
2014-10-02 22:30:25 +10:00
if ( this . value instanceof String [ ] ) {
2014-10-03 12:36:30 +10:00
return Arrays . asList ( ( String [ ] ) this . value ) ;
2014-10-02 22:30:25 +10:00
}
2014-09-26 22:53:42 +02:00
return this . value ;
}
public String getConstant ( ) {
return this . constant ;
}
public Object getDefaultValue ( ) {
2014-10-02 22:30:25 +10:00
if ( this . default_value instanceof String [ ] ) {
2014-10-03 12:36:30 +10:00
return StringUtils . join ( ( String [ ] ) this . default_value , " , " ) ;
2014-10-02 22:30:25 +10:00
}
2014-09-26 22:53:42 +02:00
return this . default_value ;
}
public String getDescription ( ) {
return this . description ;
}
}
2014-09-28 10:21:18 +02:00
private class SetupObject {
String world ;
int current = 0 ;
2014-10-03 12:36:30 +10:00
SetupStep [ ] step = new SetupStep [ ] { new SetupStep ( " road.height " , PlotWorld . ROAD_HEIGHT_DEFAULT , " Height of road " , " integer " , false ) , new SetupStep ( " plot.height " , PlotWorld . PLOT_HEIGHT_DEFAULT , " Height of plot " , " integer " , false ) , new SetupStep ( " wall.height " , PlotWorld . WALL_HEIGHT_DEFAULT , " Height of wall " , " integer " , false ) , new SetupStep ( " plot.size " , PlotWorld . PLOT_WIDTH_DEFAULT , " Size of plot " , " integer " , false ) , new SetupStep ( " road.width " , PlotWorld . ROAD_WIDTH_DEFAULT , " Width of road " , " integer " , false ) , new SetupStep ( " plot.biome " , PlotWorld . PLOT_BIOME_DEFAULT , " Plot biome " , " biome " , false ) , new SetupStep ( " plot.filling " , PlotWorld . MAIN_BLOCK_DEFAULT , " Plot filling " , " blocklist " , false ) , new SetupStep ( " plot.floor " , PlotWorld . TOP_BLOCK_DEFAULT , " Plot floor " , " blocklist " , false ) , new SetupStep ( " wall.block " , PlotWorld . WALL_BLOCK_DEFAULT , " Wall block " , " block " , false ) , new SetupStep ( " wall.filling " , PlotWorld . WALL_FILLING_DEFAULT , " Wall filling " , " block " , false ) ,
new SetupStep ( " road.enable_stripes " , PlotWorld . ROAD_STRIPES_ENABLED_DEFAULT , " Enable road stripes " , " boolean " , false ) , new SetupStep ( " road.stripes " , PlotWorld . ROAD_STRIPES_DEFAULT , " Road stripes block " , " block " , true ) , new SetupStep ( " road.block " , PlotWorld . ROAD_BLOCK_DEFAULT , " Road block " , " block " , false ) , } ;
2014-09-26 22:53:42 +02:00
public SetupObject ( String world ) {
this . world = world ;
}
2014-09-28 10:21:18 +02:00
public int getCurrent ( ) {
return this . current ;
}
2014-09-26 22:53:42 +02:00
public int getMax ( ) {
return this . step . length ;
}
}
public Setup ( ) {
2014-10-05 13:58:35 +11:00
super ( " setup " , " plots.admin " , " Setup a PlotWorld " , " setup {world} " , " setup " , CommandCategory . ACTIONS ) ;
2014-09-26 22:53:42 +02:00
}
2014-10-03 12:36:30 +10:00
2014-09-26 22:53:42 +02:00
@Override
public boolean execute ( Player plr , String . . . args ) {
2014-10-03 12:36:30 +10:00
if ( setupMap . containsKey ( plr . getName ( ) ) ) {
2014-09-28 10:21:18 +02:00
SetupObject object = setupMap . get ( plr . getName ( ) ) ;
2014-10-03 12:36:30 +10:00
if ( object . getCurrent ( ) = = object . getMax ( ) ) {
2014-09-28 10:21:18 +02:00
sendMessage ( plr , C . SETUP_FINISHED , object . world ) ;
2014-10-03 12:36:30 +10:00
2014-10-02 18:21:34 +10:00
SetupStep [ ] steps = object . step ;
String world = object . world ;
2014-10-03 12:36:30 +10:00
for ( SetupStep step : steps ) {
PlotMain . config . set ( " worlds. " + world + " . " + step . constant , step . getValue ( ) ) ;
2014-10-02 18:21:34 +10:00
}
try {
PlotMain . config . save ( PlotMain . configFile ) ;
} catch ( IOException e ) {
e . printStackTrace ( ) ;
}
2014-10-03 12:36:30 +10:00
// World newWorld = WorldCreator.name(world).generator(new
// WorldGenerator(world)).createWorld();
// plr.teleport(newWorld.getSpawnLocation());
2014-10-02 18:21:34 +10:00
setupMap . remove ( plr . getName ( ) ) ;
2014-10-03 12:36:30 +10:00
2014-09-28 10:21:18 +02:00
return true ;
}
SetupStep step = object . step [ object . current ] ;
2014-10-03 12:36:30 +10:00
if ( args . length < 1 ) {
2014-10-02 18:21:34 +10:00
sendMessage ( plr , C . SETUP_STEP , object . current + 1 + " " , step . getDescription ( ) , step . getType ( ) , step . getDefaultValue ( ) + " " ) ;
2014-09-28 10:21:18 +02:00
return true ;
} else {
2014-10-02 18:21:34 +10:00
if ( args [ 0 ] . equalsIgnoreCase ( " cancel " ) ) {
setupMap . remove ( plr . getName ( ) ) ;
PlayerFunctions . sendMessage ( plr , " &cCancelled setup. " ) ;
return true ;
}
if ( args [ 0 ] . equalsIgnoreCase ( " back " ) ) {
2014-10-03 12:36:30 +10:00
if ( object . current > 0 ) {
2014-10-02 18:21:34 +10:00
object . current - - ;
step = object . step [ object . current ] ;
sendMessage ( plr , C . SETUP_STEP , object . current + 1 + " " , step . getDescription ( ) , step . getType ( ) , step . getDefaultValue ( ) + " " ) ;
return true ;
2014-10-03 12:36:30 +10:00
} else {
2014-10-02 18:21:34 +10:00
sendMessage ( plr , C . SETUP_STEP , object . current + 1 + " " , step . getDescription ( ) , step . getType ( ) , step . getDefaultValue ( ) + " " ) ;
return true ;
}
}
2014-09-28 10:21:18 +02:00
boolean valid = step . validValue ( args [ 0 ] ) ;
2014-10-03 12:36:30 +10:00
if ( valid ) {
2014-10-02 18:21:34 +10:00
sendMessage ( plr , C . SETUP_VALID_ARG , step . getConstant ( ) , args [ 0 ] ) ;
step . setValue ( args [ 0 ] ) ;
2014-09-28 10:21:18 +02:00
object . current + + ;
2014-10-03 12:36:30 +10:00
if ( object . getCurrent ( ) = = object . getMax ( ) ) {
2014-10-02 22:30:25 +10:00
execute ( plr , args ) ;
return true ;
}
2014-10-02 18:21:34 +10:00
step = object . step [ object . current ] ;
sendMessage ( plr , C . SETUP_STEP , object . current + 1 + " " , step . getDescription ( ) , step . getType ( ) , step . getDefaultValue ( ) + " " ) ;
2014-10-02 22:30:25 +10:00
return true ;
2014-09-28 10:21:18 +02:00
} else {
2014-10-02 18:21:34 +10:00
sendMessage ( plr , C . SETUP_INVALID_ARG , args [ 0 ] , step . getConstant ( ) ) ;
sendMessage ( plr , C . SETUP_STEP , object . current + 1 + " " , step . getDescription ( ) , step . getType ( ) , step . getDefaultValue ( ) + " " ) ;
2014-10-02 22:30:25 +10:00
return true ;
2014-09-28 10:21:18 +02:00
}
}
} else {
if ( args . length < 1 ) {
sendMessage ( plr , C . SETUP_MISSING_WORLD ) ;
return true ;
}
String world = args [ 0 ] ;
2014-10-02 18:21:34 +10:00
if ( StringUtils . isNumeric ( args [ 0 ] ) ) {
sendMessage ( plr , C . SETUP_WORLD_TAKEN , world ) ;
return true ;
}
2014-10-03 12:36:30 +10:00
if ( PlotMain . getWorldSettings ( world ) ! = null ) {
2014-09-28 10:21:18 +02:00
sendMessage ( plr , C . SETUP_WORLD_TAKEN , world ) ;
return true ;
}
setupMap . put ( plr . getName ( ) , new SetupObject ( world ) ) ;
sendMessage ( plr , C . SETUP_INIT ) ;
2014-10-02 18:21:34 +10:00
SetupObject object = setupMap . get ( plr . getName ( ) ) ;
SetupStep step = object . step [ object . current ] ;
sendMessage ( plr , C . SETUP_STEP , object . current + 1 + " " , step . getDescription ( ) , step . getType ( ) , step . getDefaultValue ( ) + " " ) ;
2014-09-26 22:53:42 +02:00
return true ;
}
}
}