2020-04-16 04:52:39 +02:00
/ *
* _____ _ _ _____ _
* | __ \ | | | | / ____ | | |
* | | __ ) | | ___ | | _ | ( ___ __ _ _ _ __ _ _ __ ___ __ | |
* | ___ / | | / _ \ | __ | \ ___ \ / _ ` | | | | / _ ` | ' __ / _ \ / _ ` |
* | | | | ( _ ) | | _ ____ ) | ( _ | | | _ | | ( _ | | | | __ / ( _ | |
* | _ | | _ | \ ___ / \ __ | _____ / \ __ , | \ __ , _ | \ __ , _ | _ | \ ___ | \ __ , _ |
* | |
* | _ |
* PlotSquared plot management system for Minecraft
* Copyright ( C ) 2020 IntellectualSites
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
* /
2020-04-15 21:26:54 +02:00
package com.plotsquared.core.command ;
2014-09-22 13:02:14 +02:00
2020-07-11 17:19:19 +02:00
import com.google.inject.Inject ;
2020-04-16 06:14:33 +02:00
import com.plotsquared.core.configuration.CaptionUtility ;
import com.plotsquared.core.configuration.Captions ;
import com.plotsquared.core.configuration.Settings ;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.database.DBFunc ;
import com.plotsquared.core.events.PlayerClaimPlotEvent ;
import com.plotsquared.core.events.PlotMergeEvent ;
import com.plotsquared.core.events.Result ;
import com.plotsquared.core.location.Direction ;
import com.plotsquared.core.location.Location ;
2020-07-23 19:18:21 +02:00
import com.plotsquared.core.player.MetaDataAccess ;
import com.plotsquared.core.player.PlayerMetaDataKeys ;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.player.PlotPlayer ;
2020-04-15 21:26:54 +02:00
import com.plotsquared.core.plot.Plot ;
import com.plotsquared.core.plot.PlotArea ;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.util.EconHandler ;
2020-07-10 18:17:44 +02:00
import com.plotsquared.core.util.EventDispatcher ;
2020-04-30 12:33:59 +02:00
import com.plotsquared.core.util.Expression ;
import com.plotsquared.core.util.Permissions ;
import com.plotsquared.core.util.task.TaskManager ;
2020-07-12 21:49:05 +02:00
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
2014-12-28 13:02:30 +01:00
2020-07-17 16:25:57 +02:00
import javax.annotation.Nonnull ;
import javax.annotation.Nullable ;
@CommandDeclaration ( command = " claim " , aliases = " c " , description = " Claim the current plot you're standing on " , category = CommandCategory . CLAIMING , requiredType = RequiredType . PLAYER , permission = " plots.claim " , usage = " /plot claim " )
2015-09-13 06:04:31 +02:00
public class Claim extends SubCommand {
2016-03-23 02:41:37 +01:00
2020-07-17 16:25:57 +02:00
private static final Logger logger =
LoggerFactory . getLogger ( " P2/ " + Claim . class . getSimpleName ( ) ) ;
2020-07-12 21:49:05 +02:00
2020-07-10 18:17:44 +02:00
private final EventDispatcher eventDispatcher ;
2020-07-11 17:19:19 +02:00
private final EconHandler econHandler ;
2020-07-14 19:05:05 +02:00
2020-07-14 18:49:40 +02:00
@Inject public Claim ( @Nonnull final EventDispatcher eventDispatcher ,
2020-07-17 16:25:57 +02:00
@Nullable final EconHandler econHandler ) {
2020-07-10 18:17:44 +02:00
this . eventDispatcher = eventDispatcher ;
2020-07-11 17:19:19 +02:00
this . econHandler = econHandler ;
2020-07-10 18:17:44 +02:00
}
2020-07-14 19:05:05 +02:00
2020-05-27 14:08:48 +02:00
@Override public boolean onCommand ( final PlotPlayer < ? > player , String [ ] args ) {
2020-03-15 12:22:49 +01:00
String schematic = null ;
2015-09-13 06:04:31 +02:00
if ( args . length > = 1 ) {
2014-11-16 10:48:18 +01:00
schematic = args [ 0 ] ;
}
2019-09-08 20:02:45 +02:00
Location location = player . getLocation ( ) ;
2020-07-10 18:17:44 +02:00
Plot plot = location . getPlotAbs ( ) ;
2015-09-13 06:04:31 +02:00
if ( plot = = null ) {
2019-02-22 17:51:06 +01:00
return sendMessage ( player , Captions . NOT_IN_PLOT ) ;
2015-09-13 06:04:31 +02:00
}
2020-07-10 18:17:44 +02:00
final PlayerClaimPlotEvent event = this . eventDispatcher . callClaim ( player , plot , schematic ) ;
2020-03-15 12:22:49 +01:00
schematic = event . getSchematic ( ) ;
if ( event . getEventResult ( ) = = Result . DENY ) {
sendMessage ( player , Captions . EVENT_DENIED , " Claim " ) ;
return true ;
}
boolean force = event . getEventResult ( ) = = Result . FORCE ;
2019-09-08 20:02:45 +02:00
int currentPlots = Settings . Limit . GLOBAL ?
player . getPlotCount ( ) :
2020-07-10 12:21:29 +02:00
player . getPlotCount ( location . getWorldName ( ) ) ;
2020-07-23 19:18:21 +02:00
final PlotArea area = plot . getArea ( ) ;
2020-07-23 20:52:32 +02:00
try ( final MetaDataAccess < Integer > metaDataAccess = player . accessPersistentMetaData ( PlayerMetaDataKeys . PERSISTENT_GRANTED_PLOTS ) ) {
2020-07-23 19:18:21 +02:00
int grants = 0 ;
if ( currentPlots > = player . getAllowedPlots ( ) & & ! force ) {
if ( metaDataAccess . has ( ) ) {
grants = metaDataAccess . get ( ) . orElse ( 0 ) ;
if ( grants < = 0 ) {
metaDataAccess . remove ( ) ;
return sendMessage ( player , Captions . CANT_CLAIM_MORE_PLOTS ) ;
}
} else {
2019-02-22 17:51:06 +01:00
return sendMessage ( player , Captions . CANT_CLAIM_MORE_PLOTS ) ;
2016-01-16 17:07:42 +01:00
}
}
2020-07-23 19:18:21 +02:00
if ( ! plot . canClaim ( player ) ) {
return sendMessage ( player , Captions . PLOT_IS_CLAIMED ) ;
}
if ( schematic ! = null & & ! schematic . isEmpty ( ) ) {
if ( area . isSchematicClaimSpecify ( ) ) {
if ( ! area . hasSchematic ( schematic ) ) {
return sendMessage ( player , Captions . SCHEMATIC_INVALID , " non-existent: " + schematic ) ;
}
if ( ! Permissions . hasPermission ( player , CaptionUtility . format ( player , Captions . PERMISSION_CLAIM_SCHEMATIC . getTranslated ( ) ,
schematic ) ) & & ! Permissions . hasPermission ( player , Captions . PERMISSION_ADMIN_COMMAND_SCHEMATIC )
& & ! force ) {
return sendMessage ( player , Captions . NO_SCHEMATIC_PERMISSION , schematic ) ;
}
2016-12-09 15:09:07 +01:00
}
}
2020-07-23 19:18:21 +02:00
if ( ( this . econHandler ! = null ) & & area . useEconomy ( ) & & ! force ) {
Expression < Double > costExr = area . getPrices ( ) . get ( " claim " ) ;
double cost = costExr . evaluate ( ( double ) currentPlots ) ;
if ( cost > 0d ) {
if ( this . econHandler . getMoney ( player ) < cost ) {
return sendMessage ( player , Captions . CANNOT_AFFORD_PLOT , " " + cost ) ;
}
this . econHandler . withdrawMoney ( player , cost ) ;
sendMessage ( player , Captions . REMOVED_BALANCE , cost + " " ) ;
2015-09-13 06:04:31 +02:00
}
2014-11-16 10:48:18 +01:00
}
2020-07-23 19:18:21 +02:00
if ( grants > 0 ) {
if ( grants = = 1 ) {
metaDataAccess . remove ( ) ;
} else {
metaDataAccess . set ( grants - 1 ) ;
}
sendMessage ( player , Captions . REMOVED_GRANTED_PLOT , " 1 " , ( grants - 1 ) ) ;
2016-02-27 21:04:57 +01:00
}
2016-01-16 17:07:42 +01:00
}
2020-03-15 12:22:49 +01:00
int border = area . getBorder ( ) ;
if ( border ! = Integer . MAX_VALUE & & plot . getDistanceFromOrigin ( ) > border & & ! force ) {
return ! sendMessage ( player , Captions . BORDER ) ;
}
2020-04-10 14:05:01 +02:00
plot . setOwnerAbs ( player . getUUID ( ) ) ;
2020-03-15 12:22:49 +01:00
final String finalSchematic = schematic ;
2020-07-17 16:25:57 +02:00
DBFunc . createPlotSafe ( plot , ( ) - > {
try {
2020-07-17 16:38:07 +02:00
TaskManager . getPlatformImplementation ( ) . sync ( ( ) - > {
2020-07-17 16:25:57 +02:00
if ( ! plot . claim ( player , true , finalSchematic , false ) ) {
logger . info ( Captions . PREFIX . getTranslated ( ) + String
. format ( " Failed to claim plot %s " , plot . getId ( ) . toCommaSeparatedString ( ) ) ) ;
sendMessage ( player , Captions . PLOT_NOT_CLAIMED ) ;
plot . setOwnerAbs ( null ) ;
} else if ( area . isAutoMerge ( ) ) {
PlotMergeEvent mergeEvent = Claim . this . eventDispatcher
. callMerge ( plot , Direction . ALL , Integer . MAX_VALUE , player ) ;
if ( mergeEvent . getEventResult ( ) = = Result . DENY ) {
sendMessage ( player , Captions . EVENT_DENIED , " Auto merge on claim " ) ;
} else {
plot . autoMerge ( mergeEvent . getDir ( ) , mergeEvent . getMax ( ) , player . getUUID ( ) , true ) ;
}
2019-02-06 23:30:25 +01:00
}
2020-07-17 16:25:57 +02:00
return null ;
} ) ;
} catch ( final Exception e ) {
e . printStackTrace ( ) ;
2020-03-15 12:22:49 +01:00
}
2020-07-17 16:25:57 +02:00
} , ( ) - > {
2020-07-12 21:49:05 +02:00
logger . info ( Captions . PREFIX . getTranslated ( ) + String
2020-04-30 12:33:59 +02:00
. format ( " Failed to add plot %s to the database " ,
plot . getId ( ) . toCommaSeparatedString ( ) ) ) ;
2020-04-07 22:12:52 +02:00
sendMessage ( player , Captions . PLOT_NOT_CLAIMED ) ;
2020-04-10 14:05:01 +02:00
plot . setOwnerAbs ( null ) ;
2020-04-07 22:12:52 +02:00
} ) ;
2020-03-15 12:22:49 +01:00
return true ;
2014-11-16 10:48:18 +01:00
}
2014-09-22 13:02:14 +02:00
}