Use Bukkit's inbuilt command permissions system (Fixes #186)

Also fix plugin not disabling when dependencies aren't loaded correctly
This commit is contained in:
Phoenix616 2018-11-25 00:59:13 +01:00
parent bc14736a37
commit 6fe6f80538
6 changed files with 35 additions and 34 deletions

View File

@ -49,6 +49,8 @@ import org.apache.logging.log4j.message.Message;
import org.bukkit.Bukkit;
import org.bukkit.Server;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.PluginCommand;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.event.Event;
import org.bukkit.event.Listener;
@ -58,6 +60,7 @@ import org.bukkit.plugin.java.JavaPlugin;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.FileHandler;
import java.util.logging.Logger;
@ -78,6 +81,8 @@ public class ChestShop extends JavaPlugin {
private static Logger logger;
private FileHandler handler;
private List<PluginCommand> commands = new ArrayList<>();
public ChestShop() {
dataFolder = getDataFolder();
logger = getLogger();
@ -93,19 +98,22 @@ public class ChestShop extends JavaPlugin {
@Override
public void onEnable() {
Configuration.pairFileAndClass(loadFile("config.yml"), Properties.class);
Configuration.pairFileAndClass(loadFile("local.yml"), Messages.class);
turnOffDatabaseLogging();
if (!handleMigrations()) {
return;
}
registerCommand("iteminfo", new ItemInfo(), Permission.ITEMINFO);
registerCommand("csVersion", new Version(), Permission.ADMIN);
registerCommand("csGive", new Give(), Permission.ADMIN);
registerCommand("cstoggle", new Toggle(), Permission.NOTIFY_TOGGLE);
loadConfig();
itemDatabase = new ItemDatabase();
NameManager.load();
if (!Dependencies.loadPlugins()) {
getServer().getPluginManager().disablePlugin(this);
return;
}
@ -125,15 +133,26 @@ public class ChestShop extends JavaPlugin {
logger.setUseParentHandlers(false);
}
getCommand("iteminfo").setExecutor(new ItemInfo());
getCommand("csVersion").setExecutor(new Version());
getCommand("csGive").setExecutor(new Give());
getCommand("cstoggle").setExecutor(new Toggle());
startStatistics();
startUpdater();
}
private void registerCommand(String name, CommandExecutor executor, Permission permission) {
PluginCommand command = getCommand(name);
command.setExecutor(executor);
command.setPermission(permission.toString());
commands.add(command);
}
public void loadConfig() {
Configuration.pairFileAndClass(loadFile("config.yml"), Properties.class);
Configuration.pairFileAndClass(loadFile("local.yml"), Messages.class);
NameManager.load();
commands.forEach(c -> c.setPermissionMessage(Messages.prefix(Messages.ACCESS_DENIED)));
}
private void turnOffDatabaseLogging() {
LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
org.apache.logging.log4j.core.config.Configuration config = ctx.getConfiguration();

View File

@ -22,11 +22,6 @@ public class Give implements CommandExecutor {
@Override
public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
if (!Permission.has(sender, Permission.ADMIN)) {
sender.sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
return true;
}
if (args.length < 1) {
return false;
}

View File

@ -24,11 +24,6 @@ public class Toggle implements CommandExecutor {
return false;
}
if (!Permission.has(sender, Permission.NOTIFY_TOGGLE)) {
sender.sendMessage(Messages.ACCESS_DENIED);
return true;
}
Player player = (Player) sender;
if (args.length != 0) {

View File

@ -1,11 +1,6 @@
package com.Acrobot.ChestShop.Commands;
import com.Acrobot.Breeze.Configuration.Configuration;
import com.Acrobot.ChestShop.ChestShop;
import com.Acrobot.ChestShop.Configuration.Messages;
import com.Acrobot.ChestShop.Configuration.Properties;
import com.Acrobot.ChestShop.Permission;
import com.Acrobot.ChestShop.UUIDs.NameManager;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
@ -16,15 +11,8 @@ import org.bukkit.command.CommandSender;
*/
public class Version implements CommandExecutor {
public boolean onCommand(CommandSender sender, Command cmd, String label, String[] args) {
if (!Permission.has(sender, Permission.ADMIN)) {
sender.sendMessage(Messages.prefix(Messages.ACCESS_DENIED));
return true;
}
if (args.length > 0 && args[0].equals("reload")) {
Configuration.pairFileAndClass(ChestShop.loadFile("config.yml"), Properties.class);
Configuration.pairFileAndClass(ChestShop.loadFile("local.yml"), Messages.class);
NameManager.load();
ChestShop.getPlugin().loadConfig();
sender.sendMessage(ChatColor.DARK_GREEN + "The config was reloaded.");
return true;

View File

@ -37,7 +37,8 @@ public enum Permission {
NOFEE("ChestShop.nofee"),
DISCOUNT("ChestShop.discount."),
NOTIFY_TOGGLE("ChestShop.toggle");
NOTIFY_TOGGLE("ChestShop.toggle"),
ITEMINFO("ChestShop.iteminfo");
private final String permission;

View File

@ -68,6 +68,9 @@ permissions:
ChestShop.toggle:
description: Allows user to toggle messages.
default: true
ChestShop.iteminfo:
description: Allows user to see item info with the command.
default: true
ChestShop.admin:
description: Allows user to modify/destroy other stores and create an Admin Shops
default: op