added permission support for commands

This commit is contained in:
Erik Bročko 2018-09-05 18:26:21 +02:00
parent 8795e6d9cc
commit 91460c8367
3 changed files with 29 additions and 2 deletions

View File

@ -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";
}
}

View File

@ -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;
}
}

View File

@ -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<String> 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));