use of me.wiefferink.areashop.regions.GeneralRegion in project AreaShop by NLthijs48.
the class FileManager method checkRegionAdd.
/**
* Check if a player can add a certain region as rent or buy region.
* @param sender The player/console that wants to add a region
* @param region The WorldGuard region to add
* @param type The type the region should have in AreaShop
* @return The result if a player would want to add this region
*/
public AddResult checkRegionAdd(CommandSender sender, ProtectedRegion region, RegionType type) {
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());
boolean isOwner = player != null && plugin.getWorldGuardHandler().containsOwner(region, player.getUniqueId());
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) {
return AddResult.ALREADYADDED;
} else if (plugin.getFileManager().isBlacklisted(region.getId())) {
return AddResult.BLACKLISTED;
} else {
return AddResult.SUCCESS;
}
}
use of me.wiefferink.areashop.regions.GeneralRegion in project AreaShop by NLthijs48.
the class FileManager method postUpdateFiles.
/**
* Checks for old file formats and converts them to the latest format.
* This is to be triggered after the load of the region files.
*/
private void postUpdateFiles() {
Integer fileStatus = versions.get(AreaShop.versionFiles);
// If the the files are already the current version
if (fileStatus != null && fileStatus == AreaShop.versionFilesCurrent) {
return;
}
// 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
versions.put(AreaShop.versionFiles, 3);
saveVersions();
if (getRegions().size() > 0) {
AreaShop.info(" Added last active time to regions (v2 to v3)");
}
}
}
use of me.wiefferink.areashop.regions.GeneralRegion in project AreaShop by NLthijs48.
the class FileManager method loadRegionFilesNow.
private void loadRegionFilesNow() {
File file = new File(regionsPath);
File[] regionFiles = file.listFiles();
if (regionFiles == null) {
plugin.setReady(true);
return;
}
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) {
if (regionFile.exists() && regionFile.isFile()) {
// Load the region file from disk in UTF8 mode
YamlConfiguration config;
try (InputStreamReader reader = new InputStreamReader(new FileInputStream(regionFile), Charsets.UTF_8)) {
config = YamlConfiguration.loadConfiguration(reader);
if (config.getKeys(false).size() == 0) {
AreaShop.warn("Region file '" + regionFile.getName() + "' is empty, check for errors in the log.");
}
} catch (IOException e) {
AreaShop.warn("Something went wrong reading region file: " + regionFile.getAbsolutePath());
continue;
}
// Construct the correct type of region
String type = config.getString("general.type");
GeneralRegion region;
if (RegionType.RENT.getValue().equals(type)) {
region = new RentRegion(config);
} else if (RegionType.BUY.getValue().equals(type)) {
region = new BuyRegion(config);
} else {
noNamePaths.add(regionFile.getPath());
continue;
}
// 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;
if (region instanceof RentRegion) {
addRentNoSave((RentRegion) region);
} else if (region instanceof BuyRegion) {
addBuyNoSave((BuyRegion) region);
}
}
if (!added) {
region.destroy();
}
}
}
// All files are loaded, print problems to the console
if (!noRegionType.isEmpty()) {
AreaShop.warn("The following region files do no have a region type: " + Utils.createCommaSeparatedList(noRegionType));
}
if (!noNamePaths.isEmpty()) {
AreaShop.warn("The following region files do no have a name in their file: " + Utils.createCommaSeparatedList(noNamePaths));
}
if (!noRegion.isEmpty()) {
List<String> noRegionNames = new ArrayList<>();
for (GeneralRegion region : noRegion) {
noRegionNames.add(region.getName());
}
AreaShop.warn("AreaShop regions that are missing their WorldGuard region: " + Utils.createCommaSeparatedList(noRegionNames));
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));
}
}
List<String> noWorldNames = new ArrayList<>();
for (GeneralRegion region : toDisplay) {
noWorldNames.add(region.getName());
}
AreaShop.warn("World " + missingWorld + " is not loaded, the following AreaShop regions are not functional now: " + Utils.createCommaSeparatedList(noWorldNames));
noWorld.removeAll(toDisplay);
}
if (noWorldRegions) {
AreaShop.warn("Remove these regions from AreaShop with '/as del' or load the world(s) on the server again.");
}
if (!incorrectDuration.isEmpty()) {
List<String> incorrectDurationNames = new ArrayList<>();
for (GeneralRegion region : incorrectDuration) {
incorrectDurationNames.add(region.getName());
}
AreaShop.warn("The following regions have an incorrect time format as duration: " + Utils.createCommaSeparatedList(incorrectDurationNames));
}
plugin.setReady(true);
}
use of me.wiefferink.areashop.regions.GeneralRegion in project AreaShop by NLthijs48.
the class Utils method getImportantRegions.
/**
* Get the most important AreaShop regions.
* - Returns highest priority, child instead of parent regions.
* @param location The location to check for regions
* @param type The type of regions to look for, null for all
* @return empty list if no regions found, 1 member if 1 region is a priority, more if regions with the same priority
*/
public static List<GeneralRegion> getImportantRegions(Location location, GeneralRegion.RegionType type) {
List<GeneralRegion> result = new ArrayList<>();
Set<ProtectedRegion> regions = AreaShop.getInstance().getWorldGuardHandler().getApplicableRegionsSet(location);
if (regions != null) {
List<GeneralRegion> candidates = new ArrayList<>();
for (ProtectedRegion pr : regions) {
GeneralRegion region = AreaShop.getInstance().getFileManager().getRegion(pr.getId());
if (region != null && ((type == GeneralRegion.RegionType.RENT && region instanceof RentRegion) || (type == GeneralRegion.RegionType.BUY && region instanceof BuyRegion) || type == null)) {
candidates.add(region);
}
}
boolean first = true;
for (GeneralRegion region : candidates) {
if (region == null) {
AreaShop.debug("skipped null region");
continue;
}
if (first) {
result.add(region);
first = false;
} else {
if (region.getRegion().getPriority() > result.get(0).getRegion().getPriority()) {
result.clear();
result.add(region);
} else if (region.getRegion().getParent() != null && region.getRegion().getParent().equals(result.get(0).getRegion())) {
result.clear();
result.add(region);
} else {
result.add(region);
}
}
}
}
return new ArrayList<>(result);
}
use of me.wiefferink.areashop.regions.GeneralRegion in project AreaShop by NLthijs48.
the class SetownerCommand method execute.
@Override
public void execute(CommandSender sender, String[] args) {
if (!sender.hasPermission("areashop.setownerrent") && !sender.hasPermission("areashop.setownerbuy")) {
plugin.message(sender, "setowner-noPermission");
return;
}
GeneralRegion region;
if (args.length < 2) {
plugin.message(sender, "setowner-help");
return;
}
if (args.length == 2) {
if (sender instanceof Player) {
// get the region by location
List<GeneralRegion> regions = Utils.getImportantRegions(((Player) sender).getLocation());
if (regions.isEmpty()) {
plugin.message(sender, "cmd-noRegionsAtLocation");
return;
} else if (regions.size() > 1) {
plugin.message(sender, "cmd-moreRegionsAtLocation");
return;
} else {
region = regions.get(0);
}
} else {
plugin.message(sender, "cmd-automaticRegionOnlyByPlayer");
return;
}
} else {
region = plugin.getFileManager().getRegion(args[2]);
}
if (region == null) {
plugin.message(sender, "setowner-notRegistered");
return;
}
if (region instanceof RentRegion && !sender.hasPermission("areashop.setownerrent")) {
plugin.message(sender, "setowner-noPermissionRent", region);
return;
}
if (region instanceof BuyRegion && !sender.hasPermission("areashop.setownerbuy")) {
plugin.message(sender, "setowner-noPermissionBuy", region);
return;
}
UUID uuid = null;
@SuppressWarnings("deprecation") OfflinePlayer player = Bukkit.getOfflinePlayer(args[1]);
if (player != null) {
uuid = player.getUniqueId();
}
if (uuid == null) {
plugin.message(sender, "setowner-noPlayer", args[1], region);
return;
}
if (region instanceof RentRegion) {
RentRegion rent = (RentRegion) region;
if (rent.isRenter(uuid)) {
// extend
rent.setRentedUntil(rent.getRentedUntil() + rent.getDuration());
rent.setRenter(uuid);
plugin.message(sender, "setowner-succesRentExtend", region);
} else {
// change
if (!rent.isRented()) {
rent.setRentedUntil(Calendar.getInstance().getTimeInMillis() + rent.getDuration());
}
rent.setRenter(uuid);
plugin.message(sender, "setowner-succesRent", region);
}
}
if (region instanceof BuyRegion) {
BuyRegion buy = (BuyRegion) region;
buy.setBuyer(uuid);
plugin.message(sender, "setowner-succesBuy", region);
}
region.getFriendsFeature().deleteFriend(region.getOwner(), null);
region.update();
region.saveRequired();
}
Aggregations