use of me.wiefferink.areashop.regions.RentRegion 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.RentRegion 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.RentRegion in project AreaShop by NLthijs48.
the class SetdurationCommand method execute.
@Override
public void execute(CommandSender sender, String[] args) {
if (!sender.hasPermission("areashop.setduration") && (!sender.hasPermission("areashop.setduration.landlord") && sender instanceof Player)) {
plugin.message(sender, "setduration-noPermission");
return;
}
if (args.length < 3 || args[1] == null || args[2] == null) {
plugin.message(sender, "setduration-help");
return;
}
int regionArgument = 3;
if (args.length >= 2 && ("default".equalsIgnoreCase(args[1]) || "reset".equalsIgnoreCase(args[1]))) {
regionArgument = 2;
}
RentRegion rent;
if (args.length <= regionArgument) {
if (sender instanceof Player) {
// get the region by location
List<RentRegion> regions = Utils.getImportantRentRegions(((Player) sender).getLocation());
if (regions.isEmpty()) {
plugin.message(sender, "cmd-noRegionsAtLocation");
return;
} else if (regions.size() > 1) {
plugin.message(sender, "cmd-moreRegionsAtLocation");
return;
} else {
rent = regions.get(0);
}
} else {
plugin.message(sender, "cmd-automaticRegionOnlyByPlayer");
return;
}
} else {
rent = plugin.getFileManager().getRent(args[regionArgument]);
}
if (rent == null) {
plugin.message(sender, "setduration-notRegistered", args[regionArgument]);
return;
}
if (!sender.hasPermission("areashop.setduration") && !(sender instanceof Player && rent.isLandlord(((Player) sender).getUniqueId()))) {
plugin.message(sender, "setduration-noLandlord", rent);
return;
}
if ("default".equalsIgnoreCase(args[1]) || "reset".equalsIgnoreCase(args[1])) {
rent.setDuration(null);
rent.update();
plugin.message(sender, "setduration-successRemoved", rent);
return;
}
try {
Integer.parseInt(args[1]);
} catch (NumberFormatException e) {
plugin.message(sender, "setduration-wrongAmount", args[1], rent);
return;
}
if (!Utils.checkTimeFormat(args[1] + " " + args[2])) {
plugin.message(sender, "setduration-wrongFormat", args[1] + " " + args[2], rent);
return;
}
rent.setDuration(args[1] + " " + args[2]);
rent.update();
plugin.message(sender, "setduration-success", rent);
}
use of me.wiefferink.areashop.regions.RentRegion 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();
}
use of me.wiefferink.areashop.regions.RentRegion in project AreaShop by NLthijs48.
the class AddfriendCommand method execute.
@SuppressWarnings("deprecation")
@Override
public void execute(CommandSender sender, String[] args) {
if (!sender.hasPermission("areashop.addfriend") && !sender.hasPermission("areashop.addfriendall")) {
plugin.message(sender, "addfriend-noPermission");
return;
}
if (args.length < 2) {
plugin.message(sender, "addfriend-help");
return;
}
GeneralRegion region;
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, "cmd-notRegistered", args[2]);
return;
}
}
if (sender.hasPermission("areashop.addfriendall")) {
if ((region instanceof RentRegion && !((RentRegion) region).isRented()) || (region instanceof BuyRegion && !((BuyRegion) region).isSold())) {
plugin.message(sender, "addfriend-noOwner", region);
return;
}
OfflinePlayer friend = Bukkit.getOfflinePlayer(args[1]);
if (friend.getLastPlayed() == 0 && !friend.isOnline() && !plugin.getConfig().getBoolean("addFriendNotExistingPlayers")) {
plugin.message(sender, "addfriend-notVisited", args[1], region);
return;
}
if (region.getFriendsFeature().getFriends().contains(friend.getUniqueId())) {
plugin.message(sender, "addfriend-alreadyAdded", friend.getName(), region);
return;
}
if (region.isOwner(friend.getUniqueId())) {
plugin.message(sender, "addfriend-self", friend.getName(), region);
return;
}
if (region.getFriendsFeature().addFriend(friend.getUniqueId(), sender)) {
region.update();
plugin.message(sender, "addfriend-successOther", friend.getName(), region);
}
} else {
if (sender.hasPermission("areashop.addfriend") && sender instanceof Player) {
if (region.isOwner((Player) sender)) {
OfflinePlayer friend = Bukkit.getOfflinePlayer(args[1]);
if (friend.getLastPlayed() == 0 && !friend.isOnline() && !plugin.getConfig().getBoolean("addFriendNotExistingPlayers")) {
plugin.message(sender, "addfriend-notVisited", args[1], region);
return;
}
if (region.getFriendsFeature().getFriends().contains(friend.getUniqueId())) {
plugin.message(sender, "addfriend-alreadyAdded", friend.getName(), region);
return;
}
if (region.isOwner(friend.getUniqueId())) {
plugin.message(sender, "addfriend-self", friend.getName(), region);
return;
}
if (region.getFriendsFeature().addFriend(friend.getUniqueId(), sender)) {
region.update();
plugin.message(sender, "addfriend-success", friend.getName(), region);
}
} else {
plugin.message(sender, "addfriend-noPermissionOther", region);
}
} else {
plugin.message(sender, "addfriend-noPermission", region);
}
}
}
Aggregations