Added permissions "shopchest.create.buy" and "shopchest.create.sell"

Both of these also work with item nodes. Examples:

- shopchest.create.buy.CHEST (Allows only shops selling chests)
- shopchest.create.sell.APPLE (Allows only shops buying apples)
- shopchest.create.STONE.3 (Allows only shops buying or selling diorite)
- shopchest.create.buy (Allows only shops that are selling anything)
- shopchest.create.sell (Allows only shops that are buying anything)
This commit is contained in:
Eric 2017-06-05 14:13:40 +02:00
parent 8cdff11aaf
commit 8f0d2aaf5f
4 changed files with 98 additions and 42 deletions

View File

@ -1,19 +1,34 @@
package de.epiceric.shopchest;
import de.epiceric.shopchest.config.Placeholder;
import de.epiceric.shopchest.event.*;
import de.epiceric.shopchest.event.ShopPreCreateEvent;
import de.epiceric.shopchest.event.ShopPreInfoEvent;
import de.epiceric.shopchest.event.ShopPreOpenEvent;
import de.epiceric.shopchest.event.ShopPreRemoveEvent;
import de.epiceric.shopchest.event.ShopReloadEvent;
import de.epiceric.shopchest.event.ShopRemoveAllEvent;
import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.LocalizedMessage;
import de.epiceric.shopchest.nms.JsonBuilder;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.shop.Shop.ShopType;
import de.epiceric.shopchest.utils.*;
import de.epiceric.shopchest.utils.Callback;
import de.epiceric.shopchest.utils.ClickType;
import de.epiceric.shopchest.utils.ClickType.EnumClickType;
import de.epiceric.shopchest.utils.ItemUtils;
import de.epiceric.shopchest.utils.Permissions;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.UpdateChecker;
import de.epiceric.shopchest.utils.UpdateChecker.UpdateCheckerResult;
import de.epiceric.shopchest.utils.Utils;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.OfflinePlayer;
import org.bukkit.command.*;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandMap;
import org.bukkit.command.CommandSender;
import org.bukkit.command.PluginCommand;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
@ -109,7 +124,6 @@ class ShopCommand implements CommandExecutor {
Player p = (Player) sender;
if (args[0].equalsIgnoreCase("create")) {
if (Utils.hasPermissionToCreateShop(p, Utils.getPreferredItemInHand(p))) {
if (args.length == 4) {
needsHelp = false;
create(args, ShopType.NORMAL, p);
@ -126,10 +140,6 @@ class ShopCommand implements CommandExecutor {
}
}
}
} else {
needsHelp = false;
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_CREATE));
}
} else if (args[0].equalsIgnoreCase("remove")) {
needsHelp = false;
remove(p);
@ -286,6 +296,23 @@ class ShopCommand implements CommandExecutor {
int amount;
double buyPrice, sellPrice;
// Check if amount and prices are valid
try {
amount = Integer.parseInt(args[1]);
buyPrice = Double.parseDouble(args[2]);
sellPrice = Double.parseDouble(args[3]);
} catch (NumberFormatException e) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.AMOUNT_PRICE_NOT_NUMBER));
plugin.debug(p.getName() + " has entered an invalid amount and/or prices");
return;
}
if (!Utils.hasPermissionToCreateShop(p, Utils.getPreferredItemInHand(p), buyPrice > 0, sellPrice > 0)) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_CREATE));
plugin.debug(p.getName() + " is not permitted to create the shop");
return;
}
// Check for limits
int limit = shopUtils.getShopLimit(p);
if (limit != -1) {
@ -298,17 +325,6 @@ class ShopCommand implements CommandExecutor {
}
}
// Check if amount and prices are valid
try {
amount = Integer.parseInt(args[1]);
buyPrice = Double.parseDouble(args[2]);
sellPrice = Double.parseDouble(args[3]);
} catch (NumberFormatException e) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.AMOUNT_PRICE_NOT_NUMBER));
plugin.debug(p.getName() + " has entered an invalid amount");
return;
}
if (amount <= 0) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.AMOUNT_IS_ZERO));
plugin.debug(p.getName() + " has entered an invalid amount");

View File

@ -3,6 +3,8 @@ package de.epiceric.shopchest.utils;
public class Permissions {
public static final String CREATE = "shopchest.create";
public static final String CREATE_BUY = "shopchest.create.buy";
public static final String CREATE_SELL = "shopchest.create.sell";
public static final String CREATE_ADMIN = "shopchest.create.admin";
public static final String CREATE_PROTECTED = "shopchest.create.protected";
public static final String REMOVE_OTHER = "shopchest.remove.other";

View File

@ -258,32 +258,59 @@ public class Utils {
}
/**
* <p>Check if a player is allowed to create a shop that sells (or buys) the given item.</p>
* The player is allowed to create the shop if has either the permission {@code shopchest.create},
* {@code shopchest.create.[ITEM]} or {@code shopchest.create.[ITEM].[DURABILITY]}
* <p>Check if a player is allowed to create a shop that sells or buys the given item.</p>
* @param player Player to check
* @param item Item to be sold or bought
* @param buy Whether buying should be enabled
* @param sell Whether selling should be enabled
* @return Whether the player is allowed
*/
public static boolean hasPermissionToCreateShop(Player player, ItemStack item) {
if (player.hasPermission(Permissions.CREATE)) {
public static boolean hasPermissionToCreateShop(Player player, ItemStack item, boolean buy, boolean sell) {
if (hasPermissionToCreateShop(player, item, Permissions.CREATE)) {
return true;
} else if (!sell && buy && hasPermissionToCreateShop(player, item,Permissions.CREATE_BUY)) {
return true;
} else if (!buy && sell && hasPermissionToCreateShop(player, item, Permissions.CREATE_SELL)) {
return true;
} else if (buy && sell && hasPermissionToCreateShop(player, item, Permissions.CREATE_BUY, Permissions.CREATE_SELL)) {
return true;
}
return false;
}
private static boolean hasPermissionToCreateShop(Player player, ItemStack item, String... permissions) {
for (String permission : permissions) {
boolean b1 = false;
boolean b2 = false;
boolean b3 = false;
if (player.hasPermission(permission)) {
b1 = true;
}
if (item != null) {
if (item.getDurability() == 0) {
if (player.hasPermission(Permissions.CREATE + "." + item.getType().toString())) {
return true;
String perm1 = permission + "." + item.getType().toString();
String perm2 = permission + "." + item.getType().toString() + ".0";
if (player.hasPermission(perm1) || player.hasPermission(perm2)) {
b2 = true;
}
}
if (player.hasPermission(Permissions.CREATE + "." + item.getType().toString() + "." + item.getDurability())) {
return true;
if (player.hasPermission(permission + "." + item.getType().toString() + "." + item.getDurability())) {
b3 = true;
}
}
if (!(b1 || b2 || b3)) {
return false;
}
}
return true;
}
/**
* @param className Name of the class

View File

@ -14,6 +14,8 @@ permissions:
description: Gives access to all ShopChest permissions.
children:
shopchest.create: true
shopchest.create.buy: true
shopchest.create.sell: true
shopchest.create.admin: true
shopchest.create.protected: true
shopchest.remove.other: true
@ -30,6 +32,15 @@ permissions:
shopchest.external.bypass: true
shopchest.create:
description: Allows you to create a shop.
children:
shopchest.create.buy: true
shopchest.create.sell: true
default: true
shopchest.create.buy:
description: Allows you to create a buy-shop.
default: true
shopchest.create.sell:
description: Allows you to create a sell-shop.
default: true
shopchest.create.admin:
description: Allows you to create an admin shop.