Add new feature, can now use permode to give image/map count for each player/group. #154

This commit is contained in:
Vlammar 2021-11-08 18:29:31 +01:00
parent 68f37d8247
commit 6a2c531a4d
3 changed files with 67 additions and 1 deletions

View File

@ -37,7 +37,11 @@
package fr.moribus.imageonmap;
import fr.zcraft.quartzlib.components.i18n.I;
import fr.zcraft.quartzlib.tools.PluginLogger;
import java.util.Set;
import org.bukkit.permissions.Permissible;
import org.bukkit.permissions.PermissionAttachmentInfo;
public enum Permissions {
NEW("imageonmap.new", "imageonmap.userender"),
@ -54,6 +58,8 @@ public enum Permissions {
UPDATEOTHER("imageonmap.updateother"),
ADMINISTRATIVE("imageonmap.administrative"),
BYPASS_SIZE("imageonmap.bypasssize"),
BYPASS_IMAGE_LIMIT("imageonmap.bypassimagelimit"),
BYPASS_MAP_LIMIT("imageonmap.bypassmaplimit"),
GIVE("imageonmap.give");
private final String permission;
@ -83,4 +89,37 @@ public enum Permissions {
return false;
}
/**
* Return the limit of map the user is allowed to make
*
* @param permissible The permissible to check.
* @return the limit
*/
public int getLimitPermission(Permissible permissible, LimitType type) {
Set<PermissionAttachmentInfo> perms = permissible.getEffectivePermissions();
String prefix = String.format("imageonmap.%slimit.", type.name());
for (PermissionAttachmentInfo pai : perms) {
String permString = pai.getPermission().toLowerCase();
if (permString.startsWith(prefix)) {
if (pai.getValue()) {
try {
int limit = Integer.parseInt(permString.split(prefix)[1].trim());
return limit;
} catch (Exception e) {
PluginLogger.warning(
I.t("The correct syntax for setting map limit node is: ImageOnMap.mapLimit.X "
+ "where you can replace X with the limit of map a player is allowed to have"));
}
}
}
}
return 2147483647; //Virtually no limit
}
public enum LimitType {
map,
image
}
}

View File

@ -41,6 +41,7 @@ import fr.moribus.imageonmap.commands.IoMCommand;
import fr.moribus.imageonmap.image.ImageRendererExecutor;
import fr.moribus.imageonmap.image.ImageUtils;
import fr.moribus.imageonmap.map.ImageMap;
import fr.moribus.imageonmap.map.MapManager;
import fr.moribus.imageonmap.map.PosterMap;
import fr.zcraft.quartzlib.components.commands.CommandException;
import fr.zcraft.quartzlib.components.commands.CommandInfo;
@ -87,7 +88,23 @@ public class NewCommand extends IoMCommand {
if (args.length < 1) {
throwInvalidArgument(I.t("You must give an URL to take the image from."));
}
//Checking if the map limit and image limit
if (!Permissions.BYPASS_IMAGE_LIMIT.grantedTo(player)) {
int imageLimit = Permissions.NEW.getLimitPermission(player, Permissions.LimitType.image);
int imageCount = MapManager.getPlayerMapStore(player.getUniqueId()).getImagesCount();
if (imageLimit <= imageCount) {
throwInvalidArgument(
I.t("Your image limit is set to {0} and you currently have {1} ", imageLimit, imageCount));
}
}
if (!Permissions.BYPASS_MAP_LIMIT.grantedTo(player)) {
int mapLimit = Permissions.NEW.getLimitPermission(player, Permissions.LimitType.map);
int mapCount = MapManager.getPlayerMapStore(player.getUniqueId()).getMapCount();
if (mapLimit <= mapCount) {
throwInvalidArgument(
I.t("Your map limit is set to {0} and you currently have {1} ", mapLimit, mapCount));
}
}
try {
url = new URL(args[0]);
} catch (MalformedURLException ex) {

View File

@ -38,6 +38,8 @@ permissions:
imageonmap.give: false
imageonmap.update: true
imageonmap.updateother: false
imageonmap.bypassmaplimit: false
imageonmap.bypassimagelimit: false
imageonmap.userender:
description: "Allows you to use /tomap and related commands (/maptool getremaining). Alias of imageonmap.new."
@ -110,3 +112,11 @@ permissions:
imageonmap.updateother:
description: "Allows you to update an existing map of an other player with a new image."
default: op
imageonmap.bypassmaplimit:
description: "Allows you to bypass permission node check for the number of used map (by default users have an unlimited amount of maps)."
default: op
imageonmap.bypassimagelimit:
description: "Allows you to bypass permission node check for the number of images in the playerMapStore(by default users have an unlimited amount of images)."
default: op