2016-03-02 19:28:01 +00:00
package me.totalfreedom.totalfreedommod.command ;
2012-11-28 16:35:31 +00:00
2014-04-23 15:00:44 +00:00
import java.util.ArrayList ;
import java.util.List ;
2016-03-06 15:56:15 +00:00
import me.totalfreedom.totalfreedommod.rank.Rank ;
2015-10-19 17:43:46 +00:00
import me.totalfreedom.totalfreedommod.util.FUtil ;
2019-01-28 01:49:07 +00:00
import me.totalfreedom.totalfreedommod.util.Groups ;
2019-07-28 06:04:16 +00:00
import org.apache.commons.lang.StringUtils ;
2012-11-28 16:35:31 +00:00
import org.bukkit.ChatColor ;
2016-05-12 19:40:39 +00:00
import org.bukkit.Location ;
2012-11-28 16:35:31 +00:00
import org.bukkit.Material ;
2014-08-30 19:18:45 +00:00
import org.bukkit.World ;
2016-05-12 19:40:39 +00:00
import org.bukkit.block.Block ;
2019-11-29 16:35:03 +00:00
import org.bukkit.block.data.BlockData ;
import org.bukkit.block.data.Waterlogged ;
2012-11-28 16:35:31 +00:00
import org.bukkit.command.Command ;
import org.bukkit.command.CommandSender ;
import org.bukkit.entity.Player ;
2020-09-28 02:51:15 +00:00
@CommandPermissions ( level = Rank . NEW_ADMIN , source = SourceType . BOTH )
2013-04-10 02:05:24 +00:00
@CommandParameters ( description = " Remove all blocks of a certain type in the radius of certain players. " , usage = " /<command> <block> [radius (default=50)] [player] " )
2015-10-19 17:43:46 +00:00
public class Command_ro extends FreedomCommand
2012-11-28 16:35:31 +00:00
{
2015-11-22 18:26:47 +00:00
2012-11-28 16:35:31 +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 )
2012-11-28 16:35:31 +00:00
{
2012-12-01 20:11:00 +00:00
if ( args . length < 1 | | args . length > 3 )
2012-11-28 16:35:31 +00:00
{
2012-12-01 20:11:00 +00:00
return false ;
2012-11-28 16:35:31 +00:00
}
2016-03-02 19:28:01 +00:00
final List < Material > materials = new ArrayList < > ( ) ;
2018-07-25 02:08:29 +00:00
String names = null ;
if ( args [ 0 ] . equalsIgnoreCase ( " shulker_boxes " ) | | args [ 0 ] . equalsIgnoreCase ( " shulkers " ) )
{
2019-01-28 01:49:07 +00:00
materials . addAll ( Groups . SHULKER_BOXES ) ;
2018-07-25 02:08:29 +00:00
names = " shulker boxes " ;
}
2020-01-04 04:41:16 +00:00
else if ( args [ 0 ] . equalsIgnoreCase ( " banners " ) | | args [ 0 ] . equalsIgnoreCase ( " banner " ) )
{
materials . addAll ( Groups . BANNERS ) ;
names = " banners " ;
}
2018-07-25 02:08:29 +00:00
else
2014-04-23 15:00:44 +00:00
{
2018-07-25 02:08:29 +00:00
for ( String materialName : StringUtils . split ( args [ 0 ] , " , " ) )
2012-12-01 20:11:00 +00:00
{
2018-07-25 02:08:29 +00:00
Material fromMaterial = Material . matchMaterial ( materialName ) ;
if ( fromMaterial = = null | | fromMaterial = = Material . AIR | | ! fromMaterial . isBlock ( ) )
2014-04-23 15:00:44 +00:00
{
2018-07-25 02:08:29 +00:00
msg ( " Invalid material: " + materialName , ChatColor . RED ) ;
return true ;
2014-04-23 15:00:44 +00:00
}
2012-12-01 20:11:00 +00:00
2018-07-25 02:08:29 +00:00
materials . add ( fromMaterial ) ;
2012-11-28 16:35:31 +00:00
}
2012-12-01 20:11:00 +00:00
}
2020-07-29 03:14:58 +00:00
int radius = 50 ;
2012-12-01 20:11:00 +00:00
if ( args . length > = 2 )
{
try
2012-11-28 16:35:31 +00:00
{
2012-12-01 20:11:00 +00:00
radius = Math . max ( 1 , Math . min ( 50 , Integer . parseInt ( args [ 1 ] ) ) ) ;
}
2013-08-14 13:28:19 +00:00
catch ( NumberFormatException ex )
2012-12-01 20:11:00 +00:00
{
2016-03-02 19:28:01 +00:00
msg ( " Invalid radius: " + args [ 1 ] , ChatColor . RED ) ;
2012-11-28 16:35:31 +00:00
return true ;
}
}
2014-04-23 15:00:44 +00:00
final Player targetPlayer ;
2012-12-01 20:11:00 +00:00
if ( args . length = = 3 )
2012-11-28 16:35:31 +00:00
{
2014-04-26 11:55:24 +00:00
targetPlayer = getPlayer ( args [ 2 ] ) ;
if ( targetPlayer = = null )
2012-11-28 16:35:31 +00:00
{
2016-03-02 19:28:01 +00:00
msg ( FreedomCommand . PLAYER_NOT_FOUND ) ;
2012-11-28 16:35:31 +00:00
return true ;
}
}
2014-04-23 15:00:44 +00:00
else
{
targetPlayer = null ;
}
2012-11-28 16:35:31 +00:00
2018-07-25 02:08:29 +00:00
if ( names = = null )
{
names = StringUtils . join ( materials , " , " ) ;
}
2012-12-01 20:50:33 +00:00
2020-08-15 16:35:48 +00:00
World staffWorld = null ;
2014-08-30 19:18:45 +00:00
try
{
2020-08-15 16:35:48 +00:00
staffWorld = plugin . wm . staffworld . getWorld ( ) ;
2014-08-30 19:18:45 +00:00
}
catch ( Exception ex )
{
}
2014-04-23 15:00:44 +00:00
int affected = 0 ;
2013-08-14 14:55:37 +00:00
if ( targetPlayer = = null )
2012-11-28 16:35:31 +00:00
{
2020-08-15 06:44:57 +00:00
FUtil . staffAction ( sender . getName ( ) , " Removing all " + names + " within " + radius + " blocks of all players... Brace for lag! " , false ) ;
2014-04-23 15:00:44 +00:00
2014-08-30 19:18:45 +00:00
for ( final Player player : server . getOnlinePlayers ( ) )
2012-11-28 16:35:31 +00:00
{
2020-08-15 16:35:48 +00:00
if ( player . getWorld ( ) = = staffWorld )
2014-08-30 19:18:45 +00:00
{
continue ;
}
for ( final Material material : materials )
2014-04-23 15:00:44 +00:00
{
2016-05-12 19:40:39 +00:00
affected + = replaceBlocks ( player . getLocation ( ) , material , Material . AIR , radius ) ;
2014-04-23 15:00:44 +00:00
}
2012-11-28 16:35:31 +00:00
}
}
else
{
2020-08-15 16:35:48 +00:00
if ( targetPlayer . getWorld ( ) ! = staffWorld )
2014-04-23 15:00:44 +00:00
{
2020-08-15 06:44:57 +00:00
FUtil . staffAction ( sender . getName ( ) , " Removing all " + names + " within " + radius + " blocks of " + targetPlayer . getName ( ) , false ) ;
2014-08-30 19:18:45 +00:00
for ( Material material : materials )
{
2016-05-12 19:40:39 +00:00
affected + = replaceBlocks ( targetPlayer . getLocation ( ) , material , Material . AIR , radius ) ;
2014-08-30 19:18:45 +00:00
}
2014-04-23 15:00:44 +00:00
}
2012-11-28 16:35:31 +00:00
}
2012-12-01 20:11:00 +00:00
2020-08-15 06:44:57 +00:00
FUtil . staffAction ( sender . getName ( ) , " Remove complete! " + affected + " blocks removed. " , false ) ;
2012-11-28 16:35:31 +00:00
return true ;
}
2016-05-12 19:40:39 +00:00
public static int replaceBlocks ( Location center , Material fromMaterial , Material toMaterial , int radius )
{
int affected = 0 ;
Block centerBlock = center . getBlock ( ) ;
for ( int xOffset = - radius ; xOffset < = radius ; xOffset + + )
{
for ( int yOffset = - radius ; yOffset < = radius ; yOffset + + )
{
for ( int zOffset = - radius ; zOffset < = radius ; zOffset + + )
{
Block block = centerBlock . getRelative ( xOffset , yOffset , zOffset ) ;
2019-11-29 16:35:03 +00:00
BlockData data = block . getBlockData ( ) ;
2019-12-01 23:37:19 +00:00
if ( block . getLocation ( ) . distanceSquared ( center ) < ( radius * radius ) )
2016-05-12 19:40:39 +00:00
{
2019-12-01 23:37:19 +00:00
if ( fromMaterial . equals ( Material . WATER ) & & data instanceof Waterlogged )
2016-05-12 19:40:39 +00:00
{
2019-12-01 23:37:19 +00:00
if ( data instanceof Waterlogged )
2019-11-29 16:35:03 +00:00
{
2019-12-01 23:37:19 +00:00
Waterlogged waterloggedData = ( Waterlogged ) data ;
2020-04-23 11:18:03 +00:00
if ( waterloggedData . isWaterlogged ( ) )
{
waterloggedData . setWaterlogged ( false ) ;
block . setBlockData ( waterloggedData ) ;
affected + + ;
continue ;
}
2019-11-29 16:35:03 +00:00
}
2016-05-12 19:40:39 +00:00
block . setType ( toMaterial ) ;
affected + + ;
}
2019-12-01 23:37:19 +00:00
else if ( block . getType ( ) . equals ( fromMaterial ) )
{
block . setType ( toMaterial ) ;
affected + + ;
}
2016-05-12 19:40:39 +00:00
}
}
}
}
return affected ;
}
2020-07-29 03:14:58 +00:00
}