2016-06-07 22:39:45 +02:00
package me.wiefferink.areashop.managers ;
import com.google.common.base.Charsets ;
import com.google.common.io.Files ;
import com.sk89q.worldguard.protection.managers.RegionManager ;
import com.sk89q.worldguard.protection.regions.ProtectedRegion ;
import me.wiefferink.areashop.AreaShop ;
2019-01-28 23:37:43 +01:00
import me.wiefferink.areashop.events.ask.AddingRegionEvent ;
import me.wiefferink.areashop.events.ask.DeletingRegionEvent ;
2016-06-07 22:39:45 +02:00
import me.wiefferink.areashop.events.notify.AddedRegionEvent ;
2016-08-28 22:45:20 +02:00
import me.wiefferink.areashop.events.notify.DeletedRegionEvent ;
2016-06-07 22:39:45 +02:00
import me.wiefferink.areashop.regions.BuyRegion ;
import me.wiefferink.areashop.regions.GeneralRegion ;
import me.wiefferink.areashop.regions.GeneralRegion.RegionEvent ;
import me.wiefferink.areashop.regions.GeneralRegion.RegionType ;
import me.wiefferink.areashop.regions.RegionGroup ;
import me.wiefferink.areashop.regions.RentRegion ;
2016-12-23 16:03:22 +01:00
import me.wiefferink.areashop.tools.Utils ;
2017-11-26 15:47:44 +01:00
import me.wiefferink.bukkitdo.Do ;
2017-04-17 21:34:32 +02:00
import org.bukkit.Bukkit ;
import org.bukkit.Location ;
import org.bukkit.Material ;
import org.bukkit.OfflinePlayer ;
import org.bukkit.World ;
2016-06-07 22:39:45 +02:00
import org.bukkit.command.CommandSender ;
import org.bukkit.configuration.ConfigurationSection ;
import org.bukkit.configuration.file.YamlConfiguration ;
import org.bukkit.entity.Player ;
2017-04-17 21:34:32 +02:00
import java.io.File ;
import java.io.FileInputStream ;
import java.io.FileOutputStream ;
import java.io.IOException ;
import java.io.InputStream ;
import java.io.InputStreamReader ;
import java.io.ObjectInputStream ;
import java.io.ObjectOutputStream ;
import java.io.OutputStream ;
import java.util.ArrayList ;
import java.util.Collection ;
import java.util.HashMap ;
import java.util.HashSet ;
import java.util.List ;
import java.util.Set ;
2016-06-07 22:39:45 +02:00
import java.util.regex.Matcher ;
import java.util.regex.Pattern ;
2016-12-23 16:03:22 +01:00
public class FileManager extends Manager {
2016-06-07 22:39:45 +02:00
private HashMap < String , GeneralRegion > regions = null ;
private String regionsPath = null ;
private HashMap < String , RegionGroup > groups = null ;
private String configPath = null ;
private YamlConfiguration config = null ;
private String groupsPath = null ;
private YamlConfiguration groupsConfig = null ;
private String defaultPath = null ;
private YamlConfiguration defaultConfig = null ;
2016-12-23 14:07:26 +01:00
private YamlConfiguration defaultConfigFallback = null ;
2016-06-07 22:39:45 +02:00
private boolean saveGroupsRequired = false ;
2018-05-23 23:46:57 +02:00
private final Set < String > worldRegionsRequireSaving ;
2017-03-29 20:15:44 +02:00
private HashMap < String , Integer > versions = null ;
2016-06-07 22:39:45 +02:00
private String versionPath = null ;
private String schemFolder = null ;
// Enum for region types
2017-03-29 20:15:44 +02:00
public enum AddResult {
2016-06-07 22:39:45 +02:00
BLACKLISTED ( " blacklisted " ) ,
NOPERMISSION ( " nopermission " ) ,
ALREADYADDED ( " alreadyadded " ) ,
2018-05-23 22:51:46 +02:00
ALREADYADDEDOTHERWORLD ( " alreadyaddedotherworld " ) ,
2016-06-07 22:39:45 +02:00
SUCCESS ( " success " ) ;
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
private final String value ;
AddResult ( String value ) {
this . value = value ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
public String getValue ( ) {
return value ;
}
2017-03-29 20:15:44 +02:00
}
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Constructor , initialize variabeles .
2016-06-07 22:39:45 +02:00
* /
2016-12-23 16:03:22 +01:00
public FileManager ( ) {
2016-06-07 22:39:45 +02:00
regions = new HashMap < > ( ) ;
regionsPath = plugin . getDataFolder ( ) + File . separator + AreaShop . regionsFolder ;
configPath = plugin . getDataFolder ( ) + File . separator + " config.yml " ;
groups = new HashMap < > ( ) ;
groupsPath = plugin . getDataFolder ( ) + File . separator + AreaShop . groupsFile ;
defaultPath = plugin . getDataFolder ( ) + File . separator + AreaShop . defaultFile ;
versionPath = plugin . getDataFolder ( ) . getPath ( ) + File . separator + AreaShop . versionFile ;
schemFolder = plugin . getDataFolder ( ) + File . separator + AreaShop . schematicFolder ;
worldRegionsRequireSaving = new HashSet < > ( ) ;
File schemFile = new File ( schemFolder ) ;
if ( ! schemFile . exists ( ) & ! schemFile . mkdirs ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not create schematic files directory: " + schemFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
}
loadVersions ( ) ;
}
2016-12-23 16:03:22 +01:00
@Override
public void shutdown ( ) {
// Update lastactive time for players that are online now
for ( GeneralRegion region : getRegions ( ) ) {
Player player = Bukkit . getPlayer ( region . getOwner ( ) ) ;
if ( player ! = null ) {
region . updateLastActiveTime ( ) ;
}
2016-06-07 22:39:45 +02:00
}
2016-12-23 16:03:22 +01:00
// Save files that need to be saved
saveRequiredFilesAtOnce ( ) ;
2016-06-07 22:39:45 +02:00
}
2016-12-23 16:03:22 +01:00
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
//////////////////////////////////////////////////////////
// GETTERS
//////////////////////////////////////////////////////////
2017-03-29 20:15:44 +02:00
/ * *
* Get the folder where schematics are stored .
* @return The folder where schematics are stored
* /
2016-06-07 22:39:45 +02:00
public String getSchematicFolder ( ) {
return schemFolder ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get a group .
* @param name The name of the group to get ( will be normalized )
* @return The group if found , otherwise null
* /
2016-06-07 22:39:45 +02:00
public RegionGroup getGroup ( String name ) {
return groups . get ( name . toLowerCase ( ) ) ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get all groups .
* @return Collection with all groups ( safe to modify )
* /
2016-06-07 22:39:45 +02:00
public Collection < RegionGroup > getGroups ( ) {
return groups . values ( ) ;
}
2016-12-23 14:07:26 +01:00
2017-03-29 20:15:44 +02:00
/ * *
* Get the default region settings as provided by the user ( default . yml ) .
* @return YamlConfiguration with the settings ( might miss settings , which should be filled in with { @link # getFallbackRegionSettings ( ) } )
* /
2016-12-23 14:07:26 +01:00
public YamlConfiguration getRegionSettings ( ) {
2016-06-07 22:39:45 +02:00
return defaultConfig ;
}
2016-12-23 14:07:26 +01:00
2017-03-29 20:15:44 +02:00
/ * *
* Get the default regions settings as provided by AreaShop ( default . yml ) .
* @return YamlConfiguration with the default settings
* /
2016-12-23 14:07:26 +01:00
public YamlConfiguration getFallbackRegionSettings ( ) {
return defaultConfigFallback ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get the config file ( config . yml ) .
* @return YamlConfiguration with the settings of users , with fallback to the settings provided by AreaShop
* /
2016-06-07 22:39:45 +02:00
public YamlConfiguration getConfig ( ) {
return config ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get a region .
* @param name The name of the region to get ( will be normalized )
* @return The region if found , otherwise null
* /
2016-06-07 22:39:45 +02:00
public GeneralRegion getRegion ( String name ) {
return regions . get ( name . toLowerCase ( ) ) ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get a rental region .
* @param name The name of the rental region ( will be normalized )
* @return RentRegion if it could be found , otherwise null
* /
2016-06-07 22:39:45 +02:00
public RentRegion getRent ( String name ) {
GeneralRegion region = regions . get ( name . toLowerCase ( ) ) ;
2018-05-23 23:46:57 +02:00
if ( region instanceof RentRegion ) {
2017-03-29 20:15:44 +02:00
return ( RentRegion ) region ;
2016-06-07 22:39:45 +02:00
}
return null ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get a buy region .
* @param name The name of the buy region ( will be normalized )
* @return BuyRegion if it could be found , otherwise null
* /
2016-06-07 22:39:45 +02:00
public BuyRegion getBuy ( String name ) {
GeneralRegion region = regions . get ( name . toLowerCase ( ) ) ;
2018-05-23 23:46:57 +02:00
if ( region instanceof BuyRegion ) {
2017-03-29 20:15:44 +02:00
return ( BuyRegion ) region ;
2016-06-07 22:39:45 +02:00
}
return null ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get all rental regions .
* @return List of all rental regions
* /
2016-06-07 22:39:45 +02:00
public List < RentRegion > getRents ( ) {
List < RentRegion > result = new ArrayList < > ( ) ;
for ( GeneralRegion region : regions . values ( ) ) {
2017-03-29 20:15:44 +02:00
if ( region instanceof RentRegion ) {
result . add ( ( RentRegion ) region ) ;
2016-06-07 22:39:45 +02:00
}
}
return result ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get all buy regions .
* @return List of all buy regions
* /
2016-06-07 22:39:45 +02:00
public List < BuyRegion > getBuys ( ) {
List < BuyRegion > result = new ArrayList < > ( ) ;
for ( GeneralRegion region : regions . values ( ) ) {
2017-03-29 20:15:44 +02:00
if ( region instanceof BuyRegion ) {
result . add ( ( BuyRegion ) region ) ;
2016-06-07 22:39:45 +02:00
}
}
return result ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get all regions .
* @return List of all regions ( it is safe to modify the list )
* /
2016-06-07 22:39:45 +02:00
public List < GeneralRegion > getRegions ( ) {
return new ArrayList < > ( regions . values ( ) ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Get a list of names of all buy regions .
2016-06-07 22:39:45 +02:00
* @return A String list with all the names
* /
public List < String > getBuyNames ( ) {
ArrayList < String > result = new ArrayList < > ( ) ;
for ( BuyRegion region : getBuys ( ) ) {
result . add ( region . getName ( ) ) ;
}
return result ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get a list of names of all rent regions .
* @return A String list with all the names
* /
2016-06-07 22:39:45 +02:00
public List < String > getRentNames ( ) {
ArrayList < String > result = new ArrayList < > ( ) ;
for ( RentRegion region : getRents ( ) ) {
result . add ( region . getName ( ) ) ;
}
return result ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get a list of names of all regions .
* @return A String list with all the names
* /
2016-06-07 22:39:45 +02:00
public List < String > getRegionNames ( ) {
ArrayList < String > result = new ArrayList < > ( ) ;
for ( GeneralRegion region : getRegions ( ) ) {
result . add ( region . getName ( ) ) ;
}
return result ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Get a list of names of all groups .
* @return A String list with all the names
* /
2016-06-07 22:39:45 +02:00
public List < String > getGroupNames ( ) {
ArrayList < String > result = new ArrayList < > ( ) ;
for ( RegionGroup group : getGroups ( ) ) {
result . add ( group . getName ( ) ) ;
}
return result ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2019-01-28 23:37:43 +01:00
* Add a region to the list and mark it as to - be - saved .
* @param region Then region to add
* @return true when successful , otherwise false ( denied by an event listener )
2016-06-07 22:39:45 +02:00
* /
2019-01-28 23:37:43 +01:00
public AddingRegionEvent addRegion ( GeneralRegion region ) {
AddingRegionEvent event = addRegionNoSave ( region ) ;
if ( event . isCancelled ( ) ) {
return event ;
2016-06-07 22:39:45 +02:00
}
2019-01-28 23:37:43 +01:00
region . saveRequired ( ) ;
markGroupsAutoDirty ( ) ;
return event ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2019-01-28 23:37:43 +01:00
* Add a region to the list without saving it to disk ( useful for loading at startup ) .
* @param region The region to add
* @return true when successful , otherwise false ( denied by an event listener )
2016-06-07 22:39:45 +02:00
* /
2019-01-28 23:37:43 +01:00
public AddingRegionEvent addRegionNoSave ( GeneralRegion region ) {
AddingRegionEvent event = new AddingRegionEvent ( region ) ;
if ( region = = null ) {
AreaShop . debug ( " Tried adding a null region! " ) ;
event . cancel ( " null region " ) ;
return event ;
}
Bukkit . getPluginManager ( ) . callEvent ( event ) ;
if ( event . isCancelled ( ) ) {
return event ;
}
regions . put ( region . getName ( ) . toLowerCase ( ) , region ) ;
Bukkit . getPluginManager ( ) . callEvent ( new AddedRegionEvent ( region ) ) ;
return event ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-10-20 13:40:53 +02:00
* Mark all RegionGroups that they should regenerate regions .
* /
public void markGroupsAutoDirty ( ) {
for ( RegionGroup group : getGroups ( ) ) {
group . autoDirty ( ) ;
}
}
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Add a RegionGroup .
2016-06-07 22:39:45 +02:00
* @param group The RegionGroup to add
* /
public void addGroup ( RegionGroup group ) {
groups . put ( group . getName ( ) . toLowerCase ( ) , group ) ;
String lowGroup = group . getName ( ) . toLowerCase ( ) ;
groupsConfig . set ( lowGroup + " .name " , group . getName ( ) ) ;
groupsConfig . set ( lowGroup + " .priority " , 0 ) ;
saveGroupsIsRequired ( ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Check if a player can add a certain region as rent or buy region .
2016-06-07 22:39:45 +02:00
* @param sender The player / console that wants to add a region
* @param region The WorldGuard region to add
2018-05-23 22:51:46 +02:00
* @param world The world the ProtectedRegion is located in
2017-03-29 20:15:44 +02:00
* @param type The type the region should have in AreaShop
2016-06-07 22:39:45 +02:00
* @return The result if a player would want to add this region
* /
2018-05-23 22:51:46 +02:00
public AddResult checkRegionAdd ( CommandSender sender , ProtectedRegion region , World world , RegionType type ) {
2016-06-07 22:39:45 +02:00
Player player = null ;
if ( sender instanceof Player ) {
player = ( Player ) sender ;
}
// Determine if the player is an owner or member of the region
boolean isMember = player ! = null & & plugin . getWorldGuardHandler ( ) . containsMember ( region , player . getUniqueId ( ) ) ;
2017-03-29 20:15:44 +02:00
boolean isOwner = player ! = null & & plugin . getWorldGuardHandler ( ) . containsOwner ( region , player . getUniqueId ( ) ) ;
2016-06-07 22:39:45 +02:00
AreaShop . debug ( " checkRegionAdd: isOwner= " + isOwner + " , isMember= " + isMember ) ;
String typeString ;
if ( type = = RegionType . RENT ) {
typeString = " rent " ;
} else {
typeString = " buy " ;
}
AreaShop . debug ( " permissions: .create= " + sender . hasPermission ( " areashop.create " + typeString ) + " , .create.owner= " + sender . hasPermission ( " areashop.create " + typeString + " .owner " ) + " , .create.member= " + sender . hasPermission ( " areashop.create " + typeString + " .member " ) ) ;
if ( ! ( sender . hasPermission ( " areashop.create " + typeString )
| | ( sender . hasPermission ( " areashop.create " + typeString + " .owner " ) & & isOwner )
| | ( sender . hasPermission ( " areashop.create " + typeString + " .member " ) & & isMember ) ) ) {
return AddResult . NOPERMISSION ;
}
GeneralRegion asRegion = plugin . getFileManager ( ) . getRegion ( region . getId ( ) ) ;
if ( asRegion ! = null ) {
2018-05-23 22:51:46 +02:00
if ( asRegion . getWorld ( ) . equals ( world ) ) {
return AddResult . ALREADYADDED ;
} else {
return AddResult . ALREADYADDEDOTHERWORLD ;
}
2016-06-07 22:39:45 +02:00
} else if ( plugin . getFileManager ( ) . isBlacklisted ( region . getId ( ) ) ) {
return AddResult . BLACKLISTED ;
} else {
return AddResult . SUCCESS ;
}
}
2017-03-29 20:15:44 +02:00
2019-01-28 23:37:43 +01:00
2016-06-07 22:39:45 +02:00
/ * *
2019-01-28 23:37:43 +01:00
* Remove a region from the list .
* @param region The region to remove
* @param giveMoneyBack use true to give money back to the player if someone is currently holding this region , otherwise false
* @return true if the region has been removed , false otherwise
2016-06-07 22:39:45 +02:00
* /
2019-01-28 23:37:43 +01:00
public DeletingRegionEvent deleteRegion ( GeneralRegion region , boolean giveMoneyBack ) {
DeletingRegionEvent event = new DeletingRegionEvent ( region ) ;
if ( region = = null ) {
event . cancel ( " null region " ) ;
return event ;
}
2016-06-07 22:39:45 +02:00
2019-01-28 23:37:43 +01:00
Bukkit . getPluginManager ( ) . callEvent ( event ) ;
if ( event . isCancelled ( ) ) {
return event ;
2017-03-29 20:15:44 +02:00
}
2019-01-28 23:37:43 +01:00
region . setDeleted ( ) ;
if ( region instanceof RentRegion & & ( ( RentRegion ) region ) . isRented ( ) ) {
( ( RentRegion ) region ) . unRent ( giveMoneyBack , null ) ;
} else if ( region instanceof BuyRegion & & ( ( BuyRegion ) region ) . isSold ( ) ) {
( ( BuyRegion ) region ) . sell ( giveMoneyBack , null ) ;
}
2017-03-29 20:15:44 +02:00
2019-01-28 23:37:43 +01:00
// Handle schematics
region . handleSchematicEvent ( RegionEvent . DELETED ) ;
2017-03-29 20:15:44 +02:00
2019-01-28 23:37:43 +01:00
// Delete the signs
if ( region . getWorld ( ) ! = null ) {
for ( Location sign : region . getSignsFeature ( ) . getSignLocations ( ) ) {
sign . getBlock ( ) . setType ( Material . AIR ) ;
2016-06-07 22:39:45 +02:00
}
2019-01-28 23:37:43 +01:00
}
2017-03-29 20:15:44 +02:00
2019-01-28 23:37:43 +01:00
// Remove from RegionGroups
RegionGroup [ ] regionGroups = getGroups ( ) . toArray ( new RegionGroup [ 0 ] ) ;
for ( RegionGroup group : regionGroups ) {
group . removeMember ( region ) ;
}
2016-06-07 22:39:45 +02:00
2019-01-28 23:37:43 +01:00
region . resetRegionFlags ( ) ;
regions . remove ( region . getLowerCaseName ( ) ) ;
2016-06-07 22:39:45 +02:00
2019-01-28 23:37:43 +01:00
// Remove file
File file = new File ( plugin . getDataFolder ( ) + File . separator + AreaShop . regionsFolder + File . separator + region . getLowerCaseName ( ) + " .yml " ) ;
if ( file . exists ( ) ) {
boolean deleted ;
try {
deleted = file . delete ( ) ;
} catch ( Exception e ) {
deleted = false ;
}
if ( ! deleted ) {
AreaShop . warn ( " File could not be deleted: " + file . toString ( ) ) ;
}
2017-03-29 20:15:44 +02:00
}
2019-01-28 23:37:43 +01:00
// Broadcast event
Bukkit . getPluginManager ( ) . callEvent ( new DeletedRegionEvent ( region ) ) ;
return event ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Remove a group .
2016-06-07 22:39:45 +02:00
* @param group Group to remove
* /
public void removeGroup ( RegionGroup group ) {
groups . remove ( group . getLowerCaseName ( ) ) ;
groupsConfig . set ( group . getLowerCaseName ( ) , null ) ;
saveGroupsIsRequired ( ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Update all signs that need periodic updating .
2016-06-07 22:39:45 +02:00
* /
public void performPeriodicSignUpdate ( ) {
2017-11-26 15:47:44 +01:00
Do . forAll (
plugin . getConfig ( ) . getInt ( " signs.regionsPerTick " ) ,
getRents ( ) ,
region - > {
if ( region . needsPeriodicUpdate ( ) ) {
region . update ( ) ;
2016-06-07 22:39:45 +02:00
}
}
2017-11-26 15:47:44 +01:00
) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Send out rent expire warnings .
2016-06-07 22:39:45 +02:00
* /
public void sendRentExpireWarnings ( ) {
2017-11-26 15:47:44 +01:00
Do . forAll (
plugin . getConfig ( ) . getInt ( " expireWarning.regionsPerTick " ) ,
getRents ( ) ,
RentRegion : : sendExpirationWarnings
) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Update regions in a task to minimize lag .
* @param regions Regions to update
2016-06-13 20:36:17 +02:00
* @param confirmationReceiver The CommandSender that should be notified at completion
2016-06-07 22:39:45 +02:00
* /
public void updateRegions ( final List < GeneralRegion > regions , final CommandSender confirmationReceiver ) {
final int regionsPerTick = plugin . getConfig ( ) . getInt ( " update.regionsPerTick " ) ;
if ( confirmationReceiver ! = null ) {
2017-03-29 20:15:44 +02:00
plugin . message ( confirmationReceiver , " reload-updateStart " , regions . size ( ) , regionsPerTick * 20 ) ;
2016-06-07 22:39:45 +02:00
}
2017-11-26 15:47:44 +01:00
Do . forAll (
regionsPerTick ,
regions ,
GeneralRegion : : update ,
( ) - > {
if ( confirmationReceiver ! = null ) {
plugin . message ( confirmationReceiver , " reload-updateComplete " ) ;
2016-06-07 22:39:45 +02:00
}
}
2017-11-26 15:47:44 +01:00
) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
/ * *
2019-06-01 17:08:30 +02:00
* Update a list of regions .
2017-03-29 20:15:44 +02:00
* @param regions The list of regions to update .
* /
2016-06-07 22:39:45 +02:00
public void updateRegions ( List < GeneralRegion > regions ) {
updateRegions ( regions , null ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Update all regions , happens in a task to minimize lag .
2016-06-07 22:39:45 +02:00
* /
public void updateAllRegions ( ) {
updateRegions ( getRegions ( ) , null ) ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Update all regions .
* @param confirmationReceiver Optional CommandSender that should receive progress messages
* /
2016-06-07 22:39:45 +02:00
public void updateAllRegions ( CommandSender confirmationReceiver ) {
updateRegions ( getRegions ( ) , confirmationReceiver ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Save the group file to disk .
2016-06-07 22:39:45 +02:00
* /
public void saveGroupsIsRequired ( ) {
saveGroupsRequired = true ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Check if saving the groups file is required .
* @return true if changes are made and saving is required , otherwise false
* /
2016-06-07 22:39:45 +02:00
public boolean isSaveGroupsRequired ( ) {
return saveGroupsRequired ;
}
2017-03-29 20:15:44 +02:00
/ * *
* Save the groups file to disk synchronously .
* /
2016-06-07 22:39:45 +02:00
public void saveGroupsNow ( ) {
AreaShop . debug ( " saveGroupsNow() done " ) ;
saveGroupsRequired = false ;
try {
groupsConfig . save ( groupsPath ) ;
2017-03-29 20:15:44 +02:00
} catch ( IOException e ) {
AreaShop . warn ( " Groups file could not be saved: " + groupsPath ) ;
2016-06-07 22:39:45 +02:00
}
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Save all region related files spread over time ( low load ) .
2016-06-07 22:39:45 +02:00
* /
public void saveRequiredFiles ( ) {
if ( isSaveGroupsRequired ( ) ) {
saveGroupsNow ( ) ;
}
this . saveWorldGuardRegions ( ) ;
2017-11-26 15:47:44 +01:00
Do . forAll (
plugin . getConfig ( ) . getInt ( " saving.regionsPerTick " ) ,
getRegions ( ) ,
region - > {
if ( region . isSaveRequired ( ) ) {
region . saveNow ( ) ;
2016-06-07 22:39:45 +02:00
}
}
2017-11-26 15:47:44 +01:00
) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Save all region related files directly ( only for cases like onDisable ( ) ) .
2016-06-07 22:39:45 +02:00
* /
public void saveRequiredFilesAtOnce ( ) {
if ( isSaveGroupsRequired ( ) ) {
saveGroupsNow ( ) ;
}
for ( GeneralRegion region : getRegions ( ) ) {
if ( region . isSaveRequired ( ) ) {
region . saveNow ( ) ;
2017-03-29 20:15:44 +02:00
}
2016-06-07 22:39:45 +02:00
}
this . saveWorldGuardRegions ( ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Indicates that a / multiple WorldGuard regions need to be saved .
2016-06-07 22:39:45 +02:00
* @param worldName The world where the regions that should be saved is in
* /
public void saveIsRequiredForRegionWorld ( String worldName ) {
worldRegionsRequireSaving . add ( worldName ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Save all worldGuard regions that need saving .
2016-06-07 22:39:45 +02:00
* /
public void saveWorldGuardRegions ( ) {
for ( String world : worldRegionsRequireSaving ) {
World bukkitWorld = Bukkit . getWorld ( world ) ;
if ( bukkitWorld ! = null ) {
2018-08-05 00:34:50 +02:00
RegionManager manager = plugin . getRegionManager ( bukkitWorld ) ;
2016-06-07 22:39:45 +02:00
if ( manager ! = null ) {
try {
if ( plugin . getWorldGuard ( ) . getDescription ( ) . getVersion ( ) . startsWith ( " 5. " ) ) {
manager . save ( ) ;
2017-03-29 20:15:44 +02:00
} else {
2016-06-07 22:39:45 +02:00
manager . saveChanges ( ) ;
}
} catch ( Exception e ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " WorldGuard regions in world " + world + " could not be saved " ) ;
2016-06-07 22:39:45 +02:00
}
}
}
}
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Get the folder the region files are located in .
2016-06-13 20:36:17 +02:00
* @return The folder where the region . yml files are in
2016-06-07 22:39:45 +02:00
* /
public String getRegionFolder ( ) {
return regionsPath ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Check if a region is on the adding blacklist .
2016-06-07 22:39:45 +02:00
* @param region The region name to check
* @return true if the region may not be added , otherwise false
* /
public boolean isBlacklisted ( String region ) {
for ( String line : plugin . getConfig ( ) . getStringList ( " blacklist " ) ) {
Pattern pattern = Pattern . compile ( line , Pattern . CASE_INSENSITIVE ) ;
Matcher matcher = pattern . matcher ( region ) ;
if ( matcher . matches ( ) ) {
return true ;
}
}
return false ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Unrent regions that have no time left , regions to check per tick is in the config .
2016-06-07 22:39:45 +02:00
* /
public void checkRents ( ) {
2017-11-26 15:47:44 +01:00
Do . forAll (
plugin . getConfig ( ) . getInt ( " expiration.regionsPerTick " ) ,
getRents ( ) ,
RentRegion : : checkExpiration
2017-06-12 19:56:49 +02:00
) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Check all regions and unrent / sell them if the player is inactive for too long .
2016-06-07 22:39:45 +02:00
* /
public void checkForInactiveRegions ( ) {
2017-11-26 15:47:44 +01:00
Do . forAll (
plugin . getConfig ( ) . getInt ( " inactive.regionsPerTick " ) ,
getRegions ( ) ,
GeneralRegion : : checkInactive
) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Load the file with the versions , used to check if the other files need conversion .
2016-06-07 22:39:45 +02:00
* /
@SuppressWarnings ( " unchecked " )
public void loadVersions ( ) {
File file = new File ( versionPath ) ;
if ( file . exists ( ) ) {
// Load versions from the file
2019-01-26 21:23:49 +01:00
try ( ObjectInputStream input = new ObjectInputStream ( new FileInputStream ( versionPath ) ) ) {
versions = ( HashMap < String , Integer > ) input . readObject ( ) ;
2017-03-29 20:15:44 +02:00
} catch ( IOException | ClassNotFoundException | ClassCastException e ) {
AreaShop . warn ( " Something went wrong reading file: " + versionPath ) ;
2016-06-07 22:39:45 +02:00
versions = null ;
}
}
2019-01-26 21:23:49 +01:00
if ( versions = = null | | versions . isEmpty ( ) ) {
2016-06-07 22:39:45 +02:00
versions = new HashMap < > ( ) ;
versions . put ( AreaShop . versionFiles , 0 ) ;
this . saveVersions ( ) ;
}
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Save the versions file to disk .
2016-06-07 22:39:45 +02:00
* /
public void saveVersions ( ) {
if ( ! ( new File ( versionPath ) . exists ( ) ) ) {
2017-03-02 18:13:52 +01:00
AreaShop . debug ( " versions file created, this should happen only after installing or upgrading the plugin " ) ;
2016-06-07 22:39:45 +02:00
}
2019-01-26 21:23:49 +01:00
try ( ObjectOutputStream output = new ObjectOutputStream ( new FileOutputStream ( versionPath ) ) ) {
2016-06-07 22:39:45 +02:00
output . writeObject ( versions ) ;
2017-03-29 20:15:44 +02:00
} catch ( IOException e ) {
AreaShop . warn ( " File could not be saved: " + versionPath ) ;
2016-06-07 22:39:45 +02:00
}
}
/ * *
2017-03-29 20:15:44 +02:00
* Load all files from disk .
2017-02-02 23:03:55 +01:00
* @param thisTick Load files in the current tick or a tick later
2016-06-07 22:39:45 +02:00
* @return true if the files are loaded correctly , otherwise false
* /
2016-12-25 23:02:23 +01:00
public boolean loadFiles ( boolean thisTick ) {
2016-06-07 22:39:45 +02:00
// Load config.yml + add defaults from .jar
boolean result = loadConfigFile ( ) ;
// Load default.yml + add defaults from .jar
result & = loadDefaultFile ( ) ;
// Convert old formats to the latest (object saving to .yml saving)
preUpdateFiles ( ) ;
2017-04-17 21:32:15 +02:00
if ( thisTick ) {
// Load region files (regions folder)
loadRegionFiles ( ) ;
// Convert old formats to the latest (changes in .yml saving format)
postUpdateFiles ( ) ;
// Load groups.yml
result & = loadGroupsFile ( ) ;
} else {
2017-11-26 15:47:44 +01:00
Do . sync ( ( ) - > {
// Load region files (regions folder)
loadRegionFiles ( ) ;
// Convert old formats to the latest (changes in .yml saving format)
postUpdateFiles ( ) ;
// Load groups.yml
loadGroupsFile ( ) ;
} ) ;
2017-04-17 21:32:15 +02:00
}
2016-06-07 22:39:45 +02:00
return result ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
* Load the default . yml file
* @return true if it has been loaded successfully , otherwise false
* /
public boolean loadDefaultFile ( ) {
boolean result = true ;
File defaultFile = new File ( defaultPath ) ;
// Safe the file from the jar to disk if it does not exist
if ( ! defaultFile . exists ( ) ) {
try (
InputStream input = plugin . getResource ( AreaShop . defaultFile ) ;
OutputStream output = new FileOutputStream ( defaultFile )
) {
int read ;
2017-03-29 20:15:44 +02:00
byte [ ] bytes = new byte [ 1024 ] ;
while ( ( read = input . read ( bytes ) ) ! = - 1 ) {
2016-06-07 22:39:45 +02:00
output . write ( bytes , 0 , read ) ;
2017-03-29 20:15:44 +02:00
}
2016-08-01 16:18:05 +02:00
AreaShop . info ( " File with default region settings has been saved, should only happen on first startup " ) ;
2016-06-07 22:39:45 +02:00
} catch ( IOException e ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Something went wrong saving the default region settings: " + defaultFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
}
}
// Load default.yml from the plugin folder, and as backup the default one
try (
InputStreamReader custom = new InputStreamReader ( new FileInputStream ( defaultFile ) , Charsets . UTF_8 ) ;
InputStreamReader normal = new InputStreamReader ( plugin . getResource ( AreaShop . defaultFile ) , Charsets . UTF_8 )
) {
defaultConfig = YamlConfiguration . loadConfiguration ( custom ) ;
2019-01-26 21:23:49 +01:00
if ( defaultConfig . getKeys ( false ) . isEmpty ( ) ) {
2016-08-01 16:18:05 +02:00
AreaShop . warn ( " File 'default.yml' is empty, check for errors in the log. " ) ;
2016-06-07 22:39:45 +02:00
result = false ;
}
2016-12-23 14:07:26 +01:00
defaultConfigFallback = YamlConfiguration . loadConfiguration ( normal ) ;
2016-06-07 22:39:45 +02:00
} catch ( IOException e ) {
result = false ;
}
return result ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
* Load the default . yml file
* @return true if it has been loaded successfully , otherwise false
* /
public boolean loadConfigFile ( ) {
boolean result = true ;
File configFile = new File ( configPath ) ;
// Safe the file from the jar to disk if it does not exist
if ( ! configFile . exists ( ) ) {
try (
InputStream input = plugin . getResource ( AreaShop . configFile ) ;
OutputStream output = new FileOutputStream ( configFile )
) {
int read ;
2017-03-29 20:15:44 +02:00
byte [ ] bytes = new byte [ 1024 ] ;
while ( ( read = input . read ( bytes ) ) ! = - 1 ) {
2016-06-07 22:39:45 +02:00
output . write ( bytes , 0 , read ) ;
}
2016-08-01 16:18:05 +02:00
AreaShop . info ( " Default config file has been saved, should only happen on first startup " ) ;
2016-06-07 22:39:45 +02:00
} catch ( IOException e ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Something went wrong saving the config file: " + configFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
}
}
// Load config.yml from the plugin folder
try (
InputStreamReader custom = new InputStreamReader ( new FileInputStream ( configFile ) , Charsets . UTF_8 ) ;
2016-06-13 17:03:20 +02:00
InputStreamReader normal = new InputStreamReader ( plugin . getResource ( AreaShop . configFile ) , Charsets . UTF_8 ) ;
InputStreamReader hidden = new InputStreamReader ( plugin . getResource ( AreaShop . configFileHidden ) , Charsets . UTF_8 )
2016-06-07 22:39:45 +02:00
) {
config = YamlConfiguration . loadConfiguration ( custom ) ;
2019-01-26 21:23:49 +01:00
if ( config . getKeys ( false ) . isEmpty ( ) ) {
2016-08-01 16:18:05 +02:00
AreaShop . warn ( " File 'config.yml' is empty, check for errors in the log. " ) ;
2016-06-07 22:39:45 +02:00
result = false ;
} else {
config . addDefaults ( YamlConfiguration . loadConfiguration ( normal ) ) ;
2016-06-13 17:03:20 +02:00
config . addDefaults ( YamlConfiguration . loadConfiguration ( hidden ) ) ;
2016-06-07 22:39:45 +02:00
// Set the debug and chatprefix variables
plugin . setDebug ( this . getConfig ( ) . getBoolean ( " debug " ) ) ;
if ( getConfig ( ) . isList ( " chatPrefix " ) ) {
plugin . setChatprefix ( getConfig ( ) . getStringList ( " chatPrefix " ) ) ;
} else {
ArrayList < String > list = new ArrayList < > ( ) ;
list . add ( getConfig ( ) . getString ( " chatPrefix " ) ) ;
plugin . setChatprefix ( list ) ;
}
}
} catch ( IOException e ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Something went wrong while reading the config.yml file: " + configFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
result = false ;
}
Utils . initialize ( config ) ;
return result ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
* Load the groups . yml file from disk
2016-06-13 20:36:17 +02:00
* @return true if succeeded , otherwise false
2016-06-07 22:39:45 +02:00
* /
public boolean loadGroupsFile ( ) {
boolean result = true ;
File groupFile = new File ( groupsPath ) ;
if ( groupFile . exists ( ) & & groupFile . isFile ( ) ) {
try (
InputStreamReader reader = new InputStreamReader ( new FileInputStream ( groupFile ) , Charsets . UTF_8 )
) {
groupsConfig = YamlConfiguration . loadConfiguration ( reader ) ;
} catch ( IOException e ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not load groups.yml file: " + groupFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
}
}
if ( groupsConfig = = null ) {
groupsConfig = new YamlConfiguration ( ) ;
}
for ( String groupName : groupsConfig . getKeys ( false ) ) {
RegionGroup group = new RegionGroup ( plugin , groupName ) ;
groups . put ( groupName , group ) ;
}
return result ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Load all region files .
2016-06-07 22:39:45 +02:00
* /
2017-04-17 21:32:15 +02:00
public void loadRegionFiles ( ) {
2016-06-07 22:39:45 +02:00
regions . clear ( ) ;
2016-12-04 21:14:22 +01:00
final File file = new File ( regionsPath ) ;
2016-06-07 22:39:45 +02:00
if ( ! file . exists ( ) ) {
if ( ! file . mkdirs ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not create region files directory: " + file . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
return ;
}
plugin . setReady ( true ) ;
} else if ( file . isDirectory ( ) ) {
2017-04-17 21:32:15 +02:00
loadRegionFilesNow ( ) ;
2016-12-25 23:02:23 +01:00
}
}
2016-12-04 21:14:22 +01:00
2016-12-25 23:02:23 +01:00
private void loadRegionFilesNow ( ) {
File file = new File ( regionsPath ) ;
File [ ] regionFiles = file . listFiles ( ) ;
if ( regionFiles = = null ) {
plugin . setReady ( true ) ;
return ;
}
2016-12-04 21:14:22 +01:00
2016-12-25 23:02:23 +01:00
List < String > noRegionType = new ArrayList < > ( ) ;
List < String > noNamePaths = new ArrayList < > ( ) ;
List < GeneralRegion > noWorld = new ArrayList < > ( ) ;
List < GeneralRegion > noRegion = new ArrayList < > ( ) ;
List < GeneralRegion > incorrectDuration = new ArrayList < > ( ) ;
for ( File regionFile : regionFiles ) {
2018-05-29 22:58:15 +02:00
if ( regionFile . exists ( ) & & regionFile . isFile ( ) & & ! regionFile . isHidden ( ) ) {
2016-12-04 21:14:22 +01:00
2016-12-25 23:02:23 +01:00
// Load the region file from disk in UTF8 mode
2019-01-26 21:23:49 +01:00
YamlConfiguration regionConfig ;
2016-12-25 23:02:23 +01:00
try (
InputStreamReader reader = new InputStreamReader ( new FileInputStream ( regionFile ) , Charsets . UTF_8 )
) {
2019-01-26 21:23:49 +01:00
regionConfig = YamlConfiguration . loadConfiguration ( reader ) ;
if ( regionConfig . getKeys ( false ) . isEmpty ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Region file ' " + regionFile . getName ( ) + " ' is empty, check for errors in the log. " ) ;
2016-12-04 21:14:22 +01:00
}
2016-12-25 23:02:23 +01:00
} catch ( IOException e ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Something went wrong reading region file: " + regionFile . getAbsolutePath ( ) ) ;
2016-12-25 23:02:23 +01:00
continue ;
}
2016-12-04 21:14:22 +01:00
2016-12-25 23:02:23 +01:00
// Construct the correct type of region
2019-01-26 21:23:49 +01:00
String type = regionConfig . getString ( " general.type " ) ;
2016-12-25 23:02:23 +01:00
GeneralRegion region ;
if ( RegionType . RENT . getValue ( ) . equals ( type ) ) {
2019-01-26 21:23:49 +01:00
region = new RentRegion ( regionConfig ) ;
2016-12-25 23:02:23 +01:00
} else if ( RegionType . BUY . getValue ( ) . equals ( type ) ) {
2019-01-26 21:23:49 +01:00
region = new BuyRegion ( regionConfig ) ;
2016-12-25 23:02:23 +01:00
} else {
2019-01-26 21:23:49 +01:00
noRegionType . add ( regionFile . getPath ( ) ) ;
2016-12-25 23:02:23 +01:00
continue ;
}
2016-12-04 21:14:22 +01:00
2016-12-25 23:02:23 +01:00
// Check consistency
boolean added = false ;
if ( region . getName ( ) = = null ) {
noNamePaths . add ( regionFile . getPath ( ) ) ;
} else if ( region . getWorld ( ) = = null ) {
noWorld . add ( region ) ;
} else if ( region . getRegion ( ) = = null ) {
noRegion . add ( region ) ;
} else if ( region instanceof RentRegion & & ! Utils . checkTimeFormat ( ( ( RentRegion ) region ) . getDurationString ( ) ) ) {
incorrectDuration . add ( region ) ;
} else {
added = true ;
2019-01-28 23:37:43 +01:00
addRegionNoSave ( region ) ;
2016-12-25 23:02:23 +01:00
}
if ( ! added ) {
region . destroy ( ) ;
}
}
}
2016-12-04 21:14:22 +01:00
2016-12-25 23:02:23 +01:00
// All files are loaded, print problems to the console
if ( ! noRegionType . isEmpty ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " The following region files do no have a region type: " + Utils . createCommaSeparatedList ( noRegionType ) ) ;
2016-12-25 23:02:23 +01:00
}
2016-12-04 21:14:22 +01:00
2016-12-25 23:02:23 +01:00
if ( ! noNamePaths . isEmpty ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " The following region files do no have a name in their file: " + Utils . createCommaSeparatedList ( noNamePaths ) ) ;
2016-12-25 23:02:23 +01:00
}
2016-12-04 21:14:22 +01:00
2016-12-25 23:02:23 +01:00
if ( ! noRegion . isEmpty ( ) ) {
List < String > noRegionNames = new ArrayList < > ( ) ;
for ( GeneralRegion region : noRegion ) {
noRegionNames . add ( region . getName ( ) ) ;
}
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " AreaShop regions that are missing their WorldGuard region: " + Utils . createCommaSeparatedList ( noRegionNames ) ) ;
2016-12-25 23:02:23 +01:00
AreaShop . warn ( " Remove these regions from AreaShop with '/as del' or recreate their regions in WorldGuard. " ) ;
}
boolean noWorldRegions = ! noWorld . isEmpty ( ) ;
while ( ! noWorld . isEmpty ( ) ) {
List < GeneralRegion > toDisplay = new ArrayList < > ( ) ;
String missingWorld = noWorld . get ( 0 ) . getWorldName ( ) ;
toDisplay . add ( noWorld . get ( 0 ) ) ;
for ( int i = 1 ; i < noWorld . size ( ) ; i + + ) {
if ( noWorld . get ( i ) . getWorldName ( ) . equalsIgnoreCase ( missingWorld ) ) {
toDisplay . add ( noWorld . get ( i ) ) ;
2016-06-07 22:39:45 +02:00
}
2016-12-25 23:02:23 +01:00
}
List < String > noWorldNames = new ArrayList < > ( ) ;
2018-04-02 20:00:00 +02:00
for ( GeneralRegion region : toDisplay ) {
2016-12-25 23:02:23 +01:00
noWorldNames . add ( region . getName ( ) ) ;
}
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " World " + missingWorld + " is not loaded, the following AreaShop regions are not functional now: " + Utils . createCommaSeparatedList ( noWorldNames ) ) ;
2016-12-25 23:02:23 +01:00
noWorld . removeAll ( toDisplay ) ;
}
if ( noWorldRegions ) {
AreaShop . warn ( " Remove these regions from AreaShop with '/as del' or load the world(s) on the server again. " ) ;
}
2016-12-04 21:14:22 +01:00
2016-12-25 23:02:23 +01:00
if ( ! incorrectDuration . isEmpty ( ) ) {
List < String > incorrectDurationNames = new ArrayList < > ( ) ;
for ( GeneralRegion region : incorrectDuration ) {
incorrectDurationNames . add ( region . getName ( ) ) ;
}
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " The following regions have an incorrect time format as duration: " + Utils . createCommaSeparatedList ( incorrectDurationNames ) ) ;
2016-06-07 22:39:45 +02:00
}
2016-12-25 23:02:23 +01:00
plugin . setReady ( true ) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
* Checks for old file formats and converts them to the latest format .
2017-03-29 20:15:44 +02:00
* After conversion the region files need to be loaded .
2016-06-07 22:39:45 +02:00
* /
@SuppressWarnings ( " unchecked " )
2017-03-02 18:13:52 +01:00
private void preUpdateFiles ( ) {
2016-06-07 22:39:45 +02:00
Integer fileStatus = versions . get ( AreaShop . versionFiles ) ;
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
// If the the files are already the current version
if ( fileStatus ! = null & & fileStatus = = AreaShop . versionFilesCurrent ) {
return ;
}
2016-08-01 16:18:05 +02:00
AreaShop . info ( " Updating AreaShop data to the latest format: " ) ;
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
// Update to YAML based format
if ( fileStatus = = null | | fileStatus < 2 ) {
String rentPath = plugin . getDataFolder ( ) + File . separator + " rents " ;
String buyPath = plugin . getDataFolder ( ) + File . separator + " buys " ;
File rentFile = new File ( rentPath ) ;
File buyFile = new File ( buyPath ) ;
String oldFolderPath = plugin . getDataFolder ( ) + File . separator + " #old " + File . separator ;
File oldFolderFile = new File ( oldFolderPath ) ;
// Convert old rent files
boolean buyFileFound = false , rentFileFound = false ;
if ( rentFile . exists ( ) ) {
rentFileFound = true ;
if ( ! oldFolderFile . exists ( ) & ! oldFolderFile . mkdirs ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not create directory: " + oldFolderFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2017-11-26 16:01:05 +01:00
versions . putIfAbsent ( " rents " , - 1 ) ;
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
HashMap < String , HashMap < String , String > > rents = null ;
try {
ObjectInputStream input = new ObjectInputStream ( new FileInputStream ( rentPath ) ) ;
2017-03-29 20:15:44 +02:00
rents = ( HashMap < String , HashMap < String , String > > ) input . readObject ( ) ;
2016-06-07 22:39:45 +02:00
input . close ( ) ;
2017-03-29 20:15:44 +02:00
} catch ( IOException | ClassNotFoundException | ClassCastException e ) {
AreaShop . warn ( " Error: Something went wrong reading file: " + rentPath ) ;
2016-06-07 22:39:45 +02:00
}
// Delete the file if it is totally wrong
if ( rents = = null ) {
try {
if ( ! rentFile . delete ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not delete file: " + rentFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
}
} catch ( Exception e ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not delete file: " + rentFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
}
} else {
// Move old file
try {
Files . move ( new File ( rentPath ) , new File ( oldFolderPath + " rents " ) ) ;
2017-03-29 20:15:44 +02:00
} catch ( Exception e ) {
AreaShop . warn ( " Could not create a backup of ' " + rentPath + " ', check the file permissions (conversion to next version continues) " ) ;
2016-06-07 22:39:45 +02:00
}
// Check if conversion is needed
if ( versions . get ( " rents " ) < 1 ) {
// Upgrade the rent to the latest version
if ( versions . get ( " rents " ) < 0 ) {
for ( String rentName : rents . keySet ( ) ) {
2017-03-29 20:15:44 +02:00
HashMap < String , String > rent = rents . get ( rentName ) ;
2016-06-07 22:39:45 +02:00
// Save the rentName in the hashmap and use a small caps rentName as key
if ( rent . get ( " name " ) = = null ) {
rent . put ( " name " , rentName ) ;
rents . remove ( rentName ) ;
rents . put ( rentName . toLowerCase ( ) , rent ) ;
}
// Save the default setting for region restoring
2017-11-26 16:01:05 +01:00
rent . putIfAbsent ( " restore " , " general " ) ;
2016-06-07 22:39:45 +02:00
// Save the default setting for the region restore profile
2017-11-26 16:01:05 +01:00
rent . putIfAbsent ( " profile " , " default " ) ;
2016-06-07 22:39:45 +02:00
// Change to version 0
versions . put ( " rents " , 0 ) ;
}
2017-03-29 20:15:44 +02:00
AreaShop . info ( " Updated version of ' " + buyPath + " ' from -1 to 0 (switch to using lowercase region names, adding default schematic enabling and profile) " ) ;
2016-06-07 22:39:45 +02:00
}
if ( versions . get ( " rents " ) < 1 ) {
for ( String rentName : rents . keySet ( ) ) {
2017-03-29 20:15:44 +02:00
HashMap < String , String > rent = rents . get ( rentName ) ;
2016-06-07 22:39:45 +02:00
if ( rent . get ( " player " ) ! = null ) {
@SuppressWarnings ( " deprecation " ) // Fake deprecation by Bukkit to inform developers, method will stay
2017-11-26 15:47:44 +01:00
OfflinePlayer offlinePlayer = Bukkit . getOfflinePlayer ( rent . get ( " player " ) ) ;
2017-03-29 20:15:44 +02:00
rent . put ( " playeruuid " , offlinePlayer . getUniqueId ( ) . toString ( ) ) ;
2016-06-07 22:39:45 +02:00
rent . remove ( " player " ) ;
}
// Change version to 1
versions . put ( " rents " , 1 ) ;
}
2017-03-29 20:15:44 +02:00
AreaShop . info ( " Updated version of ' " + rentPath + " ' from 0 to 1 (switch to UUID's for player identification) " ) ;
}
}
2016-06-07 22:39:45 +02:00
// Save rents to new format
File regionsFile = new File ( regionsPath ) ;
if ( ! regionsFile . exists ( ) & ! regionsFile . mkdirs ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not create directory: " + regionsFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
return ;
}
for ( HashMap < String , String > rent : rents . values ( ) ) {
2019-01-26 21:23:49 +01:00
YamlConfiguration regionConfig = new YamlConfiguration ( ) ;
regionConfig . set ( " general.name " , rent . get ( " name " ) . toLowerCase ( ) ) ;
regionConfig . set ( " general.type " , " rent " ) ;
regionConfig . set ( " general.world " , rent . get ( " world " ) ) ;
regionConfig . set ( " general.signs.0.location.world " , rent . get ( " world " ) ) ;
regionConfig . set ( " general.signs.0.location.x " , Double . parseDouble ( rent . get ( " x " ) ) ) ;
regionConfig . set ( " general.signs.0.location.y " , Double . parseDouble ( rent . get ( " y " ) ) ) ;
regionConfig . set ( " general.signs.0.location.z " , Double . parseDouble ( rent . get ( " z " ) ) ) ;
regionConfig . set ( " rent.price " , Double . parseDouble ( rent . get ( " price " ) ) ) ;
regionConfig . set ( " rent.duration " , rent . get ( " duration " ) ) ;
2016-06-07 22:39:45 +02:00
if ( rent . get ( " restore " ) ! = null & & ! rent . get ( " restore " ) . equals ( " general " ) ) {
2019-01-26 21:23:49 +01:00
regionConfig . set ( " general.enableRestore " , rent . get ( " restore " ) ) ;
2016-06-07 22:39:45 +02:00
}
if ( rent . get ( " profile " ) ! = null & & ! rent . get ( " profile " ) . equals ( " default " ) ) {
2019-01-26 21:23:49 +01:00
regionConfig . set ( " general.schematicProfile " , rent . get ( " profile " ) ) ;
2016-06-07 22:39:45 +02:00
}
if ( rent . get ( " tpx " ) ! = null ) {
2019-01-26 21:23:49 +01:00
regionConfig . set ( " general.teleportLocation.world " , rent . get ( " world " ) ) ;
regionConfig . set ( " general.teleportLocation.x " , Double . parseDouble ( rent . get ( " tpx " ) ) ) ;
regionConfig . set ( " general.teleportLocation.y " , Double . parseDouble ( rent . get ( " tpy " ) ) ) ;
regionConfig . set ( " general.teleportLocation.z " , Double . parseDouble ( rent . get ( " tpz " ) ) ) ;
regionConfig . set ( " general.teleportLocation.yaw " , rent . get ( " tpyaw " ) ) ;
regionConfig . set ( " general.teleportLocation.pitch " , rent . get ( " tppitch " ) ) ;
2016-06-07 22:39:45 +02:00
}
if ( rent . get ( " playeruuid " ) ! = null ) {
2019-01-26 21:23:49 +01:00
regionConfig . set ( " rent.renter " , rent . get ( " playeruuid " ) ) ;
regionConfig . set ( " rent.renterName " , Utils . toName ( rent . get ( " playeruuid " ) ) ) ;
regionConfig . set ( " rent.rentedUntil " , Long . parseLong ( rent . get ( " rented " ) ) ) ;
2016-06-07 22:39:45 +02:00
}
try {
2019-01-26 21:23:49 +01:00
regionConfig . save ( new File ( regionsPath + File . separator + rent . get ( " name " ) . toLowerCase ( ) + " .yml " ) ) ;
2017-03-29 20:15:44 +02:00
} catch ( IOException e ) {
AreaShop . warn ( " Error: Could not save region file while converting: " + regionsPath + File . separator + rent . get ( " name " ) . toLowerCase ( ) + " .yml " ) ;
2016-06-07 22:39:45 +02:00
}
}
2016-08-01 16:18:05 +02:00
AreaShop . info ( " Updated rent regions to new .yml format (check the /regions folder) " ) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
// Change version number
versions . remove ( " rents " ) ;
2017-03-29 20:15:44 +02:00
versions . put ( AreaShop . versionFiles , AreaShop . versionFilesCurrent ) ;
saveVersions ( ) ;
2016-06-07 22:39:45 +02:00
}
if ( buyFile . exists ( ) ) {
buyFileFound = true ;
if ( ! oldFolderFile . exists ( ) & ! oldFolderFile . mkdirs ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not create directory: " + oldFolderFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
return ;
}
2017-03-29 20:15:44 +02:00
2017-11-26 16:01:05 +01:00
versions . putIfAbsent ( " buys " , - 1 ) ;
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
HashMap < String , HashMap < String , String > > buys = null ;
try {
ObjectInputStream input = new ObjectInputStream ( new FileInputStream ( buyPath ) ) ;
2017-03-29 20:15:44 +02:00
buys = ( HashMap < String , HashMap < String , String > > ) input . readObject ( ) ;
2016-06-07 22:39:45 +02:00
input . close ( ) ;
2017-03-29 20:15:44 +02:00
} catch ( IOException | ClassNotFoundException | ClassCastException e ) {
AreaShop . warn ( " Something went wrong reading file: " + buyPath ) ;
2016-06-07 22:39:45 +02:00
}
// Delete the file if it is totally wrong
if ( buys = = null ) {
try {
if ( ! buyFile . delete ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not delete file: " + buyFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
}
} catch ( Exception e ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not delete file: " + buyFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
}
} else {
// Backup current file
try {
Files . move ( new File ( buyPath ) , new File ( oldFolderPath + " buys " ) ) ;
2017-03-29 20:15:44 +02:00
} catch ( Exception e ) {
AreaShop . warn ( " Could not create a backup of ' " + buyPath + " ', check the file permissions (conversion to next version continues) " ) ;
2016-06-07 22:39:45 +02:00
}
// Check if conversion is needed
if ( versions . get ( " buys " ) < 1 ) {
// Upgrade the buy to the latest version
if ( versions . get ( " buys " ) < 0 ) {
for ( String buyName : buys . keySet ( ) ) {
2017-03-29 20:15:44 +02:00
HashMap < String , String > buy = buys . get ( buyName ) ;
2016-06-07 22:39:45 +02:00
// Save the buyName in the hashmap and use a small caps buyName as key
if ( buy . get ( " name " ) = = null ) {
buy . put ( " name " , buyName ) ;
buys . remove ( buyName ) ;
buys . put ( buyName . toLowerCase ( ) , buy ) ;
}
// Save the default setting for region restoring
2017-11-26 16:01:05 +01:00
buy . putIfAbsent ( " restore " , " general " ) ;
2016-06-07 22:39:45 +02:00
// Save the default setting for the region restore profile
2017-11-26 16:01:05 +01:00
buy . putIfAbsent ( " profile " , " default " ) ;
2016-06-07 22:39:45 +02:00
// Change to version 0
versions . put ( " buys " , 0 ) ;
}
2017-03-29 20:15:44 +02:00
AreaShop . info ( " Updated version of ' " + buyPath + " ' from -1 to 0 (switch to using lowercase region names, adding default schematic enabling and profile) " ) ;
2016-06-07 22:39:45 +02:00
}
if ( versions . get ( " buys " ) < 1 ) {
for ( String buyName : buys . keySet ( ) ) {
2017-03-29 20:15:44 +02:00
HashMap < String , String > buy = buys . get ( buyName ) ;
2016-06-07 22:39:45 +02:00
if ( buy . get ( " player " ) ! = null ) {
@SuppressWarnings ( " deprecation " ) // Fake deprecation by Bukkit to inform developers, method will stay
2017-03-29 20:15:44 +02:00
OfflinePlayer offlinePlayer = Bukkit . getOfflinePlayer ( buy . get ( " player " ) ) ;
buy . put ( " playeruuid " , offlinePlayer . getUniqueId ( ) . toString ( ) ) ;
2016-06-07 22:39:45 +02:00
buy . remove ( " player " ) ;
}
// Change version to 1
versions . put ( " buys " , 1 ) ;
}
2017-03-29 20:15:44 +02:00
AreaShop . info ( " Updated version of ' " + buyPath + " ' from 0 to 1 (switch to UUID's for player identification) " ) ;
}
}
2016-06-07 22:39:45 +02:00
// Save buys to new format
File regionsFile = new File ( regionsPath ) ;
if ( ! regionsFile . exists ( ) & ! regionsFile . mkdirs ( ) ) {
2017-03-29 20:15:44 +02:00
AreaShop . warn ( " Could not create directory: " + regionsFile . getAbsolutePath ( ) ) ;
2016-06-07 22:39:45 +02:00
}
for ( HashMap < String , String > buy : buys . values ( ) ) {
2019-01-26 21:23:49 +01:00
YamlConfiguration regionConfig = new YamlConfiguration ( ) ;
regionConfig . set ( " general.name " , buy . get ( " name " ) . toLowerCase ( ) ) ;
regionConfig . set ( " general.type " , " buy " ) ;
regionConfig . set ( " general.world " , buy . get ( " world " ) ) ;
regionConfig . set ( " general.signs.0.location.world " , buy . get ( " world " ) ) ;
regionConfig . set ( " general.signs.0.location.x " , Double . parseDouble ( buy . get ( " x " ) ) ) ;
regionConfig . set ( " general.signs.0.location.y " , Double . parseDouble ( buy . get ( " y " ) ) ) ;
regionConfig . set ( " general.signs.0.location.z " , Double . parseDouble ( buy . get ( " z " ) ) ) ;
regionConfig . set ( " buy.price " , Double . parseDouble ( buy . get ( " price " ) ) ) ;
2016-06-07 22:39:45 +02:00
if ( buy . get ( " restore " ) ! = null & & ! buy . get ( " restore " ) . equals ( " general " ) ) {
2019-01-26 21:23:49 +01:00
regionConfig . set ( " general.enableRestore " , buy . get ( " restore " ) ) ;
2016-06-07 22:39:45 +02:00
}
if ( buy . get ( " profile " ) ! = null & & ! buy . get ( " profile " ) . equals ( " default " ) ) {
2019-01-26 21:23:49 +01:00
regionConfig . set ( " general.schematicProfile " , buy . get ( " profile " ) ) ;
2016-06-07 22:39:45 +02:00
}
if ( buy . get ( " tpx " ) ! = null ) {
2019-01-26 21:23:49 +01:00
regionConfig . set ( " general.teleportLocation.world " , buy . get ( " world " ) ) ;
regionConfig . set ( " general.teleportLocation.x " , Double . parseDouble ( buy . get ( " tpx " ) ) ) ;
regionConfig . set ( " general.teleportLocation.y " , Double . parseDouble ( buy . get ( " tpy " ) ) ) ;
regionConfig . set ( " general.teleportLocation.z " , Double . parseDouble ( buy . get ( " tpz " ) ) ) ;
regionConfig . set ( " general.teleportLocation.yaw " , buy . get ( " tpyaw " ) ) ;
regionConfig . set ( " general.teleportLocation.pitch " , buy . get ( " tppitch " ) ) ;
2016-06-07 22:39:45 +02:00
}
if ( buy . get ( " playeruuid " ) ! = null ) {
2019-01-26 21:23:49 +01:00
regionConfig . set ( " buy.buyer " , buy . get ( " playeruuid " ) ) ;
regionConfig . set ( " buy.buyerName " , Utils . toName ( buy . get ( " playeruuid " ) ) ) ;
2016-06-07 22:39:45 +02:00
}
try {
2019-01-26 21:23:49 +01:00
regionConfig . save ( new File ( regionsPath + File . separator + buy . get ( " name " ) . toLowerCase ( ) + " .yml " ) ) ;
2017-03-29 20:15:44 +02:00
} catch ( IOException e ) {
AreaShop . warn ( " Error: Could not save region file while converting: " + regionsPath + File . separator + buy . get ( " name " ) . toLowerCase ( ) + " .yml " ) ;
2016-06-07 22:39:45 +02:00
}
}
2016-08-01 16:18:05 +02:00
AreaShop . info ( " Updated buy regions to new .yml format (check the /regions folder) " ) ;
2016-06-07 22:39:45 +02:00
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
// Change version number
2017-03-29 20:15:44 +02:00
versions . remove ( " buys " ) ;
2016-06-07 22:39:45 +02:00
}
// Separate try-catch blocks to try them all individually (don't stop after 1 has failed)
try {
Files . move ( new File ( rentPath + " .old " ) , new File ( oldFolderPath + " rents.old " ) ) ;
} catch ( Exception e ) {
// Ignore
}
try {
Files . move ( new File ( buyPath + " .old " ) , new File ( oldFolderPath + " buys.old " ) ) ;
} catch ( Exception e ) {
// Ignore
}
if ( buyFileFound | | rentFileFound ) {
try {
Files . move ( new File ( plugin . getDataFolder ( ) + File . separator + " config.yml " ) , new File ( oldFolderPath + " config.yml " ) ) ;
} catch ( Exception e ) {
// Ignore
}
}
// Update versions file to 2
2017-03-29 20:15:44 +02:00
versions . put ( AreaShop . versionFiles , 2 ) ;
2016-06-07 22:39:45 +02:00
saveVersions ( ) ;
2017-03-29 20:15:44 +02:00
if ( buyFileFound | | rentFileFound ) {
2017-03-02 18:13:52 +01:00
AreaShop . info ( " Updated to YAML based storage (v1 to v2) " ) ;
}
2016-06-07 22:39:45 +02:00
}
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
* Checks for old file formats and converts them to the latest format .
2017-03-29 20:15:44 +02:00
* This is to be triggered after the load of the region files .
2016-06-07 22:39:45 +02:00
* /
2017-03-02 18:13:52 +01:00
private void postUpdateFiles ( ) {
2016-06-07 22:39:45 +02:00
Integer fileStatus = versions . get ( AreaShop . versionFiles ) ;
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
// If the the files are already the current version
if ( fileStatus ! = null & & fileStatus = = AreaShop . versionFilesCurrent ) {
return ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
// Add 'general.lastActive' to rented/bought regions (initialize at current time)
if ( fileStatus = = null | | fileStatus < 3 ) {
for ( GeneralRegion region : getRegions ( ) ) {
region . updateLastActiveTime ( ) ;
}
// Update versions file to 3
2017-03-29 20:15:44 +02:00
versions . put ( AreaShop . versionFiles , 3 ) ;
2016-06-07 22:39:45 +02:00
saveVersions ( ) ;
2019-01-26 21:23:49 +01:00
if ( ! getRegions ( ) . isEmpty ( ) ) {
2017-03-02 18:13:52 +01:00
AreaShop . info ( " Added last active time to regions (v2 to v3) " ) ;
}
2016-06-07 22:39:45 +02:00
}
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Get the settings of a group .
2016-06-07 22:39:45 +02:00
* @param groupName Name of the group to get the settings from
* @return The settings of the group
* /
public ConfigurationSection getGroupSettings ( String groupName ) {
return groupsConfig . getConfigurationSection ( groupName . toLowerCase ( ) ) ;
}
2017-03-29 20:15:44 +02:00
2016-06-07 22:39:45 +02:00
/ * *
2017-03-29 20:15:44 +02:00
* Set a setting for a group .
* @param group The group to set it for
* @param path The path to set
2016-06-07 22:39:45 +02:00
* @param setting The value to set
* /
public void setGroupSetting ( RegionGroup group , String path , Object setting ) {
groupsConfig . set ( group . getName ( ) . toLowerCase ( ) + " . " + path , setting ) ;
}
}