Permission constants & No more Vault permission system

This commit is contained in:
Eric 2016-11-18 21:14:01 +01:00
parent 1d7a534fe4
commit fdfc7419d9
9 changed files with 47 additions and 66 deletions

View File

@ -13,16 +13,12 @@ import de.epiceric.shopchest.shop.Shop.ShopType;
import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.sql.MySQL;
import de.epiceric.shopchest.sql.SQLite;
import de.epiceric.shopchest.utils.Metrics;
import de.epiceric.shopchest.utils.*;
import de.epiceric.shopchest.utils.Metrics.Graph;
import de.epiceric.shopchest.utils.Metrics.Plotter;
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 de.epiceric.shopchest.worldguard.ShopFlag;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
@ -41,7 +37,6 @@ public class ShopChest extends JavaPlugin {
private Config config = null;
private Economy econ = null;
private Permission perm = null;
private Database database;
private boolean isUpdateNeeded = false;
private String latestVersion = "";
@ -71,17 +66,6 @@ public class ShopChest extends JavaPlugin {
return econ != null;
}
/**
* Sets up the permissions of Vault
* @return Whether a permissions plugin has been registered
*/
private boolean setupPermissions() {
RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
perm = rsp.getProvider();
return perm != null;
}
@Override
public void onEnable() {
instance = this;
@ -121,13 +105,6 @@ public class ShopChest extends JavaPlugin {
return;
}
if (!setupPermissions()) {
debug("Could not find any Vault permission dependency!");
getLogger().severe("Could not find any Vault permission dependency!");
getServer().getPluginManager().disablePlugin(this);
return;
}
switch (Utils.getServerVersion()) {
case "v1_8_R1":
case "v1_8_R2":
@ -282,7 +259,7 @@ public class ShopChest extends JavaPlugin {
Bukkit.getConsoleSender().sendMessage("[ShopChest] " + LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_AVAILABLE, new LocalizedMessage.ReplacedRegex(Regex.VERSION, latestVersion)));
for (Player p : getServer().getOnlinePlayers()) {
if (perm.has(p, "shopchest.notification.update")) {
if (p.hasPermission(Permissions.UPDATE_NOTIFICATION)) {
JsonBuilder jb = new JsonBuilder(ShopChest.this, LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_AVAILABLE, new LocalizedMessage.ReplacedRegex(Regex.VERSION, latestVersion)), LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_CLICK_TO_DOWNLOAD), downloadLink);
jb.sendJson(p);
}
@ -423,13 +400,6 @@ public class ShopChest extends JavaPlugin {
return econ;
}
/**
* @return Registered Permission of Vault
*/
public Permission getPermission() {
return perm;
}
/**
* @return ShopChest's shop database
*/

View File

@ -10,13 +10,9 @@ 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.ClickType;
import de.epiceric.shopchest.utils.*;
import de.epiceric.shopchest.utils.ClickType.EnumClickType;
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 net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.Material;
@ -33,13 +29,11 @@ import java.util.List;
class ShopCommand extends BukkitCommand {
private ShopChest plugin;
private Permission perm;
private ShopUtils shopUtils;
ShopCommand(ShopChest plugin, String name, String description, String usageMessage, List<String> aliases) {
super(name, description, usageMessage, aliases);
this.plugin = plugin;
this.perm = plugin.getPermission();
this.shopUtils = plugin.getShopUtils();
}
@ -69,7 +63,7 @@ class ShopCommand extends BukkitCommand {
return true;
} else {
if (args[0].equalsIgnoreCase("create")) {
if (perm.has(p, "shopchest.create")) {
if (p.hasPermission(Permissions.CREATE)) {
if (args.length == 4) {
create(args, ShopType.NORMAL, p);
return true;
@ -78,7 +72,7 @@ class ShopCommand extends BukkitCommand {
create(args, ShopType.NORMAL, p);
return true;
} else if (args[4].equalsIgnoreCase("admin")) {
if (perm.has(p, "shopchest.create.admin")) {
if (p.hasPermission(Permissions.CREATE_ADMIN)) {
create(args, ShopType.ADMIN, p);
return true;
} else {
@ -104,7 +98,7 @@ class ShopCommand extends BukkitCommand {
info(p);
return true;
} else if (args[0].equalsIgnoreCase("reload")) {
if (perm.has(p, "shopchest.reload")) {
if (p.hasPermission(Permissions.RELOAD)) {
reload(p);
return true;
} else {
@ -112,7 +106,7 @@ class ShopCommand extends BukkitCommand {
return true;
}
} else if (args[0].equalsIgnoreCase("update")) {
if (perm.has(p, "shopchest.update")) {
if (p.hasPermission(Permissions.UPDATE)) {
checkUpdates(p);
return true;
} else {
@ -128,7 +122,7 @@ class ShopCommand extends BukkitCommand {
return true;
} else if (args[0].equalsIgnoreCase("config")) {
if (perm.has(p, "shopchest.config")) {
if (p.hasPermission(Permissions.CONFIG)) {
if (args.length >= 4) {
plugin.debug(p.getName() + " is changing the configuration");
@ -427,7 +421,7 @@ class ShopCommand extends BukkitCommand {
private void sendBasicHelpMessage(Player player) {
plugin.debug("Sending basic help message to " + player.getName());
if (player.hasPermission("shopchest.create.admin")) {
if (player.hasPermission(Permissions.CREATE_ADMIN)) {
player.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " create <amount> <buy-price> <sell-price> [normal|admin] - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CREATE));
} else {
player.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " create <amount> <buy-price> <sell-price> - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CREATE));
@ -436,17 +430,17 @@ class ShopCommand extends BukkitCommand {
player.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " remove - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_REMOVE));
player.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " info - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_INFO));
if (player.hasPermission("shopchest.reload")) {
if (player.hasPermission(Permissions.RELOAD)) {
player.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " reload - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_RELOAD));
}
if (player.hasPermission("shopchest.update")) {
if (player.hasPermission(Permissions.UPDATE)) {
player.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " update - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_UPDATE));
}
player.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " limits - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_LIMITS));
if (player.hasPermission("shopchest.config")) {
if (player.hasPermission(Permissions.CONFIG)) {
player.sendMessage(ChatColor.GREEN + "/" + plugin.getShopChestConfig().main_command_name + " config <set|add|remove> <property> <value> - " + LanguageUtils.getMessage(LocalizedMessage.Message.COMMAND_DESC_CONFIG));
}
}

View File

@ -4,6 +4,7 @@ import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.LocalizedMessage;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.Permissions;
import de.epiceric.shopchest.utils.ShopUtils;
import org.bukkit.Bukkit;
import org.bukkit.Material;
@ -45,7 +46,7 @@ public class ChestProtectListener implements Listener {
if (p.isSneaking()) {
plugin.debug(String.format("%s tries to break %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID()));
if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || p.hasPermission("shopchest.removeOther")) {
if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || p.hasPermission(Permissions.REMOVE_OTHER)) {
shopUtils.removeShop(shop, true);
if (shop.getInventoryHolder() instanceof DoubleChest) {
@ -123,7 +124,7 @@ public class ChestProtectListener implements Listener {
plugin.debug(String.format("%s tries to extend %s's shop (#%d)", p.getName(), shop.getVendor().getName(), shop.getID()));
if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || p.hasPermission("shopchest.extendOther")) {
if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || p.hasPermission(Permissions.EXTEND_OTHER)) {
if (b.getRelative(BlockFace.UP).getType() == Material.AIR) {
shopUtils.removeShop(shop, true);
Shop newShop = new Shop(shop.getID(), ShopChest.getInstance(), shop.getVendor(), shop.getProduct(), shop.getLocation(), shop.getBuyPrice(), shop.getSellPrice(), shop.getShopType());

View File

@ -5,8 +5,7 @@ import de.epiceric.shopchest.config.Regex;
import de.epiceric.shopchest.language.LanguageUtils;
import de.epiceric.shopchest.language.LocalizedMessage;
import de.epiceric.shopchest.nms.JsonBuilder;
import de.epiceric.shopchest.utils.Utils;
import net.milkbowl.vault.permission.Permission;
import de.epiceric.shopchest.utils.Permissions;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
@ -15,11 +14,9 @@ import org.bukkit.event.player.PlayerJoinEvent;
public class NotifyUpdateOnJoinListener implements Listener {
private ShopChest plugin;
private Permission perm;
public NotifyUpdateOnJoinListener(ShopChest plugin) {
this.plugin = plugin;
perm = plugin.getPermission();
}
@EventHandler
@ -27,7 +24,7 @@ public class NotifyUpdateOnJoinListener implements Listener {
Player p = e.getPlayer();
if (plugin.isUpdateNeeded()) {
if (perm.has(p, "shopchest.notification.update")) {
if (p.hasPermission(Permissions.UPDATE_NOTIFICATION)) {
JsonBuilder jb = new JsonBuilder(plugin, LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_AVAILABLE, new LocalizedMessage.ReplacedRegex(Regex.VERSION, plugin.getLatestVersion())), LanguageUtils.getMessage(LocalizedMessage.Message.UPDATE_CLICK_TO_DOWNLOAD), plugin.getDownloadLink());
jb.sendJson(p);
}

View File

@ -18,12 +18,12 @@ import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.shop.Shop.ShopType;
import de.epiceric.shopchest.sql.Database;
import de.epiceric.shopchest.utils.ClickType;
import de.epiceric.shopchest.utils.Permissions;
import de.epiceric.shopchest.utils.ShopUtils;
import de.epiceric.shopchest.utils.Utils;
import de.epiceric.shopchest.worldguard.ShopFlag;
import net.milkbowl.vault.economy.Economy;
import net.milkbowl.vault.economy.EconomyResponse;
import net.milkbowl.vault.permission.Permission;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
@ -55,7 +55,6 @@ import java.util.Map;
public class ShopInteractListener implements Listener {
private ShopChest plugin;
private Permission perm;
private Economy econ;
private Database database;
private ShopUtils shopUtils;
@ -64,7 +63,6 @@ public class ShopInteractListener implements Listener {
public ShopInteractListener(ShopChest plugin) {
this.plugin = plugin;
this.perm = plugin.getPermission();
this.econ = plugin.getEconomy();
this.database = plugin.getShopDatabase();
this.shopUtils = plugin.getShopUtils();
@ -92,7 +90,7 @@ public class ShopInteractListener implements Listener {
worldGuardAllowed = query.testState(b.getLocation(), p, ShopFlag.CREATE_SHOP);
}
if ((e.isCancelled() || !worldGuardAllowed) && !perm.has(p, "shopchest.create.protected")) {
if ((e.isCancelled() || !worldGuardAllowed) && !p.hasPermission(Permissions.CREATE_PROTECTED)) {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_CREATE_PROTECTED));
ClickType.removePlayerClickType(p);
plugin.debug(p.getName() + " is not allowed to create a shop on the selected chest");
@ -161,7 +159,7 @@ public class ShopInteractListener implements Listener {
if (shopUtils.isShop(b.getLocation())) {
Shop shop = shopUtils.getShop(b.getLocation());
if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || perm.has(p, "shopchest.removeOther")) {
if (shop.getVendor().getUniqueId().equals(p.getUniqueId()) || p.hasPermission(Permissions.REMOVE_OTHER)) {
remove(p, shop);
} else {
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.NO_PERMISSION_REMOVE_OTHERS));
@ -187,7 +185,7 @@ public class ShopInteractListener implements Listener {
if (Utils.getPreferredItemInHand(p) == null) {
e.setCancelled(true);
if (!shop.getVendor().getUniqueId().equals(p.getUniqueId())) {
if (perm.has(p, "shopchest.openOther")) {
if (p.hasPermission(Permissions.OPEN_OTHER)) {
String vendorName = (shop.getVendor().getName() == null ? shop.getVendor().getUniqueId().toString() : shop.getVendor().getName());
p.sendMessage(LanguageUtils.getMessage(LocalizedMessage.Message.OPENED_SHOP, new LocalizedMessage.ReplacedRegex(Regex.VENDOR, vendorName)));
plugin.debug(p.getName() + " is opening " + vendorName + "'s shop (#" + shop.getID() + ")");
@ -211,7 +209,7 @@ public class ShopInteractListener implements Listener {
if (shop.getShopType() == ShopType.ADMIN || !shop.getVendor().getUniqueId().equals(p.getUniqueId())) {
plugin.debug(p.getName() + " wants to buy");
if (shop.getBuyPrice() > 0) {
if (perm.has(p, "shopchest.buy")) {
if (p.hasPermission(Permissions.BUY)) {
boolean worldGuardAllowed = true;
if (shop.getShopType() == ShopType.ADMIN) {
@ -280,7 +278,7 @@ public class ShopInteractListener implements Listener {
if ((shop.getShopType() == ShopType.ADMIN) || (!shop.getVendor().getUniqueId().equals(p.getUniqueId()))) {
plugin.debug(p.getName() + " wants to sell");
if (shop.getSellPrice() > 0) {
if (perm.has(p, "shopchest.sell")) {
if (p.hasPermission(Permissions.SELL)) {
boolean worldGuardAllowed = true;
if (plugin.hasWorldGuard()) {

View File

@ -9,6 +9,7 @@ import com.sk89q.worldguard.protection.flags.StateFlag;
import de.epiceric.shopchest.ShopChest;
import de.epiceric.shopchest.shop.Shop;
import de.epiceric.shopchest.utils.ClickType;
import de.epiceric.shopchest.utils.Permissions;
import de.epiceric.shopchest.worldguard.ShopFlag;
import org.bukkit.Location;
import org.bukkit.Material;
@ -60,7 +61,7 @@ public class WorldGuardListener implements Listener {
}
if (!shop.getVendor().getUniqueId().equals(p.getUniqueId()) && p.isSneaking()) {
return p.hasPermission("shopchest.openOther");
return p.hasPermission(Permissions.OPEN_OTHER);
}
StateFlag flag = (shop.getShopType() == Shop.ShopType.NORMAL ? ShopFlag.USE_SHOP : ShopFlag.USE_ADMIN_SHOP);

View File

@ -0,0 +1,19 @@
package de.epiceric.shopchest.utils;
public class Permissions {
public static final String CREATE = "shopchest.create";
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.removeOther";
public static final String BUY = "shopchest.buy";
public static final String SELL = "shopchest.sell";
public static final String OPEN_OTHER = "shopchest.openOther";
public static final String UPDATE_NOTIFICATION = "shopchest.notification.update";
public static final String RELOAD = "shopchest.reload";
public static final String UPDATE = "shopchest.update";
public static final String NO_LIMIT = "shopchest.limit.*";
public static final String CONFIG = "shopchest.config";
public static final String EXTEND_OTHER = "shopchest.extendOther";
}

View File

@ -125,7 +125,7 @@ public class ShopUtils {
for (PermissionAttachmentInfo permInfo : p.getEffectivePermissions()) {
if (permInfo.getPermission().startsWith("shopchest.limit.")) {
if (permInfo.getPermission().contains("shopchest.limit.*")) {
if (permInfo.getPermission().equalsIgnoreCase(Permissions.NO_LIMIT)) {
limit = -1;
break;
} else {

View File

@ -3,6 +3,7 @@ package de.epiceric.shopchest.utils;
import de.epiceric.shopchest.ShopChest;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
@ -50,7 +51,7 @@ public class UpdateChecker {
return UpdateCheckerResult.TRUE;
}
} catch (Exception e) {
} catch (IOException e) {
plugin.debug("Failed to check for updates");
plugin.debug(e);
return UpdateCheckerResult.ERROR;