2016-03-02 19:28:01 +00:00
package me.totalfreedom.totalfreedommod.command ;
2013-09-15 02:00:11 +00:00
import java.util.List ;
2016-03-06 15:56:15 +00:00
import me.totalfreedom.totalfreedommod.rank.Rank ;
2013-09-15 02:00:11 +00:00
import org.bukkit.Location ;
import org.bukkit.Material ;
import org.bukkit.World ;
import org.bukkit.block.Block ;
2018-07-25 02:08:29 +00:00
import org.bukkit.block.BlockState ;
2020-12-25 19:46:43 +00:00
import org.bukkit.block.data.BlockData ;
import org.bukkit.block.data.type.Switch ;
2013-09-15 02:00:11 +00:00
import org.bukkit.command.Command ;
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
2016-03-06 15:56:15 +00:00
@CommandPermissions ( level = Rank . NON_OP , source = SourceType . BOTH )
2013-09-15 02:00:11 +00:00
@CommandParameters ( description = " Set the on/off state of the lever at position x, y, z in world 'worldname'. " , usage = " /<command> <x> <y> <z> <worldname> <on|off> " )
2015-10-19 17:43:46 +00:00
public class Command_setlever extends FreedomCommand
2013-09-15 02:00:11 +00:00
{
2015-11-22 18:26:47 +00:00
2013-09-15 02:00:11 +00:00
@Override
2015-11-22 18:26:47 +00:00
public boolean run ( CommandSender sender , Player playerSender , Command cmd , String commandLabel , String [ ] args , boolean senderIsConsole )
2013-09-15 02:00:11 +00:00
{
if ( args . length ! = 5 )
{
return false ;
}
double x , y , z ;
try
{
x = Double . parseDouble ( args [ 0 ] ) ;
y = Double . parseDouble ( args [ 1 ] ) ;
z = Double . parseDouble ( args [ 2 ] ) ;
}
catch ( NumberFormatException ex )
{
2016-03-02 19:28:01 +00:00
msg ( " Invalid coordinates. " ) ;
2013-09-15 02:00:11 +00:00
return true ;
}
World world = null ;
final String needleWorldName = args [ 3 ] . trim ( ) ;
final List < World > worlds = server . getWorlds ( ) ;
for ( final World testWorld : worlds )
{
if ( testWorld . getName ( ) . trim ( ) . equalsIgnoreCase ( needleWorldName ) )
{
world = testWorld ;
break ;
}
}
if ( world = = null )
{
2016-03-02 19:28:01 +00:00
msg ( " Invalid world name. " ) ;
2013-09-15 02:00:11 +00:00
return true ;
}
final Location leverLocation = new Location ( world , x , y , z ) ;
2014-07-20 01:02:00 +00:00
final boolean leverOn = ( args [ 4 ] . trim ( ) . equalsIgnoreCase ( " on " ) | | args [ 4 ] . trim ( ) . equalsIgnoreCase ( " 1 " ) ) ;
2013-09-15 02:00:11 +00:00
final Block targetBlock = leverLocation . getBlock ( ) ;
if ( targetBlock . getType ( ) = = Material . LEVER )
{
2018-07-25 02:08:29 +00:00
BlockState state = targetBlock . getState ( ) ;
2020-12-25 19:46:43 +00:00
BlockData data = state . getBlockData ( ) ;
Switch caster = ( Switch ) data ;
caster . setPowered ( leverOn ) ;
state . setBlockData ( data ) ;
2018-07-25 02:08:29 +00:00
state . update ( ) ;
2013-09-15 02:00:11 +00:00
}
else
{
2016-03-02 19:28:01 +00:00
msg ( " Target block " + targetBlock + " is not a lever. " ) ;
2013-09-15 02:00:11 +00:00
return true ;
}
return true ;
}
}