Prevent actions on regions when it is already deleted from AreaShop

When a region is deleted with '/as del' it wil set a flag in
GeneralRegion to indicate it is deleted (isDeleted() can get it). If
this flag is set then no sign updates, saves to disk etc are being
processed by the tasks anymore. This prevents the following case: #1
task starts saving regions (makes copy of region list). #2 User 
executes '/as del' for a region, removes region file. #3 Task arrives at
the region the user just deleted and saves it to disk again #4 The
region will get loaded again next time the server starts.
This commit is contained in:
Thijs Wiefferink 2014-12-04 11:50:40 +01:00
parent e719930364
commit 0c79525144
4 changed files with 50 additions and 23 deletions

View File

@ -246,6 +246,7 @@ public class FileManager {
public boolean removeRent(RentRegion rent, boolean giveMoneyBack) {
boolean result = false;
if(rent != null) {
rent.setDeleted();
if(rent.isRented()) {
rent.unRent(giveMoneyBack);
}
@ -257,6 +258,7 @@ public class FileManager {
if(rent.getWorld() != null) {
for(Location sign : rent.getSignLocations()) {
sign.getBlock().setType(Material.AIR);
AreaShop.debug("Removed sign at: " + sign.toString());
}
}
RegionGroup[] groups = getGroups().toArray(new RegionGroup[getGroups().size()]);
@ -268,16 +270,16 @@ public class FileManager {
regions.remove(rent.getLowerCaseName());
File file = new File(plugin.getDataFolder() + File.separator + AreaShop.regionsFolder + File.separator + rent.getLowerCaseName() + ".yml");
boolean deleted = true;
try {
deleted = file.delete();
} catch(Exception e) {
deleted = false;
if(file.exists()) {
try {
deleted = file.delete();
} catch(Exception e) {
deleted = false;
}
if(!deleted) {
plugin.getLogger().warning("File could not be deleted: " + file.toString());
}
}
if(!deleted) {
plugin.getLogger().warning("File could not be deleted: " + file.toString());
}
result = true;
// Run commands
@ -307,6 +309,7 @@ public class FileManager {
public boolean removeBuy(BuyRegion buy, boolean giveMoneyBack) {
boolean result = false;
if(buy != null) {
buy.setDeleted();
if(buy.isSold()) {
buy.sell(giveMoneyBack);
}
@ -319,7 +322,7 @@ public class FileManager {
for(Location sign : buy.getSignLocations()) {
sign.getBlock().setType(Material.AIR);
}
}
}
regions.remove(buy.getLowerCaseName());
buy.resetRegionFlags();
@ -332,13 +335,15 @@ public class FileManager {
// Deleting the file
File file = new File(plugin.getDataFolder() + File.separator + AreaShop.regionsFolder + File.separator + buy.getLowerCaseName() + ".yml");
boolean deleted = true;
try {
deleted = file.delete();
} catch(Exception e) {
deleted = false;
}
if(!deleted) {
plugin.getLogger().warning("File could not be deleted: " + file.toString());
if(file.exists()) {
try {
deleted = file.delete();
} catch(Exception e) {
deleted = false;
}
if(!deleted) {
plugin.getLogger().warning("File could not be deleted: " + file.toString());
}
}
result = true;

View File

@ -397,7 +397,7 @@ public class BuyRegion extends GeneralRegion {
@Override
public boolean checkInactive() {
if(!isSold()) {
if(isDeleted() || !isSold()) {
return false;
}
OfflinePlayer player = Bukkit.getOfflinePlayer(getBuyer());

View File

@ -55,6 +55,7 @@ public abstract class GeneralRegion {
private static ArrayList<Material> cannotSpawnBeside = new ArrayList<Material>(Arrays.asList(Material.LAVA, Material.STATIONARY_LAVA, Material.CACTUS));
protected AreaShop plugin = null;
private boolean saveRequired = false;
private boolean deleted = false;
/* Enum for region types */
public enum RegionType {
@ -236,6 +237,21 @@ public abstract class GeneralRegion {
return result;
}
/**
* Check if the region has been deleted
* @return
*/
public boolean isDeleted() {
return deleted;
}
/**
* Indicate that this region has been deleted
*/
public void setDeleted() {
deleted = true;
}
/**
* Get the name of the region
* @return The region name
@ -635,7 +651,7 @@ public abstract class GeneralRegion {
* @return
*/
public boolean needsPeriodicUpdating() {
if(!isRentRegion()) {
if(isDeleted() || !isRentRegion()) {
return false;
}
Set<String> signs = null;
@ -667,6 +683,9 @@ public abstract class GeneralRegion {
* @return true if the update was successful, otherwise false
*/
public boolean updateSigns() {
if(isDeleted()) {
return false;
}
boolean result = true;
Set<String> signs = null;
if(config.getConfigurationSection("general.signs") != null) {
@ -1138,7 +1157,7 @@ public abstract class GeneralRegion {
* @return true if a save is required because some data changed, otherwise false
*/
public boolean isSaveRequired() {
return saveRequired;
return saveRequired && !isDeleted();
}
/**
@ -1146,6 +1165,9 @@ public abstract class GeneralRegion {
* @return true if the region is saved successfully, otherwise false
*/
public boolean saveNow() {
if(isDeleted()) {
return false;
}
saveRequired = false;
File file = new File(plugin.getFileManager().getRegionFolder() + File.separator + getName().toLowerCase() + ".yml");
try {

View File

@ -364,7 +364,7 @@ public class RentRegion extends GeneralRegion {
*/
public boolean checkExpiration() {
long now = Calendar.getInstance().getTimeInMillis();
if(isRented() && now > getRentedUntil()) {
if(!isDeleted() && isRented() && now > getRentedUntil()) {
/* Send message to the player if online */
Player player = Bukkit.getPlayer(getRenter());
if(player != null) {
@ -382,7 +382,7 @@ public class RentRegion extends GeneralRegion {
*/
public void sendExpirationWarnings() {
// send from warningsDoneUntil to current+delay
if(!isRented()) {
if(isDeleted() || !isRented()) {
return;
}
Player player = Bukkit.getPlayer(getRenter());
@ -599,7 +599,7 @@ public class RentRegion extends GeneralRegion {
@Override
public boolean checkInactive() {
if(!isRented()) {
if(isDeleted() || !isRented()) {
return false;
}
OfflinePlayer player = Bukkit.getOfflinePlayer(getRenter());