2016-01-15 18:06:34 +00:00
/ *
GriefPrevention Server Plugin for Minecraft
Copyright ( C ) 2012 Ryan Hamshire
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-06-09 04:57:55 +00:00
package me.ryanhamshire.GriefPrevention ;
2016-01-15 18:06:34 +00:00
2022-02-15 13:07:31 +00:00
import me.ryanhamshire.GriefPrevention.alttd.config.AlttdConfig ;
2020-06-09 04:57:55 +00:00
import me.ryanhamshire.GriefPrevention.events.ClaimExpirationEvent ;
2016-02-02 21:34:54 +00:00
import org.bukkit.Bukkit ;
2016-04-16 04:16:33 +00:00
import org.bukkit.OfflinePlayer ;
2016-02-02 21:34:54 +00:00
2020-06-09 04:57:55 +00:00
import java.util.Calendar ;
import java.util.Date ;
import java.util.Vector ;
2016-02-02 21:34:54 +00:00
2020-06-09 04:57:55 +00:00
class CleanupUnusedClaimTask implements Runnable
{
2016-01-15 18:06:34 +00:00
Claim claim ;
PlayerData ownerData ;
2016-04-16 04:16:33 +00:00
OfflinePlayer ownerInfo ;
2022-02-15 13:07:31 +00:00
boolean forced ;
2020-06-09 04:57:55 +00:00
CleanupUnusedClaimTask ( Claim claim , PlayerData ownerData , OfflinePlayer ownerInfo )
2022-02-15 13:07:31 +00:00
{
this ( claim , ownerData , ownerInfo , false ) ;
}
CleanupUnusedClaimTask ( Claim claim , PlayerData ownerData , OfflinePlayer ownerInfo , boolean forced )
2020-06-09 04:57:55 +00:00
{
this . claim = claim ;
this . ownerData = ownerData ;
this . ownerInfo = ownerInfo ;
2022-02-15 13:07:31 +00:00
this . forced = forced ;
2020-06-09 04:57:55 +00:00
}
@Override
public void run ( )
{
//determine area of the default chest claim
int areaOfDefaultClaim = 0 ;
if ( GriefPrevention . instance . config_claims_automaticClaimsForNewPlayersRadius > = 0 )
{
areaOfDefaultClaim = ( int ) Math . pow ( GriefPrevention . instance . config_claims_automaticClaimsForNewPlayersRadius * 2 + 1 , 2 ) ;
}
//if this claim is a chest claim and those are set to expire
2021-09-28 15:40:49 +00:00
if ( ownerData . getClaims ( ) . size ( ) = = 1 & & claim . getArea ( ) < = areaOfDefaultClaim & & GriefPrevention . instance . config_claims_chestClaimExpirationDays > 0 )
2020-06-09 04:57:55 +00:00
{
2016-01-15 18:06:34 +00:00
//if the owner has been gone at least a week, and if he has ONLY the new player claim, it will be removed
2020-06-09 04:57:55 +00:00
Calendar sevenDaysAgo = Calendar . getInstance ( ) ;
sevenDaysAgo . add ( Calendar . DATE , - GriefPrevention . instance . config_claims_chestClaimExpirationDays ) ;
2021-09-28 15:40:49 +00:00
if ( sevenDaysAgo . getTime ( ) . after ( new Date ( ownerInfo . getLastPlayed ( ) ) ) )
2020-06-09 04:57:55 +00:00
{
if ( expireEventCanceled ( ) )
return ;
claim . removeSurfaceFluids ( null ) ;
GriefPrevention . instance . dataStore . deleteClaim ( claim , true , true ) ;
//if configured to do so, restore the land to natural
if ( GriefPrevention . instance . creativeRulesApply ( claim . getLesserBoundaryCorner ( ) ) | | GriefPrevention . instance . config_claims_survivalAutoNatureRestoration )
{
GriefPrevention . instance . restoreClaim ( claim , 0 ) ;
}
GriefPrevention . AddLogEntry ( " " + claim . getOwnerName ( ) + " 's new player claim expired. " , CustomLogEntryTypes . AdminActivity ) ;
}
}
//if configured to always remove claims after some inactivity period without exceptions...
else if ( GriefPrevention . instance . config_claims_expirationDays > 0 )
{
Calendar earliestPermissibleLastLogin = Calendar . getInstance ( ) ;
earliestPermissibleLastLogin . add ( Calendar . DATE , - GriefPrevention . instance . config_claims_expirationDays ) ;
2022-02-15 13:07:31 +00:00
if ( earliestPermissibleLastLogin . getTime ( ) . after ( new Date ( ownerInfo . getLastPlayed ( ) ) ) | | forced )
2020-06-09 04:57:55 +00:00
{
if ( expireEventCanceled ( ) )
return ;
//make a copy of this player's claim list
2020-09-19 21:14:11 +00:00
Vector < Claim > claims = new Vector < > ( ownerData . getClaims ( ) ) ;
2022-02-15 13:07:31 +00:00
// Alternative logic for deleting claims
if ( AlttdConfig . alternativeClaimExpiring ) {
for ( Claim claim : claims )
{
// remove all subclaims a claim has
for ( int j = 1 ; ( j - 1 ) < claim . children . size ( ) ; j + + )
{
GriefPrevention . instance . dataStore . deleteClaim ( claim . children . get ( j - 1 ) , true ) ;
}
// remove all trusted players
claim . clearPermissions ( ) ;
// public trust
claim . setPermission ( " public " , ClaimPermission . Build ) ;
// make the claim an (expiring) admin claim
GriefPrevention . instance . dataStore . changeClaimOwner ( claim , null ) ;
AlttdConfig . addExpiringClaim ( claim . id ) ;
}
GriefPrevention . AddLogEntry ( " All of " + claim . getOwnerName ( ) + " 's claims have expired and converted to a temp claim. " , CustomLogEntryTypes . AdminActivity ) ;
GriefPrevention . AddLogEntry ( " earliestPermissibleLastLogin#getTime: " + earliestPermissibleLastLogin . getTime ( ) , CustomLogEntryTypes . Debug , true ) ;
GriefPrevention . AddLogEntry ( " ownerInfo#getLastPlayed: " + ownerInfo . getLastPlayed ( ) , CustomLogEntryTypes . Debug , true ) ;
return ;
}
2020-06-09 04:57:55 +00:00
//delete them
GriefPrevention . instance . dataStore . deleteClaimsForPlayer ( claim . ownerID , true ) ;
GriefPrevention . AddLogEntry ( " All of " + claim . getOwnerName ( ) + " 's claims have expired. " , CustomLogEntryTypes . AdminActivity ) ;
2020-09-14 23:37:20 +00:00
GriefPrevention . AddLogEntry ( " earliestPermissibleLastLogin#getTime: " + earliestPermissibleLastLogin . getTime ( ) , CustomLogEntryTypes . Debug , true ) ;
GriefPrevention . AddLogEntry ( " ownerInfo#getLastPlayed: " + ownerInfo . getLastPlayed ( ) , CustomLogEntryTypes . Debug , true ) ;
2020-06-09 04:57:55 +00:00
2020-09-19 21:14:11 +00:00
for ( Claim claim : claims )
2020-06-09 04:57:55 +00:00
{
//if configured to do so, restore the land to natural
2020-09-19 21:14:11 +00:00
if ( GriefPrevention . instance . creativeRulesApply ( claim . getLesserBoundaryCorner ( ) ) | | GriefPrevention . instance . config_claims_survivalAutoNatureRestoration )
2020-06-09 04:57:55 +00:00
{
2020-09-19 21:14:11 +00:00
GriefPrevention . instance . restoreClaim ( claim , 0 ) ;
2020-06-09 04:57:55 +00:00
}
}
}
2020-06-09 06:15:52 +00:00
}
else if ( GriefPrevention . instance . config_claims_unusedClaimExpirationDays > 0 & & GriefPrevention . instance . creativeRulesApply ( claim . getLesserBoundaryCorner ( ) ) )
2020-06-09 04:57:55 +00:00
{
//avoid scanning large claims and administrative claims
if ( claim . isAdminClaim ( ) | | claim . getWidth ( ) > 25 | | claim . getHeight ( ) > 25 ) return ;
//otherwise scan the claim content
int minInvestment = 400 ;
long investmentScore = claim . getPlayerInvestmentScore ( ) ;
if ( investmentScore < minInvestment )
{
//if the owner has been gone at least a week, and if he has ONLY the new player claim, it will be removed
Calendar sevenDaysAgo = Calendar . getInstance ( ) ;
sevenDaysAgo . add ( Calendar . DATE , - GriefPrevention . instance . config_claims_unusedClaimExpirationDays ) ;
boolean claimExpired = sevenDaysAgo . getTime ( ) . after ( new Date ( ownerInfo . getLastPlayed ( ) ) ) ;
if ( claimExpired )
{
if ( expireEventCanceled ( ) )
return ;
GriefPrevention . instance . dataStore . deleteClaim ( claim , true , true ) ;
GriefPrevention . AddLogEntry ( " Removed " + claim . getOwnerName ( ) + " 's unused claim @ " + GriefPrevention . getfriendlyLocationString ( claim . getLesserBoundaryCorner ( ) ) , CustomLogEntryTypes . AdminActivity ) ;
//restore the claim area to natural state
GriefPrevention . instance . restoreClaim ( claim , 0 ) ;
}
}
}
}
public boolean expireEventCanceled ( )
{
//see if any other plugins don't want this claim deleted
ClaimExpirationEvent event = new ClaimExpirationEvent ( this . claim ) ;
Bukkit . getPluginManager ( ) . callEvent ( event ) ;
return event . isCancelled ( ) ;
}
2016-01-15 18:06:34 +00:00
}