From 91460c8367e4b286bb8a037f887a0695499b2d9c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Bro=C4=8Dko?= Date: Wed, 5 Sep 2018 18:26:21 +0200 Subject: [PATCH] added permission support for commands --- src/cz/marwland/mc/core/MarwCore.java | 4 ++++ src/cz/marwland/mc/core/features/Feature.java | 17 ++++++++++++++++- .../mc/essentials/features/Borders.java | 10 +++++++++- 3 files changed, 29 insertions(+), 2 deletions(-) diff --git a/src/cz/marwland/mc/core/MarwCore.java b/src/cz/marwland/mc/core/MarwCore.java index cf321d0..443e806 100644 --- a/src/cz/marwland/mc/core/MarwCore.java +++ b/src/cz/marwland/mc/core/MarwCore.java @@ -54,5 +54,9 @@ public class MarwCore extends JavaPlugin { public Feature getFeature(String name) { return this.features.get(name); } + + public String getPermissionPath() { + return "marw.core"; + } } diff --git a/src/cz/marwland/mc/core/features/Feature.java b/src/cz/marwland/mc/core/features/Feature.java index ca2a1b2..9b5d0dd 100644 --- a/src/cz/marwland/mc/core/features/Feature.java +++ b/src/cz/marwland/mc/core/features/Feature.java @@ -5,11 +5,13 @@ import java.lang.reflect.Method; import java.util.ArrayList; import org.bukkit.command.CommandMap; +import org.bukkit.command.CommandSender; import org.bukkit.command.defaults.BukkitCommand; import org.bukkit.configuration.file.FileConfiguration; import org.bukkit.event.Listener; import cz.marwland.mc.core.MarwCore; +import net.md_5.bungee.api.ChatColor; public abstract class Feature implements Listener { protected final MarwCore plugin; @@ -20,6 +22,9 @@ public abstract class Feature implements Listener { public Feature(MarwCore plugin, String name) { this.plugin = plugin; this.name = name; + if (name == null) { + this.name = this.getClass().getSimpleName().toLowerCase(); + } Method commandMap; try { commandMap = plugin.getServer().getClass().getMethod("getCommandMap"); @@ -27,7 +32,6 @@ public abstract class Feature implements Listener { } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) { e.printStackTrace(); } - } public void onEnable() { @@ -74,4 +78,15 @@ public abstract class Feature implements Listener { return plugin.getConfigManager().getConfig(path); } + public String getPermissionPath() { + return plugin.getPermissionPath() + "." + this.getName(); + } + + public boolean permissionMissingCheck(CommandSender sender, String permission) { + if (sender.hasPermission(permission)) + return true; + sender.sendMessage(ChatColor.RED + "You don't have permission " + ChatColor.YELLOW + permission + ChatColor.RED + "!"); + return false; + } + } diff --git a/src/cz/marwland/mc/essentials/features/Borders.java b/src/cz/marwland/mc/essentials/features/Borders.java index 648da76..60aecfc 100644 --- a/src/cz/marwland/mc/essentials/features/Borders.java +++ b/src/cz/marwland/mc/essentials/features/Borders.java @@ -26,7 +26,7 @@ public class Borders extends Feature { EntryManager emgr = new EntryManager(); public Borders(MarwCore plugin) { - super(plugin, "heil"); + super(plugin, null); // String name, String description, String usageMessage, List aliases) this.addCommand(new BukkitCommand( "worldborder", @@ -35,6 +35,9 @@ public class Borders extends Feature { Arrays.asList() ) { @Override public boolean execute(CommandSender sender, String commandLabel, String[] args) { + if (!permissionMissingCheck(sender, this.getPermission())) + return false; + if (args.length < 2) { sender.sendMessage( ChatColor.YELLOW + "Format: " + @@ -121,6 +124,11 @@ public class Borders extends Feature { Collections.sort(hints, String.CASE_INSENSITIVE_ORDER); return hints; } + + @Override + public String getPermission() { + return getPermissionPath() + "." + this.getName().toLowerCase(); + } }); emgr.addEntry(new ConfigEntry<>("center", ValueValidator.VECTOR, new Vector(0, 0, 0))); // emgr.addEntry(new ConfigEntry<>("size", ValueValidator.DOUBLE));